blob: 260332a2ac69b2cd81c7deae7e496d5c0d5a524f [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
Victor Stinnerdc9b1ea2011-06-30 15:40:22 +02009import os
10import sys
Jeremy Hylton115fdc62002-06-04 21:05:05 +000011
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000012from distutils.debug import DEBUG
Tarek Ziadé36797272010-07-22 12:50:05 +000013from distutils.errors import *
Greg Ward71257c72000-06-21 02:59:14 +000014from distutils.util import grok_environment_error
Greg Warda76bbd42000-05-31 01:11:20 +000015
16# Mainly import these so setup scripts can "from distutils.core import" them.
Greg Wardfe6462c2000-04-04 01:40:52 +000017from distutils.dist import Distribution
18from distutils.cmd import Command
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000019from distutils.config import PyPIRCCommand
Greg Warda76bbd42000-05-31 01:11:20 +000020from distutils.extension import Extension
21
Greg Ward4c96db12000-02-18 00:26:23 +000022# This is a barebones help message generated displayed when the user
23# runs the setup script with no arguments at all. More useful help
24# is generated with various --help options: global help, list commands,
25# and per-command help.
Greg Ward9821bf42000-08-29 01:15:18 +000026USAGE = """\
27usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
28 or: %(script)s --help [cmd1 cmd2 ...]
29 or: %(script)s --help-commands
30 or: %(script)s cmd --help
31"""
Greg Ward2689e3d1999-03-22 14:52:19 +000032
Tarek Ziadé36797272010-07-22 12:50:05 +000033def gen_usage (script_name):
Greg Ward9821bf42000-08-29 01:15:18 +000034 script = os.path.basename(script_name)
Tarek Ziadé36797272010-07-22 12:50:05 +000035 return USAGE % vars()
Greg Ward9821bf42000-08-29 01:15:18 +000036
Greg Ward37af1c32000-05-26 00:54:52 +000037
Greg Warde3644e22000-09-01 00:52:45 +000038# Some mild magic to control the behaviour of 'setup()' from 'run_setup()'.
39_setup_stop_after = None
40_setup_distribution = None
41
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000042# Legal keyword arguments for the setup() function
43setup_keywords = ('distclass', 'script_name', 'script_args', 'options',
44 'name', 'version', 'author', 'author_email',
45 'maintainer', 'maintainer_email', 'url', 'license',
46 'description', 'long_description', 'keywords',
Fred Drakedb7b0022005-03-20 22:19:47 +000047 'platforms', 'classifiers', 'download_url',
48 'requires', 'provides', 'obsoletes',
49 )
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000050
51# Legal keyword arguments for the Extension constructor
52extension_keywords = ('name', 'sources', 'include_dirs',
53 'define_macros', 'undef_macros',
54 'library_dirs', 'libraries', 'runtime_library_dirs',
55 'extra_objects', 'extra_compile_args', 'extra_link_args',
Anthony Baxtera0240342004-10-14 10:02:08 +000056 'swig_opts', 'export_symbols', 'depends', 'language')
Greg Warde3644e22000-09-01 00:52:45 +000057
Tarek Ziadé36797272010-07-22 12:50:05 +000058def setup (**attrs):
Greg Ward8ff5a3f2000-06-02 00:44:53 +000059 """The gateway to the Distutils: do everything your setup script needs
60 to do, in a highly flexible and user-driven way. Briefly: create a
61 Distribution instance; find and parse config files; parse the command
Greg Ward9821bf42000-08-29 01:15:18 +000062 line; run each Distutils command found there, customized by the options
63 supplied to 'setup()' (as keyword arguments), in config files, and on
64 the command line.
Greg Ward2689e3d1999-03-22 14:52:19 +000065
Greg Ward8ff5a3f2000-06-02 00:44:53 +000066 The Distribution instance might be an instance of a class supplied via
67 the 'distclass' keyword argument to 'setup'; if no such class is
68 supplied, then the Distribution class (in dist.py) is instantiated.
69 All other arguments to 'setup' (except for 'cmdclass') are used to set
70 attributes of the Distribution instance.
Greg Ward2689e3d1999-03-22 14:52:19 +000071
Greg Ward8ff5a3f2000-06-02 00:44:53 +000072 The 'cmdclass' argument, if supplied, is a dictionary mapping command
73 names to command classes. Each command encountered on the command line
74 will be turned into a command class, which is in turn instantiated; any
75 class found in 'cmdclass' is used in place of the default, which is
76 (for command 'foo_bar') class 'foo_bar' in module
77 'distutils.command.foo_bar'. The command class must provide a
78 'user_options' attribute which is a list of option specifiers for
79 'distutils.fancy_getopt'. Any command-line options between the current
80 and the next command are used to set attributes of the current command
81 object.
Greg Ward2689e3d1999-03-22 14:52:19 +000082
Greg Ward8ff5a3f2000-06-02 00:44:53 +000083 When the entire command-line has been successfully parsed, calls the
84 'run()' method on each command object in turn. This method will be
85 driven entirely by the Distribution object (which each command object
86 has a reference to, thanks to its constructor), and the
87 command-specific options that became attributes of each command
88 object.
89 """
Greg Ward2689e3d1999-03-22 14:52:19 +000090
Greg Warde3644e22000-09-01 00:52:45 +000091 global _setup_stop_after, _setup_distribution
92
Greg Ward2689e3d1999-03-22 14:52:19 +000093 # Determine the distribution class -- either caller-supplied or
94 # our Distribution (see below).
Greg Wardbe86bde2000-09-26 01:56:15 +000095 klass = attrs.get('distclass')
Greg Ward2689e3d1999-03-22 14:52:19 +000096 if klass:
97 del attrs['distclass']
98 else:
99 klass = Distribution
100
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000101 if 'script_name' not in attrs:
Thomas Heller8560bb82002-11-07 16:41:38 +0000102 attrs['script_name'] = os.path.basename(sys.argv[0])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000103 if 'script_args' not in attrs:
Greg Ward9821bf42000-08-29 01:15:18 +0000104 attrs['script_args'] = sys.argv[1:]
105
Greg Ward2689e3d1999-03-22 14:52:19 +0000106 # Create the Distribution instance, using the remaining arguments
107 # (ie. everything except distclass) to initialize it
Greg Ward39851512000-06-03 01:02:06 +0000108 try:
Greg Wardbe86bde2000-09-26 01:56:15 +0000109 _setup_distribution = dist = klass(attrs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000110 except DistutilsSetupError as msg:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000111 if 'name' not in attrs:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000112 raise SystemExit("error in setup command: %s" % msg)
Collin Winter2c8fef02007-07-17 00:38:21 +0000113 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000114 raise SystemExit("error in %s setup command: %s" % \
115 (attrs['name'], msg))
Greg Ward2689e3d1999-03-22 14:52:19 +0000116
Greg Warde3644e22000-09-01 00:52:45 +0000117 if _setup_stop_after == "init":
118 return dist
119
Gregory P. Smithbb8c71d2000-05-12 00:42:19 +0000120 # Find and parse the config file(s): they will override options from
121 # the setup script, but be overridden by the command line.
122 dist.parse_config_files()
Fred Drakeb94b8492001-12-06 20:51:35 +0000123
Greg Wardf7a55072000-06-02 01:55:36 +0000124 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000125 print("options (after parsing config files):")
Greg Wardf7a55072000-06-02 01:55:36 +0000126 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +0000127
Greg Warde3644e22000-09-01 00:52:45 +0000128 if _setup_stop_after == "config":
129 return dist
130
Tarek Ziadé36797272010-07-22 12:50:05 +0000131 # Parse the command line; any command-line errors are the end user's
132 # fault, so turn them into SystemExit to suppress tracebacks.
Greg Ward2689e3d1999-03-22 14:52:19 +0000133 try:
Greg Ward9821bf42000-08-29 01:15:18 +0000134 ok = dist.parse_command_line()
Guido van Rossumb940e112007-01-10 16:19:56 +0000135 except DistutilsArgError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000136 raise SystemExit(gen_usage(dist.script_name) + "\nerror: %s" % msg)
Greg Ward2689e3d1999-03-22 14:52:19 +0000137
Greg Wardf7a55072000-06-02 01:55:36 +0000138 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000139 print("options (after parsing command line):")
Greg Wardf7a55072000-06-02 01:55:36 +0000140 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +0000141
Greg Warde3644e22000-09-01 00:52:45 +0000142 if _setup_stop_after == "commandline":
143 return dist
144
Greg Ward2689e3d1999-03-22 14:52:19 +0000145 # And finally, run all the commands found on the command line.
Greg Wardc9c37b11999-12-12 16:51:44 +0000146 if ok:
147 try:
Greg Wardbe86bde2000-09-26 01:56:15 +0000148 dist.run_commands()
Greg Wardc9c37b11999-12-12 16:51:44 +0000149 except KeyboardInterrupt:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000150 raise SystemExit("interrupted")
Guido van Rossumb940e112007-01-10 16:19:56 +0000151 except (IOError, os.error) as exc:
Greg Wardcf0e2dd2000-06-17 02:17:45 +0000152 error = grok_environment_error(exc)
Greg Ward37af1c32000-05-26 00:54:52 +0000153
154 if DEBUG:
155 sys.stderr.write(error + "\n")
156 raise
157 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000158 raise SystemExit(error)
Fred Drakeb94b8492001-12-06 20:51:35 +0000159
Andrew M. Kuchling91e77532002-11-08 16:18:24 +0000160 except (DistutilsError,
Guido van Rossumb940e112007-01-10 16:19:56 +0000161 CCompilerError) as msg:
Greg Ward37af1c32000-05-26 00:54:52 +0000162 if DEBUG:
163 raise
164 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000165 raise SystemExit("error: " + str(msg))
Greg Ward2689e3d1999-03-22 14:52:19 +0000166
Greg Warde3644e22000-09-01 00:52:45 +0000167 return dist
168
Tarek Ziadé36797272010-07-22 12:50:05 +0000169# setup ()
Greg Warde3644e22000-09-01 00:52:45 +0000170
Tarek Ziadé36797272010-07-22 12:50:05 +0000171
172def run_setup (script_name, script_args=None, stop_after="run"):
Greg Warde3644e22000-09-01 00:52:45 +0000173 """Run a setup script in a somewhat controlled environment, and
174 return the Distribution instance that drives things. This is useful
175 if you need to find out the distribution meta-data (passed as
176 keyword args from 'script' to 'setup()', or the contents of the
177 config files or command-line.
178
Neal Norwitz01688022007-08-12 00:43:29 +0000179 'script_name' is a file that will be read and run with 'exec()';
Greg Warde3644e22000-09-01 00:52:45 +0000180 'sys.argv[0]' will be replaced with 'script' for the duration of the
181 call. 'script_args' is a list of strings; if supplied,
182 'sys.argv[1:]' will be replaced by 'script_args' for the duration of
183 the call.
184
185 'stop_after' tells 'setup()' when to stop processing; possible
186 values:
187 init
188 stop after the Distribution instance has been created and
189 populated with the keyword arguments to 'setup()'
190 config
191 stop after config files have been parsed (and their data
192 stored in the Distribution instance)
193 commandline
194 stop after the command-line ('sys.argv[1:]' or 'script_args')
195 have been parsed (and the data stored in the Distribution)
196 run [default]
197 stop after all commands have been run (the same as if 'setup()'
198 had been called in the usual way
199
200 Returns the Distribution instance, which provides all information
201 used to drive the Distutils.
202 """
203 if stop_after not in ('init', 'config', 'commandline', 'run'):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000204 raise ValueError("invalid value for 'stop_after': %r" % (stop_after,))
Greg Warde3644e22000-09-01 00:52:45 +0000205
206 global _setup_stop_after, _setup_distribution
207 _setup_stop_after = stop_after
208
209 save_argv = sys.argv
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000210 g = {'__file__': script_name}
Greg Warde3644e22000-09-01 00:52:45 +0000211 l = {}
212 try:
213 try:
214 sys.argv[0] = script_name
215 if script_args is not None:
216 sys.argv[1:] = script_args
Victor Stinnerdc9b1ea2011-06-30 15:40:22 +0200217 with open(script_name, 'rb') as f:
Éric Araujobee5cef2010-11-05 23:51:56 +0000218 exec(f.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?
Tarek Ziadé36797272010-07-22 12:50:05 +0000236 #print "_setup_distribution:", _setup_distribution
Greg Warde3644e22000-09-01 00:52:45 +0000237 return _setup_distribution
Tarek Ziadé36797272010-07-22 12:50:05 +0000238
239# run_setup ()