blob: 6e4892039eef92dbd4513f0b176c076a49bdcd6b [file] [log] [blame]
Greg Ward2689e3d1999-03-22 14:52:19 +00001"""distutils.core
2
3The only module that needs to be imported to use the Distutils; provides
Greg Wardfe6462c2000-04-04 01:40:52 +00004the 'setup' function (which is to be called from the setup script). Also
5indirectly provides the Distribution and Command classes, although they are
Greg Ward8ff5a3f2000-06-02 00:44:53 +00006really defined in distutils.dist and distutils.cmd.
7"""
Greg Ward2689e3d1999-03-22 14:52:19 +00008
Greg Ward3ce77fd2000-03-02 01:49:45 +00009__revision__ = "$Id$"
Greg Ward2689e3d1999-03-22 14:52:19 +000010
Greg Wardd80506c2000-04-22 03:20:49 +000011import sys, os
Jeremy Hylton115fdc62002-06-04 21:05:05 +000012
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000013from distutils.debug import DEBUG
Greg Ward2689e3d1999-03-22 14:52:19 +000014from distutils.errors import *
Greg Ward71257c72000-06-21 02:59:14 +000015from distutils.util import grok_environment_error
Greg Warda76bbd42000-05-31 01:11:20 +000016
17# Mainly import these so setup scripts can "from distutils.core import" them.
Greg Wardfe6462c2000-04-04 01:40:52 +000018from distutils.dist import Distribution
19from distutils.cmd import Command
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000020from distutils.config import PyPIRCCommand
Greg Warda76bbd42000-05-31 01:11:20 +000021from distutils.extension import Extension
22
Greg Ward4c96db12000-02-18 00:26:23 +000023# This is a barebones help message generated displayed when the user
24# runs the setup script with no arguments at all. More useful help
25# is generated with various --help options: global help, list commands,
26# and per-command help.
Greg Ward9821bf42000-08-29 01:15:18 +000027USAGE = """\
28usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
29 or: %(script)s --help [cmd1 cmd2 ...]
30 or: %(script)s --help-commands
31 or: %(script)s cmd --help
32"""
Greg Ward2689e3d1999-03-22 14:52:19 +000033
Greg Ward9821bf42000-08-29 01:15:18 +000034def gen_usage (script_name):
35 script = os.path.basename(script_name)
36 return USAGE % vars()
37
Greg Ward37af1c32000-05-26 00:54:52 +000038
Greg Warde3644e22000-09-01 00:52:45 +000039# Some mild magic to control the behaviour of 'setup()' from 'run_setup()'.
40_setup_stop_after = None
41_setup_distribution = None
42
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000043# Legal keyword arguments for the setup() function
44setup_keywords = ('distclass', 'script_name', 'script_args', 'options',
45 'name', 'version', 'author', 'author_email',
46 'maintainer', 'maintainer_email', 'url', 'license',
47 'description', 'long_description', 'keywords',
Fred Drakedb7b0022005-03-20 22:19:47 +000048 'platforms', 'classifiers', 'download_url',
49 'requires', 'provides', 'obsoletes',
50 )
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000051
52# Legal keyword arguments for the Extension constructor
53extension_keywords = ('name', 'sources', 'include_dirs',
54 'define_macros', 'undef_macros',
55 'library_dirs', 'libraries', 'runtime_library_dirs',
56 'extra_objects', 'extra_compile_args', 'extra_link_args',
Anthony Baxtera0240342004-10-14 10:02:08 +000057 'swig_opts', 'export_symbols', 'depends', 'language')
Greg Warde3644e22000-09-01 00:52:45 +000058
Greg Ward2689e3d1999-03-22 14:52:19 +000059def setup (**attrs):
Greg Ward8ff5a3f2000-06-02 00:44:53 +000060 """The gateway to the Distutils: do everything your setup script needs
61 to do, in a highly flexible and user-driven way. Briefly: create a
62 Distribution instance; find and parse config files; parse the command
Greg Ward9821bf42000-08-29 01:15:18 +000063 line; run each Distutils command found there, customized by the options
64 supplied to 'setup()' (as keyword arguments), in config files, and on
65 the command line.
Greg Ward2689e3d1999-03-22 14:52:19 +000066
Greg Ward8ff5a3f2000-06-02 00:44:53 +000067 The Distribution instance might be an instance of a class supplied via
68 the 'distclass' keyword argument to 'setup'; if no such class is
69 supplied, then the Distribution class (in dist.py) is instantiated.
70 All other arguments to 'setup' (except for 'cmdclass') are used to set
71 attributes of the Distribution instance.
Greg Ward2689e3d1999-03-22 14:52:19 +000072
Greg Ward8ff5a3f2000-06-02 00:44:53 +000073 The 'cmdclass' argument, if supplied, is a dictionary mapping command
74 names to command classes. Each command encountered on the command line
75 will be turned into a command class, which is in turn instantiated; any
76 class found in 'cmdclass' is used in place of the default, which is
77 (for command 'foo_bar') class 'foo_bar' in module
78 'distutils.command.foo_bar'. The command class must provide a
79 'user_options' attribute which is a list of option specifiers for
80 'distutils.fancy_getopt'. Any command-line options between the current
81 and the next command are used to set attributes of the current command
82 object.
Greg Ward2689e3d1999-03-22 14:52:19 +000083
Greg Ward8ff5a3f2000-06-02 00:44:53 +000084 When the entire command-line has been successfully parsed, calls the
85 'run()' method on each command object in turn. This method will be
86 driven entirely by the Distribution object (which each command object
87 has a reference to, thanks to its constructor), and the
88 command-specific options that became attributes of each command
89 object.
90 """
Greg Ward2689e3d1999-03-22 14:52:19 +000091
Greg Warde3644e22000-09-01 00:52:45 +000092 global _setup_stop_after, _setup_distribution
93
Greg Ward2689e3d1999-03-22 14:52:19 +000094 # Determine the distribution class -- either caller-supplied or
95 # our Distribution (see below).
Greg Wardbe86bde2000-09-26 01:56:15 +000096 klass = attrs.get('distclass')
Greg Ward2689e3d1999-03-22 14:52:19 +000097 if klass:
98 del attrs['distclass']
99 else:
100 klass = Distribution
101
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000102 if 'script_name' not in attrs:
Thomas Heller8560bb82002-11-07 16:41:38 +0000103 attrs['script_name'] = os.path.basename(sys.argv[0])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000104 if 'script_args' not in attrs:
Greg Ward9821bf42000-08-29 01:15:18 +0000105 attrs['script_args'] = sys.argv[1:]
106
Greg Ward2689e3d1999-03-22 14:52:19 +0000107 # Create the Distribution instance, using the remaining arguments
108 # (ie. everything except distclass) to initialize it
Greg Ward39851512000-06-03 01:02:06 +0000109 try:
Greg Wardbe86bde2000-09-26 01:56:15 +0000110 _setup_distribution = dist = klass(attrs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000111 except DistutilsSetupError as msg:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000112 if 'name' not in attrs:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000113 raise SystemExit("error in setup command: %s" % msg)
Collin Winter2c8fef02007-07-17 00:38:21 +0000114 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000115 raise SystemExit("error in %s setup command: %s" % \
116 (attrs['name'], msg))
Greg Ward2689e3d1999-03-22 14:52:19 +0000117
Greg Warde3644e22000-09-01 00:52:45 +0000118 if _setup_stop_after == "init":
119 return dist
120
Gregory P. Smithbb8c71d2000-05-12 00:42:19 +0000121 # Find and parse the config file(s): they will override options from
122 # the setup script, but be overridden by the command line.
123 dist.parse_config_files()
Fred Drakeb94b8492001-12-06 20:51:35 +0000124
Greg Wardf7a55072000-06-02 01:55:36 +0000125 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000126 print("options (after parsing config files):")
Greg Wardf7a55072000-06-02 01:55:36 +0000127 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +0000128
Greg Warde3644e22000-09-01 00:52:45 +0000129 if _setup_stop_after == "config":
130 return dist
131
Gregory P. Smithbb8c71d2000-05-12 00:42:19 +0000132 # Parse the command line; any command-line errors are the end user's
Greg Ward42926dd1999-09-08 02:41:09 +0000133 # fault, so turn them into SystemExit to suppress tracebacks.
Greg Ward2689e3d1999-03-22 14:52:19 +0000134 try:
Greg Ward9821bf42000-08-29 01:15:18 +0000135 ok = dist.parse_command_line()
Guido van Rossumb940e112007-01-10 16:19:56 +0000136 except DistutilsArgError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000137 raise SystemExit(gen_usage(dist.script_name) + "\nerror: %s" % msg)
Greg Ward2689e3d1999-03-22 14:52:19 +0000138
Greg Wardf7a55072000-06-02 01:55:36 +0000139 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000140 print("options (after parsing command line):")
Greg Wardf7a55072000-06-02 01:55:36 +0000141 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +0000142
Greg Warde3644e22000-09-01 00:52:45 +0000143 if _setup_stop_after == "commandline":
144 return dist
145
Greg Ward2689e3d1999-03-22 14:52:19 +0000146 # And finally, run all the commands found on the command line.
Greg Wardc9c37b11999-12-12 16:51:44 +0000147 if ok:
148 try:
Greg Wardbe86bde2000-09-26 01:56:15 +0000149 dist.run_commands()
Greg Wardc9c37b11999-12-12 16:51:44 +0000150 except KeyboardInterrupt:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000151 raise SystemExit("interrupted")
Guido van Rossumb940e112007-01-10 16:19:56 +0000152 except (IOError, os.error) as exc:
Greg Wardcf0e2dd2000-06-17 02:17:45 +0000153 error = grok_environment_error(exc)
Greg Ward37af1c32000-05-26 00:54:52 +0000154
155 if DEBUG:
156 sys.stderr.write(error + "\n")
157 raise
158 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000159 raise SystemExit(error)
Fred Drakeb94b8492001-12-06 20:51:35 +0000160
Andrew M. Kuchling91e77532002-11-08 16:18:24 +0000161 except (DistutilsError,
Guido van Rossumb940e112007-01-10 16:19:56 +0000162 CCompilerError) as msg:
Greg Ward37af1c32000-05-26 00:54:52 +0000163 if DEBUG:
164 raise
165 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000166 raise SystemExit("error: " + str(msg))
Greg Ward2689e3d1999-03-22 14:52:19 +0000167
Greg Warde3644e22000-09-01 00:52:45 +0000168 return dist
169
Greg Ward2689e3d1999-03-22 14:52:19 +0000170# setup ()
Greg Warde3644e22000-09-01 00:52:45 +0000171
172
173def run_setup (script_name, script_args=None, stop_after="run"):
174 """Run a setup script in a somewhat controlled environment, and
175 return the Distribution instance that drives things. This is useful
176 if you need to find out the distribution meta-data (passed as
177 keyword args from 'script' to 'setup()', or the contents of the
178 config files or command-line.
179
Neal Norwitz01688022007-08-12 00:43:29 +0000180 'script_name' is a file that will be read and run with 'exec()';
Greg Warde3644e22000-09-01 00:52:45 +0000181 'sys.argv[0]' will be replaced with 'script' for the duration of the
182 call. 'script_args' is a list of strings; if supplied,
183 'sys.argv[1:]' will be replaced by 'script_args' for the duration of
184 the call.
185
186 'stop_after' tells 'setup()' when to stop processing; possible
187 values:
188 init
189 stop after the Distribution instance has been created and
190 populated with the keyword arguments to 'setup()'
191 config
192 stop after config files have been parsed (and their data
193 stored in the Distribution instance)
194 commandline
195 stop after the command-line ('sys.argv[1:]' or 'script_args')
196 have been parsed (and the data stored in the Distribution)
197 run [default]
198 stop after all commands have been run (the same as if 'setup()'
199 had been called in the usual way
200
201 Returns the Distribution instance, which provides all information
202 used to drive the Distutils.
203 """
204 if stop_after not in ('init', 'config', 'commandline', 'run'):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000205 raise ValueError("invalid value for 'stop_after': %r" % (stop_after,))
Greg Warde3644e22000-09-01 00:52:45 +0000206
207 global _setup_stop_after, _setup_distribution
208 _setup_stop_after = stop_after
209
210 save_argv = sys.argv
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000211 g = {'__file__': script_name}
Greg Warde3644e22000-09-01 00:52:45 +0000212 l = {}
213 try:
214 try:
215 sys.argv[0] = script_name
216 if script_args is not None:
217 sys.argv[1:] = script_args
Neal Norwitz01688022007-08-12 00:43:29 +0000218 exec(open(script_name).read(), g, l)
Greg Warde3644e22000-09-01 00:52:45 +0000219 finally:
220 sys.argv = save_argv
221 _setup_stop_after = None
222 except SystemExit:
223 # Hmm, should we do something if exiting with a non-zero code
224 # (ie. error)?
225 pass
226 except:
227 raise
228
229 if _setup_distribution is None:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000230 raise RuntimeError(("'distutils.core.setup()' was never called -- "
Greg Warde3644e22000-09-01 00:52:45 +0000231 "perhaps '%s' is not a Distutils setup script?") % \
Collin Winter5b7e9d72007-08-30 03:52:21 +0000232 script_name)
Greg Warde3644e22000-09-01 00:52:45 +0000233
234 # I wonder if the setup script's namespace -- g and l -- would be of
235 # any interest to callers?
236 #print "_setup_distribution:", _setup_distribution
237 return _setup_distribution
238
239# run_setup ()