blob: 2bfe66aa2f49d5c699f62ab7cbe3a88cd78d318a [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 Warda76bbd42000-05-31 01:11:20 +000014
15# Mainly import these so setup scripts can "from distutils.core import" them.
Greg Wardfe6462c2000-04-04 01:40:52 +000016from distutils.dist import Distribution
17from distutils.cmd import Command
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000018from distutils.config import PyPIRCCommand
Greg Warda76bbd42000-05-31 01:11:20 +000019from distutils.extension import Extension
20
Greg Ward4c96db12000-02-18 00:26:23 +000021# This is a barebones help message generated displayed when the user
22# runs the setup script with no arguments at all. More useful help
23# is generated with various --help options: global help, list commands,
24# and per-command help.
Greg Ward9821bf42000-08-29 01:15:18 +000025USAGE = """\
26usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
27 or: %(script)s --help [cmd1 cmd2 ...]
28 or: %(script)s --help-commands
29 or: %(script)s cmd --help
30"""
Greg Ward2689e3d1999-03-22 14:52:19 +000031
Tarek Ziadé36797272010-07-22 12:50:05 +000032def gen_usage (script_name):
Greg Ward9821bf42000-08-29 01:15:18 +000033 script = os.path.basename(script_name)
Tarek Ziadé36797272010-07-22 12:50:05 +000034 return USAGE % vars()
Greg Ward9821bf42000-08-29 01:15:18 +000035
Greg Ward37af1c32000-05-26 00:54:52 +000036
Greg Warde3644e22000-09-01 00:52:45 +000037# Some mild magic to control the behaviour of 'setup()' from 'run_setup()'.
38_setup_stop_after = None
39_setup_distribution = None
40
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000041# Legal keyword arguments for the setup() function
42setup_keywords = ('distclass', 'script_name', 'script_args', 'options',
43 'name', 'version', 'author', 'author_email',
44 'maintainer', 'maintainer_email', 'url', 'license',
45 'description', 'long_description', 'keywords',
Fred Drakedb7b0022005-03-20 22:19:47 +000046 'platforms', 'classifiers', 'download_url',
47 'requires', 'provides', 'obsoletes',
48 )
Andrew M. Kuchling6ffdaab2003-01-27 16:30:36 +000049
50# Legal keyword arguments for the Extension constructor
51extension_keywords = ('name', 'sources', 'include_dirs',
52 'define_macros', 'undef_macros',
53 'library_dirs', 'libraries', 'runtime_library_dirs',
54 'extra_objects', 'extra_compile_args', 'extra_link_args',
Anthony Baxtera0240342004-10-14 10:02:08 +000055 'swig_opts', 'export_symbols', 'depends', 'language')
Greg Warde3644e22000-09-01 00:52:45 +000056
Tarek Ziadé36797272010-07-22 12:50:05 +000057def setup (**attrs):
Greg Ward8ff5a3f2000-06-02 00:44:53 +000058 """The gateway to the Distutils: do everything your setup script needs
59 to do, in a highly flexible and user-driven way. Briefly: create a
60 Distribution instance; find and parse config files; parse the command
Greg Ward9821bf42000-08-29 01:15:18 +000061 line; run each Distutils command found there, customized by the options
62 supplied to 'setup()' (as keyword arguments), in config files, and on
63 the command line.
Greg Ward2689e3d1999-03-22 14:52:19 +000064
Greg Ward8ff5a3f2000-06-02 00:44:53 +000065 The Distribution instance might be an instance of a class supplied via
66 the 'distclass' keyword argument to 'setup'; if no such class is
67 supplied, then the Distribution class (in dist.py) is instantiated.
68 All other arguments to 'setup' (except for 'cmdclass') are used to set
69 attributes of the Distribution instance.
Greg Ward2689e3d1999-03-22 14:52:19 +000070
Greg Ward8ff5a3f2000-06-02 00:44:53 +000071 The 'cmdclass' argument, if supplied, is a dictionary mapping command
72 names to command classes. Each command encountered on the command line
73 will be turned into a command class, which is in turn instantiated; any
74 class found in 'cmdclass' is used in place of the default, which is
75 (for command 'foo_bar') class 'foo_bar' in module
76 'distutils.command.foo_bar'. The command class must provide a
77 'user_options' attribute which is a list of option specifiers for
78 'distutils.fancy_getopt'. Any command-line options between the current
79 and the next command are used to set attributes of the current command
80 object.
Greg Ward2689e3d1999-03-22 14:52:19 +000081
Greg Ward8ff5a3f2000-06-02 00:44:53 +000082 When the entire command-line has been successfully parsed, calls the
83 'run()' method on each command object in turn. This method will be
84 driven entirely by the Distribution object (which each command object
85 has a reference to, thanks to its constructor), and the
86 command-specific options that became attributes of each command
87 object.
88 """
Greg Ward2689e3d1999-03-22 14:52:19 +000089
Greg Warde3644e22000-09-01 00:52:45 +000090 global _setup_stop_after, _setup_distribution
91
Greg Ward2689e3d1999-03-22 14:52:19 +000092 # Determine the distribution class -- either caller-supplied or
93 # our Distribution (see below).
Greg Wardbe86bde2000-09-26 01:56:15 +000094 klass = attrs.get('distclass')
Greg Ward2689e3d1999-03-22 14:52:19 +000095 if klass:
96 del attrs['distclass']
97 else:
98 klass = Distribution
99
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000100 if 'script_name' not in attrs:
Thomas Heller8560bb82002-11-07 16:41:38 +0000101 attrs['script_name'] = os.path.basename(sys.argv[0])
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000102 if 'script_args' not in attrs:
Greg Ward9821bf42000-08-29 01:15:18 +0000103 attrs['script_args'] = sys.argv[1:]
104
Greg Ward2689e3d1999-03-22 14:52:19 +0000105 # Create the Distribution instance, using the remaining arguments
106 # (ie. everything except distclass) to initialize it
Greg Ward39851512000-06-03 01:02:06 +0000107 try:
Greg Wardbe86bde2000-09-26 01:56:15 +0000108 _setup_distribution = dist = klass(attrs)
Guido van Rossumb940e112007-01-10 16:19:56 +0000109 except DistutilsSetupError as msg:
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000110 if 'name' not in attrs:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000111 raise SystemExit("error in setup command: %s" % msg)
Collin Winter2c8fef02007-07-17 00:38:21 +0000112 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000113 raise SystemExit("error in %s setup command: %s" % \
114 (attrs['name'], msg))
Greg Ward2689e3d1999-03-22 14:52:19 +0000115
Greg Warde3644e22000-09-01 00:52:45 +0000116 if _setup_stop_after == "init":
117 return dist
118
Gregory P. Smithbb8c71d2000-05-12 00:42:19 +0000119 # Find and parse the config file(s): they will override options from
120 # the setup script, but be overridden by the command line.
121 dist.parse_config_files()
Fred Drakeb94b8492001-12-06 20:51:35 +0000122
Greg Wardf7a55072000-06-02 01:55:36 +0000123 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000124 print("options (after parsing config files):")
Greg Wardf7a55072000-06-02 01:55:36 +0000125 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +0000126
Greg Warde3644e22000-09-01 00:52:45 +0000127 if _setup_stop_after == "config":
128 return dist
129
Andrew Kuchling2a1838b2013-11-10 18:11:00 -0500130 # Parse the command line and override config files; any
131 # command-line errors are the end user's fault, so turn them into
132 # 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")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200151 except OSError as exc:
Greg Ward37af1c32000-05-26 00:54:52 +0000152 if DEBUG:
Éric Araujofc773a22014-03-12 03:34:02 -0400153 sys.stderr.write("error: %s\n" % (exc,))
Greg Ward37af1c32000-05-26 00:54:52 +0000154 raise
155 else:
Éric Araujofc773a22014-03-12 03:34:02 -0400156 raise SystemExit("error: %s" % (exc,))
Fred Drakeb94b8492001-12-06 20:51:35 +0000157
Andrew M. Kuchling91e77532002-11-08 16:18:24 +0000158 except (DistutilsError,
Guido van Rossumb940e112007-01-10 16:19:56 +0000159 CCompilerError) as msg:
Greg Ward37af1c32000-05-26 00:54:52 +0000160 if DEBUG:
161 raise
162 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000163 raise SystemExit("error: " + str(msg))
Greg Ward2689e3d1999-03-22 14:52:19 +0000164
Greg Warde3644e22000-09-01 00:52:45 +0000165 return dist
166
Tarek Ziadé36797272010-07-22 12:50:05 +0000167# setup ()
Greg Warde3644e22000-09-01 00:52:45 +0000168
Tarek Ziadé36797272010-07-22 12:50:05 +0000169
170def run_setup (script_name, script_args=None, stop_after="run"):
Greg Warde3644e22000-09-01 00:52:45 +0000171 """Run a setup script in a somewhat controlled environment, and
172 return the Distribution instance that drives things. This is useful
173 if you need to find out the distribution meta-data (passed as
174 keyword args from 'script' to 'setup()', or the contents of the
175 config files or command-line.
176
Neal Norwitz01688022007-08-12 00:43:29 +0000177 'script_name' is a file that will be read and run with 'exec()';
Greg Warde3644e22000-09-01 00:52:45 +0000178 'sys.argv[0]' will be replaced with 'script' for the duration of the
179 call. 'script_args' is a list of strings; if supplied,
180 'sys.argv[1:]' will be replaced by 'script_args' for the duration of
181 the call.
182
183 'stop_after' tells 'setup()' when to stop processing; possible
184 values:
185 init
186 stop after the Distribution instance has been created and
187 populated with the keyword arguments to 'setup()'
188 config
189 stop after config files have been parsed (and their data
190 stored in the Distribution instance)
191 commandline
192 stop after the command-line ('sys.argv[1:]' or 'script_args')
193 have been parsed (and the data stored in the Distribution)
194 run [default]
195 stop after all commands have been run (the same as if 'setup()'
196 had been called in the usual way
197
198 Returns the Distribution instance, which provides all information
199 used to drive the Distutils.
200 """
201 if stop_after not in ('init', 'config', 'commandline', 'run'):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000202 raise ValueError("invalid value for 'stop_after': %r" % (stop_after,))
Greg Warde3644e22000-09-01 00:52:45 +0000203
204 global _setup_stop_after, _setup_distribution
205 _setup_stop_after = stop_after
206
207 save_argv = sys.argv
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000208 g = {'__file__': script_name}
Greg Warde3644e22000-09-01 00:52:45 +0000209 l = {}
210 try:
211 try:
212 sys.argv[0] = script_name
213 if script_args is not None:
214 sys.argv[1:] = script_args
Victor Stinnerdc9b1ea2011-06-30 15:40:22 +0200215 with open(script_name, 'rb') as f:
Éric Araujobee5cef2010-11-05 23:51:56 +0000216 exec(f.read(), g, l)
Greg Warde3644e22000-09-01 00:52:45 +0000217 finally:
218 sys.argv = save_argv
219 _setup_stop_after = None
220 except SystemExit:
221 # Hmm, should we do something if exiting with a non-zero code
222 # (ie. error)?
223 pass
224 except:
225 raise
226
227 if _setup_distribution is None:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000228 raise RuntimeError(("'distutils.core.setup()' was never called -- "
Greg Warde3644e22000-09-01 00:52:45 +0000229 "perhaps '%s' is not a Distutils setup script?") % \
Collin Winter5b7e9d72007-08-30 03:52:21 +0000230 script_name)
Greg Warde3644e22000-09-01 00:52:45 +0000231
232 # I wonder if the setup script's namespace -- g and l -- would be of
233 # any interest to callers?
Tarek Ziadé36797272010-07-22 12:50:05 +0000234 #print "_setup_distribution:", _setup_distribution
Greg Warde3644e22000-09-01 00:52:45 +0000235 return _setup_distribution
Tarek Ziadé36797272010-07-22 12:50:05 +0000236
237# run_setup ()