blob: 3a690696bb57c499be8737ff981507389155a3b9 [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.dist
2
3Provides the Distribution class, which represents the module distribution
Greg Ward8ff5a3f2000-06-02 00:44:53 +00004being built/installed/distributed.
5"""
Greg Wardfe6462c2000-04-04 01:40:52 +00006
Greg Wardfe6462c2000-04-04 01:40:52 +00007__revision__ = "$Id$"
8
Gregory P. Smith14263542000-05-12 00:41:33 +00009import sys, os, string, re
Greg Wardfe6462c2000-04-04 01:40:52 +000010from types import *
11from copy import copy
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000012
13try:
14 import warnings
Andrew M. Kuchlingccf4e422002-10-31 13:39:33 +000015except ImportError:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000016 warnings = None
17
Greg Wardfe6462c2000-04-04 01:40:52 +000018from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000019from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000020from distutils.util import check_environ, strtobool, rfc822_escape
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000021from distutils import log
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000022from distutils.debug import DEBUG
Greg Wardfe6462c2000-04-04 01:40:52 +000023
24# Regex to define acceptable Distutils command names. This is not *quite*
25# the same as a Python NAME -- I don't allow leading underscores. The fact
26# that they're very similar is no coincidence; the default naming scheme is
27# to look for a Python module named after the command.
28command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
29
30
31class Distribution:
Greg Ward8ff5a3f2000-06-02 00:44:53 +000032 """The core of the Distutils. Most of the work hiding behind 'setup'
33 is really done within a Distribution instance, which farms the work out
34 to the Distutils commands specified on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +000035
Greg Ward8ff5a3f2000-06-02 00:44:53 +000036 Setup scripts will almost never instantiate Distribution directly,
37 unless the 'setup()' function is totally inadequate to their needs.
38 However, it is conceivable that a setup script might wish to subclass
39 Distribution for some specialized purpose, and then pass the subclass
40 to 'setup()' as the 'distclass' keyword argument. If so, it is
41 necessary to respect the expectations that 'setup' has of Distribution.
42 See the code for 'setup()', in core.py, for details.
43 """
Greg Wardfe6462c2000-04-04 01:40:52 +000044
45
46 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000047 # supplied to the setup script prior to any actual commands.
48 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000049 # these global options. This list should be kept to a bare minimum,
50 # since every global option is also valid as a command option -- and we
51 # don't want to pollute the commands with too many options that they
52 # have minimal control over.
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000053 # The fourth entry for verbose means that it can be repeated.
54 global_options = [('verbose', 'v', "run verbosely (default)", 1),
Greg Wardd5d8a992000-05-23 01:42:17 +000055 ('quiet', 'q', "run quietly (turns verbosity off)"),
56 ('dry-run', 'n', "don't actually do anything"),
57 ('help', 'h', "show detailed help message"),
Greg Wardfe6462c2000-04-04 01:40:52 +000058 ]
Greg Ward82715e12000-04-21 02:28:14 +000059
60 # options that are not propagated to the commands
61 display_options = [
62 ('help-commands', None,
63 "list all available commands"),
64 ('name', None,
65 "print package name"),
66 ('version', 'V',
67 "print package version"),
68 ('fullname', None,
69 "print <package name>-<version>"),
70 ('author', None,
71 "print the author's name"),
72 ('author-email', None,
73 "print the author's email address"),
74 ('maintainer', None,
75 "print the maintainer's name"),
76 ('maintainer-email', None,
77 "print the maintainer's email address"),
78 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000079 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000080 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000081 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000082 ('url', None,
83 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +000084 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +000085 "print the license of the package"),
86 ('licence', None,
87 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +000088 ('description', None,
89 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +000090 ('long-description', None,
91 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000092 ('platforms', None,
93 "print the list of platforms"),
94 ('keywords', None,
95 "print the list of keywords"),
Greg Ward82715e12000-04-21 02:28:14 +000096 ]
Greg Ward2f2b6c62000-09-25 01:58:07 +000097 display_option_names = map(lambda x: translate_longopt(x[0]),
98 display_options)
Greg Ward82715e12000-04-21 02:28:14 +000099
100 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +0000101 negative_opt = {'quiet': 'verbose'}
102
103
104 # -- Creation/initialization methods -------------------------------
Fred Drakeb94b8492001-12-06 20:51:35 +0000105
Greg Wardfe6462c2000-04-04 01:40:52 +0000106 def __init__ (self, attrs=None):
107 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000108 attributes of a Distribution, and then use 'attrs' (a dictionary
109 mapping attribute names to values) to assign some of those
110 attributes their "real" values. (Any attributes not mentioned in
111 'attrs' will be assigned to some null value: 0, None, an empty list
112 or dictionary, etc.) Most importantly, initialize the
113 'command_obj' attribute to the empty dictionary; this will be
114 filled in with real command objects by 'parse_command_line()'.
115 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000116
117 # Default values for our command-line options
118 self.verbose = 1
119 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000120 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000121 for attr in self.display_option_names:
122 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000123
Greg Ward82715e12000-04-21 02:28:14 +0000124 # Store the distribution meta-data (name, version, author, and so
125 # forth) in a separate object -- we're getting to have enough
126 # information here (and enough command-line options) that it's
127 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
128 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000129 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000130 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000131 method_name = "get_" + basename
132 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000133
134 # 'cmdclass' maps command names to class objects, so we
135 # can 1) quickly figure out which class to instantiate when
136 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000137 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000138 self.cmdclass = {}
139
Greg Ward9821bf42000-08-29 01:15:18 +0000140 # 'script_name' and 'script_args' are usually set to sys.argv[0]
141 # and sys.argv[1:], but they can be overridden when the caller is
142 # not necessarily a setup script run from the command-line.
143 self.script_name = None
144 self.script_args = None
145
Greg Wardd5d8a992000-05-23 01:42:17 +0000146 # 'command_options' is where we store command options between
147 # parsing them (from config files, the command-line, etc.) and when
148 # they are actually needed -- ie. when the command in question is
149 # instantiated. It is a dictionary of dictionaries of 2-tuples:
150 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000151 self.command_options = {}
152
Greg Wardfe6462c2000-04-04 01:40:52 +0000153 # These options are really the business of various commands, rather
154 # than of the Distribution itself. We provide aliases for them in
155 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000156 self.packages = None
157 self.package_dir = None
158 self.py_modules = None
159 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000160 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000161 self.ext_modules = None
162 self.ext_package = None
163 self.include_dirs = None
164 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000165 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000166 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000167
168 # And now initialize bookkeeping stuff that can't be supplied by
169 # the caller at all. 'command_obj' maps command names to
170 # Command instances -- that's how we enforce that every command
171 # class is a singleton.
172 self.command_obj = {}
173
174 # 'have_run' maps command names to boolean values; it keeps track
175 # of whether we have actually run a particular command, to make it
176 # cheap to "run" a command whenever we think we might need to -- if
177 # it's already been done, no need for expensive filesystem
178 # operations, we just check the 'have_run' dictionary and carry on.
179 # It's only safe to query 'have_run' for a command class that has
180 # been instantiated -- a false value will be inserted when the
181 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000182 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000183 # '.get()' rather than a straight lookup.
184 self.have_run = {}
185
186 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000187 # the setup script) to possibly override any or all of these
188 # distribution options.
189
Greg Wardfe6462c2000-04-04 01:40:52 +0000190 if attrs:
191
192 # Pull out the set of command options and work on them
193 # specifically. Note that this order guarantees that aliased
194 # command options will override any supplied redundantly
195 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000196 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000197 if options:
198 del attrs['options']
199 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000200 opt_dict = self.get_option_dict(command)
201 for (opt, val) in cmd_options.items():
202 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000203
204 # Now work on the rest of the attributes. Any attribute that's
205 # not already defined is invalid!
206 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000207 if hasattr(self.metadata, key):
208 setattr(self.metadata, key, val)
209 elif hasattr(self, key):
210 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000211 else:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000212 msg = "Unknown distribution option: %s" % repr(key)
213 if warnings is not None:
214 warnings.warn(msg)
215 else:
216 sys.stderr.write(msg + "\n")
Greg Wardfe6462c2000-04-04 01:40:52 +0000217
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000218 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000219
Greg Wardfe6462c2000-04-04 01:40:52 +0000220 # __init__ ()
221
222
Greg Ward0e48cfd2000-05-26 01:00:15 +0000223 def get_option_dict (self, command):
224 """Get the option dictionary for a given command. If that
225 command's option dictionary hasn't been created yet, then create it
226 and return the new dictionary; otherwise, return the existing
227 option dictionary.
228 """
229
230 dict = self.command_options.get(command)
231 if dict is None:
232 dict = self.command_options[command] = {}
233 return dict
234
235
Greg Wardc32d9a62000-05-28 23:53:06 +0000236 def dump_option_dicts (self, header=None, commands=None, indent=""):
237 from pprint import pformat
238
239 if commands is None: # dump all command option dicts
240 commands = self.command_options.keys()
241 commands.sort()
242
243 if header is not None:
244 print indent + header
245 indent = indent + " "
246
247 if not commands:
248 print indent + "no commands known yet"
249 return
250
251 for cmd_name in commands:
252 opt_dict = self.command_options.get(cmd_name)
253 if opt_dict is None:
254 print indent + "no option dict for '%s' command" % cmd_name
255 else:
256 print indent + "option dict for '%s' command:" % cmd_name
257 out = pformat(opt_dict)
258 for line in string.split(out, "\n"):
259 print indent + " " + line
260
261 # dump_option_dicts ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000262
Greg Wardc32d9a62000-05-28 23:53:06 +0000263
264
Greg Wardd5d8a992000-05-23 01:42:17 +0000265 # -- Config file finding/parsing methods ---------------------------
266
Gregory P. Smith14263542000-05-12 00:41:33 +0000267 def find_config_files (self):
268 """Find as many configuration files as should be processed for this
269 platform, and return a list of filenames in the order in which they
270 should be parsed. The filenames returned are guaranteed to exist
271 (modulo nasty race conditions).
272
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000273 There are three possible config files: distutils.cfg in the
274 Distutils installation directory (ie. where the top-level
275 Distutils __inst__.py file lives), a file in the user's home
276 directory named .pydistutils.cfg on Unix and pydistutils.cfg
277 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000278 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000279 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000280 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000281
Greg Ward11696872000-06-07 02:29:03 +0000282 # Where to look for the system-wide Distutils config file
283 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
284
285 # Look for the system config file
286 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000287 if os.path.isfile(sys_file):
288 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000289
Greg Ward11696872000-06-07 02:29:03 +0000290 # What to call the per-user config file
291 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000292 user_filename = ".pydistutils.cfg"
293 else:
294 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000295
Greg Ward11696872000-06-07 02:29:03 +0000296 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000297 if os.environ.has_key('HOME'):
298 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000299 if os.path.isfile(user_file):
300 files.append(user_file)
301
Gregory P. Smith14263542000-05-12 00:41:33 +0000302 # All platforms support local setup.cfg
303 local_file = "setup.cfg"
304 if os.path.isfile(local_file):
305 files.append(local_file)
306
307 return files
308
309 # find_config_files ()
310
311
312 def parse_config_files (self, filenames=None):
313
314 from ConfigParser import ConfigParser
315
316 if filenames is None:
317 filenames = self.find_config_files()
318
Greg Ward2bd3f422000-06-02 01:59:33 +0000319 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000320
Gregory P. Smith14263542000-05-12 00:41:33 +0000321 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000322 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000323 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000324 parser.read(filename)
325 for section in parser.sections():
326 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000327 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000328
Greg Wardd5d8a992000-05-23 01:42:17 +0000329 for opt in options:
330 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000331 val = parser.get(section,opt)
332 opt = string.replace(opt, '-', '_')
333 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000334
Greg Ward47460772000-05-23 03:47:35 +0000335 # Make the ConfigParser forget everything (so we retain
336 # the original filenames that options come from) -- gag,
337 # retch, puke -- another good reason for a distutils-
338 # specific config parser (sigh...)
339 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000340
Greg Wardceb9e222000-09-25 01:23:52 +0000341 # If there was a "global" section in the config file, use it
342 # to set Distribution options.
343
344 if self.command_options.has_key('global'):
345 for (opt, (src, val)) in self.command_options['global'].items():
346 alias = self.negative_opt.get(opt)
347 try:
348 if alias:
349 setattr(self, alias, not strtobool(val))
350 elif opt in ('verbose', 'dry_run'): # ugh!
351 setattr(self, opt, strtobool(val))
352 except ValueError, msg:
353 raise DistutilsOptionError, msg
354
355 # parse_config_files ()
356
Gregory P. Smith14263542000-05-12 00:41:33 +0000357
Greg Wardd5d8a992000-05-23 01:42:17 +0000358 # -- Command-line parsing methods ----------------------------------
359
Greg Ward9821bf42000-08-29 01:15:18 +0000360 def parse_command_line (self):
361 """Parse the setup script's command line, taken from the
362 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
363 -- see 'setup()' in core.py). This list is first processed for
364 "global options" -- options that set attributes of the Distribution
365 instance. Then, it is alternately scanned for Distutils commands
366 and options for that command. Each new command terminates the
367 options for the previous command. The allowed options for a
368 command are determined by the 'user_options' attribute of the
369 command class -- thus, we have to be able to load command classes
370 in order to parse the command line. Any error in that 'options'
371 attribute raises DistutilsGetoptError; any error on the
372 command-line raises DistutilsArgError. If no Distutils commands
373 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000374 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000375 on with executing commands; false if no errors but we shouldn't
376 execute commands (currently, this only happens if user asks for
377 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000378 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000379 #
Fred Drake981a1782001-08-10 18:59:30 +0000380 # We now have enough information to show the Macintosh dialog
381 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000382 #
383 if sys.platform == 'mac':
384 import EasyDialogs
385 cmdlist = self.get_command_list()
386 self.script_args = EasyDialogs.GetArgv(
387 self.global_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000388
Greg Wardfe6462c2000-04-04 01:40:52 +0000389 # We have to parse the command line a bit at a time -- global
390 # options, then the first command, then its options, and so on --
391 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000392 # the options that are valid for a particular class aren't known
393 # until we have loaded the command class, which doesn't happen
394 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000395
396 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000397 parser = FancyGetopt(self.global_options + self.display_options)
398 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000399 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000400 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000401 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000402 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000403
Greg Ward82715e12000-04-21 02:28:14 +0000404 # for display options we return immediately
405 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000406 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000407
Greg Wardfe6462c2000-04-04 01:40:52 +0000408 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000409 args = self._parse_command_opts(parser, args)
410 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000411 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000412
Greg Wardd5d8a992000-05-23 01:42:17 +0000413 # Handle the cases of --help as a "global" option, ie.
414 # "setup.py --help" and "setup.py --help command ...". For the
415 # former, we show global options (--verbose, --dry-run, etc.)
416 # and display-only options (--name, --version, etc.); for the
417 # latter, we omit the display-only options and show help for
418 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000419 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000420 self._show_help(parser,
421 display_options=len(self.commands) == 0,
422 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000423 return
424
425 # Oops, no commands found -- an end-user error
426 if not self.commands:
427 raise DistutilsArgError, "no commands supplied"
428
429 # All is well: return true
430 return 1
431
432 # parse_command_line()
433
Greg Wardd5d8a992000-05-23 01:42:17 +0000434 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000435 """Parse the command-line options for a single command.
436 'parser' must be a FancyGetopt instance; 'args' must be the list
437 of arguments, starting with the current command (whose options
438 we are about to parse). Returns a new version of 'args' with
439 the next command at the front of the list; will be the empty
440 list if there are no more commands on the command line. Returns
441 None if the user asked for help on this command.
442 """
443 # late import because of mutual dependence between these modules
444 from distutils.cmd import Command
445
446 # Pull the current command from the head of the command line
447 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000448 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000449 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000450 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000451
452 # Dig up the command class that implements this command, so we
453 # 1) know that it's a valid command, and 2) know which options
454 # it takes.
455 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000456 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000457 except DistutilsModuleError, msg:
458 raise DistutilsArgError, msg
459
460 # Require that the command class be derived from Command -- want
461 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000462 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000463 raise DistutilsClassError, \
464 "command class %s must subclass Command" % cmd_class
465
466 # Also make sure that the command object provides a list of its
467 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000468 if not (hasattr(cmd_class, 'user_options') and
469 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000470 raise DistutilsClassError, \
471 ("command class %s must provide " +
472 "'user_options' attribute (a list of tuples)") % \
473 cmd_class
474
475 # If the command class has a list of negative alias options,
476 # merge it in with the global negative aliases.
477 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000478 if hasattr(cmd_class, 'negative_opt'):
479 negative_opt = copy(negative_opt)
480 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000481
Greg Wardfa9ff762000-10-14 04:06:40 +0000482 # Check for help_options in command class. They have a different
483 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000484 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000485 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000486 help_options = fix_help_options(cmd_class.help_options)
487 else:
Greg Ward55fced32000-06-24 01:22:41 +0000488 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000489
Greg Ward9d17a7a2000-06-07 03:00:06 +0000490
Greg Wardd5d8a992000-05-23 01:42:17 +0000491 # All commands support the global options too, just by adding
492 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000493 parser.set_option_table(self.global_options +
494 cmd_class.user_options +
495 help_options)
496 parser.set_negative_aliases(negative_opt)
497 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000498 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000499 self._show_help(parser, display_options=0, commands=[cmd_class])
500 return
501
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000502 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000503 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000504 help_option_found=0
505 for (help_option, short, desc, func) in cmd_class.help_options:
506 if hasattr(opts, parser.get_attr_name(help_option)):
507 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000508 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000509 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000510
511 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000512 func()
Greg Ward55fced32000-06-24 01:22:41 +0000513 else:
Fred Drake981a1782001-08-10 18:59:30 +0000514 raise DistutilsClassError(
515 "invalid help function %s for help option '%s': "
516 "must be a callable object (function, etc.)"
517 % (`func`, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000518
Fred Drakeb94b8492001-12-06 20:51:35 +0000519 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000520 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000521
Greg Wardd5d8a992000-05-23 01:42:17 +0000522 # Put the options from the command-line into their official
523 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000524 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000525 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000526 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000527
528 return args
529
530 # _parse_command_opts ()
531
532
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000533 def finalize_options (self):
534 """Set final values for all the options on the Distribution
535 instance, analogous to the .finalize_options() method of Command
536 objects.
537 """
538
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000539 keywords = self.metadata.keywords
540 if keywords is not None:
541 if type(keywords) is StringType:
542 keywordlist = string.split(keywords, ',')
543 self.metadata.keywords = map(string.strip, keywordlist)
544
545 platforms = self.metadata.platforms
546 if platforms is not None:
547 if type(platforms) is StringType:
548 platformlist = string.split(platforms, ',')
549 self.metadata.platforms = map(string.strip, platformlist)
550
Greg Wardd5d8a992000-05-23 01:42:17 +0000551 def _show_help (self,
552 parser,
553 global_options=1,
554 display_options=1,
555 commands=[]):
556 """Show help for the setup script command-line in the form of
557 several lists of command-line options. 'parser' should be a
558 FancyGetopt instance; do not expect it to be returned in the
559 same state, as its option table will be reset to make it
560 generate the correct help text.
561
562 If 'global_options' is true, lists the global options:
563 --verbose, --dry-run, etc. If 'display_options' is true, lists
564 the "display-only" options: --name, --version, etc. Finally,
565 lists per-command help for every command name or command class
566 in 'commands'.
567 """
568 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000569 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000570 from distutils.cmd import Command
571
572 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000573 parser.set_option_table(self.global_options)
574 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000575 print
576
577 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000578 parser.set_option_table(self.display_options)
579 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000580 "Information display options (just display " +
581 "information, ignore any commands)")
582 print
583
584 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000585 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000586 klass = command
587 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000588 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000589 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000590 type(klass.help_options) is ListType):
591 parser.set_option_table(klass.user_options +
592 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000593 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000594 parser.set_option_table(klass.user_options)
595 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000596 print
597
Greg Ward9821bf42000-08-29 01:15:18 +0000598 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000599 return
600
601 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000602
Greg Wardd5d8a992000-05-23 01:42:17 +0000603
Greg Ward82715e12000-04-21 02:28:14 +0000604 def handle_display_options (self, option_order):
605 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000606 (--help-commands or the metadata display options) on the command
607 line, display the requested info and return true; else return
608 false.
609 """
Greg Ward9821bf42000-08-29 01:15:18 +0000610 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000611
612 # User just wants a list of commands -- we'll print it out and stop
613 # processing now (ie. if they ran "setup --help-commands foo bar",
614 # we ignore "foo bar").
615 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000616 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000617 print
Greg Ward9821bf42000-08-29 01:15:18 +0000618 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000619 return 1
620
621 # If user supplied any of the "display metadata" options, then
622 # display that metadata in the order in which the user supplied the
623 # metadata options.
624 any_display_options = 0
625 is_display_option = {}
626 for option in self.display_options:
627 is_display_option[option[0]] = 1
628
629 for (opt, val) in option_order:
630 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000631 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000632 value = getattr(self.metadata, "get_"+opt)()
633 if opt in ['keywords', 'platforms']:
634 print string.join(value, ',')
635 else:
636 print value
Greg Ward82715e12000-04-21 02:28:14 +0000637 any_display_options = 1
638
639 return any_display_options
640
641 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000642
643 def print_command_list (self, commands, header, max_length):
644 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000645 'print_commands()'.
646 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000647
648 print header + ":"
649
650 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000651 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000652 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000653 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000654 try:
655 description = klass.description
656 except AttributeError:
657 description = "(no description available)"
658
659 print " %-*s %s" % (max_length, cmd, description)
660
661 # print_command_list ()
662
663
664 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000665 """Print out a help message listing all available commands with a
666 description of each. The list is divided into "standard commands"
667 (listed in distutils.command.__all__) and "extra commands"
668 (mentioned in self.cmdclass, but not a standard command). The
669 descriptions come from the command class attribute
670 'description'.
671 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000672
673 import distutils.command
674 std_commands = distutils.command.__all__
675 is_std = {}
676 for cmd in std_commands:
677 is_std[cmd] = 1
678
679 extra_commands = []
680 for cmd in self.cmdclass.keys():
681 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000682 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000683
684 max_length = 0
685 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000686 if len(cmd) > max_length:
687 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000688
Greg Wardfd7b91e2000-09-26 01:52:25 +0000689 self.print_command_list(std_commands,
690 "Standard commands",
691 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000692 if extra_commands:
693 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000694 self.print_command_list(extra_commands,
695 "Extra commands",
696 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000697
698 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000699
Greg Wardf6fc8752000-11-11 02:47:11 +0000700 def get_command_list (self):
701 """Get a list of (command, description) tuples.
702 The list is divided into "standard commands" (listed in
703 distutils.command.__all__) and "extra commands" (mentioned in
704 self.cmdclass, but not a standard command). The descriptions come
705 from the command class attribute 'description'.
706 """
707 # Currently this is only used on Mac OS, for the Mac-only GUI
708 # Distutils interface (by Jack Jansen)
709
710 import distutils.command
711 std_commands = distutils.command.__all__
712 is_std = {}
713 for cmd in std_commands:
714 is_std[cmd] = 1
715
716 extra_commands = []
717 for cmd in self.cmdclass.keys():
718 if not is_std.get(cmd):
719 extra_commands.append(cmd)
720
721 rv = []
722 for cmd in (std_commands + extra_commands):
723 klass = self.cmdclass.get(cmd)
724 if not klass:
725 klass = self.get_command_class(cmd)
726 try:
727 description = klass.description
728 except AttributeError:
729 description = "(no description available)"
730 rv.append((cmd, description))
731 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000732
733 # -- Command class/object methods ----------------------------------
734
Greg Wardd5d8a992000-05-23 01:42:17 +0000735 def get_command_class (self, command):
736 """Return the class that implements the Distutils command named by
737 'command'. First we check the 'cmdclass' dictionary; if the
738 command is mentioned there, we fetch the class object from the
739 dictionary and return it. Otherwise we load the command module
740 ("distutils.command." + command) and fetch the command class from
741 the module. The loaded class is also stored in 'cmdclass'
742 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000743
Gregory P. Smith14263542000-05-12 00:41:33 +0000744 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000745 found, or if that module does not define the expected class.
746 """
747 klass = self.cmdclass.get(command)
748 if klass:
749 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000750
751 module_name = 'distutils.command.' + command
752 klass_name = command
753
754 try:
755 __import__ (module_name)
756 module = sys.modules[module_name]
757 except ImportError:
758 raise DistutilsModuleError, \
759 "invalid command '%s' (no module named '%s')" % \
760 (command, module_name)
761
762 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000763 klass = getattr(module, klass_name)
764 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000765 raise DistutilsModuleError, \
766 "invalid command '%s' (no class '%s' in module '%s')" \
767 % (command, klass_name, module_name)
768
Greg Wardd5d8a992000-05-23 01:42:17 +0000769 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000770 return klass
771
Greg Wardd5d8a992000-05-23 01:42:17 +0000772 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000773
Greg Wardd5d8a992000-05-23 01:42:17 +0000774 def get_command_obj (self, command, create=1):
775 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000776 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000777 object for 'command' is in the cache, then we either create and
778 return it (if 'create' is true) or return None.
779 """
780 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000781 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000782 if DEBUG:
783 print "Distribution.get_command_obj(): " \
784 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000785
Greg Wardd5d8a992000-05-23 01:42:17 +0000786 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000787 cmd_obj = self.command_obj[command] = klass(self)
788 self.have_run[command] = 0
789
790 # Set any options that were supplied in config files
791 # or on the command line. (NB. support for error
792 # reporting is lame here: any errors aren't reported
793 # until 'finalize_options()' is called, which means
794 # we won't report the source of the error.)
795 options = self.command_options.get(command)
796 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000797 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000798
799 return cmd_obj
800
Greg Wardc32d9a62000-05-28 23:53:06 +0000801 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000802 """Set the options for 'command_obj' from 'option_dict'. Basically
803 this means copying elements of a dictionary ('option_dict') to
804 attributes of an instance ('command').
805
Greg Wardceb9e222000-09-25 01:23:52 +0000806 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000807 supplied, uses the standard option dictionary for this command
808 (from 'self.command_options').
809 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000810 command_name = command_obj.get_command_name()
811 if option_dict is None:
812 option_dict = self.get_option_dict(command_name)
813
814 if DEBUG: print " setting options for '%s' command:" % command_name
815 for (option, (source, value)) in option_dict.items():
816 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000817 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000818 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000819 except AttributeError:
820 bool_opts = []
821 try:
822 neg_opt = command_obj.negative_opt
823 except AttributeError:
824 neg_opt = {}
825
826 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000827 is_string = type(value) is StringType
828 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000829 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000830 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000831 setattr(command_obj, option, strtobool(value))
832 elif hasattr(command_obj, option):
833 setattr(command_obj, option, value)
834 else:
835 raise DistutilsOptionError, \
836 ("error in %s: command '%s' has no such option '%s'"
837 % (source, command_name, option))
838 except ValueError, msg:
839 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000840
Greg Wardf449ea52000-09-16 15:23:28 +0000841 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000842 """Reinitializes a command to the state it was in when first
843 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000844 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000845 values in programmatically, overriding or supplementing
846 user-supplied values from the config files and command line.
847 You'll have to re-finalize the command object (by calling
848 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000849 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000850
Greg Wardf449ea52000-09-16 15:23:28 +0000851 'command' should be a command name (string) or command object. If
852 'reinit_subcommands' is true, also reinitializes the command's
853 sub-commands, as declared by the 'sub_commands' class attribute (if
854 it has one). See the "install" command for an example. Only
855 reinitializes the sub-commands that actually matter, ie. those
856 whose test predicates return true.
857
Greg Wardc32d9a62000-05-28 23:53:06 +0000858 Returns the reinitialized command object.
859 """
860 from distutils.cmd import Command
861 if not isinstance(command, Command):
862 command_name = command
863 command = self.get_command_obj(command_name)
864 else:
865 command_name = command.get_command_name()
866
867 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000868 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000869 command.initialize_options()
870 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000871 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000872 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000873
Greg Wardf449ea52000-09-16 15:23:28 +0000874 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000875 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000876 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000877
Greg Wardc32d9a62000-05-28 23:53:06 +0000878 return command
879
Fred Drakeb94b8492001-12-06 20:51:35 +0000880
Greg Wardfe6462c2000-04-04 01:40:52 +0000881 # -- Methods that operate on the Distribution ----------------------
882
883 def announce (self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000884 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000885
886 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000887 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000888 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000889 created by 'get_command_obj()'.
890 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000891 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000892 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000893
894
Greg Wardfe6462c2000-04-04 01:40:52 +0000895 # -- Methods that operate on its Commands --------------------------
896
897 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000898 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000899 if the command has already been run). Specifically: if we have
900 already created and run the command named by 'command', return
901 silently without doing anything. If the command named by 'command'
902 doesn't even have a command object yet, create one. Then invoke
903 'run()' on that command object (or an existing one).
904 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000905 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000906 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000907 return
908
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000909 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000910 cmd_obj = self.get_command_obj(command)
911 cmd_obj.ensure_finalized()
912 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000913 self.have_run[command] = 1
914
915
Greg Wardfe6462c2000-04-04 01:40:52 +0000916 # -- Distribution query methods ------------------------------------
917
918 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000919 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000920
921 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000922 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000923
924 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000925 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000926
927 def has_modules (self):
928 return self.has_pure_modules() or self.has_ext_modules()
929
Greg Ward51def7d2000-05-27 01:36:14 +0000930 def has_headers (self):
931 return self.headers and len(self.headers) > 0
932
Greg Ward44a61bb2000-05-20 15:06:48 +0000933 def has_scripts (self):
934 return self.scripts and len(self.scripts) > 0
935
936 def has_data_files (self):
937 return self.data_files and len(self.data_files) > 0
938
Greg Wardfe6462c2000-04-04 01:40:52 +0000939 def is_pure (self):
940 return (self.has_pure_modules() and
941 not self.has_ext_modules() and
942 not self.has_c_libraries())
943
Greg Ward82715e12000-04-21 02:28:14 +0000944 # -- Metadata query methods ----------------------------------------
945
946 # If you're looking for 'get_name()', 'get_version()', and so forth,
947 # they are defined in a sneaky way: the constructor binds self.get_XXX
948 # to self.metadata.get_XXX. The actual code is in the
949 # DistributionMetadata class, below.
950
951# class Distribution
952
953
954class DistributionMetadata:
955 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000956 author, and so forth.
957 """
Greg Ward82715e12000-04-21 02:28:14 +0000958
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000959 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
960 "maintainer", "maintainer_email", "url",
961 "license", "description", "long_description",
962 "keywords", "platforms", "fullname", "contact",
963 "contact_email", "licence")
964
Greg Ward82715e12000-04-21 02:28:14 +0000965 def __init__ (self):
966 self.name = None
967 self.version = None
968 self.author = None
969 self.author_email = None
970 self.maintainer = None
971 self.maintainer_email = None
972 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000973 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000974 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000975 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000976 self.keywords = None
977 self.platforms = None
Fred Drakeb94b8492001-12-06 20:51:35 +0000978
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000979 def write_pkg_info (self, base_dir):
980 """Write the PKG-INFO file into the release tree.
981 """
982
983 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
984
985 pkg_info.write('Metadata-Version: 1.0\n')
986 pkg_info.write('Name: %s\n' % self.get_name() )
987 pkg_info.write('Version: %s\n' % self.get_version() )
988 pkg_info.write('Summary: %s\n' % self.get_description() )
989 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +0000990 pkg_info.write('Author: %s\n' % self.get_contact() )
991 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000992 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000993
994 long_desc = rfc822_escape( self.get_long_description() )
995 pkg_info.write('Description: %s\n' % long_desc)
996
997 keywords = string.join( self.get_keywords(), ',')
998 if keywords:
999 pkg_info.write('Keywords: %s\n' % keywords )
1000
1001 for platform in self.get_platforms():
1002 pkg_info.write('Platform: %s\n' % platform )
1003
1004 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001005
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001006 # write_pkg_info ()
Fred Drakeb94b8492001-12-06 20:51:35 +00001007
Greg Ward82715e12000-04-21 02:28:14 +00001008 # -- Metadata query methods ----------------------------------------
1009
Greg Wardfe6462c2000-04-04 01:40:52 +00001010 def get_name (self):
1011 return self.name or "UNKNOWN"
1012
Greg Ward82715e12000-04-21 02:28:14 +00001013 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001014 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001015
Greg Ward82715e12000-04-21 02:28:14 +00001016 def get_fullname (self):
1017 return "%s-%s" % (self.get_name(), self.get_version())
1018
1019 def get_author(self):
1020 return self.author or "UNKNOWN"
1021
1022 def get_author_email(self):
1023 return self.author_email or "UNKNOWN"
1024
1025 def get_maintainer(self):
1026 return self.maintainer or "UNKNOWN"
1027
1028 def get_maintainer_email(self):
1029 return self.maintainer_email or "UNKNOWN"
1030
1031 def get_contact(self):
1032 return (self.maintainer or
1033 self.author or
1034 "UNKNOWN")
1035
1036 def get_contact_email(self):
1037 return (self.maintainer_email or
1038 self.author_email or
1039 "UNKNOWN")
1040
1041 def get_url(self):
1042 return self.url or "UNKNOWN"
1043
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001044 def get_license(self):
1045 return self.license or "UNKNOWN"
1046 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001047
Greg Ward82715e12000-04-21 02:28:14 +00001048 def get_description(self):
1049 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001050
1051 def get_long_description(self):
1052 return self.long_description or "UNKNOWN"
1053
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001054 def get_keywords(self):
1055 return self.keywords or []
1056
1057 def get_platforms(self):
1058 return self.platforms or ["UNKNOWN"]
1059
Greg Ward82715e12000-04-21 02:28:14 +00001060# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001061
Greg Ward2ff78872000-06-24 00:23:20 +00001062
1063def fix_help_options (options):
1064 """Convert a 4-tuple 'help_options' list as found in various command
1065 classes to the 3-tuple form required by FancyGetopt.
1066 """
1067 new_options = []
1068 for help_tuple in options:
1069 new_options.append(help_tuple[0:3])
1070 return new_options
1071
1072
Greg Wardfe6462c2000-04-04 01:40:52 +00001073if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001074 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001075 print "ok"