Debian Codegraph @

mesonbuild/scripts/pycompile.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2016 The Meson development team
3
4 # ignore all lints for this file, since it is run by python2 as well
5
6 # type: ignore
7 # pylint: disable=deprecated-module
8
9 import json, os, subprocess, sys
10 from compileall import compile_file
11
12 quiet = int(os.environ.get('MESON_INSTALL_QUIET', 0))
13
14 def compileall(files):
15 for f in files:
16 # f is prefixed by {py_xxxxlib}, both variants are 12 chars
17 # the key is the middle 10 chars of the prefix
18 key = f[1:11].upper()
19 f = f[12:]
20
21 ddir = None
22 fullpath = absf = os.environ['MESON_INSTALL_DESTDIR_'+key] + f
23 f = os.environ['MESON_INSTALL_'+key] + f
24
25 if absf != f:
26 ddir = os.path.dirname(f)
27
28 if os.path.isdir(absf):
29 for root, _, files in os.walk(absf):
30 if ddir is not None:
31 ddir = root.replace(absf, f, 1)
32 for dirf in files:
33 if dirf.endswith('.py'):
34 fullpath = os.path.join(root, dirf)
35 compile_file(fullpath, ddir, force=True, quiet=quiet)
36 else:
37 compile_file(fullpath, ddir, force=True, quiet=quiet)
38
39 def run(manifest):
40 data_file = os.path.join(os.path.dirname(__file__), manifest)
41 with open(data_file, 'rb') as f:
42 dat = json.load(f)
43 compileall(dat)
44
45 if __name__ == '__main__':
46 manifest = sys.argv[1]
47 run(manifest)
48 if len(sys.argv) > 2:
49 optlevel = int(sys.argv[2])
50 # python2 only needs one or the other
51 if optlevel == 1 or (sys.version_info >= (3,) and optlevel > 0):
52 subprocess.check_call([sys.executable, '-O'] + sys.argv[:2])
53 if optlevel == 2:
54 subprocess.check_call([sys.executable, '-OO'] + sys.argv[:2])