github.com/replit/upm@v0.0.0-20240423230255-9ce4fc3ea24c/internal/backends/python/gen_pypi_map/dump_setup.py (about) 1 # Everything is in a try catch to ensure no errors sneak past the caller 2 try: 3 import json 4 import os 5 import runpy 6 import sys 7 8 metadata = None 9 10 def setup_callback(*args, **kwargs): 11 global metadata 12 if metadata is None: 13 metadata = kwargs 14 else: 15 raise Exception("Setup called more then once") 16 17 # Monkeypatch setup functions to use our callback 18 try: 19 import setuptools 20 setuptools.setup = setup_callback 21 except ModuleNotFoundError: 22 pass 23 24 try: 25 import distutils.core 26 distutils.core.setup = setup_callback 27 except ModuleNotFoundError: 28 pass 29 30 # Locate setup.py 31 setup_py = os.path.abspath(sys.argv[1]) 32 working_dir = os.path.dirname(setup_py) 33 os.chdir(working_dir) 34 35 # Redirect stdout to stderr for the setup scipt so we don't pollute stdout 36 stdout = sys.stdout 37 sys.stdout = sys.stderr 38 39 # Run setup.py 40 runpy.run_path(setup_py, run_name="__main__") 41 42 # Put stdout back so we can report metadata 43 sys.stdout = stdout 44 45 # Make sure setup was called once 46 if metadata is None: 47 raise Exception("Setup was never called") 48 49 # Report metadata to caller 50 print(json.dumps(metadata, default=lambda o: repr(o))) 51 52 except Exception as e: 53 # If any errors occur, serialize and report 54 print(json.dumps({'error': str(e)}))