__main__.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Mypyc command-line tool.
  2. Usage:
  3. $ mypyc foo.py [...]
  4. $ python3 -c 'import foo' # Uses compiled 'foo'
  5. This is just a thin wrapper that generates a setup.py file that uses
  6. mypycify, suitable for prototyping and testing.
  7. """
  8. from __future__ import annotations
  9. import os
  10. import os.path
  11. import subprocess
  12. import sys
  13. base_path = os.path.join(os.path.dirname(__file__), "..")
  14. setup_format = """\
  15. from setuptools import setup
  16. from mypyc.build import mypycify
  17. setup(name='mypyc_output',
  18. ext_modules=mypycify({}, opt_level="{}", debug_level="{}"),
  19. )
  20. """
  21. def main() -> None:
  22. build_dir = "build" # can this be overridden??
  23. try:
  24. os.mkdir(build_dir)
  25. except FileExistsError:
  26. pass
  27. opt_level = os.getenv("MYPYC_OPT_LEVEL", "3")
  28. debug_level = os.getenv("MYPYC_DEBUG_LEVEL", "1")
  29. setup_file = os.path.join(build_dir, "setup.py")
  30. with open(setup_file, "w") as f:
  31. f.write(setup_format.format(sys.argv[1:], opt_level, debug_level))
  32. # We don't use run_setup (like we do in the test suite) because it throws
  33. # away the error code from distutils, and we don't care about the slight
  34. # performance loss here.
  35. env = os.environ.copy()
  36. base_path = os.path.join(os.path.dirname(__file__), "..")
  37. env["PYTHONPATH"] = base_path + os.pathsep + env.get("PYTHONPATH", "")
  38. cmd = subprocess.run([sys.executable, setup_file, "build_ext", "--inplace"], env=env)
  39. sys.exit(cmd.returncode)
  40. if __name__ == "__main__":
  41. main()