Debian Codegraph @

mypyc/__main__.py

raw
1 """Mypyc command-line tool.
2
3 Usage:
4
5 $ mypyc foo.py [...]
6 $ python3 -c 'import foo' # Uses compiled 'foo'
7
8
9 This is just a thin wrapper that generates a setup.py file that uses
10 mypycify, suitable for prototyping and testing.
11 """
12
13 from __future__ import annotations
14
15 import os
16 import os.path
17 import subprocess
18 import sys
19
20 base_path = os.path.join(os.path.dirname(__file__), "..")
21
22 setup_format = """\
23 from setuptools import setup
24 from mypyc.build import mypycify
25
26 setup(
27 name='mypyc_output',
28 ext_modules=mypycify(
29 {},
30 opt_level="{}",
31 debug_level="{}",
32 strict_dunder_typing={},
33 log_trace={},
34 ),
35 )
36 """
37
38
39 def main() -> None:
40 build_dir = "build" # can this be overridden??
41 try:
42 os.mkdir(build_dir)
43 except FileExistsError:
44 pass
45
46 opt_level = os.getenv("MYPYC_OPT_LEVEL", "3")
47 debug_level = os.getenv("MYPYC_DEBUG_LEVEL", "1")
48 strict_dunder_typing = bool(int(os.getenv("MYPYC_STRICT_DUNDER_TYPING", "0")))
49 # If enabled, compiled code writes a sampled log of executed ops (or events) to
50 # mypyc_trace.txt.
51 log_trace = bool(int(os.getenv("MYPYC_LOG_TRACE", "0")))
52
53 setup_file = os.path.join(build_dir, "setup.py")
54 with open(setup_file, "w") as f:
55 f.write(
56 setup_format.format(
57 sys.argv[1:], opt_level, debug_level, strict_dunder_typing, log_trace
58 )
59 )
60
61 # We don't use run_setup (like we do in the test suite) because it throws
62 # away the error code from distutils, and we don't care about the slight
63 # performance loss here.
64 env = os.environ.copy()
65 base_path = os.path.join(os.path.dirname(__file__), "..")
66 env["PYTHONPATH"] = base_path + os.pathsep + env.get("PYTHONPATH", "")
67 cmd = subprocess.run([sys.executable, setup_file, "build_ext", "--inplace"], env=env)
68 sys.exit(cmd.returncode)
69
70
71 if __name__ == "__main__":
72 main()