blob: a84004f4c704335adb60fad3628583d9beb24a72 [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
7# created 2000/04/03, Greg Ward
8# (extricated from core.py; actually dates back to the beginning)
9
10__revision__ = "$Id$"
11
Gregory P. Smith14263542000-05-12 00:41:33 +000012import sys, os, string, re
Greg Wardfe6462c2000-04-04 01:40:52 +000013from types import *
14from copy import copy
15from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000016from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000017from distutils.util import check_environ, strtobool, rfc822_escape
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000018from distutils import log
Greg Wardfe6462c2000-04-04 01:40:52 +000019
20# Regex to define acceptable Distutils command names. This is not *quite*
21# the same as a Python NAME -- I don't allow leading underscores. The fact
22# that they're very similar is no coincidence; the default naming scheme is
23# to look for a Python module named after the command.
24command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
25
26
27class Distribution:
Greg Ward8ff5a3f2000-06-02 00:44:53 +000028 """The core of the Distutils. Most of the work hiding behind 'setup'
29 is really done within a Distribution instance, which farms the work out
30 to the Distutils commands specified on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +000031
Greg Ward8ff5a3f2000-06-02 00:44:53 +000032 Setup scripts will almost never instantiate Distribution directly,
33 unless the 'setup()' function is totally inadequate to their needs.
34 However, it is conceivable that a setup script might wish to subclass
35 Distribution for some specialized purpose, and then pass the subclass
36 to 'setup()' as the 'distclass' keyword argument. If so, it is
37 necessary to respect the expectations that 'setup' has of Distribution.
38 See the code for 'setup()', in core.py, for details.
39 """
Greg Wardfe6462c2000-04-04 01:40:52 +000040
41
42 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000043 # supplied to the setup script prior to any actual commands.
44 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000045 # these global options. This list should be kept to a bare minimum,
46 # since every global option is also valid as a command option -- and we
47 # don't want to pollute the commands with too many options that they
48 # have minimal control over.
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000049 # The fourth entry for verbose means that it can be repeated.
50 global_options = [('verbose', 'v', "run verbosely (default)", 1),
Greg Wardd5d8a992000-05-23 01:42:17 +000051 ('quiet', 'q', "run quietly (turns verbosity off)"),
52 ('dry-run', 'n', "don't actually do anything"),
53 ('help', 'h', "show detailed help message"),
Greg Wardfe6462c2000-04-04 01:40:52 +000054 ]
Greg Ward82715e12000-04-21 02:28:14 +000055
56 # options that are not propagated to the commands
57 display_options = [
58 ('help-commands', None,
59 "list all available commands"),
60 ('name', None,
61 "print package name"),
62 ('version', 'V',
63 "print package version"),
64 ('fullname', None,
65 "print <package name>-<version>"),
66 ('author', None,
67 "print the author's name"),
68 ('author-email', None,
69 "print the author's email address"),
70 ('maintainer', None,
71 "print the maintainer's name"),
72 ('maintainer-email', None,
73 "print the maintainer's email address"),
74 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000075 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000076 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000077 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000078 ('url', None,
79 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +000080 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +000081 "print the license of the package"),
82 ('licence', None,
83 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +000084 ('description', None,
85 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +000086 ('long-description', None,
87 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000088 ('platforms', None,
89 "print the list of platforms"),
90 ('keywords', None,
91 "print the list of keywords"),
Greg Ward82715e12000-04-21 02:28:14 +000092 ]
Greg Ward2f2b6c62000-09-25 01:58:07 +000093 display_option_names = map(lambda x: translate_longopt(x[0]),
94 display_options)
Greg Ward82715e12000-04-21 02:28:14 +000095
96 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +000097 negative_opt = {'quiet': 'verbose'}
98
99
100 # -- Creation/initialization methods -------------------------------
Fred Drakeb94b8492001-12-06 20:51:35 +0000101
Greg Wardfe6462c2000-04-04 01:40:52 +0000102 def __init__ (self, attrs=None):
103 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000104 attributes of a Distribution, and then use 'attrs' (a dictionary
105 mapping attribute names to values) to assign some of those
106 attributes their "real" values. (Any attributes not mentioned in
107 'attrs' will be assigned to some null value: 0, None, an empty list
108 or dictionary, etc.) Most importantly, initialize the
109 'command_obj' attribute to the empty dictionary; this will be
110 filled in with real command objects by 'parse_command_line()'.
111 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000112
113 # Default values for our command-line options
114 self.verbose = 1
115 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000116 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000117 for attr in self.display_option_names:
118 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000119
Greg Ward82715e12000-04-21 02:28:14 +0000120 # Store the distribution meta-data (name, version, author, and so
121 # forth) in a separate object -- we're getting to have enough
122 # information here (and enough command-line options) that it's
123 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
124 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000125 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000126 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000127 method_name = "get_" + basename
128 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000129
130 # 'cmdclass' maps command names to class objects, so we
131 # can 1) quickly figure out which class to instantiate when
132 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000133 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000134 self.cmdclass = {}
135
Greg Ward9821bf42000-08-29 01:15:18 +0000136 # 'script_name' and 'script_args' are usually set to sys.argv[0]
137 # and sys.argv[1:], but they can be overridden when the caller is
138 # not necessarily a setup script run from the command-line.
139 self.script_name = None
140 self.script_args = None
141
Greg Wardd5d8a992000-05-23 01:42:17 +0000142 # 'command_options' is where we store command options between
143 # parsing them (from config files, the command-line, etc.) and when
144 # they are actually needed -- ie. when the command in question is
145 # instantiated. It is a dictionary of dictionaries of 2-tuples:
146 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000147 self.command_options = {}
148
Greg Wardfe6462c2000-04-04 01:40:52 +0000149 # These options are really the business of various commands, rather
150 # than of the Distribution itself. We provide aliases for them in
151 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000152 self.packages = None
153 self.package_dir = None
154 self.py_modules = None
155 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000156 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000157 self.ext_modules = None
158 self.ext_package = None
159 self.include_dirs = None
160 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000161 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000162 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000163
164 # And now initialize bookkeeping stuff that can't be supplied by
165 # the caller at all. 'command_obj' maps command names to
166 # Command instances -- that's how we enforce that every command
167 # class is a singleton.
168 self.command_obj = {}
169
170 # 'have_run' maps command names to boolean values; it keeps track
171 # of whether we have actually run a particular command, to make it
172 # cheap to "run" a command whenever we think we might need to -- if
173 # it's already been done, no need for expensive filesystem
174 # operations, we just check the 'have_run' dictionary and carry on.
175 # It's only safe to query 'have_run' for a command class that has
176 # been instantiated -- a false value will be inserted when the
177 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000178 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000179 # '.get()' rather than a straight lookup.
180 self.have_run = {}
181
182 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000183 # the setup script) to possibly override any or all of these
184 # distribution options.
185
Greg Wardfe6462c2000-04-04 01:40:52 +0000186 if attrs:
187
188 # Pull out the set of command options and work on them
189 # specifically. Note that this order guarantees that aliased
190 # command options will override any supplied redundantly
191 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000192 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000193 if options:
194 del attrs['options']
195 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000196 opt_dict = self.get_option_dict(command)
197 for (opt, val) in cmd_options.items():
198 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000199
200 # Now work on the rest of the attributes. Any attribute that's
201 # not already defined is invalid!
202 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000203 if hasattr(self.metadata, key):
204 setattr(self.metadata, key, val)
205 elif hasattr(self, key):
206 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000207 else:
Greg Ward02a1a2b2000-04-15 22:15:07 +0000208 raise DistutilsSetupError, \
Greg Wardfe6462c2000-04-04 01:40:52 +0000209 "invalid distribution option '%s'" % key
210
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000211 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000212
Greg Wardfe6462c2000-04-04 01:40:52 +0000213 # __init__ ()
214
215
Greg Ward0e48cfd2000-05-26 01:00:15 +0000216 def get_option_dict (self, command):
217 """Get the option dictionary for a given command. If that
218 command's option dictionary hasn't been created yet, then create it
219 and return the new dictionary; otherwise, return the existing
220 option dictionary.
221 """
222
223 dict = self.command_options.get(command)
224 if dict is None:
225 dict = self.command_options[command] = {}
226 return dict
227
228
Greg Wardc32d9a62000-05-28 23:53:06 +0000229 def dump_option_dicts (self, header=None, commands=None, indent=""):
230 from pprint import pformat
231
232 if commands is None: # dump all command option dicts
233 commands = self.command_options.keys()
234 commands.sort()
235
236 if header is not None:
237 print indent + header
238 indent = indent + " "
239
240 if not commands:
241 print indent + "no commands known yet"
242 return
243
244 for cmd_name in commands:
245 opt_dict = self.command_options.get(cmd_name)
246 if opt_dict is None:
247 print indent + "no option dict for '%s' command" % cmd_name
248 else:
249 print indent + "option dict for '%s' command:" % cmd_name
250 out = pformat(opt_dict)
251 for line in string.split(out, "\n"):
252 print indent + " " + line
253
254 # dump_option_dicts ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000255
Greg Wardc32d9a62000-05-28 23:53:06 +0000256
257
Greg Wardd5d8a992000-05-23 01:42:17 +0000258 # -- Config file finding/parsing methods ---------------------------
259
Gregory P. Smith14263542000-05-12 00:41:33 +0000260 def find_config_files (self):
261 """Find as many configuration files as should be processed for this
262 platform, and return a list of filenames in the order in which they
263 should be parsed. The filenames returned are guaranteed to exist
264 (modulo nasty race conditions).
265
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000266 There are three possible config files: distutils.cfg in the
267 Distutils installation directory (ie. where the top-level
268 Distutils __inst__.py file lives), a file in the user's home
269 directory named .pydistutils.cfg on Unix and pydistutils.cfg
270 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000271 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000272 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000273 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000274
Greg Ward11696872000-06-07 02:29:03 +0000275 # Where to look for the system-wide Distutils config file
276 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
277
278 # Look for the system config file
279 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000280 if os.path.isfile(sys_file):
281 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000282
Greg Ward11696872000-06-07 02:29:03 +0000283 # What to call the per-user config file
284 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000285 user_filename = ".pydistutils.cfg"
286 else:
287 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000288
Greg Ward11696872000-06-07 02:29:03 +0000289 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000290 if os.environ.has_key('HOME'):
291 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000292 if os.path.isfile(user_file):
293 files.append(user_file)
294
Gregory P. Smith14263542000-05-12 00:41:33 +0000295 # All platforms support local setup.cfg
296 local_file = "setup.cfg"
297 if os.path.isfile(local_file):
298 files.append(local_file)
299
300 return files
301
302 # find_config_files ()
303
304
305 def parse_config_files (self, filenames=None):
306
307 from ConfigParser import ConfigParser
Greg Ward2bd3f422000-06-02 01:59:33 +0000308 from distutils.core import DEBUG
Gregory P. Smith14263542000-05-12 00:41:33 +0000309
310 if filenames is None:
311 filenames = self.find_config_files()
312
Greg Ward2bd3f422000-06-02 01:59:33 +0000313 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000314
Gregory P. Smith14263542000-05-12 00:41:33 +0000315 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000316 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000317 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000318 parser.read(filename)
319 for section in parser.sections():
320 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000321 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000322
Greg Wardd5d8a992000-05-23 01:42:17 +0000323 for opt in options:
324 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000325 val = parser.get(section,opt)
326 opt = string.replace(opt, '-', '_')
327 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000328
Greg Ward47460772000-05-23 03:47:35 +0000329 # Make the ConfigParser forget everything (so we retain
330 # the original filenames that options come from) -- gag,
331 # retch, puke -- another good reason for a distutils-
332 # specific config parser (sigh...)
333 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000334
Greg Wardceb9e222000-09-25 01:23:52 +0000335 # If there was a "global" section in the config file, use it
336 # to set Distribution options.
337
338 if self.command_options.has_key('global'):
339 for (opt, (src, val)) in self.command_options['global'].items():
340 alias = self.negative_opt.get(opt)
341 try:
342 if alias:
343 setattr(self, alias, not strtobool(val))
344 elif opt in ('verbose', 'dry_run'): # ugh!
345 setattr(self, opt, strtobool(val))
346 except ValueError, msg:
347 raise DistutilsOptionError, msg
348
349 # parse_config_files ()
350
Gregory P. Smith14263542000-05-12 00:41:33 +0000351
Greg Wardd5d8a992000-05-23 01:42:17 +0000352 # -- Command-line parsing methods ----------------------------------
353
Greg Ward9821bf42000-08-29 01:15:18 +0000354 def parse_command_line (self):
355 """Parse the setup script's command line, taken from the
356 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
357 -- see 'setup()' in core.py). This list is first processed for
358 "global options" -- options that set attributes of the Distribution
359 instance. Then, it is alternately scanned for Distutils commands
360 and options for that command. Each new command terminates the
361 options for the previous command. The allowed options for a
362 command are determined by the 'user_options' attribute of the
363 command class -- thus, we have to be able to load command classes
364 in order to parse the command line. Any error in that 'options'
365 attribute raises DistutilsGetoptError; any error on the
366 command-line raises DistutilsArgError. If no Distutils commands
367 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000368 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000369 on with executing commands; false if no errors but we shouldn't
370 execute commands (currently, this only happens if user asks for
371 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000372 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000373 #
Fred Drake981a1782001-08-10 18:59:30 +0000374 # We now have enough information to show the Macintosh dialog
375 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000376 #
377 if sys.platform == 'mac':
378 import EasyDialogs
379 cmdlist = self.get_command_list()
380 self.script_args = EasyDialogs.GetArgv(
381 self.global_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000382
Greg Wardfe6462c2000-04-04 01:40:52 +0000383 # We have to parse the command line a bit at a time -- global
384 # options, then the first command, then its options, and so on --
385 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000386 # the options that are valid for a particular class aren't known
387 # until we have loaded the command class, which doesn't happen
388 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000389
390 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000391 parser = FancyGetopt(self.global_options + self.display_options)
392 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000393 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000394 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000395 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000396 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000397
Greg Ward82715e12000-04-21 02:28:14 +0000398 # for display options we return immediately
399 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000400 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000401
Greg Wardfe6462c2000-04-04 01:40:52 +0000402 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000403 args = self._parse_command_opts(parser, args)
404 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000405 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000406
Greg Wardd5d8a992000-05-23 01:42:17 +0000407 # Handle the cases of --help as a "global" option, ie.
408 # "setup.py --help" and "setup.py --help command ...". For the
409 # former, we show global options (--verbose, --dry-run, etc.)
410 # and display-only options (--name, --version, etc.); for the
411 # latter, we omit the display-only options and show help for
412 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000413 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000414 self._show_help(parser,
415 display_options=len(self.commands) == 0,
416 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000417 return
418
419 # Oops, no commands found -- an end-user error
420 if not self.commands:
421 raise DistutilsArgError, "no commands supplied"
422
423 # All is well: return true
424 return 1
425
426 # parse_command_line()
427
Greg Wardd5d8a992000-05-23 01:42:17 +0000428 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000429 """Parse the command-line options for a single command.
430 'parser' must be a FancyGetopt instance; 'args' must be the list
431 of arguments, starting with the current command (whose options
432 we are about to parse). Returns a new version of 'args' with
433 the next command at the front of the list; will be the empty
434 list if there are no more commands on the command line. Returns
435 None if the user asked for help on this command.
436 """
437 # late import because of mutual dependence between these modules
438 from distutils.cmd import Command
439
440 # Pull the current command from the head of the command line
441 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000442 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000443 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000444 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000445
446 # Dig up the command class that implements this command, so we
447 # 1) know that it's a valid command, and 2) know which options
448 # it takes.
449 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000450 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000451 except DistutilsModuleError, msg:
452 raise DistutilsArgError, msg
453
454 # Require that the command class be derived from Command -- want
455 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000456 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000457 raise DistutilsClassError, \
458 "command class %s must subclass Command" % cmd_class
459
460 # Also make sure that the command object provides a list of its
461 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000462 if not (hasattr(cmd_class, 'user_options') and
463 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000464 raise DistutilsClassError, \
465 ("command class %s must provide " +
466 "'user_options' attribute (a list of tuples)") % \
467 cmd_class
468
469 # If the command class has a list of negative alias options,
470 # merge it in with the global negative aliases.
471 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000472 if hasattr(cmd_class, 'negative_opt'):
473 negative_opt = copy(negative_opt)
474 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000475
Greg Wardfa9ff762000-10-14 04:06:40 +0000476 # Check for help_options in command class. They have a different
477 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000478 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000479 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000480 help_options = fix_help_options(cmd_class.help_options)
481 else:
Greg Ward55fced32000-06-24 01:22:41 +0000482 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000483
Greg Ward9d17a7a2000-06-07 03:00:06 +0000484
Greg Wardd5d8a992000-05-23 01:42:17 +0000485 # All commands support the global options too, just by adding
486 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000487 parser.set_option_table(self.global_options +
488 cmd_class.user_options +
489 help_options)
490 parser.set_negative_aliases(negative_opt)
491 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000492 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000493 self._show_help(parser, display_options=0, commands=[cmd_class])
494 return
495
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000496 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000497 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000498 help_option_found=0
499 for (help_option, short, desc, func) in cmd_class.help_options:
500 if hasattr(opts, parser.get_attr_name(help_option)):
501 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000502 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000503 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000504
505 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000506 func()
Greg Ward55fced32000-06-24 01:22:41 +0000507 else:
Fred Drake981a1782001-08-10 18:59:30 +0000508 raise DistutilsClassError(
509 "invalid help function %s for help option '%s': "
510 "must be a callable object (function, etc.)"
511 % (`func`, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000512
Fred Drakeb94b8492001-12-06 20:51:35 +0000513 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000514 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000515
Greg Wardd5d8a992000-05-23 01:42:17 +0000516 # Put the options from the command-line into their official
517 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000518 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000519 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000520 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000521
522 return args
523
524 # _parse_command_opts ()
525
526
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000527 def finalize_options (self):
528 """Set final values for all the options on the Distribution
529 instance, analogous to the .finalize_options() method of Command
530 objects.
531 """
532
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000533 keywords = self.metadata.keywords
534 if keywords is not None:
535 if type(keywords) is StringType:
536 keywordlist = string.split(keywords, ',')
537 self.metadata.keywords = map(string.strip, keywordlist)
538
539 platforms = self.metadata.platforms
540 if platforms is not None:
541 if type(platforms) is StringType:
542 platformlist = string.split(platforms, ',')
543 self.metadata.platforms = map(string.strip, platformlist)
544
Greg Wardd5d8a992000-05-23 01:42:17 +0000545 def _show_help (self,
546 parser,
547 global_options=1,
548 display_options=1,
549 commands=[]):
550 """Show help for the setup script command-line in the form of
551 several lists of command-line options. 'parser' should be a
552 FancyGetopt instance; do not expect it to be returned in the
553 same state, as its option table will be reset to make it
554 generate the correct help text.
555
556 If 'global_options' is true, lists the global options:
557 --verbose, --dry-run, etc. If 'display_options' is true, lists
558 the "display-only" options: --name, --version, etc. Finally,
559 lists per-command help for every command name or command class
560 in 'commands'.
561 """
562 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000563 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000564 from distutils.cmd import Command
565
566 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000567 parser.set_option_table(self.global_options)
568 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000569 print
570
571 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000572 parser.set_option_table(self.display_options)
573 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000574 "Information display options (just display " +
575 "information, ignore any commands)")
576 print
577
578 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000579 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000580 klass = command
581 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000582 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000583 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000584 type(klass.help_options) is ListType):
585 parser.set_option_table(klass.user_options +
586 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000587 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000588 parser.set_option_table(klass.user_options)
589 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000590 print
591
Greg Ward9821bf42000-08-29 01:15:18 +0000592 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000593 return
594
595 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000596
Greg Wardd5d8a992000-05-23 01:42:17 +0000597
Greg Ward82715e12000-04-21 02:28:14 +0000598 def handle_display_options (self, option_order):
599 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000600 (--help-commands or the metadata display options) on the command
601 line, display the requested info and return true; else return
602 false.
603 """
Greg Ward9821bf42000-08-29 01:15:18 +0000604 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000605
606 # User just wants a list of commands -- we'll print it out and stop
607 # processing now (ie. if they ran "setup --help-commands foo bar",
608 # we ignore "foo bar").
609 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000610 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000611 print
Greg Ward9821bf42000-08-29 01:15:18 +0000612 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000613 return 1
614
615 # If user supplied any of the "display metadata" options, then
616 # display that metadata in the order in which the user supplied the
617 # metadata options.
618 any_display_options = 0
619 is_display_option = {}
620 for option in self.display_options:
621 is_display_option[option[0]] = 1
622
623 for (opt, val) in option_order:
624 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000625 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000626 value = getattr(self.metadata, "get_"+opt)()
627 if opt in ['keywords', 'platforms']:
628 print string.join(value, ',')
629 else:
630 print value
Greg Ward82715e12000-04-21 02:28:14 +0000631 any_display_options = 1
632
633 return any_display_options
634
635 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000636
637 def print_command_list (self, commands, header, max_length):
638 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000639 'print_commands()'.
640 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000641
642 print header + ":"
643
644 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000645 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000646 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000647 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000648 try:
649 description = klass.description
650 except AttributeError:
651 description = "(no description available)"
652
653 print " %-*s %s" % (max_length, cmd, description)
654
655 # print_command_list ()
656
657
658 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000659 """Print out a help message listing all available commands with a
660 description of each. The list is divided into "standard commands"
661 (listed in distutils.command.__all__) and "extra commands"
662 (mentioned in self.cmdclass, but not a standard command). The
663 descriptions come from the command class attribute
664 'description'.
665 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000666
667 import distutils.command
668 std_commands = distutils.command.__all__
669 is_std = {}
670 for cmd in std_commands:
671 is_std[cmd] = 1
672
673 extra_commands = []
674 for cmd in self.cmdclass.keys():
675 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000676 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000677
678 max_length = 0
679 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000680 if len(cmd) > max_length:
681 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000682
Greg Wardfd7b91e2000-09-26 01:52:25 +0000683 self.print_command_list(std_commands,
684 "Standard commands",
685 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000686 if extra_commands:
687 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000688 self.print_command_list(extra_commands,
689 "Extra commands",
690 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000691
692 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000693
Greg Wardf6fc8752000-11-11 02:47:11 +0000694 def get_command_list (self):
695 """Get a list of (command, description) tuples.
696 The list is divided into "standard commands" (listed in
697 distutils.command.__all__) and "extra commands" (mentioned in
698 self.cmdclass, but not a standard command). The descriptions come
699 from the command class attribute 'description'.
700 """
701 # Currently this is only used on Mac OS, for the Mac-only GUI
702 # Distutils interface (by Jack Jansen)
703
704 import distutils.command
705 std_commands = distutils.command.__all__
706 is_std = {}
707 for cmd in std_commands:
708 is_std[cmd] = 1
709
710 extra_commands = []
711 for cmd in self.cmdclass.keys():
712 if not is_std.get(cmd):
713 extra_commands.append(cmd)
714
715 rv = []
716 for cmd in (std_commands + extra_commands):
717 klass = self.cmdclass.get(cmd)
718 if not klass:
719 klass = self.get_command_class(cmd)
720 try:
721 description = klass.description
722 except AttributeError:
723 description = "(no description available)"
724 rv.append((cmd, description))
725 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000726
727 # -- Command class/object methods ----------------------------------
728
Greg Wardd5d8a992000-05-23 01:42:17 +0000729 def get_command_class (self, command):
730 """Return the class that implements the Distutils command named by
731 'command'. First we check the 'cmdclass' dictionary; if the
732 command is mentioned there, we fetch the class object from the
733 dictionary and return it. Otherwise we load the command module
734 ("distutils.command." + command) and fetch the command class from
735 the module. The loaded class is also stored in 'cmdclass'
736 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000737
Gregory P. Smith14263542000-05-12 00:41:33 +0000738 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000739 found, or if that module does not define the expected class.
740 """
741 klass = self.cmdclass.get(command)
742 if klass:
743 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000744
745 module_name = 'distutils.command.' + command
746 klass_name = command
747
748 try:
749 __import__ (module_name)
750 module = sys.modules[module_name]
751 except ImportError:
752 raise DistutilsModuleError, \
753 "invalid command '%s' (no module named '%s')" % \
754 (command, module_name)
755
756 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000757 klass = getattr(module, klass_name)
758 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000759 raise DistutilsModuleError, \
760 "invalid command '%s' (no class '%s' in module '%s')" \
761 % (command, klass_name, module_name)
762
Greg Wardd5d8a992000-05-23 01:42:17 +0000763 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000764 return klass
765
Greg Wardd5d8a992000-05-23 01:42:17 +0000766 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000767
Greg Wardd5d8a992000-05-23 01:42:17 +0000768 def get_command_obj (self, command, create=1):
769 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000770 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000771 object for 'command' is in the cache, then we either create and
772 return it (if 'create' is true) or return None.
773 """
Greg Ward2bd3f422000-06-02 01:59:33 +0000774 from distutils.core import DEBUG
Greg Wardd5d8a992000-05-23 01:42:17 +0000775 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000776 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000777 if DEBUG:
778 print "Distribution.get_command_obj(): " \
779 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000780
Greg Wardd5d8a992000-05-23 01:42:17 +0000781 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000782 cmd_obj = self.command_obj[command] = klass(self)
783 self.have_run[command] = 0
784
785 # Set any options that were supplied in config files
786 # or on the command line. (NB. support for error
787 # reporting is lame here: any errors aren't reported
788 # until 'finalize_options()' is called, which means
789 # we won't report the source of the error.)
790 options = self.command_options.get(command)
791 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000792 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000793
794 return cmd_obj
795
Greg Wardc32d9a62000-05-28 23:53:06 +0000796 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000797 """Set the options for 'command_obj' from 'option_dict'. Basically
798 this means copying elements of a dictionary ('option_dict') to
799 attributes of an instance ('command').
800
Greg Wardceb9e222000-09-25 01:23:52 +0000801 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000802 supplied, uses the standard option dictionary for this command
803 (from 'self.command_options').
804 """
805 from distutils.core import DEBUG
Fred Drakeb94b8492001-12-06 20:51:35 +0000806
Greg Wardc32d9a62000-05-28 23:53:06 +0000807 command_name = command_obj.get_command_name()
808 if option_dict is None:
809 option_dict = self.get_option_dict(command_name)
810
811 if DEBUG: print " setting options for '%s' command:" % command_name
812 for (option, (source, value)) in option_dict.items():
813 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000814 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000815 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000816 except AttributeError:
817 bool_opts = []
818 try:
819 neg_opt = command_obj.negative_opt
820 except AttributeError:
821 neg_opt = {}
822
823 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000824 is_string = type(value) is StringType
825 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000826 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000827 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000828 setattr(command_obj, option, strtobool(value))
829 elif hasattr(command_obj, option):
830 setattr(command_obj, option, value)
831 else:
832 raise DistutilsOptionError, \
833 ("error in %s: command '%s' has no such option '%s'"
834 % (source, command_name, option))
835 except ValueError, msg:
836 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000837
Greg Wardf449ea52000-09-16 15:23:28 +0000838 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000839 """Reinitializes a command to the state it was in when first
840 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000841 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000842 values in programmatically, overriding or supplementing
843 user-supplied values from the config files and command line.
844 You'll have to re-finalize the command object (by calling
845 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000846 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000847
Greg Wardf449ea52000-09-16 15:23:28 +0000848 'command' should be a command name (string) or command object. If
849 'reinit_subcommands' is true, also reinitializes the command's
850 sub-commands, as declared by the 'sub_commands' class attribute (if
851 it has one). See the "install" command for an example. Only
852 reinitializes the sub-commands that actually matter, ie. those
853 whose test predicates return true.
854
Greg Wardc32d9a62000-05-28 23:53:06 +0000855 Returns the reinitialized command object.
856 """
857 from distutils.cmd import Command
858 if not isinstance(command, Command):
859 command_name = command
860 command = self.get_command_obj(command_name)
861 else:
862 command_name = command.get_command_name()
863
864 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000865 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000866 command.initialize_options()
867 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000868 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000869 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000870
Greg Wardf449ea52000-09-16 15:23:28 +0000871 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000872 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000873 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000874
Greg Wardc32d9a62000-05-28 23:53:06 +0000875 return command
876
Fred Drakeb94b8492001-12-06 20:51:35 +0000877
Greg Wardfe6462c2000-04-04 01:40:52 +0000878 # -- Methods that operate on the Distribution ----------------------
879
880 def announce (self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000881 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000882
883 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000884 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000885 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000886 created by 'get_command_obj()'.
887 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000888 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000889 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000890
891
Greg Wardfe6462c2000-04-04 01:40:52 +0000892 # -- Methods that operate on its Commands --------------------------
893
894 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000895 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000896 if the command has already been run). Specifically: if we have
897 already created and run the command named by 'command', return
898 silently without doing anything. If the command named by 'command'
899 doesn't even have a command object yet, create one. Then invoke
900 'run()' on that command object (or an existing one).
901 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000902 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000903 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000904 return
905
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000906 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000907 cmd_obj = self.get_command_obj(command)
908 cmd_obj.ensure_finalized()
909 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000910 self.have_run[command] = 1
911
912
Greg Wardfe6462c2000-04-04 01:40:52 +0000913 # -- Distribution query methods ------------------------------------
914
915 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000916 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000917
918 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000919 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000920
921 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000922 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000923
924 def has_modules (self):
925 return self.has_pure_modules() or self.has_ext_modules()
926
Greg Ward51def7d2000-05-27 01:36:14 +0000927 def has_headers (self):
928 return self.headers and len(self.headers) > 0
929
Greg Ward44a61bb2000-05-20 15:06:48 +0000930 def has_scripts (self):
931 return self.scripts and len(self.scripts) > 0
932
933 def has_data_files (self):
934 return self.data_files and len(self.data_files) > 0
935
Greg Wardfe6462c2000-04-04 01:40:52 +0000936 def is_pure (self):
937 return (self.has_pure_modules() and
938 not self.has_ext_modules() and
939 not self.has_c_libraries())
940
Greg Ward82715e12000-04-21 02:28:14 +0000941 # -- Metadata query methods ----------------------------------------
942
943 # If you're looking for 'get_name()', 'get_version()', and so forth,
944 # they are defined in a sneaky way: the constructor binds self.get_XXX
945 # to self.metadata.get_XXX. The actual code is in the
946 # DistributionMetadata class, below.
947
948# class Distribution
949
950
951class DistributionMetadata:
952 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000953 author, and so forth.
954 """
Greg Ward82715e12000-04-21 02:28:14 +0000955
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000956 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
957 "maintainer", "maintainer_email", "url",
958 "license", "description", "long_description",
959 "keywords", "platforms", "fullname", "contact",
960 "contact_email", "licence")
961
Greg Ward82715e12000-04-21 02:28:14 +0000962 def __init__ (self):
963 self.name = None
964 self.version = None
965 self.author = None
966 self.author_email = None
967 self.maintainer = None
968 self.maintainer_email = None
969 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000970 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000971 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000972 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000973 self.keywords = None
974 self.platforms = None
Fred Drakeb94b8492001-12-06 20:51:35 +0000975
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000976 def write_pkg_info (self, base_dir):
977 """Write the PKG-INFO file into the release tree.
978 """
979
980 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
981
982 pkg_info.write('Metadata-Version: 1.0\n')
983 pkg_info.write('Name: %s\n' % self.get_name() )
984 pkg_info.write('Version: %s\n' % self.get_version() )
985 pkg_info.write('Summary: %s\n' % self.get_description() )
986 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +0000987 pkg_info.write('Author: %s\n' % self.get_contact() )
988 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000989 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000990
991 long_desc = rfc822_escape( self.get_long_description() )
992 pkg_info.write('Description: %s\n' % long_desc)
993
994 keywords = string.join( self.get_keywords(), ',')
995 if keywords:
996 pkg_info.write('Keywords: %s\n' % keywords )
997
998 for platform in self.get_platforms():
999 pkg_info.write('Platform: %s\n' % platform )
1000
1001 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001002
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001003 # write_pkg_info ()
Fred Drakeb94b8492001-12-06 20:51:35 +00001004
Greg Ward82715e12000-04-21 02:28:14 +00001005 # -- Metadata query methods ----------------------------------------
1006
Greg Wardfe6462c2000-04-04 01:40:52 +00001007 def get_name (self):
1008 return self.name or "UNKNOWN"
1009
Greg Ward82715e12000-04-21 02:28:14 +00001010 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001011 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001012
Greg Ward82715e12000-04-21 02:28:14 +00001013 def get_fullname (self):
1014 return "%s-%s" % (self.get_name(), self.get_version())
1015
1016 def get_author(self):
1017 return self.author or "UNKNOWN"
1018
1019 def get_author_email(self):
1020 return self.author_email or "UNKNOWN"
1021
1022 def get_maintainer(self):
1023 return self.maintainer or "UNKNOWN"
1024
1025 def get_maintainer_email(self):
1026 return self.maintainer_email or "UNKNOWN"
1027
1028 def get_contact(self):
1029 return (self.maintainer or
1030 self.author or
1031 "UNKNOWN")
1032
1033 def get_contact_email(self):
1034 return (self.maintainer_email or
1035 self.author_email or
1036 "UNKNOWN")
1037
1038 def get_url(self):
1039 return self.url or "UNKNOWN"
1040
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001041 def get_license(self):
1042 return self.license or "UNKNOWN"
1043 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001044
Greg Ward82715e12000-04-21 02:28:14 +00001045 def get_description(self):
1046 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001047
1048 def get_long_description(self):
1049 return self.long_description or "UNKNOWN"
1050
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001051 def get_keywords(self):
1052 return self.keywords or []
1053
1054 def get_platforms(self):
1055 return self.platforms or ["UNKNOWN"]
1056
Greg Ward82715e12000-04-21 02:28:14 +00001057# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001058
Greg Ward2ff78872000-06-24 00:23:20 +00001059
1060def fix_help_options (options):
1061 """Convert a 4-tuple 'help_options' list as found in various command
1062 classes to the 3-tuple form required by FancyGetopt.
1063 """
1064 new_options = []
1065 for help_tuple in options:
1066 new_options.append(help_tuple[0:3])
1067 return new_options
1068
1069
Greg Wardfe6462c2000-04-04 01:40:52 +00001070if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001071 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001072 print "ok"