blob: 0c77f85e7b3ea0977feb42fe30031dff7d3d761e [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.dist
2
3Provides the Distribution class, which represents the module distribution
Greg Ward8ff5a3f2000-06-02 00:44:53 +00004being built/installed/distributed.
5"""
Greg Wardfe6462c2000-04-04 01:40:52 +00006
Greg Wardfe6462c2000-04-04 01:40:52 +00007__revision__ = "$Id$"
8
Gregory P. Smith14263542000-05-12 00:41:33 +00009import sys, os, string, re
Greg Wardfe6462c2000-04-04 01:40:52 +000010from types import *
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000011
12try:
13 import warnings
Andrew M. Kuchlingccf4e422002-10-31 13:39:33 +000014except ImportError:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000015 warnings = None
16
Greg Wardfe6462c2000-04-04 01:40:52 +000017from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000018from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000019from distutils.util import check_environ, strtobool, rfc822_escape
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000020from distutils import log
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000021from distutils.debug import DEBUG
Greg Wardfe6462c2000-04-04 01:40:52 +000022
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +000023# Encoding used for the PKG-INFO files
24PKG_INFO_ENCODING = 'utf-8'
25
Greg Wardfe6462c2000-04-04 01:40:52 +000026# Regex to define acceptable Distutils command names. This is not *quite*
27# the same as a Python NAME -- I don't allow leading underscores. The fact
28# that they're very similar is no coincidence; the default naming scheme is
29# to look for a Python module named after the command.
30command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
31
32
33class Distribution:
Greg Ward8ff5a3f2000-06-02 00:44:53 +000034 """The core of the Distutils. Most of the work hiding behind 'setup'
35 is really done within a Distribution instance, which farms the work out
36 to the Distutils commands specified on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +000037
Greg Ward8ff5a3f2000-06-02 00:44:53 +000038 Setup scripts will almost never instantiate Distribution directly,
39 unless the 'setup()' function is totally inadequate to their needs.
40 However, it is conceivable that a setup script might wish to subclass
41 Distribution for some specialized purpose, and then pass the subclass
42 to 'setup()' as the 'distclass' keyword argument. If so, it is
43 necessary to respect the expectations that 'setup' has of Distribution.
44 See the code for 'setup()', in core.py, for details.
45 """
Greg Wardfe6462c2000-04-04 01:40:52 +000046
47
48 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000049 # supplied to the setup script prior to any actual commands.
50 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000051 # these global options. This list should be kept to a bare minimum,
52 # since every global option is also valid as a command option -- and we
53 # don't want to pollute the commands with too many options that they
54 # have minimal control over.
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000055 # The fourth entry for verbose means that it can be repeated.
56 global_options = [('verbose', 'v', "run verbosely (default)", 1),
Greg Wardd5d8a992000-05-23 01:42:17 +000057 ('quiet', 'q', "run quietly (turns verbosity off)"),
58 ('dry-run', 'n', "don't actually do anything"),
59 ('help', 'h', "show detailed help message"),
Greg Wardfe6462c2000-04-04 01:40:52 +000060 ]
Greg Ward82715e12000-04-21 02:28:14 +000061
Martin v. Löwis8ed338a2005-03-03 08:12:27 +000062 # 'common_usage' is a short (2-3 line) string describing the common
63 # usage of the setup script.
64 common_usage = """\
65Common commands: (see '--help-commands' for more)
66
67 setup.py build will build the package underneath 'build/'
68 setup.py install will install the package
69"""
70
Greg Ward82715e12000-04-21 02:28:14 +000071 # options that are not propagated to the commands
72 display_options = [
73 ('help-commands', None,
74 "list all available commands"),
75 ('name', None,
76 "print package name"),
77 ('version', 'V',
78 "print package version"),
79 ('fullname', None,
80 "print <package name>-<version>"),
81 ('author', None,
82 "print the author's name"),
83 ('author-email', None,
84 "print the author's email address"),
85 ('maintainer', None,
86 "print the maintainer's name"),
87 ('maintainer-email', None,
88 "print the maintainer's email address"),
89 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000090 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000091 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000092 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000093 ('url', None,
94 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +000095 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +000096 "print the license of the package"),
97 ('licence', None,
98 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +000099 ('description', None,
100 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +0000101 ('long-description', None,
102 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000103 ('platforms', None,
104 "print the list of platforms"),
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000105 ('classifiers', None,
106 "print the list of classifiers"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000107 ('keywords', None,
108 "print the list of keywords"),
Fred Drakedb7b0022005-03-20 22:19:47 +0000109 ('provides', None,
110 "print the list of packages/modules provided"),
111 ('requires', None,
112 "print the list of packages/modules required"),
113 ('obsoletes', None,
114 "print the list of packages/modules made obsolete")
Greg Ward82715e12000-04-21 02:28:14 +0000115 ]
Greg Ward2f2b6c62000-09-25 01:58:07 +0000116 display_option_names = map(lambda x: translate_longopt(x[0]),
117 display_options)
Greg Ward82715e12000-04-21 02:28:14 +0000118
119 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +0000120 negative_opt = {'quiet': 'verbose'}
121
122
123 # -- Creation/initialization methods -------------------------------
Fred Drakeb94b8492001-12-06 20:51:35 +0000124
Greg Wardfe6462c2000-04-04 01:40:52 +0000125 def __init__ (self, attrs=None):
126 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000127 attributes of a Distribution, and then use 'attrs' (a dictionary
128 mapping attribute names to values) to assign some of those
129 attributes their "real" values. (Any attributes not mentioned in
130 'attrs' will be assigned to some null value: 0, None, an empty list
131 or dictionary, etc.) Most importantly, initialize the
132 'command_obj' attribute to the empty dictionary; this will be
133 filled in with real command objects by 'parse_command_line()'.
134 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000135
136 # Default values for our command-line options
137 self.verbose = 1
138 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000139 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000140 for attr in self.display_option_names:
141 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000142
Greg Ward82715e12000-04-21 02:28:14 +0000143 # Store the distribution meta-data (name, version, author, and so
144 # forth) in a separate object -- we're getting to have enough
145 # information here (and enough command-line options) that it's
146 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
147 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000148 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000149 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000150 method_name = "get_" + basename
151 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000152
153 # 'cmdclass' maps command names to class objects, so we
154 # can 1) quickly figure out which class to instantiate when
155 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000156 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000157 self.cmdclass = {}
158
Fred Draked04573f2004-08-03 16:37:40 +0000159 # 'command_packages' is a list of packages in which commands
160 # are searched for. The factory for command 'foo' is expected
161 # to be named 'foo' in the module 'foo' in one of the packages
162 # named here. This list is searched from the left; an error
163 # is raised if no named package provides the command being
164 # searched for. (Always access using get_command_packages().)
165 self.command_packages = None
166
Greg Ward9821bf42000-08-29 01:15:18 +0000167 # 'script_name' and 'script_args' are usually set to sys.argv[0]
168 # and sys.argv[1:], but they can be overridden when the caller is
169 # not necessarily a setup script run from the command-line.
170 self.script_name = None
171 self.script_args = None
172
Greg Wardd5d8a992000-05-23 01:42:17 +0000173 # 'command_options' is where we store command options between
174 # parsing them (from config files, the command-line, etc.) and when
175 # they are actually needed -- ie. when the command in question is
176 # instantiated. It is a dictionary of dictionaries of 2-tuples:
177 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000178 self.command_options = {}
179
Martin v. Löwis98da5622005-03-23 18:54:36 +0000180 # 'dist_files' is the list of (command, pyversion, file) that
181 # have been created by any dist commands run so far. This is
182 # filled regardless of whether the run is dry or not. pyversion
183 # gives sysconfig.get_python_version() if the dist file is
184 # specific to a Python version, 'any' if it is good for all
185 # Python versions on the target platform, and '' for a source
186 # file. pyversion should not be used to specify minimum or
187 # maximum required Python versions; use the metainfo for that
188 # instead.
Martin v. Löwis55f1bb82005-03-21 20:56:35 +0000189 self.dist_files = []
190
Greg Wardfe6462c2000-04-04 01:40:52 +0000191 # These options are really the business of various commands, rather
192 # than of the Distribution itself. We provide aliases for them in
193 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000194 self.packages = None
Fred Drake0eb32a62004-06-11 21:50:33 +0000195 self.package_data = {}
Greg Wardfe6462c2000-04-04 01:40:52 +0000196 self.package_dir = None
197 self.py_modules = None
198 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000199 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000200 self.ext_modules = None
201 self.ext_package = None
202 self.include_dirs = None
203 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000204 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000205 self.data_files = None
Tarek Ziadé1a240fb2009-01-08 23:56:31 +0000206 self.password = ''
Greg Wardfe6462c2000-04-04 01:40:52 +0000207
208 # And now initialize bookkeeping stuff that can't be supplied by
209 # the caller at all. 'command_obj' maps command names to
210 # Command instances -- that's how we enforce that every command
211 # class is a singleton.
212 self.command_obj = {}
213
214 # 'have_run' maps command names to boolean values; it keeps track
215 # of whether we have actually run a particular command, to make it
216 # cheap to "run" a command whenever we think we might need to -- if
217 # it's already been done, no need for expensive filesystem
218 # operations, we just check the 'have_run' dictionary and carry on.
219 # It's only safe to query 'have_run' for a command class that has
220 # been instantiated -- a false value will be inserted when the
221 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000222 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000223 # '.get()' rather than a straight lookup.
224 self.have_run = {}
225
226 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000227 # the setup script) to possibly override any or all of these
228 # distribution options.
229
Greg Wardfe6462c2000-04-04 01:40:52 +0000230 if attrs:
Greg Wardfe6462c2000-04-04 01:40:52 +0000231 # Pull out the set of command options and work on them
232 # specifically. Note that this order guarantees that aliased
233 # command options will override any supplied redundantly
234 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000235 options = attrs.get('options')
Tarek Ziadéc13acb12008-12-29 22:23:53 +0000236 if options is not None:
Greg Wardfe6462c2000-04-04 01:40:52 +0000237 del attrs['options']
238 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000239 opt_dict = self.get_option_dict(command)
240 for (opt, val) in cmd_options.items():
241 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000242
Guido van Rossum8bc09652008-02-21 18:18:37 +0000243 if 'licence' in attrs:
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +0000244 attrs['license'] = attrs['licence']
245 del attrs['licence']
246 msg = "'licence' distribution option is deprecated; use 'license'"
247 if warnings is not None:
248 warnings.warn(msg)
249 else:
250 sys.stderr.write(msg + "\n")
251
Greg Wardfe6462c2000-04-04 01:40:52 +0000252 # Now work on the rest of the attributes. Any attribute that's
253 # not already defined is invalid!
254 for (key,val) in attrs.items():
Fred Drakedb7b0022005-03-20 22:19:47 +0000255 if hasattr(self.metadata, "set_" + key):
256 getattr(self.metadata, "set_" + key)(val)
257 elif hasattr(self.metadata, key):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000258 setattr(self.metadata, key, val)
259 elif hasattr(self, key):
260 setattr(self, key, val)
Anthony Baxter73cc8472004-10-13 13:22:34 +0000261 else:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000262 msg = "Unknown distribution option: %s" % repr(key)
263 if warnings is not None:
264 warnings.warn(msg)
265 else:
266 sys.stderr.write(msg + "\n")
Greg Wardfe6462c2000-04-04 01:40:52 +0000267
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000268 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000269
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000270 def get_option_dict(self, command):
Greg Ward0e48cfd2000-05-26 01:00:15 +0000271 """Get the option dictionary for a given command. If that
272 command's option dictionary hasn't been created yet, then create it
273 and return the new dictionary; otherwise, return the existing
274 option dictionary.
275 """
Greg Ward0e48cfd2000-05-26 01:00:15 +0000276 dict = self.command_options.get(command)
277 if dict is None:
278 dict = self.command_options[command] = {}
279 return dict
280
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000281 def dump_option_dicts(self, header=None, commands=None, indent=""):
Greg Wardc32d9a62000-05-28 23:53:06 +0000282 from pprint import pformat
283
284 if commands is None: # dump all command option dicts
285 commands = self.command_options.keys()
286 commands.sort()
287
288 if header is not None:
289 print indent + header
290 indent = indent + " "
291
292 if not commands:
293 print indent + "no commands known yet"
294 return
295
296 for cmd_name in commands:
297 opt_dict = self.command_options.get(cmd_name)
298 if opt_dict is None:
299 print indent + "no option dict for '%s' command" % cmd_name
300 else:
301 print indent + "option dict for '%s' command:" % cmd_name
302 out = pformat(opt_dict)
303 for line in string.split(out, "\n"):
304 print indent + " " + line
305
Greg Wardd5d8a992000-05-23 01:42:17 +0000306 # -- Config file finding/parsing methods ---------------------------
307
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000308 def find_config_files(self):
Gregory P. Smith14263542000-05-12 00:41:33 +0000309 """Find as many configuration files as should be processed for this
310 platform, and return a list of filenames in the order in which they
311 should be parsed. The filenames returned are guaranteed to exist
312 (modulo nasty race conditions).
313
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000314 There are three possible config files: distutils.cfg in the
315 Distutils installation directory (ie. where the top-level
316 Distutils __inst__.py file lives), a file in the user's home
317 directory named .pydistutils.cfg on Unix and pydistutils.cfg
318 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000319 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000320 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000321 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000322
Greg Ward11696872000-06-07 02:29:03 +0000323 # Where to look for the system-wide Distutils config file
324 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
325
326 # Look for the system config file
327 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000328 if os.path.isfile(sys_file):
329 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000330
Greg Ward11696872000-06-07 02:29:03 +0000331 # What to call the per-user config file
332 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000333 user_filename = ".pydistutils.cfg"
334 else:
335 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000336
Greg Ward11696872000-06-07 02:29:03 +0000337 # And look for the user config file
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +0000338 user_file = os.path.join(os.path.expanduser('~'), user_filename)
339 if os.path.isfile(user_file):
340 files.append(user_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000341
Gregory P. Smith14263542000-05-12 00:41:33 +0000342 # All platforms support local setup.cfg
343 local_file = "setup.cfg"
344 if os.path.isfile(local_file):
345 files.append(local_file)
346
347 return files
348
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000349 def parse_config_files(self, filenames=None):
Georg Brandl392c6fc2008-05-25 07:25:25 +0000350 from ConfigParser import ConfigParser
Gregory P. Smith14263542000-05-12 00:41:33 +0000351
352 if filenames is None:
353 filenames = self.find_config_files()
354
Greg Ward2bd3f422000-06-02 01:59:33 +0000355 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000356
Gregory P. Smith14263542000-05-12 00:41:33 +0000357 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000358 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000359 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000360 parser.read(filename)
361 for section in parser.sections():
362 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000363 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000364
Greg Wardd5d8a992000-05-23 01:42:17 +0000365 for opt in options:
366 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000367 val = parser.get(section,opt)
368 opt = string.replace(opt, '-', '_')
369 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000370
Greg Ward47460772000-05-23 03:47:35 +0000371 # Make the ConfigParser forget everything (so we retain
Fred Drakef06116d2004-02-17 22:35:19 +0000372 # the original filenames that options come from)
Greg Ward47460772000-05-23 03:47:35 +0000373 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000374
Greg Wardceb9e222000-09-25 01:23:52 +0000375 # If there was a "global" section in the config file, use it
376 # to set Distribution options.
377
Guido van Rossum8bc09652008-02-21 18:18:37 +0000378 if 'global' in self.command_options:
Greg Wardceb9e222000-09-25 01:23:52 +0000379 for (opt, (src, val)) in self.command_options['global'].items():
380 alias = self.negative_opt.get(opt)
381 try:
382 if alias:
383 setattr(self, alias, not strtobool(val))
384 elif opt in ('verbose', 'dry_run'): # ugh!
385 setattr(self, opt, strtobool(val))
Fred Draked04573f2004-08-03 16:37:40 +0000386 else:
387 setattr(self, opt, val)
Greg Wardceb9e222000-09-25 01:23:52 +0000388 except ValueError, msg:
389 raise DistutilsOptionError, msg
390
Greg Wardd5d8a992000-05-23 01:42:17 +0000391 # -- Command-line parsing methods ----------------------------------
392
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000393 def parse_command_line(self):
Greg Ward9821bf42000-08-29 01:15:18 +0000394 """Parse the setup script's command line, taken from the
395 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
396 -- see 'setup()' in core.py). This list is first processed for
397 "global options" -- options that set attributes of the Distribution
398 instance. Then, it is alternately scanned for Distutils commands
399 and options for that command. Each new command terminates the
400 options for the previous command. The allowed options for a
401 command are determined by the 'user_options' attribute of the
402 command class -- thus, we have to be able to load command classes
403 in order to parse the command line. Any error in that 'options'
404 attribute raises DistutilsGetoptError; any error on the
405 command-line raises DistutilsArgError. If no Distutils commands
406 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000407 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000408 on with executing commands; false if no errors but we shouldn't
409 execute commands (currently, this only happens if user asks for
410 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000411 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000412 #
Fred Drake981a1782001-08-10 18:59:30 +0000413 # We now have enough information to show the Macintosh dialog
414 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000415 #
Fred Draked04573f2004-08-03 16:37:40 +0000416 toplevel_options = self._get_toplevel_options()
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000417 if sys.platform == 'mac':
418 import EasyDialogs
419 cmdlist = self.get_command_list()
420 self.script_args = EasyDialogs.GetArgv(
Fred Draked04573f2004-08-03 16:37:40 +0000421 toplevel_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000422
Greg Wardfe6462c2000-04-04 01:40:52 +0000423 # We have to parse the command line a bit at a time -- global
424 # options, then the first command, then its options, and so on --
425 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000426 # the options that are valid for a particular class aren't known
427 # until we have loaded the command class, which doesn't happen
428 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000429
430 self.commands = []
Fred Draked04573f2004-08-03 16:37:40 +0000431 parser = FancyGetopt(toplevel_options + self.display_options)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000432 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000433 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000434 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000435 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000436 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000437
Greg Ward82715e12000-04-21 02:28:14 +0000438 # for display options we return immediately
439 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000440 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000441
Greg Wardfe6462c2000-04-04 01:40:52 +0000442 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000443 args = self._parse_command_opts(parser, args)
444 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000445 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000446
Greg Wardd5d8a992000-05-23 01:42:17 +0000447 # Handle the cases of --help as a "global" option, ie.
448 # "setup.py --help" and "setup.py --help command ...". For the
449 # former, we show global options (--verbose, --dry-run, etc.)
450 # and display-only options (--name, --version, etc.); for the
451 # latter, we omit the display-only options and show help for
452 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000453 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000454 self._show_help(parser,
455 display_options=len(self.commands) == 0,
456 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000457 return
458
459 # Oops, no commands found -- an end-user error
460 if not self.commands:
461 raise DistutilsArgError, "no commands supplied"
462
463 # All is well: return true
464 return 1
465
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000466 def _get_toplevel_options(self):
Fred Draked04573f2004-08-03 16:37:40 +0000467 """Return the non-display options recognized at the top level.
468
469 This includes options that are recognized *only* at the top
470 level as well as options recognized for commands.
471 """
472 return self.global_options + [
473 ("command-packages=", None,
474 "list of packages that provide distutils commands"),
475 ]
476
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000477 def _parse_command_opts(self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000478 """Parse the command-line options for a single command.
479 'parser' must be a FancyGetopt instance; 'args' must be the list
480 of arguments, starting with the current command (whose options
481 we are about to parse). Returns a new version of 'args' with
482 the next command at the front of the list; will be the empty
483 list if there are no more commands on the command line. Returns
484 None if the user asked for help on this command.
485 """
486 # late import because of mutual dependence between these modules
487 from distutils.cmd import Command
488
489 # Pull the current command from the head of the command line
490 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000491 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000492 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000493 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000494
495 # Dig up the command class that implements this command, so we
496 # 1) know that it's a valid command, and 2) know which options
497 # it takes.
498 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000499 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000500 except DistutilsModuleError, msg:
501 raise DistutilsArgError, msg
502
503 # Require that the command class be derived from Command -- want
504 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000505 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000506 raise DistutilsClassError, \
507 "command class %s must subclass Command" % cmd_class
508
509 # Also make sure that the command object provides a list of its
510 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000511 if not (hasattr(cmd_class, 'user_options') and
512 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000513 raise DistutilsClassError, \
514 ("command class %s must provide " +
515 "'user_options' attribute (a list of tuples)") % \
516 cmd_class
517
518 # If the command class has a list of negative alias options,
519 # merge it in with the global negative aliases.
520 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000521 if hasattr(cmd_class, 'negative_opt'):
Antoine Pitrouf5413782009-05-15 17:27:30 +0000522 negative_opt = negative_opt.copy()
Greg Wardfd7b91e2000-09-26 01:52:25 +0000523 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000524
Greg Wardfa9ff762000-10-14 04:06:40 +0000525 # Check for help_options in command class. They have a different
526 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000527 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000528 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000529 help_options = fix_help_options(cmd_class.help_options)
530 else:
Greg Ward55fced32000-06-24 01:22:41 +0000531 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000532
Greg Ward9d17a7a2000-06-07 03:00:06 +0000533
Greg Wardd5d8a992000-05-23 01:42:17 +0000534 # All commands support the global options too, just by adding
535 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000536 parser.set_option_table(self.global_options +
537 cmd_class.user_options +
538 help_options)
539 parser.set_negative_aliases(negative_opt)
540 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000541 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000542 self._show_help(parser, display_options=0, commands=[cmd_class])
543 return
544
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000545 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000546 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000547 help_option_found=0
548 for (help_option, short, desc, func) in cmd_class.help_options:
549 if hasattr(opts, parser.get_attr_name(help_option)):
550 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000551 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000552 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000553
554 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000555 func()
Greg Ward55fced32000-06-24 01:22:41 +0000556 else:
Fred Drake981a1782001-08-10 18:59:30 +0000557 raise DistutilsClassError(
Walter Dörwald70a6b492004-02-12 17:35:32 +0000558 "invalid help function %r for help option '%s': "
Fred Drake981a1782001-08-10 18:59:30 +0000559 "must be a callable object (function, etc.)"
Walter Dörwald70a6b492004-02-12 17:35:32 +0000560 % (func, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000561
Fred Drakeb94b8492001-12-06 20:51:35 +0000562 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000563 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000564
Greg Wardd5d8a992000-05-23 01:42:17 +0000565 # Put the options from the command-line into their official
566 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000567 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000568 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000569 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000570
571 return args
572
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000573 def finalize_options(self):
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000574 """Set final values for all the options on the Distribution
575 instance, analogous to the .finalize_options() method of Command
576 objects.
577 """
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000578 keywords = self.metadata.keywords
579 if keywords is not None:
580 if type(keywords) is StringType:
581 keywordlist = string.split(keywords, ',')
582 self.metadata.keywords = map(string.strip, keywordlist)
583
584 platforms = self.metadata.platforms
585 if platforms is not None:
586 if type(platforms) is StringType:
587 platformlist = string.split(platforms, ',')
588 self.metadata.platforms = map(string.strip, platformlist)
589
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000590 def _show_help(self, parser, global_options=1, display_options=1,
591 commands=[]):
Greg Wardd5d8a992000-05-23 01:42:17 +0000592 """Show help for the setup script command-line in the form of
593 several lists of command-line options. 'parser' should be a
594 FancyGetopt instance; do not expect it to be returned in the
595 same state, as its option table will be reset to make it
596 generate the correct help text.
597
598 If 'global_options' is true, lists the global options:
599 --verbose, --dry-run, etc. If 'display_options' is true, lists
600 the "display-only" options: --name, --version, etc. Finally,
601 lists per-command help for every command name or command class
602 in 'commands'.
603 """
604 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000605 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000606 from distutils.cmd import Command
607
608 if global_options:
Fred Draked04573f2004-08-03 16:37:40 +0000609 if display_options:
610 options = self._get_toplevel_options()
611 else:
612 options = self.global_options
613 parser.set_option_table(options)
Martin v. Löwis8ed338a2005-03-03 08:12:27 +0000614 parser.print_help(self.common_usage + "\nGlobal options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000615 print
616
617 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000618 parser.set_option_table(self.display_options)
619 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000620 "Information display options (just display " +
621 "information, ignore any commands)")
622 print
623
624 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000625 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000626 klass = command
627 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000628 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000629 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000630 type(klass.help_options) is ListType):
631 parser.set_option_table(klass.user_options +
632 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000633 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000634 parser.set_option_table(klass.user_options)
635 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000636 print
637
Greg Ward9821bf42000-08-29 01:15:18 +0000638 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000639 return
640
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000641 def handle_display_options(self, option_order):
Greg Ward82715e12000-04-21 02:28:14 +0000642 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000643 (--help-commands or the metadata display options) on the command
644 line, display the requested info and return true; else return
645 false.
646 """
Greg Ward9821bf42000-08-29 01:15:18 +0000647 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000648
649 # User just wants a list of commands -- we'll print it out and stop
650 # processing now (ie. if they ran "setup --help-commands foo bar",
651 # we ignore "foo bar").
652 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000653 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000654 print
Greg Ward9821bf42000-08-29 01:15:18 +0000655 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000656 return 1
657
658 # If user supplied any of the "display metadata" options, then
659 # display that metadata in the order in which the user supplied the
660 # metadata options.
661 any_display_options = 0
662 is_display_option = {}
663 for option in self.display_options:
664 is_display_option[option[0]] = 1
665
666 for (opt, val) in option_order:
667 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000668 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000669 value = getattr(self.metadata, "get_"+opt)()
670 if opt in ['keywords', 'platforms']:
671 print string.join(value, ',')
Fred Drakedb7b0022005-03-20 22:19:47 +0000672 elif opt in ('classifiers', 'provides', 'requires',
673 'obsoletes'):
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000674 print string.join(value, '\n')
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000675 else:
676 print value
Greg Ward82715e12000-04-21 02:28:14 +0000677 any_display_options = 1
678
679 return any_display_options
680
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000681 def print_command_list(self, commands, header, max_length):
Greg Wardfe6462c2000-04-04 01:40:52 +0000682 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000683 'print_commands()'.
684 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000685 print header + ":"
686
687 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000688 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000689 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000690 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000691 try:
692 description = klass.description
693 except AttributeError:
694 description = "(no description available)"
695
696 print " %-*s %s" % (max_length, cmd, description)
697
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000698 def print_commands(self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000699 """Print out a help message listing all available commands with a
700 description of each. The list is divided into "standard commands"
701 (listed in distutils.command.__all__) and "extra commands"
702 (mentioned in self.cmdclass, but not a standard command). The
703 descriptions come from the command class attribute
704 'description'.
705 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000706 import distutils.command
707 std_commands = distutils.command.__all__
708 is_std = {}
709 for cmd in std_commands:
710 is_std[cmd] = 1
711
712 extra_commands = []
713 for cmd in self.cmdclass.keys():
714 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000715 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000716
717 max_length = 0
718 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000719 if len(cmd) > max_length:
720 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000721
Greg Wardfd7b91e2000-09-26 01:52:25 +0000722 self.print_command_list(std_commands,
723 "Standard commands",
724 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000725 if extra_commands:
726 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000727 self.print_command_list(extra_commands,
728 "Extra commands",
729 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000730
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000731 def get_command_list(self):
Greg Wardf6fc8752000-11-11 02:47:11 +0000732 """Get a list of (command, description) tuples.
733 The list is divided into "standard commands" (listed in
734 distutils.command.__all__) and "extra commands" (mentioned in
735 self.cmdclass, but not a standard command). The descriptions come
736 from the command class attribute 'description'.
737 """
738 # Currently this is only used on Mac OS, for the Mac-only GUI
739 # Distutils interface (by Jack Jansen)
740
741 import distutils.command
742 std_commands = distutils.command.__all__
743 is_std = {}
744 for cmd in std_commands:
745 is_std[cmd] = 1
746
747 extra_commands = []
748 for cmd in self.cmdclass.keys():
749 if not is_std.get(cmd):
750 extra_commands.append(cmd)
751
752 rv = []
753 for cmd in (std_commands + extra_commands):
754 klass = self.cmdclass.get(cmd)
755 if not klass:
756 klass = self.get_command_class(cmd)
757 try:
758 description = klass.description
759 except AttributeError:
760 description = "(no description available)"
761 rv.append((cmd, description))
762 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000763
764 # -- Command class/object methods ----------------------------------
765
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000766 def get_command_packages(self):
Fred Draked04573f2004-08-03 16:37:40 +0000767 """Return a list of packages from which commands are loaded."""
768 pkgs = self.command_packages
769 if not isinstance(pkgs, type([])):
770 pkgs = string.split(pkgs or "", ",")
771 for i in range(len(pkgs)):
772 pkgs[i] = string.strip(pkgs[i])
773 pkgs = filter(None, pkgs)
774 if "distutils.command" not in pkgs:
775 pkgs.insert(0, "distutils.command")
776 self.command_packages = pkgs
777 return pkgs
778
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000779 def get_command_class(self, command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000780 """Return the class that implements the Distutils command named by
781 'command'. First we check the 'cmdclass' dictionary; if the
782 command is mentioned there, we fetch the class object from the
783 dictionary and return it. Otherwise we load the command module
784 ("distutils.command." + command) and fetch the command class from
785 the module. The loaded class is also stored in 'cmdclass'
786 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000787
Gregory P. Smith14263542000-05-12 00:41:33 +0000788 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000789 found, or if that module does not define the expected class.
790 """
791 klass = self.cmdclass.get(command)
792 if klass:
793 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000794
Fred Draked04573f2004-08-03 16:37:40 +0000795 for pkgname in self.get_command_packages():
796 module_name = "%s.%s" % (pkgname, command)
797 klass_name = command
Greg Wardfe6462c2000-04-04 01:40:52 +0000798
Fred Draked04573f2004-08-03 16:37:40 +0000799 try:
800 __import__ (module_name)
801 module = sys.modules[module_name]
802 except ImportError:
803 continue
Greg Wardfe6462c2000-04-04 01:40:52 +0000804
Fred Draked04573f2004-08-03 16:37:40 +0000805 try:
806 klass = getattr(module, klass_name)
807 except AttributeError:
808 raise DistutilsModuleError, \
809 "invalid command '%s' (no class '%s' in module '%s')" \
810 % (command, klass_name, module_name)
Greg Wardfe6462c2000-04-04 01:40:52 +0000811
Fred Draked04573f2004-08-03 16:37:40 +0000812 self.cmdclass[command] = klass
813 return klass
814
815 raise DistutilsModuleError("invalid command '%s'" % command)
816
Greg Wardfe6462c2000-04-04 01:40:52 +0000817
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000818 def get_command_obj(self, command, create=1):
Greg Wardd5d8a992000-05-23 01:42:17 +0000819 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000820 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000821 object for 'command' is in the cache, then we either create and
822 return it (if 'create' is true) or return None.
823 """
824 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000825 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000826 if DEBUG:
827 print "Distribution.get_command_obj(): " \
828 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000829
Greg Wardd5d8a992000-05-23 01:42:17 +0000830 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000831 cmd_obj = self.command_obj[command] = klass(self)
832 self.have_run[command] = 0
833
834 # Set any options that were supplied in config files
835 # or on the command line. (NB. support for error
836 # reporting is lame here: any errors aren't reported
837 # until 'finalize_options()' is called, which means
838 # we won't report the source of the error.)
839 options = self.command_options.get(command)
840 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000841 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000842
843 return cmd_obj
844
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000845 def _set_command_options(self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000846 """Set the options for 'command_obj' from 'option_dict'. Basically
847 this means copying elements of a dictionary ('option_dict') to
848 attributes of an instance ('command').
849
Greg Wardceb9e222000-09-25 01:23:52 +0000850 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000851 supplied, uses the standard option dictionary for this command
852 (from 'self.command_options').
853 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000854 command_name = command_obj.get_command_name()
855 if option_dict is None:
856 option_dict = self.get_option_dict(command_name)
857
858 if DEBUG: print " setting options for '%s' command:" % command_name
859 for (option, (source, value)) in option_dict.items():
860 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000861 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000862 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000863 except AttributeError:
864 bool_opts = []
865 try:
866 neg_opt = command_obj.negative_opt
867 except AttributeError:
868 neg_opt = {}
869
870 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000871 is_string = type(value) is StringType
Guido van Rossum8bc09652008-02-21 18:18:37 +0000872 if option in neg_opt and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000873 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000874 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000875 setattr(command_obj, option, strtobool(value))
876 elif hasattr(command_obj, option):
877 setattr(command_obj, option, value)
878 else:
879 raise DistutilsOptionError, \
880 ("error in %s: command '%s' has no such option '%s'"
881 % (source, command_name, option))
882 except ValueError, msg:
883 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000884
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000885 def reinitialize_command(self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000886 """Reinitializes a command to the state it was in when first
887 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000888 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000889 values in programmatically, overriding or supplementing
890 user-supplied values from the config files and command line.
891 You'll have to re-finalize the command object (by calling
892 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000893 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000894
Greg Wardf449ea52000-09-16 15:23:28 +0000895 'command' should be a command name (string) or command object. If
896 'reinit_subcommands' is true, also reinitializes the command's
897 sub-commands, as declared by the 'sub_commands' class attribute (if
898 it has one). See the "install" command for an example. Only
899 reinitializes the sub-commands that actually matter, ie. those
900 whose test predicates return true.
901
Greg Wardc32d9a62000-05-28 23:53:06 +0000902 Returns the reinitialized command object.
903 """
904 from distutils.cmd import Command
905 if not isinstance(command, Command):
906 command_name = command
907 command = self.get_command_obj(command_name)
908 else:
909 command_name = command.get_command_name()
910
911 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000912 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000913 command.initialize_options()
914 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000915 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000916 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000917
Greg Wardf449ea52000-09-16 15:23:28 +0000918 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000919 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000920 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000921
Greg Wardc32d9a62000-05-28 23:53:06 +0000922 return command
923
Greg Wardfe6462c2000-04-04 01:40:52 +0000924 # -- Methods that operate on the Distribution ----------------------
925
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000926 def announce(self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000927 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000928
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000929 def run_commands(self):
Greg Ward82715e12000-04-21 02:28:14 +0000930 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000931 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000932 created by 'get_command_obj()'.
933 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000934 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000935 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000936
Greg Wardfe6462c2000-04-04 01:40:52 +0000937 # -- Methods that operate on its Commands --------------------------
938
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000939 def run_command(self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000940 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000941 if the command has already been run). Specifically: if we have
942 already created and run the command named by 'command', return
943 silently without doing anything. If the command named by 'command'
944 doesn't even have a command object yet, create one. Then invoke
945 'run()' on that command object (or an existing one).
946 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000947 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000948 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000949 return
950
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000951 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000952 cmd_obj = self.get_command_obj(command)
953 cmd_obj.ensure_finalized()
954 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000955 self.have_run[command] = 1
956
957
Greg Wardfe6462c2000-04-04 01:40:52 +0000958 # -- Distribution query methods ------------------------------------
959
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000960 def has_pure_modules(self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000961 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000962
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000963 def has_ext_modules(self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000964 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000965
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000966 def has_c_libraries(self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000967 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000968
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000969 def has_modules(self):
Greg Wardfe6462c2000-04-04 01:40:52 +0000970 return self.has_pure_modules() or self.has_ext_modules()
971
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000972 def has_headers(self):
Greg Ward51def7d2000-05-27 01:36:14 +0000973 return self.headers and len(self.headers) > 0
974
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000975 def has_scripts(self):
Greg Ward44a61bb2000-05-20 15:06:48 +0000976 return self.scripts and len(self.scripts) > 0
977
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000978 def has_data_files(self):
Greg Ward44a61bb2000-05-20 15:06:48 +0000979 return self.data_files and len(self.data_files) > 0
980
Tarek Ziadéae6acfc2009-05-16 18:29:40 +0000981 def is_pure(self):
Greg Wardfe6462c2000-04-04 01:40:52 +0000982 return (self.has_pure_modules() and
983 not self.has_ext_modules() and
984 not self.has_c_libraries())
985
Greg Ward82715e12000-04-21 02:28:14 +0000986 # -- Metadata query methods ----------------------------------------
987
988 # If you're looking for 'get_name()', 'get_version()', and so forth,
989 # they are defined in a sneaky way: the constructor binds self.get_XXX
990 # to self.metadata.get_XXX. The actual code is in the
991 # DistributionMetadata class, below.
992
Greg Ward82715e12000-04-21 02:28:14 +0000993class DistributionMetadata:
994 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000995 author, and so forth.
996 """
Greg Ward82715e12000-04-21 02:28:14 +0000997
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000998 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
999 "maintainer", "maintainer_email", "url",
1000 "license", "description", "long_description",
1001 "keywords", "platforms", "fullname", "contact",
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +00001002 "contact_email", "license", "classifiers",
Fred Drakedb7b0022005-03-20 22:19:47 +00001003 "download_url",
1004 # PEP 314
1005 "provides", "requires", "obsoletes",
1006 )
Neil Schemenauera8aefe52001-09-03 15:47:21 +00001007
Greg Ward82715e12000-04-21 02:28:14 +00001008 def __init__ (self):
1009 self.name = None
1010 self.version = None
1011 self.author = None
1012 self.author_email = None
1013 self.maintainer = None
1014 self.maintainer_email = None
1015 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001016 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +00001017 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +00001018 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001019 self.keywords = None
1020 self.platforms = None
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001021 self.classifiers = None
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001022 self.download_url = None
Fred Drakedb7b0022005-03-20 22:19:47 +00001023 # PEP 314
1024 self.provides = None
1025 self.requires = None
1026 self.obsoletes = None
Fred Drakeb94b8492001-12-06 20:51:35 +00001027
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001028 def write_pkg_info(self, base_dir):
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001029 """Write the PKG-INFO file into the release tree.
1030 """
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001031 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
Fred Drakedb7b0022005-03-20 22:19:47 +00001032 self.write_pkg_file(pkg_info)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001033 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001034
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001035 def write_pkg_file(self, file):
Fred Drakedb7b0022005-03-20 22:19:47 +00001036 """Write the PKG-INFO format data to a file object.
1037 """
1038 version = '1.0'
1039 if self.provides or self.requires or self.obsoletes:
1040 version = '1.1'
1041
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001042 self._write_field(file, 'Metadata-Version', version)
1043 self._write_field(file, 'Name', self.get_name())
1044 self._write_field(file, 'Version', self.get_version())
1045 self._write_field(file, 'Summary', self.get_description())
1046 self._write_field(file, 'Home-page', self.get_url())
1047 self._write_field(file, 'Author', self.get_contact())
1048 self._write_field(file, 'Author-email', self.get_contact_email())
1049 self._write_field(file, 'License', self.get_license())
Fred Drakedb7b0022005-03-20 22:19:47 +00001050 if self.download_url:
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001051 self._write_field(file, 'Download-URL', self.download_url)
Fred Drakedb7b0022005-03-20 22:19:47 +00001052
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001053 long_desc = rfc822_escape( self.get_long_description())
1054 self._write_field(file, 'Description', long_desc)
Fred Drakedb7b0022005-03-20 22:19:47 +00001055
1056 keywords = string.join( self.get_keywords(), ',')
1057 if keywords:
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001058 self._write_field(file, 'Keywords', keywords)
Fred Drakedb7b0022005-03-20 22:19:47 +00001059
1060 self._write_list(file, 'Platform', self.get_platforms())
1061 self._write_list(file, 'Classifier', self.get_classifiers())
1062
1063 # PEP 314
1064 self._write_list(file, 'Requires', self.get_requires())
1065 self._write_list(file, 'Provides', self.get_provides())
1066 self._write_list(file, 'Obsoletes', self.get_obsoletes())
1067
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001068 def _write_field(self, file, name, value):
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001069 if isinstance(value, unicode):
1070 value = value.encode(PKG_INFO_ENCODING)
1071 else:
1072 value = str(value)
1073 file.write('%s: %s\n' % (name, value))
1074
Fred Drakedb7b0022005-03-20 22:19:47 +00001075 def _write_list (self, file, name, values):
1076 for value in values:
Marc-André Lemburgb339b2a2008-09-03 11:13:56 +00001077 self._write_field(file, name, value)
Fred Drakedb7b0022005-03-20 22:19:47 +00001078
Greg Ward82715e12000-04-21 02:28:14 +00001079 # -- Metadata query methods ----------------------------------------
1080
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001081 def get_name(self):
Greg Wardfe6462c2000-04-04 01:40:52 +00001082 return self.name or "UNKNOWN"
1083
Greg Ward82715e12000-04-21 02:28:14 +00001084 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001085 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001086
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001087 def get_fullname(self):
Greg Ward82715e12000-04-21 02:28:14 +00001088 return "%s-%s" % (self.get_name(), self.get_version())
1089
1090 def get_author(self):
1091 return self.author or "UNKNOWN"
1092
1093 def get_author_email(self):
1094 return self.author_email or "UNKNOWN"
1095
1096 def get_maintainer(self):
1097 return self.maintainer or "UNKNOWN"
1098
1099 def get_maintainer_email(self):
1100 return self.maintainer_email or "UNKNOWN"
1101
1102 def get_contact(self):
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001103 return self.maintainer or self.author or "UNKNOWN"
Greg Ward82715e12000-04-21 02:28:14 +00001104
1105 def get_contact_email(self):
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001106 return self.maintainer_email or self.author_email or "UNKNOWN"
Greg Ward82715e12000-04-21 02:28:14 +00001107
1108 def get_url(self):
1109 return self.url or "UNKNOWN"
1110
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001111 def get_license(self):
1112 return self.license or "UNKNOWN"
1113 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001114
Greg Ward82715e12000-04-21 02:28:14 +00001115 def get_description(self):
1116 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001117
1118 def get_long_description(self):
1119 return self.long_description or "UNKNOWN"
1120
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001121 def get_keywords(self):
1122 return self.keywords or []
1123
1124 def get_platforms(self):
1125 return self.platforms or ["UNKNOWN"]
1126
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001127 def get_classifiers(self):
1128 return self.classifiers or []
1129
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001130 def get_download_url(self):
1131 return self.download_url or "UNKNOWN"
1132
Fred Drakedb7b0022005-03-20 22:19:47 +00001133 # PEP 314
Fred Drakedb7b0022005-03-20 22:19:47 +00001134 def get_requires(self):
1135 return self.requires or []
1136
1137 def set_requires(self, value):
1138 import distutils.versionpredicate
1139 for v in value:
1140 distutils.versionpredicate.VersionPredicate(v)
1141 self.requires = value
1142
1143 def get_provides(self):
1144 return self.provides or []
1145
1146 def set_provides(self, value):
1147 value = [v.strip() for v in value]
1148 for v in value:
1149 import distutils.versionpredicate
Fred Drake227e8ff2005-03-21 06:36:32 +00001150 distutils.versionpredicate.split_provision(v)
Fred Drakedb7b0022005-03-20 22:19:47 +00001151 self.provides = value
1152
1153 def get_obsoletes(self):
1154 return self.obsoletes or []
1155
1156 def set_obsoletes(self, value):
1157 import distutils.versionpredicate
1158 for v in value:
1159 distutils.versionpredicate.VersionPredicate(v)
1160 self.obsoletes = value
1161
Tarek Ziadéae6acfc2009-05-16 18:29:40 +00001162def fix_help_options(options):
Greg Ward2ff78872000-06-24 00:23:20 +00001163 """Convert a 4-tuple 'help_options' list as found in various command
1164 classes to the 3-tuple form required by FancyGetopt.
1165 """
1166 new_options = []
1167 for help_tuple in options:
1168 new_options.append(help_tuple[0:3])
1169 return new_options