diff options
Diffstat (limited to 'Lib/distutils/core.py')
-rw-r--r-- | Lib/distutils/core.py | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py index b89557d7679..260332a2ac6 100644 --- a/Lib/distutils/core.py +++ b/Lib/distutils/core.py @@ -6,14 +6,11 @@ indirectly provides the Distribution and Command classes, although they are really defined in distutils.dist and distutils.cmd. """ -__revision__ = "$Id$" - -import sys import os +import sys from distutils.debug import DEBUG -from distutils.errors import (DistutilsSetupError, DistutilsArgError, - DistutilsError, CCompilerError) +from distutils.errors import * from distutils.util import grok_environment_error # Mainly import these so setup scripts can "from distutils.core import" them. @@ -33,9 +30,9 @@ usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: %(script)s cmd --help """ -def gen_usage(script_name): +def gen_usage (script_name): script = os.path.basename(script_name) - return USAGE % {'script': script} + return USAGE % vars() # Some mild magic to control the behaviour of 'setup()' from 'run_setup()'. @@ -58,7 +55,7 @@ extension_keywords = ('name', 'sources', 'include_dirs', 'extra_objects', 'extra_compile_args', 'extra_link_args', 'swig_opts', 'export_symbols', 'depends', 'language') -def setup(**attrs): +def setup (**attrs): """The gateway to the Distutils: do everything your setup script needs to do, in a highly flexible and user-driven way. Briefly: create a Distribution instance; find and parse config files; parse the command @@ -103,19 +100,19 @@ def setup(**attrs): if 'script_name' not in attrs: attrs['script_name'] = os.path.basename(sys.argv[0]) - if 'script_args' not in attrs: + if 'script_args' not in attrs: attrs['script_args'] = sys.argv[1:] # Create the Distribution instance, using the remaining arguments # (ie. everything except distclass) to initialize it try: _setup_distribution = dist = klass(attrs) - except DistutilsSetupError, msg: - if 'name' in attrs: - raise SystemExit, "error in %s setup command: %s" % \ - (attrs['name'], msg) + except DistutilsSetupError as msg: + if 'name' not in attrs: + raise SystemExit("error in setup command: %s" % msg) else: - raise SystemExit, "error in setup command: %s" % msg + raise SystemExit("error in %s setup command: %s" % \ + (attrs['name'], msg)) if _setup_stop_after == "init": return dist @@ -125,22 +122,21 @@ def setup(**attrs): dist.parse_config_files() if DEBUG: - print "options (after parsing config files):" + print("options (after parsing config files):") dist.dump_option_dicts() if _setup_stop_after == "config": return dist - # Parse the command line and override config files; any - # command-line errors are the end user's fault, so turn them into - # SystemExit to suppress tracebacks. + # Parse the command line; any command-line errors are the end user's + # fault, so turn them into SystemExit to suppress tracebacks. try: ok = dist.parse_command_line() - except DistutilsArgError, msg: - raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg + except DistutilsArgError as msg: + raise SystemExit(gen_usage(dist.script_name) + "\nerror: %s" % msg) if DEBUG: - print "options (after parsing command line):" + print("options (after parsing command line):") dist.dump_option_dicts() if _setup_stop_after == "commandline": @@ -151,34 +147,36 @@ def setup(**attrs): try: dist.run_commands() except KeyboardInterrupt: - raise SystemExit, "interrupted" - except (IOError, os.error), exc: + raise SystemExit("interrupted") + except (IOError, os.error) as exc: error = grok_environment_error(exc) if DEBUG: sys.stderr.write(error + "\n") raise else: - raise SystemExit, error + raise SystemExit(error) except (DistutilsError, - CCompilerError), msg: + CCompilerError) as msg: if DEBUG: raise else: - raise SystemExit, "error: " + str(msg) + raise SystemExit("error: " + str(msg)) return dist +# setup () -def run_setup(script_name, script_args=None, stop_after="run"): + +def run_setup (script_name, script_args=None, stop_after="run"): """Run a setup script in a somewhat controlled environment, and return the Distribution instance that drives things. This is useful if you need to find out the distribution meta-data (passed as keyword args from 'script' to 'setup()', or the contents of the config files or command-line. - 'script_name' is a file that will be run with 'execfile()'; + 'script_name' is a file that will be read and run with 'exec()'; 'sys.argv[0]' will be replaced with 'script' for the duration of the call. 'script_args' is a list of strings; if supplied, 'sys.argv[1:]' will be replaced by 'script_args' for the duration of @@ -203,7 +201,7 @@ def run_setup(script_name, script_args=None, stop_after="run"): used to drive the Distutils. """ if stop_after not in ('init', 'config', 'commandline', 'run'): - raise ValueError, "invalid value for 'stop_after': %r" % (stop_after,) + raise ValueError("invalid value for 'stop_after': %r" % (stop_after,)) global _setup_stop_after, _setup_distribution _setup_stop_after = stop_after @@ -216,11 +214,8 @@ def run_setup(script_name, script_args=None, stop_after="run"): sys.argv[0] = script_name if script_args is not None: sys.argv[1:] = script_args - f = open(script_name) - try: - exec f.read() in g, l - finally: - f.close() + with open(script_name, 'rb') as f: + exec(f.read(), g, l) finally: sys.argv = save_argv _setup_stop_after = None @@ -232,11 +227,13 @@ def run_setup(script_name, script_args=None, stop_after="run"): raise if _setup_distribution is None: - raise RuntimeError, \ - ("'distutils.core.setup()' was never called -- " + raise RuntimeError(("'distutils.core.setup()' was never called -- " "perhaps '%s' is not a Distutils setup script?") % \ - script_name + script_name) # I wonder if the setup script's namespace -- g and l -- would be of # any interest to callers? + #print "_setup_distribution:", _setup_distribution return _setup_distribution + +# run_setup () |