0001 """distutils.core 0002 0003 The only module that needs to be imported to use the Distutils; provides 0004 the 'setup' function (which is to be called from the setup script). Also 0005 indirectly provides the Distribution and Command classes, although they are 0006 really defined in distutils.dist and distutils.cmd. 0007 """ 0008 0009 # This module should be kept compatible with Python 1.5.2. 0010 0011 __revision__ = "$Id: core.py,v 1.63 2004/10/14 10:02:07 anthonybaxter Exp $" 0012 0013 import sys, os 0014 from types import * 0015 0016 from distutils.debug import DEBUG 0017 from distutils.errors import * 0018 from distutils.util import grok_environment_error 0019 0020 # Mainly import these so setup scripts can "from distutils.core import" them. 0021 from distutils.dist import Distribution 0022 from distutils.cmd import Command 0023 from distutils.extension import Extension 0024 0025 # This is a barebones help message generated displayed when the user 0026 # runs the setup script with no arguments at all. More useful help 0027 # is generated with various --help options: global help, list commands, 0028 # and per-command help. 0029 USAGE = """\ 0030 usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 0031 or: %(script)s --help [cmd1 cmd2 ...] 0032 or: %(script)s --help-commands 0033 or: %(script)s cmd --help 0034 """ 0035 0036 def gen_usage (script_name): 0037 script = os.path.basename(script_name) 0038 return USAGE % vars() 0039 0040 0041 # Some mild magic to control the behaviour of 'setup()' from 'run_setup()'. 0042 _setup_stop_after = None 0043 _setup_distribution = None 0044 0045 # Legal keyword arguments for the setup() function 0046 setup_keywords = ('distclass', 'script_name', 'script_args', 'options', 0047 'name', 'version', 'author', 'author_email', 0048 'maintainer', 'maintainer_email', 'url', 'license', 0049 'description', 'long_description', 'keywords', 0050 'platforms', 'classifiers', 'download_url',) 0051 0052 # Legal keyword arguments for the Extension constructor 0053 extension_keywords = ('name', 'sources', 'include_dirs', 0054 'define_macros', 'undef_macros', 0055 'library_dirs', 'libraries', 'runtime_library_dirs', 0056 'extra_objects', 'extra_compile_args', 'extra_link_args', 0057 'swig_opts', 'export_symbols', 'depends', 'language') 0058 0059 def setup (**attrs): 0060 """The gateway to the Distutils: do everything your setup script needs 0061 to do, in a highly flexible and user-driven way. Briefly: create a 0062 Distribution instance; find and parse config files; parse the command 0063 line; run each Distutils command found there, customized by the options 0064 supplied to 'setup()' (as keyword arguments), in config files, and on 0065 the command line. 0066 0067 The Distribution instance might be an instance of a class supplied via 0068 the 'distclass' keyword argument to 'setup'; if no such class is 0069 supplied, then the Distribution class (in dist.py) is instantiated. 0070 All other arguments to 'setup' (except for 'cmdclass') are used to set 0071 attributes of the Distribution instance. 0072 0073 The 'cmdclass' argument, if supplied, is a dictionary mapping command 0074 names to command classes. Each command encountered on the command line 0075 will be turned into a command class, which is in turn instantiated; any 0076 class found in 'cmdclass' is used in place of the default, which is 0077 (for command 'foo_bar') class 'foo_bar' in module 0078 'distutils.command.foo_bar'. The command class must provide a 0079 'user_options' attribute which is a list of option specifiers for 0080 'distutils.fancy_getopt'. Any command-line options between the current 0081 and the next command are used to set attributes of the current command 0082 object. 0083 0084 When the entire command-line has been successfully parsed, calls the 0085 'run()' method on each command object in turn. This method will be 0086 driven entirely by the Distribution object (which each command object 0087 has a reference to, thanks to its constructor), and the 0088 command-specific options that became attributes of each command 0089 object. 0090 """ 0091 0092 global _setup_stop_after, _setup_distribution 0093 0094 # Determine the distribution class -- either caller-supplied or 0095 # our Distribution (see below). 0096 klass = attrs.get('distclass') 0097 if klass: 0098 del attrs['distclass'] 0099 else: 0100 klass = Distribution 0101 0102 if not attrs.has_key('script_name'): 0103 attrs['script_name'] = os.path.basename(sys.argv[0]) 0104 if not attrs.has_key('script_args'): 0105 attrs['script_args'] = sys.argv[1:] 0106 0107 # Create the Distribution instance, using the remaining arguments 0108 # (ie. everything except distclass) to initialize it 0109 try: 0110 _setup_distribution = dist = klass(attrs) 0111 except DistutilsSetupError, msg: 0112 if attrs.has_key('name'): 0113 raise SystemExit, "error in %s setup command: %s" % \ 0114 (attrs['name'], msg) 0115 else: 0116 raise SystemExit, "error in setup command: %s" % msg 0117 0118 if _setup_stop_after == "init": 0119 return dist 0120 0121 # Find and parse the config file(s): they will override options from 0122 # the setup script, but be overridden by the command line. 0123 dist.parse_config_files() 0124 0125 if DEBUG: 0126 print "options (after parsing config files):" 0127 dist.dump_option_dicts() 0128 0129 if _setup_stop_after == "config": 0130 return dist 0131 0132 # Parse the command line; any command-line errors are the end user's 0133 # fault, so turn them into SystemExit to suppress tracebacks. 0134 try: 0135 ok = dist.parse_command_line() 0136 except DistutilsArgError, msg: 0137 raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg 0138 0139 if DEBUG: 0140 print "options (after parsing command line):" 0141 dist.dump_option_dicts() 0142 0143 if _setup_stop_after == "commandline": 0144 return dist 0145 0146 # And finally, run all the commands found on the command line. 0147 if ok: 0148 try: 0149 dist.run_commands() 0150 except KeyboardInterrupt: 0151 raise SystemExit, "interrupted" 0152 except (IOError, os.error), exc: 0153 error = grok_environment_error(exc) 0154 0155 if DEBUG: 0156 sys.stderr.write(error + "\n") 0157 raise 0158 else: 0159 raise SystemExit, error 0160 0161 except (DistutilsError, 0162 CCompilerError), msg: 0163 if DEBUG: 0164 raise 0165 else: 0166 raise SystemExit, "error: " + str(msg) 0167 0168 return dist 0169 0170 # setup () 0171 0172 0173 def run_setup (script_name, script_args=None, stop_after="run"): 0174 """Run a setup script in a somewhat controlled environment, and 0175 return the Distribution instance that drives things. This is useful 0176 if you need to find out the distribution meta-data (passed as 0177 keyword args from 'script' to 'setup()', or the contents of the 0178 config files or command-line. 0179 0180 'script_name' is a file that will be run with 'execfile()'; 0181 'sys.argv[0]' will be replaced with 'script' for the duration of the 0182 call. 'script_args' is a list of strings; if supplied, 0183 'sys.argv[1:]' will be replaced by 'script_args' for the duration of 0184 the call. 0185 0186 'stop_after' tells 'setup()' when to stop processing; possible 0187 values: 0188 init 0189 stop after the Distribution instance has been created and 0190 populated with the keyword arguments to 'setup()' 0191 config 0192 stop after config files have been parsed (and their data 0193 stored in the Distribution instance) 0194 commandline 0195 stop after the command-line ('sys.argv[1:]' or 'script_args') 0196 have been parsed (and the data stored in the Distribution) 0197 run [default] 0198 stop after all commands have been run (the same as if 'setup()' 0199 had been called in the usual way 0200 0201 Returns the Distribution instance, which provides all information 0202 used to drive the Distutils. 0203 """ 0204 if stop_after not in ('init', 'config', 'commandline', 'run'): 0205 raise ValueError, "invalid value for 'stop_after': %r" % (stop_after,) 0206 0207 global _setup_stop_after, _setup_distribution 0208 _setup_stop_after = stop_after 0209 0210 save_argv = sys.argv 0211 g = {} 0212 l = {} 0213 try: 0214 try: 0215 sys.argv[0] = script_name 0216 if script_args is not None: 0217 sys.argv[1:] = script_args 0218 execfile(script_name, g, l) 0219 finally: 0220 sys.argv = save_argv 0221 _setup_stop_after = None 0222 except SystemExit: 0223 # Hmm, should we do something if exiting with a non-zero code 0224 # (ie. error)? 0225 pass 0226 except: 0227 raise 0228 0229 if _setup_distribution is None: 0230 raise RuntimeError, \ 0231 ("'distutils.core.setup()' was never called -- " 0232 "perhaps '%s' is not a Distutils setup script?") % \ 0233 script_name 0234 0235 # I wonder if the setup script's namespace -- g and l -- would be of 0236 # any interest to callers? 0237 #print "_setup_distribution:", _setup_distribution 0238 return _setup_distribution 0239 0240 # run_setup () 0241
Generated by PyXR 0.9.4