blob: 586e6bb1452780ec3bea06711bbb0651c3bf0c49 [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
Greg Ward9821bf42000-08-29 01:15:18 +0000144 # 'script_name' and 'script_args' are usually set to sys.argv[0]
145 # and sys.argv[1:], but they can be overridden when the caller is
146 # not necessarily a setup script run from the command-line.
147 self.script_name = None
148 self.script_args = None
149
Greg Wardd5d8a992000-05-23 01:42:17 +0000150 # 'command_options' is where we store command options between
151 # parsing them (from config files, the command-line, etc.) and when
152 # they are actually needed -- ie. when the command in question is
153 # instantiated. It is a dictionary of dictionaries of 2-tuples:
154 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000155 self.command_options = {}
156
Greg Wardfe6462c2000-04-04 01:40:52 +0000157 # These options are really the business of various commands, rather
158 # than of the Distribution itself. We provide aliases for them in
159 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000160 self.packages = None
161 self.package_dir = None
162 self.py_modules = None
163 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000164 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000165 self.ext_modules = None
166 self.ext_package = None
167 self.include_dirs = None
168 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000169 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000170 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000171
172 # And now initialize bookkeeping stuff that can't be supplied by
173 # the caller at all. 'command_obj' maps command names to
174 # Command instances -- that's how we enforce that every command
175 # class is a singleton.
176 self.command_obj = {}
177
178 # 'have_run' maps command names to boolean values; it keeps track
179 # of whether we have actually run a particular command, to make it
180 # cheap to "run" a command whenever we think we might need to -- if
181 # it's already been done, no need for expensive filesystem
182 # operations, we just check the 'have_run' dictionary and carry on.
183 # It's only safe to query 'have_run' for a command class that has
184 # been instantiated -- a false value will be inserted when the
185 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000186 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000187 # '.get()' rather than a straight lookup.
188 self.have_run = {}
189
190 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000191 # the setup script) to possibly override any or all of these
192 # distribution options.
193
Greg Wardfe6462c2000-04-04 01:40:52 +0000194 if attrs:
195
196 # Pull out the set of command options and work on them
197 # specifically. Note that this order guarantees that aliased
198 # command options will override any supplied redundantly
199 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000200 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000201 if options:
202 del attrs['options']
203 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000204 opt_dict = self.get_option_dict(command)
205 for (opt, val) in cmd_options.items():
206 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000207
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +0000208 if attrs.has_key('licence'):
209 attrs['license'] = attrs['licence']
210 del attrs['licence']
211 msg = "'licence' distribution option is deprecated; use 'license'"
212 if warnings is not None:
213 warnings.warn(msg)
214 else:
215 sys.stderr.write(msg + "\n")
216
Greg Wardfe6462c2000-04-04 01:40:52 +0000217 # Now work on the rest of the attributes. Any attribute that's
218 # not already defined is invalid!
219 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000220 if hasattr(self.metadata, key):
221 setattr(self.metadata, key, val)
222 elif hasattr(self, key):
223 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000224 else:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000225 msg = "Unknown distribution option: %s" % repr(key)
226 if warnings is not None:
227 warnings.warn(msg)
228 else:
229 sys.stderr.write(msg + "\n")
Greg Wardfe6462c2000-04-04 01:40:52 +0000230
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000231 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000232
Greg Wardfe6462c2000-04-04 01:40:52 +0000233 # __init__ ()
234
235
Greg Ward0e48cfd2000-05-26 01:00:15 +0000236 def get_option_dict (self, command):
237 """Get the option dictionary for a given command. If that
238 command's option dictionary hasn't been created yet, then create it
239 and return the new dictionary; otherwise, return the existing
240 option dictionary.
241 """
242
243 dict = self.command_options.get(command)
244 if dict is None:
245 dict = self.command_options[command] = {}
246 return dict
247
248
Greg Wardc32d9a62000-05-28 23:53:06 +0000249 def dump_option_dicts (self, header=None, commands=None, indent=""):
250 from pprint import pformat
251
252 if commands is None: # dump all command option dicts
253 commands = self.command_options.keys()
254 commands.sort()
255
256 if header is not None:
257 print indent + header
258 indent = indent + " "
259
260 if not commands:
261 print indent + "no commands known yet"
262 return
263
264 for cmd_name in commands:
265 opt_dict = self.command_options.get(cmd_name)
266 if opt_dict is None:
267 print indent + "no option dict for '%s' command" % cmd_name
268 else:
269 print indent + "option dict for '%s' command:" % cmd_name
270 out = pformat(opt_dict)
271 for line in string.split(out, "\n"):
272 print indent + " " + line
273
274 # dump_option_dicts ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000275
Greg Wardc32d9a62000-05-28 23:53:06 +0000276
277
Greg Wardd5d8a992000-05-23 01:42:17 +0000278 # -- Config file finding/parsing methods ---------------------------
279
Gregory P. Smith14263542000-05-12 00:41:33 +0000280 def find_config_files (self):
281 """Find as many configuration files as should be processed for this
282 platform, and return a list of filenames in the order in which they
283 should be parsed. The filenames returned are guaranteed to exist
284 (modulo nasty race conditions).
285
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000286 There are three possible config files: distutils.cfg in the
287 Distutils installation directory (ie. where the top-level
288 Distutils __inst__.py file lives), a file in the user's home
289 directory named .pydistutils.cfg on Unix and pydistutils.cfg
290 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000291 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000292 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000293 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000294
Greg Ward11696872000-06-07 02:29:03 +0000295 # Where to look for the system-wide Distutils config file
296 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
297
298 # Look for the system config file
299 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000300 if os.path.isfile(sys_file):
301 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000302
Greg Ward11696872000-06-07 02:29:03 +0000303 # What to call the per-user config file
304 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000305 user_filename = ".pydistutils.cfg"
306 else:
307 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000308
Greg Ward11696872000-06-07 02:29:03 +0000309 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000310 if os.environ.has_key('HOME'):
311 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000312 if os.path.isfile(user_file):
313 files.append(user_file)
314
Gregory P. Smith14263542000-05-12 00:41:33 +0000315 # All platforms support local setup.cfg
316 local_file = "setup.cfg"
317 if os.path.isfile(local_file):
318 files.append(local_file)
319
320 return files
321
322 # find_config_files ()
323
324
325 def parse_config_files (self, filenames=None):
326
327 from ConfigParser import ConfigParser
328
329 if filenames is None:
330 filenames = self.find_config_files()
331
Greg Ward2bd3f422000-06-02 01:59:33 +0000332 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000333
Gregory P. Smith14263542000-05-12 00:41:33 +0000334 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000335 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000336 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000337 parser.read(filename)
338 for section in parser.sections():
339 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000340 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000341
Greg Wardd5d8a992000-05-23 01:42:17 +0000342 for opt in options:
343 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000344 val = parser.get(section,opt)
345 opt = string.replace(opt, '-', '_')
346 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000347
Greg Ward47460772000-05-23 03:47:35 +0000348 # Make the ConfigParser forget everything (so we retain
Fred Drakef06116d2004-02-17 22:35:19 +0000349 # the original filenames that options come from)
Greg Ward47460772000-05-23 03:47:35 +0000350 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000351
Greg Wardceb9e222000-09-25 01:23:52 +0000352 # If there was a "global" section in the config file, use it
353 # to set Distribution options.
354
355 if self.command_options.has_key('global'):
356 for (opt, (src, val)) in self.command_options['global'].items():
357 alias = self.negative_opt.get(opt)
358 try:
359 if alias:
360 setattr(self, alias, not strtobool(val))
361 elif opt in ('verbose', 'dry_run'): # ugh!
362 setattr(self, opt, strtobool(val))
363 except ValueError, msg:
364 raise DistutilsOptionError, msg
365
366 # parse_config_files ()
367
Gregory P. Smith14263542000-05-12 00:41:33 +0000368
Greg Wardd5d8a992000-05-23 01:42:17 +0000369 # -- Command-line parsing methods ----------------------------------
370
Greg Ward9821bf42000-08-29 01:15:18 +0000371 def parse_command_line (self):
372 """Parse the setup script's command line, taken from the
373 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
374 -- see 'setup()' in core.py). This list is first processed for
375 "global options" -- options that set attributes of the Distribution
376 instance. Then, it is alternately scanned for Distutils commands
377 and options for that command. Each new command terminates the
378 options for the previous command. The allowed options for a
379 command are determined by the 'user_options' attribute of the
380 command class -- thus, we have to be able to load command classes
381 in order to parse the command line. Any error in that 'options'
382 attribute raises DistutilsGetoptError; any error on the
383 command-line raises DistutilsArgError. If no Distutils commands
384 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000385 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000386 on with executing commands; false if no errors but we shouldn't
387 execute commands (currently, this only happens if user asks for
388 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000389 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000390 #
Fred Drake981a1782001-08-10 18:59:30 +0000391 # We now have enough information to show the Macintosh dialog
392 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000393 #
394 if sys.platform == 'mac':
395 import EasyDialogs
396 cmdlist = self.get_command_list()
397 self.script_args = EasyDialogs.GetArgv(
398 self.global_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000399
Greg Wardfe6462c2000-04-04 01:40:52 +0000400 # We have to parse the command line a bit at a time -- global
401 # options, then the first command, then its options, and so on --
402 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000403 # the options that are valid for a particular class aren't known
404 # until we have loaded the command class, which doesn't happen
405 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000406
407 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000408 parser = FancyGetopt(self.global_options + self.display_options)
409 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000410 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000411 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000412 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000413 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000414
Greg Ward82715e12000-04-21 02:28:14 +0000415 # for display options we return immediately
416 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000417 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000418
Greg Wardfe6462c2000-04-04 01:40:52 +0000419 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000420 args = self._parse_command_opts(parser, args)
421 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000422 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000423
Greg Wardd5d8a992000-05-23 01:42:17 +0000424 # Handle the cases of --help as a "global" option, ie.
425 # "setup.py --help" and "setup.py --help command ...". For the
426 # former, we show global options (--verbose, --dry-run, etc.)
427 # and display-only options (--name, --version, etc.); for the
428 # latter, we omit the display-only options and show help for
429 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000430 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000431 self._show_help(parser,
432 display_options=len(self.commands) == 0,
433 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000434 return
435
436 # Oops, no commands found -- an end-user error
437 if not self.commands:
438 raise DistutilsArgError, "no commands supplied"
439
440 # All is well: return true
441 return 1
442
443 # parse_command_line()
444
Greg Wardd5d8a992000-05-23 01:42:17 +0000445 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000446 """Parse the command-line options for a single command.
447 'parser' must be a FancyGetopt instance; 'args' must be the list
448 of arguments, starting with the current command (whose options
449 we are about to parse). Returns a new version of 'args' with
450 the next command at the front of the list; will be the empty
451 list if there are no more commands on the command line. Returns
452 None if the user asked for help on this command.
453 """
454 # late import because of mutual dependence between these modules
455 from distutils.cmd import Command
456
457 # Pull the current command from the head of the command line
458 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000459 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000460 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000461 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000462
463 # Dig up the command class that implements this command, so we
464 # 1) know that it's a valid command, and 2) know which options
465 # it takes.
466 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000467 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000468 except DistutilsModuleError, msg:
469 raise DistutilsArgError, msg
470
471 # Require that the command class be derived from Command -- want
472 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000473 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000474 raise DistutilsClassError, \
475 "command class %s must subclass Command" % cmd_class
476
477 # Also make sure that the command object provides a list of its
478 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000479 if not (hasattr(cmd_class, 'user_options') and
480 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000481 raise DistutilsClassError, \
482 ("command class %s must provide " +
483 "'user_options' attribute (a list of tuples)") % \
484 cmd_class
485
486 # If the command class has a list of negative alias options,
487 # merge it in with the global negative aliases.
488 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000489 if hasattr(cmd_class, 'negative_opt'):
490 negative_opt = copy(negative_opt)
491 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000492
Greg Wardfa9ff762000-10-14 04:06:40 +0000493 # Check for help_options in command class. They have a different
494 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000495 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000496 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000497 help_options = fix_help_options(cmd_class.help_options)
498 else:
Greg Ward55fced32000-06-24 01:22:41 +0000499 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000500
Greg Ward9d17a7a2000-06-07 03:00:06 +0000501
Greg Wardd5d8a992000-05-23 01:42:17 +0000502 # All commands support the global options too, just by adding
503 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000504 parser.set_option_table(self.global_options +
505 cmd_class.user_options +
506 help_options)
507 parser.set_negative_aliases(negative_opt)
508 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000509 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000510 self._show_help(parser, display_options=0, commands=[cmd_class])
511 return
512
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000513 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000514 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000515 help_option_found=0
516 for (help_option, short, desc, func) in cmd_class.help_options:
517 if hasattr(opts, parser.get_attr_name(help_option)):
518 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000519 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000520 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000521
522 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000523 func()
Greg Ward55fced32000-06-24 01:22:41 +0000524 else:
Fred Drake981a1782001-08-10 18:59:30 +0000525 raise DistutilsClassError(
Walter Dörwald70a6b492004-02-12 17:35:32 +0000526 "invalid help function %r for help option '%s': "
Fred Drake981a1782001-08-10 18:59:30 +0000527 "must be a callable object (function, etc.)"
Walter Dörwald70a6b492004-02-12 17:35:32 +0000528 % (func, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000529
Fred Drakeb94b8492001-12-06 20:51:35 +0000530 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000531 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000532
Greg Wardd5d8a992000-05-23 01:42:17 +0000533 # Put the options from the command-line into their official
534 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000535 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000536 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000537 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000538
539 return args
540
541 # _parse_command_opts ()
542
543
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000544 def finalize_options (self):
545 """Set final values for all the options on the Distribution
546 instance, analogous to the .finalize_options() method of Command
547 objects.
548 """
549
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000550 keywords = self.metadata.keywords
551 if keywords is not None:
552 if type(keywords) is StringType:
553 keywordlist = string.split(keywords, ',')
554 self.metadata.keywords = map(string.strip, keywordlist)
555
556 platforms = self.metadata.platforms
557 if platforms is not None:
558 if type(platforms) is StringType:
559 platformlist = string.split(platforms, ',')
560 self.metadata.platforms = map(string.strip, platformlist)
561
Greg Wardd5d8a992000-05-23 01:42:17 +0000562 def _show_help (self,
563 parser,
564 global_options=1,
565 display_options=1,
566 commands=[]):
567 """Show help for the setup script command-line in the form of
568 several lists of command-line options. 'parser' should be a
569 FancyGetopt instance; do not expect it to be returned in the
570 same state, as its option table will be reset to make it
571 generate the correct help text.
572
573 If 'global_options' is true, lists the global options:
574 --verbose, --dry-run, etc. If 'display_options' is true, lists
575 the "display-only" options: --name, --version, etc. Finally,
576 lists per-command help for every command name or command class
577 in 'commands'.
578 """
579 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000580 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000581 from distutils.cmd import Command
582
583 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000584 parser.set_option_table(self.global_options)
585 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000586 print
587
588 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000589 parser.set_option_table(self.display_options)
590 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000591 "Information display options (just display " +
592 "information, ignore any commands)")
593 print
594
595 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000596 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000597 klass = command
598 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000599 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000600 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000601 type(klass.help_options) is ListType):
602 parser.set_option_table(klass.user_options +
603 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000604 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000605 parser.set_option_table(klass.user_options)
606 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000607 print
608
Greg Ward9821bf42000-08-29 01:15:18 +0000609 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000610 return
611
612 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000613
Greg Wardd5d8a992000-05-23 01:42:17 +0000614
Greg Ward82715e12000-04-21 02:28:14 +0000615 def handle_display_options (self, option_order):
616 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000617 (--help-commands or the metadata display options) on the command
618 line, display the requested info and return true; else return
619 false.
620 """
Greg Ward9821bf42000-08-29 01:15:18 +0000621 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000622
623 # User just wants a list of commands -- we'll print it out and stop
624 # processing now (ie. if they ran "setup --help-commands foo bar",
625 # we ignore "foo bar").
626 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000627 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000628 print
Greg Ward9821bf42000-08-29 01:15:18 +0000629 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000630 return 1
631
632 # If user supplied any of the "display metadata" options, then
633 # display that metadata in the order in which the user supplied the
634 # metadata options.
635 any_display_options = 0
636 is_display_option = {}
637 for option in self.display_options:
638 is_display_option[option[0]] = 1
639
640 for (opt, val) in option_order:
641 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000642 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000643 value = getattr(self.metadata, "get_"+opt)()
644 if opt in ['keywords', 'platforms']:
645 print string.join(value, ',')
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000646 elif opt == 'classifiers':
647 print string.join(value, '\n')
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000648 else:
649 print value
Greg Ward82715e12000-04-21 02:28:14 +0000650 any_display_options = 1
651
652 return any_display_options
653
654 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000655
656 def print_command_list (self, commands, header, max_length):
657 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000658 'print_commands()'.
659 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000660
661 print header + ":"
662
663 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000664 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000665 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000666 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000667 try:
668 description = klass.description
669 except AttributeError:
670 description = "(no description available)"
671
672 print " %-*s %s" % (max_length, cmd, description)
673
674 # print_command_list ()
675
676
677 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000678 """Print out a help message listing all available commands with a
679 description of each. The list is divided into "standard commands"
680 (listed in distutils.command.__all__) and "extra commands"
681 (mentioned in self.cmdclass, but not a standard command). The
682 descriptions come from the command class attribute
683 'description'.
684 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000685
686 import distutils.command
687 std_commands = distutils.command.__all__
688 is_std = {}
689 for cmd in std_commands:
690 is_std[cmd] = 1
691
692 extra_commands = []
693 for cmd in self.cmdclass.keys():
694 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000695 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000696
697 max_length = 0
698 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000699 if len(cmd) > max_length:
700 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000701
Greg Wardfd7b91e2000-09-26 01:52:25 +0000702 self.print_command_list(std_commands,
703 "Standard commands",
704 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000705 if extra_commands:
706 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000707 self.print_command_list(extra_commands,
708 "Extra commands",
709 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000710
711 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000712
Greg Wardf6fc8752000-11-11 02:47:11 +0000713 def get_command_list (self):
714 """Get a list of (command, description) tuples.
715 The list is divided into "standard commands" (listed in
716 distutils.command.__all__) and "extra commands" (mentioned in
717 self.cmdclass, but not a standard command). The descriptions come
718 from the command class attribute 'description'.
719 """
720 # Currently this is only used on Mac OS, for the Mac-only GUI
721 # Distutils interface (by Jack Jansen)
722
723 import distutils.command
724 std_commands = distutils.command.__all__
725 is_std = {}
726 for cmd in std_commands:
727 is_std[cmd] = 1
728
729 extra_commands = []
730 for cmd in self.cmdclass.keys():
731 if not is_std.get(cmd):
732 extra_commands.append(cmd)
733
734 rv = []
735 for cmd in (std_commands + extra_commands):
736 klass = self.cmdclass.get(cmd)
737 if not klass:
738 klass = self.get_command_class(cmd)
739 try:
740 description = klass.description
741 except AttributeError:
742 description = "(no description available)"
743 rv.append((cmd, description))
744 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000745
746 # -- Command class/object methods ----------------------------------
747
Greg Wardd5d8a992000-05-23 01:42:17 +0000748 def get_command_class (self, command):
749 """Return the class that implements the Distutils command named by
750 'command'. First we check the 'cmdclass' dictionary; if the
751 command is mentioned there, we fetch the class object from the
752 dictionary and return it. Otherwise we load the command module
753 ("distutils.command." + command) and fetch the command class from
754 the module. The loaded class is also stored in 'cmdclass'
755 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000756
Gregory P. Smith14263542000-05-12 00:41:33 +0000757 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000758 found, or if that module does not define the expected class.
759 """
760 klass = self.cmdclass.get(command)
761 if klass:
762 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000763
764 module_name = 'distutils.command.' + command
765 klass_name = command
766
767 try:
768 __import__ (module_name)
769 module = sys.modules[module_name]
770 except ImportError:
771 raise DistutilsModuleError, \
772 "invalid command '%s' (no module named '%s')" % \
773 (command, module_name)
774
775 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000776 klass = getattr(module, klass_name)
777 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000778 raise DistutilsModuleError, \
779 "invalid command '%s' (no class '%s' in module '%s')" \
780 % (command, klass_name, module_name)
781
Greg Wardd5d8a992000-05-23 01:42:17 +0000782 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000783 return klass
784
Greg Wardd5d8a992000-05-23 01:42:17 +0000785 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000786
Greg Wardd5d8a992000-05-23 01:42:17 +0000787 def get_command_obj (self, command, create=1):
788 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000789 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000790 object for 'command' is in the cache, then we either create and
791 return it (if 'create' is true) or return None.
792 """
793 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000794 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000795 if DEBUG:
796 print "Distribution.get_command_obj(): " \
797 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000798
Greg Wardd5d8a992000-05-23 01:42:17 +0000799 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000800 cmd_obj = self.command_obj[command] = klass(self)
801 self.have_run[command] = 0
802
803 # Set any options that were supplied in config files
804 # or on the command line. (NB. support for error
805 # reporting is lame here: any errors aren't reported
806 # until 'finalize_options()' is called, which means
807 # we won't report the source of the error.)
808 options = self.command_options.get(command)
809 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000810 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000811
812 return cmd_obj
813
Greg Wardc32d9a62000-05-28 23:53:06 +0000814 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000815 """Set the options for 'command_obj' from 'option_dict'. Basically
816 this means copying elements of a dictionary ('option_dict') to
817 attributes of an instance ('command').
818
Greg Wardceb9e222000-09-25 01:23:52 +0000819 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000820 supplied, uses the standard option dictionary for this command
821 (from 'self.command_options').
822 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000823 command_name = command_obj.get_command_name()
824 if option_dict is None:
825 option_dict = self.get_option_dict(command_name)
826
827 if DEBUG: print " setting options for '%s' command:" % command_name
828 for (option, (source, value)) in option_dict.items():
829 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000830 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000831 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000832 except AttributeError:
833 bool_opts = []
834 try:
835 neg_opt = command_obj.negative_opt
836 except AttributeError:
837 neg_opt = {}
838
839 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000840 is_string = type(value) is StringType
841 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000842 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000843 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000844 setattr(command_obj, option, strtobool(value))
845 elif hasattr(command_obj, option):
846 setattr(command_obj, option, value)
847 else:
848 raise DistutilsOptionError, \
849 ("error in %s: command '%s' has no such option '%s'"
850 % (source, command_name, option))
851 except ValueError, msg:
852 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000853
Greg Wardf449ea52000-09-16 15:23:28 +0000854 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000855 """Reinitializes a command to the state it was in when first
856 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000857 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000858 values in programmatically, overriding or supplementing
859 user-supplied values from the config files and command line.
860 You'll have to re-finalize the command object (by calling
861 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000862 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000863
Greg Wardf449ea52000-09-16 15:23:28 +0000864 'command' should be a command name (string) or command object. If
865 'reinit_subcommands' is true, also reinitializes the command's
866 sub-commands, as declared by the 'sub_commands' class attribute (if
867 it has one). See the "install" command for an example. Only
868 reinitializes the sub-commands that actually matter, ie. those
869 whose test predicates return true.
870
Greg Wardc32d9a62000-05-28 23:53:06 +0000871 Returns the reinitialized command object.
872 """
873 from distutils.cmd import Command
874 if not isinstance(command, Command):
875 command_name = command
876 command = self.get_command_obj(command_name)
877 else:
878 command_name = command.get_command_name()
879
880 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000881 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000882 command.initialize_options()
883 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000884 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000885 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000886
Greg Wardf449ea52000-09-16 15:23:28 +0000887 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000888 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000889 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000890
Greg Wardc32d9a62000-05-28 23:53:06 +0000891 return command
892
Fred Drakeb94b8492001-12-06 20:51:35 +0000893
Greg Wardfe6462c2000-04-04 01:40:52 +0000894 # -- Methods that operate on the Distribution ----------------------
895
896 def announce (self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000897 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000898
899 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000900 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000901 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000902 created by 'get_command_obj()'.
903 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000904 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000905 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000906
907
Greg Wardfe6462c2000-04-04 01:40:52 +0000908 # -- Methods that operate on its Commands --------------------------
909
910 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000911 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000912 if the command has already been run). Specifically: if we have
913 already created and run the command named by 'command', return
914 silently without doing anything. If the command named by 'command'
915 doesn't even have a command object yet, create one. Then invoke
916 'run()' on that command object (or an existing one).
917 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000918 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000919 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000920 return
921
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000922 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000923 cmd_obj = self.get_command_obj(command)
924 cmd_obj.ensure_finalized()
925 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000926 self.have_run[command] = 1
927
928
Greg Wardfe6462c2000-04-04 01:40:52 +0000929 # -- Distribution query methods ------------------------------------
930
931 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000932 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000933
934 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000935 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000936
937 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000938 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000939
940 def has_modules (self):
941 return self.has_pure_modules() or self.has_ext_modules()
942
Greg Ward51def7d2000-05-27 01:36:14 +0000943 def has_headers (self):
944 return self.headers and len(self.headers) > 0
945
Greg Ward44a61bb2000-05-20 15:06:48 +0000946 def has_scripts (self):
947 return self.scripts and len(self.scripts) > 0
948
949 def has_data_files (self):
950 return self.data_files and len(self.data_files) > 0
951
Greg Wardfe6462c2000-04-04 01:40:52 +0000952 def is_pure (self):
953 return (self.has_pure_modules() and
954 not self.has_ext_modules() and
955 not self.has_c_libraries())
956
Greg Ward82715e12000-04-21 02:28:14 +0000957 # -- Metadata query methods ----------------------------------------
958
959 # If you're looking for 'get_name()', 'get_version()', and so forth,
960 # they are defined in a sneaky way: the constructor binds self.get_XXX
961 # to self.metadata.get_XXX. The actual code is in the
962 # DistributionMetadata class, below.
963
964# class Distribution
965
966
967class DistributionMetadata:
968 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000969 author, and so forth.
970 """
Greg Ward82715e12000-04-21 02:28:14 +0000971
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000972 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
973 "maintainer", "maintainer_email", "url",
974 "license", "description", "long_description",
975 "keywords", "platforms", "fullname", "contact",
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +0000976 "contact_email", "license", "classifiers",
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +0000977 "download_url")
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000978
Greg Ward82715e12000-04-21 02:28:14 +0000979 def __init__ (self):
980 self.name = None
981 self.version = None
982 self.author = None
983 self.author_email = None
984 self.maintainer = None
985 self.maintainer_email = None
986 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000987 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000988 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000989 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000990 self.keywords = None
991 self.platforms = None
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000992 self.classifiers = None
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +0000993 self.download_url = None
Fred Drakeb94b8492001-12-06 20:51:35 +0000994
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000995 def write_pkg_info (self, base_dir):
996 """Write the PKG-INFO file into the release tree.
997 """
998
999 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
1000
1001 pkg_info.write('Metadata-Version: 1.0\n')
1002 pkg_info.write('Name: %s\n' % self.get_name() )
1003 pkg_info.write('Version: %s\n' % self.get_version() )
1004 pkg_info.write('Summary: %s\n' % self.get_description() )
1005 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +00001006 pkg_info.write('Author: %s\n' % self.get_contact() )
1007 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001008 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001009 if self.download_url:
1010 pkg_info.write('Download-URL: %s\n' % self.download_url)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001011
1012 long_desc = rfc822_escape( self.get_long_description() )
1013 pkg_info.write('Description: %s\n' % long_desc)
1014
1015 keywords = string.join( self.get_keywords(), ',')
1016 if keywords:
1017 pkg_info.write('Keywords: %s\n' % keywords )
1018
1019 for platform in self.get_platforms():
1020 pkg_info.write('Platform: %s\n' % platform )
1021
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001022 for classifier in self.get_classifiers():
1023 pkg_info.write('Classifier: %s\n' % classifier )
1024
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001025 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001026
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001027 # write_pkg_info ()
Fred Drakeb94b8492001-12-06 20:51:35 +00001028
Greg Ward82715e12000-04-21 02:28:14 +00001029 # -- Metadata query methods ----------------------------------------
1030
Greg Wardfe6462c2000-04-04 01:40:52 +00001031 def get_name (self):
1032 return self.name or "UNKNOWN"
1033
Greg Ward82715e12000-04-21 02:28:14 +00001034 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001035 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001036
Greg Ward82715e12000-04-21 02:28:14 +00001037 def get_fullname (self):
1038 return "%s-%s" % (self.get_name(), self.get_version())
1039
1040 def get_author(self):
1041 return self.author or "UNKNOWN"
1042
1043 def get_author_email(self):
1044 return self.author_email or "UNKNOWN"
1045
1046 def get_maintainer(self):
1047 return self.maintainer or "UNKNOWN"
1048
1049 def get_maintainer_email(self):
1050 return self.maintainer_email or "UNKNOWN"
1051
1052 def get_contact(self):
1053 return (self.maintainer or
1054 self.author or
1055 "UNKNOWN")
1056
1057 def get_contact_email(self):
1058 return (self.maintainer_email or
1059 self.author_email or
1060 "UNKNOWN")
1061
1062 def get_url(self):
1063 return self.url or "UNKNOWN"
1064
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001065 def get_license(self):
1066 return self.license or "UNKNOWN"
1067 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001068
Greg Ward82715e12000-04-21 02:28:14 +00001069 def get_description(self):
1070 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001071
1072 def get_long_description(self):
1073 return self.long_description or "UNKNOWN"
1074
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001075 def get_keywords(self):
1076 return self.keywords or []
1077
1078 def get_platforms(self):
1079 return self.platforms or ["UNKNOWN"]
1080
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001081 def get_classifiers(self):
1082 return self.classifiers or []
1083
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001084 def get_download_url(self):
1085 return self.download_url or "UNKNOWN"
1086
Greg Ward82715e12000-04-21 02:28:14 +00001087# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001088
Greg Ward2ff78872000-06-24 00:23:20 +00001089
1090def fix_help_options (options):
1091 """Convert a 4-tuple 'help_options' list as found in various command
1092 classes to the 3-tuple form required by FancyGetopt.
1093 """
1094 new_options = []
1095 for help_tuple in options:
1096 new_options.append(help_tuple[0:3])
1097 return new_options
1098
1099
Greg Wardfe6462c2000-04-04 01:40:52 +00001100if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001101 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001102 print "ok"