Bug 1522135 - Make .ycm_extra_conf.py work with python3. r=jgraham
authorEmilio Cobos Álvarez <emilio@crisal.io>
Wed, 23 Jan 2019 16:58:44 +0100
changeset 512391 8dcbc414dda673564951f3a28099643f3a8d1ed6
parent 512390 5592c56955caa590aaa682138a7eb034404545f8
child 512392 8659b66f06743242f9b5ca5c6ef6d9e0750bd417
push id10566
push userarchaeopteryx@coole-files.de
push dateMon, 28 Jan 2019 12:41:12 +0000
treeherdermozilla-beta@69a3d7c8d04b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1522135 - Make .ycm_extra_conf.py work with python3. r=jgraham By calling into mach instead, which knows how to get to run itself with the right python version. Differential Revision: https://phabricator.services.mozilla.com/D17371
--- a/.ycm_extra_conf.py
+++ b/.ycm_extra_conf.py
@@ -1,60 +1,55 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
-import imp
+from __future__ import absolute_import, print_function
+import json
 import os
 import shlex
+import subprocess
 import sys
-    from StringIO import StringIO
-except ImportError:
-    from io import StringIO
 old_bytecode = sys.dont_write_bytecode
 sys.dont_write_bytecode = True
-path = os.path.join(os.path.dirname(__file__), 'mach')
+path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'mach'))
+# If mach is not here, we're on the objdir go to the srcdir.
 if not os.path.exists(path):
-    path = os.path.join(os.path.dirname(__file__), 'config.status')
-    config = imp.load_module('_buildconfig', open(path), path, ('', 'r', imp.PY_SOURCE))
-    path = os.path.join(config.topsrcdir, 'mach')
-mach_module = imp.load_module('_mach', open(path), path, ('', 'r', imp.PY_SOURCE))
+    with open(os.path.join(os.path.dirname(__file__), 'mozinfo.json')) as info:
+        config = json.loads(info.read())
+    path = os.path.join(config['topsrcdir'], 'mach')
 sys.dont_write_bytecode = old_bytecode
 def _is_likely_cpp_header(filename):
     if not filename.endswith('.h'):
         return False
     if filename.endswith('Inlines.h') or filename.endswith('-inl.h'):
         return True
     cpp_file = filename[:-1] + 'cpp'
     return os.path.exists(cpp_file)
 def FlagsForFile(filename):
-    mach = mach_module.get_mach()
-    out = StringIO()
+    output = subprocess.check_output([path, 'compileflags', filename])
+    output = output.decode('utf-8')
-    # Mach calls sys.stdout.fileno(), so we need to fake it when capturing it.
-    # Returning an invalid file descriptor does the trick.
-    out.fileno = lambda: -1
-    out.encoding = None
-    mach.run(['compileflags', filename], stdout=out, stderr=out)
-    flag_list = shlex.split(out.getvalue())
+    flag_list = shlex.split(output)
     # This flag is added by Fennec for android build and causes ycmd to fail to parse the file.
     # Removing this flag is a workaround until ycmd starts to handle this flag properly.
     # https://github.com/Valloric/YouCompleteMe/issues/1490
     final_flags = [x for x in flag_list if not x.startswith('-march=armv')]
     if _is_likely_cpp_header(filename):
         final_flags += ["-x", "c++"]
     return {
         'flags': final_flags,
         'do_cache': True
+if __name__ == '__main__':
+    print(FlagsForFile(sys.argv[1]))