blob: a23a773c57353e9aea9f4b3e4086b274f710123f [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
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00007# This module should be kept compatible with Python 1.5.2.
8
Greg Wardfe6462c2000-04-04 01:40:52 +00009__revision__ = "$Id$"
10
Gregory P. Smith14263542000-05-12 00:41:33 +000011import sys, os, string, re
Greg Wardfe6462c2000-04-04 01:40:52 +000012from types import *
13from copy import copy
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000014
15try:
16 import warnings
Andrew M. Kuchlingccf4e422002-10-31 13:39:33 +000017except ImportError:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000018 warnings = None
19
Greg Wardfe6462c2000-04-04 01:40:52 +000020from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000021from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000022from distutils.util import check_environ, strtobool, rfc822_escape
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000023from distutils import log
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000024from distutils.debug import DEBUG
Greg Wardfe6462c2000-04-04 01:40:52 +000025
26# 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
62 # options that are not propagated to the commands
63 display_options = [
64 ('help-commands', None,
65 "list all available commands"),
66 ('name', None,
67 "print package name"),
68 ('version', 'V',
69 "print package version"),
70 ('fullname', None,
71 "print <package name>-<version>"),
72 ('author', None,
73 "print the author's name"),
74 ('author-email', None,
75 "print the author's email address"),
76 ('maintainer', None,
77 "print the maintainer's name"),
78 ('maintainer-email', None,
79 "print the maintainer's email address"),
80 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000081 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000082 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000083 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000084 ('url', None,
85 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +000086 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +000087 "print the license of the package"),
88 ('licence', None,
89 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +000090 ('description', None,
91 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +000092 ('long-description', None,
93 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000094 ('platforms', None,
95 "print the list of platforms"),
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +000096 ('classifiers', None,
97 "print the list of classifiers"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000098 ('keywords', None,
99 "print the list of keywords"),
Greg Ward82715e12000-04-21 02:28:14 +0000100 ]
Greg Ward2f2b6c62000-09-25 01:58:07 +0000101 display_option_names = map(lambda x: translate_longopt(x[0]),
102 display_options)
Greg Ward82715e12000-04-21 02:28:14 +0000103
104 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +0000105 negative_opt = {'quiet': 'verbose'}
106
107
108 # -- Creation/initialization methods -------------------------------
Fred Drakeb94b8492001-12-06 20:51:35 +0000109
Greg Wardfe6462c2000-04-04 01:40:52 +0000110 def __init__ (self, attrs=None):
111 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000112 attributes of a Distribution, and then use 'attrs' (a dictionary
113 mapping attribute names to values) to assign some of those
114 attributes their "real" values. (Any attributes not mentioned in
115 'attrs' will be assigned to some null value: 0, None, an empty list
116 or dictionary, etc.) Most importantly, initialize the
117 'command_obj' attribute to the empty dictionary; this will be
118 filled in with real command objects by 'parse_command_line()'.
119 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000120
121 # Default values for our command-line options
122 self.verbose = 1
123 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000124 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000125 for attr in self.display_option_names:
126 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000127
Greg Ward82715e12000-04-21 02:28:14 +0000128 # Store the distribution meta-data (name, version, author, and so
129 # forth) in a separate object -- we're getting to have enough
130 # information here (and enough command-line options) that it's
131 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
132 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000133 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000134 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000135 method_name = "get_" + basename
136 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000137
138 # 'cmdclass' maps command names to class objects, so we
139 # can 1) quickly figure out which class to instantiate when
140 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000141 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000142 self.cmdclass = {}
143
Fred Draked04573f2004-08-03 16:37:40 +0000144 # 'command_packages' is a list of packages in which commands
145 # are searched for. The factory for command 'foo' is expected
146 # to be named 'foo' in the module 'foo' in one of the packages
147 # named here. This list is searched from the left; an error
148 # is raised if no named package provides the command being
149 # searched for. (Always access using get_command_packages().)
150 self.command_packages = None
151
Greg Ward9821bf42000-08-29 01:15:18 +0000152 # 'script_name' and 'script_args' are usually set to sys.argv[0]
153 # and sys.argv[1:], but they can be overridden when the caller is
154 # not necessarily a setup script run from the command-line.
155 self.script_name = None
156 self.script_args = None
157
Greg Wardd5d8a992000-05-23 01:42:17 +0000158 # 'command_options' is where we store command options between
159 # parsing them (from config files, the command-line, etc.) and when
160 # they are actually needed -- ie. when the command in question is
161 # instantiated. It is a dictionary of dictionaries of 2-tuples:
162 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000163 self.command_options = {}
164
Greg Wardfe6462c2000-04-04 01:40:52 +0000165 # These options are really the business of various commands, rather
166 # than of the Distribution itself. We provide aliases for them in
167 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000168 self.packages = None
Fred Drake0eb32a62004-06-11 21:50:33 +0000169 self.package_data = {}
Greg Wardfe6462c2000-04-04 01:40:52 +0000170 self.package_dir = None
171 self.py_modules = None
172 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000173 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000174 self.ext_modules = None
175 self.ext_package = None
176 self.include_dirs = None
177 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000178 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000179 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000180
181 # And now initialize bookkeeping stuff that can't be supplied by
182 # the caller at all. 'command_obj' maps command names to
183 # Command instances -- that's how we enforce that every command
184 # class is a singleton.
185 self.command_obj = {}
186
187 # 'have_run' maps command names to boolean values; it keeps track
188 # of whether we have actually run a particular command, to make it
189 # cheap to "run" a command whenever we think we might need to -- if
190 # it's already been done, no need for expensive filesystem
191 # operations, we just check the 'have_run' dictionary and carry on.
192 # It's only safe to query 'have_run' for a command class that has
193 # been instantiated -- a false value will be inserted when the
194 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000195 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000196 # '.get()' rather than a straight lookup.
197 self.have_run = {}
198
199 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000200 # the setup script) to possibly override any or all of these
201 # distribution options.
202
Greg Wardfe6462c2000-04-04 01:40:52 +0000203 if attrs:
204
205 # Pull out the set of command options and work on them
206 # specifically. Note that this order guarantees that aliased
207 # command options will override any supplied redundantly
208 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000209 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000210 if options:
211 del attrs['options']
212 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000213 opt_dict = self.get_option_dict(command)
214 for (opt, val) in cmd_options.items():
215 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000216
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +0000217 if attrs.has_key('licence'):
218 attrs['license'] = attrs['licence']
219 del attrs['licence']
220 msg = "'licence' distribution option is deprecated; use 'license'"
221 if warnings is not None:
222 warnings.warn(msg)
223 else:
224 sys.stderr.write(msg + "\n")
225
Greg Wardfe6462c2000-04-04 01:40:52 +0000226 # Now work on the rest of the attributes. Any attribute that's
227 # not already defined is invalid!
228 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000229 if hasattr(self.metadata, key):
230 setattr(self.metadata, key, val)
231 elif hasattr(self, key):
232 setattr(self, key, val)
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000233 msg = "Unknown distribution option: %s" % repr(key)
234 if warnings is not None:
235 warnings.warn(msg)
236 else:
237 sys.stderr.write(msg + "\n")
Greg Wardfe6462c2000-04-04 01:40:52 +0000238
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000239 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000240
Greg Wardfe6462c2000-04-04 01:40:52 +0000241 # __init__ ()
242
243
Greg Ward0e48cfd2000-05-26 01:00:15 +0000244 def get_option_dict (self, command):
245 """Get the option dictionary for a given command. If that
246 command's option dictionary hasn't been created yet, then create it
247 and return the new dictionary; otherwise, return the existing
248 option dictionary.
249 """
250
251 dict = self.command_options.get(command)
252 if dict is None:
253 dict = self.command_options[command] = {}
254 return dict
255
256
Greg Wardc32d9a62000-05-28 23:53:06 +0000257 def dump_option_dicts (self, header=None, commands=None, indent=""):
258 from pprint import pformat
259
260 if commands is None: # dump all command option dicts
261 commands = self.command_options.keys()
262 commands.sort()
263
264 if header is not None:
265 print indent + header
266 indent = indent + " "
267
268 if not commands:
269 print indent + "no commands known yet"
270 return
271
272 for cmd_name in commands:
273 opt_dict = self.command_options.get(cmd_name)
274 if opt_dict is None:
275 print indent + "no option dict for '%s' command" % cmd_name
276 else:
277 print indent + "option dict for '%s' command:" % cmd_name
278 out = pformat(opt_dict)
279 for line in string.split(out, "\n"):
280 print indent + " " + line
281
282 # dump_option_dicts ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000283
Greg Wardc32d9a62000-05-28 23:53:06 +0000284
285
Greg Wardd5d8a992000-05-23 01:42:17 +0000286 # -- Config file finding/parsing methods ---------------------------
287
Gregory P. Smith14263542000-05-12 00:41:33 +0000288 def find_config_files (self):
289 """Find as many configuration files as should be processed for this
290 platform, and return a list of filenames in the order in which they
291 should be parsed. The filenames returned are guaranteed to exist
292 (modulo nasty race conditions).
293
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000294 There are three possible config files: distutils.cfg in the
295 Distutils installation directory (ie. where the top-level
296 Distutils __inst__.py file lives), a file in the user's home
297 directory named .pydistutils.cfg on Unix and pydistutils.cfg
298 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000299 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000300 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000301 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000302
Greg Ward11696872000-06-07 02:29:03 +0000303 # Where to look for the system-wide Distutils config file
304 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
305
306 # Look for the system config file
307 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000308 if os.path.isfile(sys_file):
309 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000310
Greg Ward11696872000-06-07 02:29:03 +0000311 # What to call the per-user config file
312 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000313 user_filename = ".pydistutils.cfg"
314 else:
315 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000316
Greg Ward11696872000-06-07 02:29:03 +0000317 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000318 if os.environ.has_key('HOME'):
319 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000320 if os.path.isfile(user_file):
321 files.append(user_file)
322
Gregory P. Smith14263542000-05-12 00:41:33 +0000323 # All platforms support local setup.cfg
324 local_file = "setup.cfg"
325 if os.path.isfile(local_file):
326 files.append(local_file)
327
328 return files
329
330 # find_config_files ()
331
332
333 def parse_config_files (self, filenames=None):
334
335 from ConfigParser import ConfigParser
336
337 if filenames is None:
338 filenames = self.find_config_files()
339
Greg Ward2bd3f422000-06-02 01:59:33 +0000340 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000341
Gregory P. Smith14263542000-05-12 00:41:33 +0000342 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000343 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000344 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000345 parser.read(filename)
346 for section in parser.sections():
347 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000348 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000349
Greg Wardd5d8a992000-05-23 01:42:17 +0000350 for opt in options:
351 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000352 val = parser.get(section,opt)
353 opt = string.replace(opt, '-', '_')
354 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000355
Greg Ward47460772000-05-23 03:47:35 +0000356 # Make the ConfigParser forget everything (so we retain
Fred Drakef06116d2004-02-17 22:35:19 +0000357 # the original filenames that options come from)
Greg Ward47460772000-05-23 03:47:35 +0000358 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000359
Greg Wardceb9e222000-09-25 01:23:52 +0000360 # If there was a "global" section in the config file, use it
361 # to set Distribution options.
362
363 if self.command_options.has_key('global'):
364 for (opt, (src, val)) in self.command_options['global'].items():
365 alias = self.negative_opt.get(opt)
366 try:
367 if alias:
368 setattr(self, alias, not strtobool(val))
369 elif opt in ('verbose', 'dry_run'): # ugh!
370 setattr(self, opt, strtobool(val))
Fred Draked04573f2004-08-03 16:37:40 +0000371 else:
372 setattr(self, opt, val)
Greg Wardceb9e222000-09-25 01:23:52 +0000373 except ValueError, msg:
374 raise DistutilsOptionError, msg
375
376 # parse_config_files ()
377
Gregory P. Smith14263542000-05-12 00:41:33 +0000378
Greg Wardd5d8a992000-05-23 01:42:17 +0000379 # -- Command-line parsing methods ----------------------------------
380
Greg Ward9821bf42000-08-29 01:15:18 +0000381 def parse_command_line (self):
382 """Parse the setup script's command line, taken from the
383 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
384 -- see 'setup()' in core.py). This list is first processed for
385 "global options" -- options that set attributes of the Distribution
386 instance. Then, it is alternately scanned for Distutils commands
387 and options for that command. Each new command terminates the
388 options for the previous command. The allowed options for a
389 command are determined by the 'user_options' attribute of the
390 command class -- thus, we have to be able to load command classes
391 in order to parse the command line. Any error in that 'options'
392 attribute raises DistutilsGetoptError; any error on the
393 command-line raises DistutilsArgError. If no Distutils commands
394 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000395 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000396 on with executing commands; false if no errors but we shouldn't
397 execute commands (currently, this only happens if user asks for
398 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000399 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000400 #
Fred Drake981a1782001-08-10 18:59:30 +0000401 # We now have enough information to show the Macintosh dialog
402 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000403 #
Fred Draked04573f2004-08-03 16:37:40 +0000404 toplevel_options = self._get_toplevel_options()
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000405 if sys.platform == 'mac':
406 import EasyDialogs
407 cmdlist = self.get_command_list()
408 self.script_args = EasyDialogs.GetArgv(
Fred Draked04573f2004-08-03 16:37:40 +0000409 toplevel_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000410
Greg Wardfe6462c2000-04-04 01:40:52 +0000411 # We have to parse the command line a bit at a time -- global
412 # options, then the first command, then its options, and so on --
413 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000414 # the options that are valid for a particular class aren't known
415 # until we have loaded the command class, which doesn't happen
416 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000417
418 self.commands = []
Fred Draked04573f2004-08-03 16:37:40 +0000419 parser = FancyGetopt(toplevel_options + self.display_options)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000420 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000421 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000422 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000423 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000424 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000425
Greg Ward82715e12000-04-21 02:28:14 +0000426 # for display options we return immediately
427 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000428 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000429
Greg Wardfe6462c2000-04-04 01:40:52 +0000430 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000431 args = self._parse_command_opts(parser, args)
432 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000433 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000434
Greg Wardd5d8a992000-05-23 01:42:17 +0000435 # Handle the cases of --help as a "global" option, ie.
436 # "setup.py --help" and "setup.py --help command ...". For the
437 # former, we show global options (--verbose, --dry-run, etc.)
438 # and display-only options (--name, --version, etc.); for the
439 # latter, we omit the display-only options and show help for
440 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000441 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000442 self._show_help(parser,
443 display_options=len(self.commands) == 0,
444 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000445 return
446
447 # Oops, no commands found -- an end-user error
448 if not self.commands:
449 raise DistutilsArgError, "no commands supplied"
450
451 # All is well: return true
452 return 1
453
454 # parse_command_line()
455
Fred Draked04573f2004-08-03 16:37:40 +0000456 def _get_toplevel_options (self):
457 """Return the non-display options recognized at the top level.
458
459 This includes options that are recognized *only* at the top
460 level as well as options recognized for commands.
461 """
462 return self.global_options + [
463 ("command-packages=", None,
464 "list of packages that provide distutils commands"),
465 ]
466
Greg Wardd5d8a992000-05-23 01:42:17 +0000467 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000468 """Parse the command-line options for a single command.
469 'parser' must be a FancyGetopt instance; 'args' must be the list
470 of arguments, starting with the current command (whose options
471 we are about to parse). Returns a new version of 'args' with
472 the next command at the front of the list; will be the empty
473 list if there are no more commands on the command line. Returns
474 None if the user asked for help on this command.
475 """
476 # late import because of mutual dependence between these modules
477 from distutils.cmd import Command
478
479 # Pull the current command from the head of the command line
480 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000481 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000482 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000483 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000484
485 # Dig up the command class that implements this command, so we
486 # 1) know that it's a valid command, and 2) know which options
487 # it takes.
488 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000489 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000490 except DistutilsModuleError, msg:
491 raise DistutilsArgError, msg
492
493 # Require that the command class be derived from Command -- want
494 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000495 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000496 raise DistutilsClassError, \
497 "command class %s must subclass Command" % cmd_class
498
499 # Also make sure that the command object provides a list of its
500 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000501 if not (hasattr(cmd_class, 'user_options') and
502 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000503 raise DistutilsClassError, \
504 ("command class %s must provide " +
505 "'user_options' attribute (a list of tuples)") % \
506 cmd_class
507
508 # If the command class has a list of negative alias options,
509 # merge it in with the global negative aliases.
510 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000511 if hasattr(cmd_class, 'negative_opt'):
512 negative_opt = copy(negative_opt)
513 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000514
Greg Wardfa9ff762000-10-14 04:06:40 +0000515 # Check for help_options in command class. They have a different
516 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000517 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000518 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000519 help_options = fix_help_options(cmd_class.help_options)
520 else:
Greg Ward55fced32000-06-24 01:22:41 +0000521 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000522
Greg Ward9d17a7a2000-06-07 03:00:06 +0000523
Greg Wardd5d8a992000-05-23 01:42:17 +0000524 # All commands support the global options too, just by adding
525 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000526 parser.set_option_table(self.global_options +
527 cmd_class.user_options +
528 help_options)
529 parser.set_negative_aliases(negative_opt)
530 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000531 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000532 self._show_help(parser, display_options=0, commands=[cmd_class])
533 return
534
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000535 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000536 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000537 help_option_found=0
538 for (help_option, short, desc, func) in cmd_class.help_options:
539 if hasattr(opts, parser.get_attr_name(help_option)):
540 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000541 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000542 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000543
544 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000545 func()
Greg Ward55fced32000-06-24 01:22:41 +0000546 else:
Fred Drake981a1782001-08-10 18:59:30 +0000547 raise DistutilsClassError(
Walter Dörwald70a6b492004-02-12 17:35:32 +0000548 "invalid help function %r for help option '%s': "
Fred Drake981a1782001-08-10 18:59:30 +0000549 "must be a callable object (function, etc.)"
Walter Dörwald70a6b492004-02-12 17:35:32 +0000550 % (func, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000551
Fred Drakeb94b8492001-12-06 20:51:35 +0000552 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000553 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000554
Greg Wardd5d8a992000-05-23 01:42:17 +0000555 # Put the options from the command-line into their official
556 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000557 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000558 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000559 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000560
561 return args
562
563 # _parse_command_opts ()
564
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000565 def finalize_options (self):
566 """Set final values for all the options on the Distribution
567 instance, analogous to the .finalize_options() method of Command
568 objects.
569 """
570
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000571 keywords = self.metadata.keywords
572 if keywords is not None:
573 if type(keywords) is StringType:
574 keywordlist = string.split(keywords, ',')
575 self.metadata.keywords = map(string.strip, keywordlist)
576
577 platforms = self.metadata.platforms
578 if platforms is not None:
579 if type(platforms) is StringType:
580 platformlist = string.split(platforms, ',')
581 self.metadata.platforms = map(string.strip, platformlist)
582
Greg Wardd5d8a992000-05-23 01:42:17 +0000583 def _show_help (self,
584 parser,
585 global_options=1,
586 display_options=1,
587 commands=[]):
588 """Show help for the setup script command-line in the form of
589 several lists of command-line options. 'parser' should be a
590 FancyGetopt instance; do not expect it to be returned in the
591 same state, as its option table will be reset to make it
592 generate the correct help text.
593
594 If 'global_options' is true, lists the global options:
595 --verbose, --dry-run, etc. If 'display_options' is true, lists
596 the "display-only" options: --name, --version, etc. Finally,
597 lists per-command help for every command name or command class
598 in 'commands'.
599 """
600 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000601 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000602 from distutils.cmd import Command
603
604 if global_options:
Fred Draked04573f2004-08-03 16:37:40 +0000605 if display_options:
606 options = self._get_toplevel_options()
607 else:
608 options = self.global_options
609 parser.set_option_table(options)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000610 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000611 print
612
613 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000614 parser.set_option_table(self.display_options)
615 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000616 "Information display options (just display " +
617 "information, ignore any commands)")
618 print
619
620 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000621 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000622 klass = command
623 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000624 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000625 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000626 type(klass.help_options) is ListType):
627 parser.set_option_table(klass.user_options +
628 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000629 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000630 parser.set_option_table(klass.user_options)
631 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000632 print
633
Greg Ward9821bf42000-08-29 01:15:18 +0000634 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000635 return
636
637 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000638
Greg Wardd5d8a992000-05-23 01:42:17 +0000639
Greg Ward82715e12000-04-21 02:28:14 +0000640 def handle_display_options (self, option_order):
641 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000642 (--help-commands or the metadata display options) on the command
643 line, display the requested info and return true; else return
644 false.
645 """
Greg Ward9821bf42000-08-29 01:15:18 +0000646 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000647
648 # User just wants a list of commands -- we'll print it out and stop
649 # processing now (ie. if they ran "setup --help-commands foo bar",
650 # we ignore "foo bar").
651 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000652 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000653 print
Greg Ward9821bf42000-08-29 01:15:18 +0000654 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000655 return 1
656
657 # If user supplied any of the "display metadata" options, then
658 # display that metadata in the order in which the user supplied the
659 # metadata options.
660 any_display_options = 0
661 is_display_option = {}
662 for option in self.display_options:
663 is_display_option[option[0]] = 1
664
665 for (opt, val) in option_order:
666 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000667 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000668 value = getattr(self.metadata, "get_"+opt)()
669 if opt in ['keywords', 'platforms']:
670 print string.join(value, ',')
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000671 elif opt == 'classifiers':
672 print string.join(value, '\n')
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000673 else:
674 print value
Greg Ward82715e12000-04-21 02:28:14 +0000675 any_display_options = 1
676
677 return any_display_options
678
679 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000680
681 def print_command_list (self, commands, header, max_length):
682 """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
686 print header + ":"
687
688 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000689 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000690 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000691 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000692 try:
693 description = klass.description
694 except AttributeError:
695 description = "(no description available)"
696
697 print " %-*s %s" % (max_length, cmd, description)
698
699 # print_command_list ()
700
701
702 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000703 """Print out a help message listing all available commands with a
704 description of each. The list is divided into "standard commands"
705 (listed in distutils.command.__all__) and "extra commands"
706 (mentioned in self.cmdclass, but not a standard command). The
707 descriptions come from the command class attribute
708 'description'.
709 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000710
711 import distutils.command
712 std_commands = distutils.command.__all__
713 is_std = {}
714 for cmd in std_commands:
715 is_std[cmd] = 1
716
717 extra_commands = []
718 for cmd in self.cmdclass.keys():
719 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000720 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000721
722 max_length = 0
723 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000724 if len(cmd) > max_length:
725 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000726
Greg Wardfd7b91e2000-09-26 01:52:25 +0000727 self.print_command_list(std_commands,
728 "Standard commands",
729 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000730 if extra_commands:
731 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000732 self.print_command_list(extra_commands,
733 "Extra commands",
734 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000735
736 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000737
Greg Wardf6fc8752000-11-11 02:47:11 +0000738 def get_command_list (self):
739 """Get a list of (command, description) tuples.
740 The list is divided into "standard commands" (listed in
741 distutils.command.__all__) and "extra commands" (mentioned in
742 self.cmdclass, but not a standard command). The descriptions come
743 from the command class attribute 'description'.
744 """
745 # Currently this is only used on Mac OS, for the Mac-only GUI
746 # Distutils interface (by Jack Jansen)
747
748 import distutils.command
749 std_commands = distutils.command.__all__
750 is_std = {}
751 for cmd in std_commands:
752 is_std[cmd] = 1
753
754 extra_commands = []
755 for cmd in self.cmdclass.keys():
756 if not is_std.get(cmd):
757 extra_commands.append(cmd)
758
759 rv = []
760 for cmd in (std_commands + extra_commands):
761 klass = self.cmdclass.get(cmd)
762 if not klass:
763 klass = self.get_command_class(cmd)
764 try:
765 description = klass.description
766 except AttributeError:
767 description = "(no description available)"
768 rv.append((cmd, description))
769 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000770
771 # -- Command class/object methods ----------------------------------
772
Fred Draked04573f2004-08-03 16:37:40 +0000773 def get_command_packages (self):
774 """Return a list of packages from which commands are loaded."""
775 pkgs = self.command_packages
776 if not isinstance(pkgs, type([])):
777 pkgs = string.split(pkgs or "", ",")
778 for i in range(len(pkgs)):
779 pkgs[i] = string.strip(pkgs[i])
780 pkgs = filter(None, pkgs)
781 if "distutils.command" not in pkgs:
782 pkgs.insert(0, "distutils.command")
783 self.command_packages = pkgs
784 return pkgs
785
Greg Wardd5d8a992000-05-23 01:42:17 +0000786 def get_command_class (self, command):
787 """Return the class that implements the Distutils command named by
788 'command'. First we check the 'cmdclass' dictionary; if the
789 command is mentioned there, we fetch the class object from the
790 dictionary and return it. Otherwise we load the command module
791 ("distutils.command." + command) and fetch the command class from
792 the module. The loaded class is also stored in 'cmdclass'
793 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000794
Gregory P. Smith14263542000-05-12 00:41:33 +0000795 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000796 found, or if that module does not define the expected class.
797 """
798 klass = self.cmdclass.get(command)
799 if klass:
800 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000801
Fred Draked04573f2004-08-03 16:37:40 +0000802 for pkgname in self.get_command_packages():
803 module_name = "%s.%s" % (pkgname, command)
804 klass_name = command
Greg Wardfe6462c2000-04-04 01:40:52 +0000805
Fred Draked04573f2004-08-03 16:37:40 +0000806 try:
807 __import__ (module_name)
808 module = sys.modules[module_name]
809 except ImportError:
810 continue
Greg Wardfe6462c2000-04-04 01:40:52 +0000811
Fred Draked04573f2004-08-03 16:37:40 +0000812 try:
813 klass = getattr(module, klass_name)
814 except AttributeError:
815 raise DistutilsModuleError, \
816 "invalid command '%s' (no class '%s' in module '%s')" \
817 % (command, klass_name, module_name)
Greg Wardfe6462c2000-04-04 01:40:52 +0000818
Fred Draked04573f2004-08-03 16:37:40 +0000819 self.cmdclass[command] = klass
820 return klass
821
822 raise DistutilsModuleError("invalid command '%s'" % command)
823
Greg Wardfe6462c2000-04-04 01:40:52 +0000824
Greg Wardd5d8a992000-05-23 01:42:17 +0000825 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000826
Greg Wardd5d8a992000-05-23 01:42:17 +0000827 def get_command_obj (self, command, create=1):
828 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000829 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000830 object for 'command' is in the cache, then we either create and
831 return it (if 'create' is true) or return None.
832 """
833 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000834 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000835 if DEBUG:
836 print "Distribution.get_command_obj(): " \
837 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000838
Greg Wardd5d8a992000-05-23 01:42:17 +0000839 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000840 cmd_obj = self.command_obj[command] = klass(self)
841 self.have_run[command] = 0
842
843 # Set any options that were supplied in config files
844 # or on the command line. (NB. support for error
845 # reporting is lame here: any errors aren't reported
846 # until 'finalize_options()' is called, which means
847 # we won't report the source of the error.)
848 options = self.command_options.get(command)
849 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000850 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000851
852 return cmd_obj
853
Greg Wardc32d9a62000-05-28 23:53:06 +0000854 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000855 """Set the options for 'command_obj' from 'option_dict'. Basically
856 this means copying elements of a dictionary ('option_dict') to
857 attributes of an instance ('command').
858
Greg Wardceb9e222000-09-25 01:23:52 +0000859 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000860 supplied, uses the standard option dictionary for this command
861 (from 'self.command_options').
862 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000863 command_name = command_obj.get_command_name()
864 if option_dict is None:
865 option_dict = self.get_option_dict(command_name)
866
867 if DEBUG: print " setting options for '%s' command:" % command_name
868 for (option, (source, value)) in option_dict.items():
869 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000870 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000871 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000872 except AttributeError:
873 bool_opts = []
874 try:
875 neg_opt = command_obj.negative_opt
876 except AttributeError:
877 neg_opt = {}
878
879 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000880 is_string = type(value) is StringType
881 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000882 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000883 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000884 setattr(command_obj, option, strtobool(value))
885 elif hasattr(command_obj, option):
886 setattr(command_obj, option, value)
887 else:
888 raise DistutilsOptionError, \
889 ("error in %s: command '%s' has no such option '%s'"
890 % (source, command_name, option))
891 except ValueError, msg:
892 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000893
Greg Wardf449ea52000-09-16 15:23:28 +0000894 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000895 """Reinitializes a command to the state it was in when first
896 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000897 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000898 values in programmatically, overriding or supplementing
899 user-supplied values from the config files and command line.
900 You'll have to re-finalize the command object (by calling
901 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000902 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000903
Greg Wardf449ea52000-09-16 15:23:28 +0000904 'command' should be a command name (string) or command object. If
905 'reinit_subcommands' is true, also reinitializes the command's
906 sub-commands, as declared by the 'sub_commands' class attribute (if
907 it has one). See the "install" command for an example. Only
908 reinitializes the sub-commands that actually matter, ie. those
909 whose test predicates return true.
910
Greg Wardc32d9a62000-05-28 23:53:06 +0000911 Returns the reinitialized command object.
912 """
913 from distutils.cmd import Command
914 if not isinstance(command, Command):
915 command_name = command
916 command = self.get_command_obj(command_name)
917 else:
918 command_name = command.get_command_name()
919
920 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000921 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000922 command.initialize_options()
923 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000924 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000925 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000926
Greg Wardf449ea52000-09-16 15:23:28 +0000927 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000928 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000929 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000930
Greg Wardc32d9a62000-05-28 23:53:06 +0000931 return command
932
Fred Drakeb94b8492001-12-06 20:51:35 +0000933
Greg Wardfe6462c2000-04-04 01:40:52 +0000934 # -- Methods that operate on the Distribution ----------------------
935
936 def announce (self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000937 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000938
939 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000940 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000941 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000942 created by 'get_command_obj()'.
943 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000944 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000945 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000946
947
Greg Wardfe6462c2000-04-04 01:40:52 +0000948 # -- Methods that operate on its Commands --------------------------
949
950 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000951 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000952 if the command has already been run). Specifically: if we have
953 already created and run the command named by 'command', return
954 silently without doing anything. If the command named by 'command'
955 doesn't even have a command object yet, create one. Then invoke
956 'run()' on that command object (or an existing one).
957 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000958 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000959 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000960 return
961
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000962 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000963 cmd_obj = self.get_command_obj(command)
964 cmd_obj.ensure_finalized()
965 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000966 self.have_run[command] = 1
967
968
Greg Wardfe6462c2000-04-04 01:40:52 +0000969 # -- Distribution query methods ------------------------------------
970
971 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000972 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000973
974 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000975 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000976
977 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000978 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000979
980 def has_modules (self):
981 return self.has_pure_modules() or self.has_ext_modules()
982
Greg Ward51def7d2000-05-27 01:36:14 +0000983 def has_headers (self):
984 return self.headers and len(self.headers) > 0
985
Greg Ward44a61bb2000-05-20 15:06:48 +0000986 def has_scripts (self):
987 return self.scripts and len(self.scripts) > 0
988
989 def has_data_files (self):
990 return self.data_files and len(self.data_files) > 0
991
Greg Wardfe6462c2000-04-04 01:40:52 +0000992 def is_pure (self):
993 return (self.has_pure_modules() and
994 not self.has_ext_modules() and
995 not self.has_c_libraries())
996
Greg Ward82715e12000-04-21 02:28:14 +0000997 # -- Metadata query methods ----------------------------------------
998
999 # If you're looking for 'get_name()', 'get_version()', and so forth,
1000 # they are defined in a sneaky way: the constructor binds self.get_XXX
1001 # to self.metadata.get_XXX. The actual code is in the
1002 # DistributionMetadata class, below.
1003
1004# class Distribution
1005
1006
1007class DistributionMetadata:
1008 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +00001009 author, and so forth.
1010 """
Greg Ward82715e12000-04-21 02:28:14 +00001011
Neil Schemenauera8aefe52001-09-03 15:47:21 +00001012 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
1013 "maintainer", "maintainer_email", "url",
1014 "license", "description", "long_description",
1015 "keywords", "platforms", "fullname", "contact",
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +00001016 "contact_email", "license", "classifiers",
Anthony Baxterf2113f02004-10-13 12:35:28 +00001017 "download_url")
Neil Schemenauera8aefe52001-09-03 15:47:21 +00001018
Greg Ward82715e12000-04-21 02:28:14 +00001019 def __init__ (self):
1020 self.name = None
1021 self.version = None
1022 self.author = None
1023 self.author_email = None
1024 self.maintainer = None
1025 self.maintainer_email = None
1026 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001027 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +00001028 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +00001029 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001030 self.keywords = None
1031 self.platforms = None
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001032 self.classifiers = None
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001033 self.download_url = None
Fred Drakeb94b8492001-12-06 20:51:35 +00001034
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001035 def write_pkg_info (self, base_dir):
1036 """Write the PKG-INFO file into the release tree.
1037 """
1038
1039 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
1040
1041 pkg_info.write('Metadata-Version: 1.0\n')
1042 pkg_info.write('Name: %s\n' % self.get_name() )
1043 pkg_info.write('Version: %s\n' % self.get_version() )
1044 pkg_info.write('Summary: %s\n' % self.get_description() )
1045 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +00001046 pkg_info.write('Author: %s\n' % self.get_contact() )
1047 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001048 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001049 if self.download_url:
1050 pkg_info.write('Download-URL: %s\n' % self.download_url)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001051
1052 long_desc = rfc822_escape( self.get_long_description() )
1053 pkg_info.write('Description: %s\n' % long_desc)
1054
1055 keywords = string.join( self.get_keywords(), ',')
1056 if keywords:
1057 pkg_info.write('Keywords: %s\n' % keywords )
1058
1059 for platform in self.get_platforms():
1060 pkg_info.write('Platform: %s\n' % platform )
1061
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001062 for classifier in self.get_classifiers():
1063 pkg_info.write('Classifier: %s\n' % classifier )
1064
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001065 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001066
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001067 # write_pkg_info ()
Fred Drakeb94b8492001-12-06 20:51:35 +00001068
Greg Ward82715e12000-04-21 02:28:14 +00001069 # -- Metadata query methods ----------------------------------------
1070
Greg Wardfe6462c2000-04-04 01:40:52 +00001071 def get_name (self):
1072 return self.name or "UNKNOWN"
1073
Greg Ward82715e12000-04-21 02:28:14 +00001074 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001075 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001076
Greg Ward82715e12000-04-21 02:28:14 +00001077 def get_fullname (self):
1078 return "%s-%s" % (self.get_name(), self.get_version())
1079
1080 def get_author(self):
1081 return self.author or "UNKNOWN"
1082
1083 def get_author_email(self):
1084 return self.author_email or "UNKNOWN"
1085
1086 def get_maintainer(self):
1087 return self.maintainer or "UNKNOWN"
1088
1089 def get_maintainer_email(self):
1090 return self.maintainer_email or "UNKNOWN"
1091
1092 def get_contact(self):
1093 return (self.maintainer or
1094 self.author or
1095 "UNKNOWN")
1096
1097 def get_contact_email(self):
1098 return (self.maintainer_email or
1099 self.author_email or
1100 "UNKNOWN")
1101
1102 def get_url(self):
1103 return self.url or "UNKNOWN"
1104
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001105 def get_license(self):
1106 return self.license or "UNKNOWN"
1107 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001108
Greg Ward82715e12000-04-21 02:28:14 +00001109 def get_description(self):
1110 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001111
1112 def get_long_description(self):
1113 return self.long_description or "UNKNOWN"
1114
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001115 def get_keywords(self):
1116 return self.keywords or []
1117
1118 def get_platforms(self):
1119 return self.platforms or ["UNKNOWN"]
1120
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001121 def get_classifiers(self):
1122 return self.classifiers or []
1123
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001124 def get_download_url(self):
1125 return self.download_url or "UNKNOWN"
1126
Greg Ward82715e12000-04-21 02:28:14 +00001127# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001128
Greg Ward2ff78872000-06-24 00:23:20 +00001129
1130def fix_help_options (options):
1131 """Convert a 4-tuple 'help_options' list as found in various command
1132 classes to the 3-tuple form required by FancyGetopt.
1133 """
1134 new_options = []
1135 for help_tuple in options:
1136 new_options.append(help_tuple[0:3])
1137 return new_options
1138
1139
Greg Wardfe6462c2000-04-04 01:40:52 +00001140if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001141 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001142 print "ok"