diff --git a/changelog.d/2080.change.rst b/changelog.d/2080.change.rst new file mode 100644 index 0000000000..d5878f4926 --- /dev/null +++ b/changelog.d/2080.change.rst @@ -0,0 +1 @@ +Add support for running `python -m setuptools cmd` as a setup.py alternative. diff --git a/setuptools/__main__.py b/setuptools/__main__.py new file mode 100644 index 0000000000..f71f47e6ac --- /dev/null +++ b/setuptools/__main__.py @@ -0,0 +1,10 @@ +""" +Command-line interface for running setup.py or new-fashioned setup.cfg after +setuptools is monkey-patched into distutils. +""" + +if __name__ == '__main__': + import sys + from setuptools.launch import run_setup + sys.argv[0] = 'setuptools' + run_setup() diff --git a/setuptools/launch.py b/setuptools/launch.py index 308283ea93..b456913b06 100644 --- a/setuptools/launch.py +++ b/setuptools/launch.py @@ -6,30 +6,43 @@ # Note that setuptools gets imported implicitly by the # invocation of this script using python -m setuptools.launch -import tokenize +import io +import os import sys +import tokenize + + +def _open_setup_script(setup_script, fallback): + if fallback and not os.path.exists(setup_script): + # Supply a default setup.py + return io.StringIO(u"from setuptools import setup; setup()") + + return getattr(tokenize, 'open', open)(setup_script) + + +def run_setup(setup_script='setup.py', fallback=True): + namespace = { + '__file__': setup_script, + '__name__': '__main__', + '__doc__': None + } + + with _open_setup_script(setup_script, fallback) as f: + code = f.read().replace(r'\r\n', r'\n') + + exec(compile(code, setup_script, 'exec'), namespace) -def run(): +def run_script(): """ Run the script in sys.argv[1] as if it had been invoked naturally. """ - __builtins__ script_name = sys.argv[1] - namespace = dict( - __file__=script_name, - __name__='__main__', - __doc__=None, - ) sys.argv[:] = sys.argv[1:] - open_ = getattr(tokenize, 'open', open) - script = open_(script_name).read() - norm_script = script.replace('\\r\\n', '\\n') - code = compile(norm_script, script_name, 'exec') - exec(code, namespace) + run_setup(script_name, fallback=False) if __name__ == '__main__': - run() + run_script()