blob: f15c945c74468b943771ac99756c0bfdc8a4c4cf [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
208 # Now work on the rest of the attributes. Any attribute that's
209 # not already defined is invalid!
210 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000211 if hasattr(self.metadata, key):
212 setattr(self.metadata, key, val)
213 elif hasattr(self, key):
214 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000215 else:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000216 msg = "Unknown distribution option: %s" % repr(key)
217 if warnings is not None:
218 warnings.warn(msg)
219 else:
220 sys.stderr.write(msg + "\n")
Greg Wardfe6462c2000-04-04 01:40:52 +0000221
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000222 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000223
Greg Wardfe6462c2000-04-04 01:40:52 +0000224 # __init__ ()
225
226
Greg Ward0e48cfd2000-05-26 01:00:15 +0000227 def get_option_dict (self, command):
228 """Get the option dictionary for a given command. If that
229 command's option dictionary hasn't been created yet, then create it
230 and return the new dictionary; otherwise, return the existing
231 option dictionary.
232 """
233
234 dict = self.command_options.get(command)
235 if dict is None:
236 dict = self.command_options[command] = {}
237 return dict
238
239
Greg Wardc32d9a62000-05-28 23:53:06 +0000240 def dump_option_dicts (self, header=None, commands=None, indent=""):
241 from pprint import pformat
242
243 if commands is None: # dump all command option dicts
244 commands = self.command_options.keys()
245 commands.sort()
246
247 if header is not None:
248 print indent + header
249 indent = indent + " "
250
251 if not commands:
252 print indent + "no commands known yet"
253 return
254
255 for cmd_name in commands:
256 opt_dict = self.command_options.get(cmd_name)
257 if opt_dict is None:
258 print indent + "no option dict for '%s' command" % cmd_name
259 else:
260 print indent + "option dict for '%s' command:" % cmd_name
261 out = pformat(opt_dict)
262 for line in string.split(out, "\n"):
263 print indent + " " + line
264
265 # dump_option_dicts ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000266
Greg Wardc32d9a62000-05-28 23:53:06 +0000267
268
Greg Wardd5d8a992000-05-23 01:42:17 +0000269 # -- Config file finding/parsing methods ---------------------------
270
Gregory P. Smith14263542000-05-12 00:41:33 +0000271 def find_config_files (self):
272 """Find as many configuration files as should be processed for this
273 platform, and return a list of filenames in the order in which they
274 should be parsed. The filenames returned are guaranteed to exist
275 (modulo nasty race conditions).
276
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000277 There are three possible config files: distutils.cfg in the
278 Distutils installation directory (ie. where the top-level
279 Distutils __inst__.py file lives), a file in the user's home
280 directory named .pydistutils.cfg on Unix and pydistutils.cfg
281 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000282 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000283 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000284 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000285
Greg Ward11696872000-06-07 02:29:03 +0000286 # Where to look for the system-wide Distutils config file
287 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
288
289 # Look for the system config file
290 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000291 if os.path.isfile(sys_file):
292 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000293
Greg Ward11696872000-06-07 02:29:03 +0000294 # What to call the per-user config file
295 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000296 user_filename = ".pydistutils.cfg"
297 else:
298 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000299
Greg Ward11696872000-06-07 02:29:03 +0000300 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000301 if os.environ.has_key('HOME'):
302 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000303 if os.path.isfile(user_file):
304 files.append(user_file)
305
Gregory P. Smith14263542000-05-12 00:41:33 +0000306 # All platforms support local setup.cfg
307 local_file = "setup.cfg"
308 if os.path.isfile(local_file):
309 files.append(local_file)
310
311 return files
312
313 # find_config_files ()
314
315
316 def parse_config_files (self, filenames=None):
317
318 from ConfigParser import ConfigParser
319
320 if filenames is None:
321 filenames = self.find_config_files()
322
Greg Ward2bd3f422000-06-02 01:59:33 +0000323 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000324
Gregory P. Smith14263542000-05-12 00:41:33 +0000325 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000326 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000327 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000328 parser.read(filename)
329 for section in parser.sections():
330 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000331 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000332
Greg Wardd5d8a992000-05-23 01:42:17 +0000333 for opt in options:
334 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000335 val = parser.get(section,opt)
336 opt = string.replace(opt, '-', '_')
337 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000338
Greg Ward47460772000-05-23 03:47:35 +0000339 # Make the ConfigParser forget everything (so we retain
340 # the original filenames that options come from) -- gag,
341 # retch, puke -- another good reason for a distutils-
342 # specific config parser (sigh...)
343 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000344
Greg Wardceb9e222000-09-25 01:23:52 +0000345 # If there was a "global" section in the config file, use it
346 # to set Distribution options.
347
348 if self.command_options.has_key('global'):
349 for (opt, (src, val)) in self.command_options['global'].items():
350 alias = self.negative_opt.get(opt)
351 try:
352 if alias:
353 setattr(self, alias, not strtobool(val))
354 elif opt in ('verbose', 'dry_run'): # ugh!
355 setattr(self, opt, strtobool(val))
356 except ValueError, msg:
357 raise DistutilsOptionError, msg
358
359 # parse_config_files ()
360
Gregory P. Smith14263542000-05-12 00:41:33 +0000361
Greg Wardd5d8a992000-05-23 01:42:17 +0000362 # -- Command-line parsing methods ----------------------------------
363
Greg Ward9821bf42000-08-29 01:15:18 +0000364 def parse_command_line (self):
365 """Parse the setup script's command line, taken from the
366 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
367 -- see 'setup()' in core.py). This list is first processed for
368 "global options" -- options that set attributes of the Distribution
369 instance. Then, it is alternately scanned for Distutils commands
370 and options for that command. Each new command terminates the
371 options for the previous command. The allowed options for a
372 command are determined by the 'user_options' attribute of the
373 command class -- thus, we have to be able to load command classes
374 in order to parse the command line. Any error in that 'options'
375 attribute raises DistutilsGetoptError; any error on the
376 command-line raises DistutilsArgError. If no Distutils commands
377 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000378 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000379 on with executing commands; false if no errors but we shouldn't
380 execute commands (currently, this only happens if user asks for
381 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000382 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000383 #
Fred Drake981a1782001-08-10 18:59:30 +0000384 # We now have enough information to show the Macintosh dialog
385 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000386 #
387 if sys.platform == 'mac':
388 import EasyDialogs
389 cmdlist = self.get_command_list()
390 self.script_args = EasyDialogs.GetArgv(
391 self.global_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000392
Greg Wardfe6462c2000-04-04 01:40:52 +0000393 # We have to parse the command line a bit at a time -- global
394 # options, then the first command, then its options, and so on --
395 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000396 # the options that are valid for a particular class aren't known
397 # until we have loaded the command class, which doesn't happen
398 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000399
400 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000401 parser = FancyGetopt(self.global_options + self.display_options)
402 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000403 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000404 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000405 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000406 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000407
Greg Ward82715e12000-04-21 02:28:14 +0000408 # for display options we return immediately
409 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000410 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000411
Greg Wardfe6462c2000-04-04 01:40:52 +0000412 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000413 args = self._parse_command_opts(parser, args)
414 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000415 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000416
Greg Wardd5d8a992000-05-23 01:42:17 +0000417 # Handle the cases of --help as a "global" option, ie.
418 # "setup.py --help" and "setup.py --help command ...". For the
419 # former, we show global options (--verbose, --dry-run, etc.)
420 # and display-only options (--name, --version, etc.); for the
421 # latter, we omit the display-only options and show help for
422 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000423 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000424 self._show_help(parser,
425 display_options=len(self.commands) == 0,
426 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000427 return
428
429 # Oops, no commands found -- an end-user error
430 if not self.commands:
431 raise DistutilsArgError, "no commands supplied"
432
433 # All is well: return true
434 return 1
435
436 # parse_command_line()
437
Greg Wardd5d8a992000-05-23 01:42:17 +0000438 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000439 """Parse the command-line options for a single command.
440 'parser' must be a FancyGetopt instance; 'args' must be the list
441 of arguments, starting with the current command (whose options
442 we are about to parse). Returns a new version of 'args' with
443 the next command at the front of the list; will be the empty
444 list if there are no more commands on the command line. Returns
445 None if the user asked for help on this command.
446 """
447 # late import because of mutual dependence between these modules
448 from distutils.cmd import Command
449
450 # Pull the current command from the head of the command line
451 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000452 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000453 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000454 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000455
456 # Dig up the command class that implements this command, so we
457 # 1) know that it's a valid command, and 2) know which options
458 # it takes.
459 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000460 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000461 except DistutilsModuleError, msg:
462 raise DistutilsArgError, msg
463
464 # Require that the command class be derived from Command -- want
465 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000466 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000467 raise DistutilsClassError, \
468 "command class %s must subclass Command" % cmd_class
469
470 # Also make sure that the command object provides a list of its
471 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000472 if not (hasattr(cmd_class, 'user_options') and
473 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000474 raise DistutilsClassError, \
475 ("command class %s must provide " +
476 "'user_options' attribute (a list of tuples)") % \
477 cmd_class
478
479 # If the command class has a list of negative alias options,
480 # merge it in with the global negative aliases.
481 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000482 if hasattr(cmd_class, 'negative_opt'):
483 negative_opt = copy(negative_opt)
484 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000485
Greg Wardfa9ff762000-10-14 04:06:40 +0000486 # Check for help_options in command class. They have a different
487 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000488 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000489 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000490 help_options = fix_help_options(cmd_class.help_options)
491 else:
Greg Ward55fced32000-06-24 01:22:41 +0000492 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000493
Greg Ward9d17a7a2000-06-07 03:00:06 +0000494
Greg Wardd5d8a992000-05-23 01:42:17 +0000495 # All commands support the global options too, just by adding
496 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000497 parser.set_option_table(self.global_options +
498 cmd_class.user_options +
499 help_options)
500 parser.set_negative_aliases(negative_opt)
501 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000502 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000503 self._show_help(parser, display_options=0, commands=[cmd_class])
504 return
505
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000506 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000507 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000508 help_option_found=0
509 for (help_option, short, desc, func) in cmd_class.help_options:
510 if hasattr(opts, parser.get_attr_name(help_option)):
511 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000512 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000513 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000514
515 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000516 func()
Greg Ward55fced32000-06-24 01:22:41 +0000517 else:
Fred Drake981a1782001-08-10 18:59:30 +0000518 raise DistutilsClassError(
519 "invalid help function %s for help option '%s': "
520 "must be a callable object (function, etc.)"
521 % (`func`, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000522
Fred Drakeb94b8492001-12-06 20:51:35 +0000523 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000524 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000525
Greg Wardd5d8a992000-05-23 01:42:17 +0000526 # Put the options from the command-line into their official
527 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000528 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000529 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000530 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000531
532 return args
533
534 # _parse_command_opts ()
535
536
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000537 def finalize_options (self):
538 """Set final values for all the options on the Distribution
539 instance, analogous to the .finalize_options() method of Command
540 objects.
541 """
542
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000543 keywords = self.metadata.keywords
544 if keywords is not None:
545 if type(keywords) is StringType:
546 keywordlist = string.split(keywords, ',')
547 self.metadata.keywords = map(string.strip, keywordlist)
548
549 platforms = self.metadata.platforms
550 if platforms is not None:
551 if type(platforms) is StringType:
552 platformlist = string.split(platforms, ',')
553 self.metadata.platforms = map(string.strip, platformlist)
554
Greg Wardd5d8a992000-05-23 01:42:17 +0000555 def _show_help (self,
556 parser,
557 global_options=1,
558 display_options=1,
559 commands=[]):
560 """Show help for the setup script command-line in the form of
561 several lists of command-line options. 'parser' should be a
562 FancyGetopt instance; do not expect it to be returned in the
563 same state, as its option table will be reset to make it
564 generate the correct help text.
565
566 If 'global_options' is true, lists the global options:
567 --verbose, --dry-run, etc. If 'display_options' is true, lists
568 the "display-only" options: --name, --version, etc. Finally,
569 lists per-command help for every command name or command class
570 in 'commands'.
571 """
572 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000573 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000574 from distutils.cmd import Command
575
576 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000577 parser.set_option_table(self.global_options)
578 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000579 print
580
581 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000582 parser.set_option_table(self.display_options)
583 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000584 "Information display options (just display " +
585 "information, ignore any commands)")
586 print
587
588 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000589 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000590 klass = command
591 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000592 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000593 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000594 type(klass.help_options) is ListType):
595 parser.set_option_table(klass.user_options +
596 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000597 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000598 parser.set_option_table(klass.user_options)
599 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000600 print
601
Greg Ward9821bf42000-08-29 01:15:18 +0000602 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000603 return
604
605 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000606
Greg Wardd5d8a992000-05-23 01:42:17 +0000607
Greg Ward82715e12000-04-21 02:28:14 +0000608 def handle_display_options (self, option_order):
609 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000610 (--help-commands or the metadata display options) on the command
611 line, display the requested info and return true; else return
612 false.
613 """
Greg Ward9821bf42000-08-29 01:15:18 +0000614 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000615
616 # User just wants a list of commands -- we'll print it out and stop
617 # processing now (ie. if they ran "setup --help-commands foo bar",
618 # we ignore "foo bar").
619 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000620 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000621 print
Greg Ward9821bf42000-08-29 01:15:18 +0000622 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000623 return 1
624
625 # If user supplied any of the "display metadata" options, then
626 # display that metadata in the order in which the user supplied the
627 # metadata options.
628 any_display_options = 0
629 is_display_option = {}
630 for option in self.display_options:
631 is_display_option[option[0]] = 1
632
633 for (opt, val) in option_order:
634 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000635 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000636 value = getattr(self.metadata, "get_"+opt)()
637 if opt in ['keywords', 'platforms']:
638 print string.join(value, ',')
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000639 elif opt == 'classifiers':
640 print string.join(value, '\n')
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000641 else:
642 print value
Greg Ward82715e12000-04-21 02:28:14 +0000643 any_display_options = 1
644
645 return any_display_options
646
647 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000648
649 def print_command_list (self, commands, header, max_length):
650 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000651 'print_commands()'.
652 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000653
654 print header + ":"
655
656 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000657 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000658 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000659 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000660 try:
661 description = klass.description
662 except AttributeError:
663 description = "(no description available)"
664
665 print " %-*s %s" % (max_length, cmd, description)
666
667 # print_command_list ()
668
669
670 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000671 """Print out a help message listing all available commands with a
672 description of each. The list is divided into "standard commands"
673 (listed in distutils.command.__all__) and "extra commands"
674 (mentioned in self.cmdclass, but not a standard command). The
675 descriptions come from the command class attribute
676 'description'.
677 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000678
679 import distutils.command
680 std_commands = distutils.command.__all__
681 is_std = {}
682 for cmd in std_commands:
683 is_std[cmd] = 1
684
685 extra_commands = []
686 for cmd in self.cmdclass.keys():
687 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000688 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000689
690 max_length = 0
691 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000692 if len(cmd) > max_length:
693 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000694
Greg Wardfd7b91e2000-09-26 01:52:25 +0000695 self.print_command_list(std_commands,
696 "Standard commands",
697 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000698 if extra_commands:
699 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000700 self.print_command_list(extra_commands,
701 "Extra commands",
702 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000703
704 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000705
Greg Wardf6fc8752000-11-11 02:47:11 +0000706 def get_command_list (self):
707 """Get a list of (command, description) tuples.
708 The list is divided into "standard commands" (listed in
709 distutils.command.__all__) and "extra commands" (mentioned in
710 self.cmdclass, but not a standard command). The descriptions come
711 from the command class attribute 'description'.
712 """
713 # Currently this is only used on Mac OS, for the Mac-only GUI
714 # Distutils interface (by Jack Jansen)
715
716 import distutils.command
717 std_commands = distutils.command.__all__
718 is_std = {}
719 for cmd in std_commands:
720 is_std[cmd] = 1
721
722 extra_commands = []
723 for cmd in self.cmdclass.keys():
724 if not is_std.get(cmd):
725 extra_commands.append(cmd)
726
727 rv = []
728 for cmd in (std_commands + extra_commands):
729 klass = self.cmdclass.get(cmd)
730 if not klass:
731 klass = self.get_command_class(cmd)
732 try:
733 description = klass.description
734 except AttributeError:
735 description = "(no description available)"
736 rv.append((cmd, description))
737 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000738
739 # -- Command class/object methods ----------------------------------
740
Greg Wardd5d8a992000-05-23 01:42:17 +0000741 def get_command_class (self, command):
742 """Return the class that implements the Distutils command named by
743 'command'. First we check the 'cmdclass' dictionary; if the
744 command is mentioned there, we fetch the class object from the
745 dictionary and return it. Otherwise we load the command module
746 ("distutils.command." + command) and fetch the command class from
747 the module. The loaded class is also stored in 'cmdclass'
748 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000749
Gregory P. Smith14263542000-05-12 00:41:33 +0000750 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000751 found, or if that module does not define the expected class.
752 """
753 klass = self.cmdclass.get(command)
754 if klass:
755 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000756
757 module_name = 'distutils.command.' + command
758 klass_name = command
759
760 try:
761 __import__ (module_name)
762 module = sys.modules[module_name]
763 except ImportError:
764 raise DistutilsModuleError, \
765 "invalid command '%s' (no module named '%s')" % \
766 (command, module_name)
767
768 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000769 klass = getattr(module, klass_name)
770 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000771 raise DistutilsModuleError, \
772 "invalid command '%s' (no class '%s' in module '%s')" \
773 % (command, klass_name, module_name)
774
Greg Wardd5d8a992000-05-23 01:42:17 +0000775 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000776 return klass
777
Greg Wardd5d8a992000-05-23 01:42:17 +0000778 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000779
Greg Wardd5d8a992000-05-23 01:42:17 +0000780 def get_command_obj (self, command, create=1):
781 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000782 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000783 object for 'command' is in the cache, then we either create and
784 return it (if 'create' is true) or return None.
785 """
786 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000787 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000788 if DEBUG:
789 print "Distribution.get_command_obj(): " \
790 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000791
Greg Wardd5d8a992000-05-23 01:42:17 +0000792 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000793 cmd_obj = self.command_obj[command] = klass(self)
794 self.have_run[command] = 0
795
796 # Set any options that were supplied in config files
797 # or on the command line. (NB. support for error
798 # reporting is lame here: any errors aren't reported
799 # until 'finalize_options()' is called, which means
800 # we won't report the source of the error.)
801 options = self.command_options.get(command)
802 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000803 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000804
805 return cmd_obj
806
Greg Wardc32d9a62000-05-28 23:53:06 +0000807 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000808 """Set the options for 'command_obj' from 'option_dict'. Basically
809 this means copying elements of a dictionary ('option_dict') to
810 attributes of an instance ('command').
811
Greg Wardceb9e222000-09-25 01:23:52 +0000812 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000813 supplied, uses the standard option dictionary for this command
814 (from 'self.command_options').
815 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000816 command_name = command_obj.get_command_name()
817 if option_dict is None:
818 option_dict = self.get_option_dict(command_name)
819
820 if DEBUG: print " setting options for '%s' command:" % command_name
821 for (option, (source, value)) in option_dict.items():
822 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000823 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000824 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000825 except AttributeError:
826 bool_opts = []
827 try:
828 neg_opt = command_obj.negative_opt
829 except AttributeError:
830 neg_opt = {}
831
832 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000833 is_string = type(value) is StringType
834 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000835 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000836 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000837 setattr(command_obj, option, strtobool(value))
838 elif hasattr(command_obj, option):
839 setattr(command_obj, option, value)
840 else:
841 raise DistutilsOptionError, \
842 ("error in %s: command '%s' has no such option '%s'"
843 % (source, command_name, option))
844 except ValueError, msg:
845 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000846
Greg Wardf449ea52000-09-16 15:23:28 +0000847 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000848 """Reinitializes a command to the state it was in when first
849 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000850 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000851 values in programmatically, overriding or supplementing
852 user-supplied values from the config files and command line.
853 You'll have to re-finalize the command object (by calling
854 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000855 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000856
Greg Wardf449ea52000-09-16 15:23:28 +0000857 'command' should be a command name (string) or command object. If
858 'reinit_subcommands' is true, also reinitializes the command's
859 sub-commands, as declared by the 'sub_commands' class attribute (if
860 it has one). See the "install" command for an example. Only
861 reinitializes the sub-commands that actually matter, ie. those
862 whose test predicates return true.
863
Greg Wardc32d9a62000-05-28 23:53:06 +0000864 Returns the reinitialized command object.
865 """
866 from distutils.cmd import Command
867 if not isinstance(command, Command):
868 command_name = command
869 command = self.get_command_obj(command_name)
870 else:
871 command_name = command.get_command_name()
872
873 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000874 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000875 command.initialize_options()
876 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000877 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000878 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000879
Greg Wardf449ea52000-09-16 15:23:28 +0000880 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000881 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000882 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000883
Greg Wardc32d9a62000-05-28 23:53:06 +0000884 return command
885
Fred Drakeb94b8492001-12-06 20:51:35 +0000886
Greg Wardfe6462c2000-04-04 01:40:52 +0000887 # -- Methods that operate on the Distribution ----------------------
888
889 def announce (self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000890 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000891
892 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000893 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000894 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000895 created by 'get_command_obj()'.
896 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000897 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000898 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000899
900
Greg Wardfe6462c2000-04-04 01:40:52 +0000901 # -- Methods that operate on its Commands --------------------------
902
903 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000904 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000905 if the command has already been run). Specifically: if we have
906 already created and run the command named by 'command', return
907 silently without doing anything. If the command named by 'command'
908 doesn't even have a command object yet, create one. Then invoke
909 'run()' on that command object (or an existing one).
910 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000911 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000912 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000913 return
914
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000915 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000916 cmd_obj = self.get_command_obj(command)
917 cmd_obj.ensure_finalized()
918 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000919 self.have_run[command] = 1
920
921
Greg Wardfe6462c2000-04-04 01:40:52 +0000922 # -- Distribution query methods ------------------------------------
923
924 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000925 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000926
927 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000928 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000929
930 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000931 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000932
933 def has_modules (self):
934 return self.has_pure_modules() or self.has_ext_modules()
935
Greg Ward51def7d2000-05-27 01:36:14 +0000936 def has_headers (self):
937 return self.headers and len(self.headers) > 0
938
Greg Ward44a61bb2000-05-20 15:06:48 +0000939 def has_scripts (self):
940 return self.scripts and len(self.scripts) > 0
941
942 def has_data_files (self):
943 return self.data_files and len(self.data_files) > 0
944
Greg Wardfe6462c2000-04-04 01:40:52 +0000945 def is_pure (self):
946 return (self.has_pure_modules() and
947 not self.has_ext_modules() and
948 not self.has_c_libraries())
949
Greg Ward82715e12000-04-21 02:28:14 +0000950 # -- Metadata query methods ----------------------------------------
951
952 # If you're looking for 'get_name()', 'get_version()', and so forth,
953 # they are defined in a sneaky way: the constructor binds self.get_XXX
954 # to self.metadata.get_XXX. The actual code is in the
955 # DistributionMetadata class, below.
956
957# class Distribution
958
959
960class DistributionMetadata:
961 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000962 author, and so forth.
963 """
Greg Ward82715e12000-04-21 02:28:14 +0000964
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000965 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
966 "maintainer", "maintainer_email", "url",
967 "license", "description", "long_description",
968 "keywords", "platforms", "fullname", "contact",
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000969 "contact_email", "licence", "classifiers")
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000970
Greg Ward82715e12000-04-21 02:28:14 +0000971 def __init__ (self):
972 self.name = None
973 self.version = None
974 self.author = None
975 self.author_email = None
976 self.maintainer = None
977 self.maintainer_email = None
978 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000979 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000980 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000981 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000982 self.keywords = None
983 self.platforms = None
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000984 self.classifiers = None
Fred Drakeb94b8492001-12-06 20:51:35 +0000985
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000986 def write_pkg_info (self, base_dir):
987 """Write the PKG-INFO file into the release tree.
988 """
989
990 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
991
992 pkg_info.write('Metadata-Version: 1.0\n')
993 pkg_info.write('Name: %s\n' % self.get_name() )
994 pkg_info.write('Version: %s\n' % self.get_version() )
995 pkg_info.write('Summary: %s\n' % self.get_description() )
996 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +0000997 pkg_info.write('Author: %s\n' % self.get_contact() )
998 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000999 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001000
1001 long_desc = rfc822_escape( self.get_long_description() )
1002 pkg_info.write('Description: %s\n' % long_desc)
1003
1004 keywords = string.join( self.get_keywords(), ',')
1005 if keywords:
1006 pkg_info.write('Keywords: %s\n' % keywords )
1007
1008 for platform in self.get_platforms():
1009 pkg_info.write('Platform: %s\n' % platform )
1010
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001011 for classifier in self.get_classifiers():
1012 pkg_info.write('Classifier: %s\n' % classifier )
1013
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001014 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001015
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001016 # write_pkg_info ()
Fred Drakeb94b8492001-12-06 20:51:35 +00001017
Greg Ward82715e12000-04-21 02:28:14 +00001018 # -- Metadata query methods ----------------------------------------
1019
Greg Wardfe6462c2000-04-04 01:40:52 +00001020 def get_name (self):
1021 return self.name or "UNKNOWN"
1022
Greg Ward82715e12000-04-21 02:28:14 +00001023 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001024 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001025
Greg Ward82715e12000-04-21 02:28:14 +00001026 def get_fullname (self):
1027 return "%s-%s" % (self.get_name(), self.get_version())
1028
1029 def get_author(self):
1030 return self.author or "UNKNOWN"
1031
1032 def get_author_email(self):
1033 return self.author_email or "UNKNOWN"
1034
1035 def get_maintainer(self):
1036 return self.maintainer or "UNKNOWN"
1037
1038 def get_maintainer_email(self):
1039 return self.maintainer_email or "UNKNOWN"
1040
1041 def get_contact(self):
1042 return (self.maintainer or
1043 self.author or
1044 "UNKNOWN")
1045
1046 def get_contact_email(self):
1047 return (self.maintainer_email or
1048 self.author_email or
1049 "UNKNOWN")
1050
1051 def get_url(self):
1052 return self.url or "UNKNOWN"
1053
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001054 def get_license(self):
1055 return self.license or "UNKNOWN"
1056 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001057
Greg Ward82715e12000-04-21 02:28:14 +00001058 def get_description(self):
1059 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001060
1061 def get_long_description(self):
1062 return self.long_description or "UNKNOWN"
1063
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001064 def get_keywords(self):
1065 return self.keywords or []
1066
1067 def get_platforms(self):
1068 return self.platforms or ["UNKNOWN"]
1069
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001070 def get_classifiers(self):
1071 return self.classifiers or []
1072
Greg Ward82715e12000-04-21 02:28:14 +00001073# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001074
Greg Ward2ff78872000-06-24 00:23:20 +00001075
1076def fix_help_options (options):
1077 """Convert a 4-tuple 'help_options' list as found in various command
1078 classes to the 3-tuple form required by FancyGetopt.
1079 """
1080 new_options = []
1081 for help_tuple in options:
1082 new_options.append(help_tuple[0:3])
1083 return new_options
1084
1085
Greg Wardfe6462c2000-04-04 01:40:52 +00001086if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001087 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001088 print "ok"