blob: faeb7b10b3a91ad7fea7a954f44d6d76c33bd047 [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.dist
2
3Provides the Distribution class, which represents the module distribution
Greg Ward8ff5a3f2000-06-02 00:44:53 +00004being built/installed/distributed.
5"""
Greg Wardfe6462c2000-04-04 01:40:52 +00006
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00007# This module should be kept compatible with Python 1.5.2.
8
Greg Wardfe6462c2000-04-04 01:40:52 +00009__revision__ = "$Id$"
10
Gregory P. Smith14263542000-05-12 00:41:33 +000011import sys, os, string, re
Greg Wardfe6462c2000-04-04 01:40:52 +000012from types import *
13from copy import copy
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000014
15try:
16 import warnings
Andrew M. Kuchlingccf4e422002-10-31 13:39:33 +000017except ImportError:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000018 warnings = None
19
Greg Wardfe6462c2000-04-04 01:40:52 +000020from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000021from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000022from distutils.util import check_environ, strtobool, rfc822_escape
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000023from distutils import log
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000024from distutils.debug import DEBUG
Greg Wardfe6462c2000-04-04 01:40:52 +000025
26# Regex to define acceptable Distutils command names. This is not *quite*
27# the same as a Python NAME -- I don't allow leading underscores. The fact
28# that they're very similar is no coincidence; the default naming scheme is
29# to look for a Python module named after the command.
30command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
31
32
33class Distribution:
Greg Ward8ff5a3f2000-06-02 00:44:53 +000034 """The core of the Distutils. Most of the work hiding behind 'setup'
35 is really done within a Distribution instance, which farms the work out
36 to the Distutils commands specified on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +000037
Greg Ward8ff5a3f2000-06-02 00:44:53 +000038 Setup scripts will almost never instantiate Distribution directly,
39 unless the 'setup()' function is totally inadequate to their needs.
40 However, it is conceivable that a setup script might wish to subclass
41 Distribution for some specialized purpose, and then pass the subclass
42 to 'setup()' as the 'distclass' keyword argument. If so, it is
43 necessary to respect the expectations that 'setup' has of Distribution.
44 See the code for 'setup()', in core.py, for details.
45 """
Greg Wardfe6462c2000-04-04 01:40:52 +000046
47
48 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000049 # supplied to the setup script prior to any actual commands.
50 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000051 # these global options. This list should be kept to a bare minimum,
52 # since every global option is also valid as a command option -- and we
53 # don't want to pollute the commands with too many options that they
54 # have minimal control over.
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000055 # The fourth entry for verbose means that it can be repeated.
56 global_options = [('verbose', 'v', "run verbosely (default)", 1),
Greg Wardd5d8a992000-05-23 01:42:17 +000057 ('quiet', 'q', "run quietly (turns verbosity off)"),
58 ('dry-run', 'n', "don't actually do anything"),
59 ('help', 'h', "show detailed help message"),
Greg Wardfe6462c2000-04-04 01:40:52 +000060 ]
Greg Ward82715e12000-04-21 02:28:14 +000061
62 # options that are not propagated to the commands
63 display_options = [
64 ('help-commands', None,
65 "list all available commands"),
66 ('name', None,
67 "print package name"),
68 ('version', 'V',
69 "print package version"),
70 ('fullname', None,
71 "print <package name>-<version>"),
72 ('author', None,
73 "print the author's name"),
74 ('author-email', None,
75 "print the author's email address"),
76 ('maintainer', None,
77 "print the maintainer's name"),
78 ('maintainer-email', None,
79 "print the maintainer's email address"),
80 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000081 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000082 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000083 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000084 ('url', None,
85 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +000086 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +000087 "print the license of the package"),
88 ('licence', None,
89 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +000090 ('description', None,
91 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +000092 ('long-description', None,
93 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000094 ('platforms', None,
95 "print the list of platforms"),
96 ('keywords', None,
97 "print the list of keywords"),
Greg Ward82715e12000-04-21 02:28:14 +000098 ]
Greg Ward2f2b6c62000-09-25 01:58:07 +000099 display_option_names = map(lambda x: translate_longopt(x[0]),
100 display_options)
Greg Ward82715e12000-04-21 02:28:14 +0000101
102 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +0000103 negative_opt = {'quiet': 'verbose'}
104
105
106 # -- Creation/initialization methods -------------------------------
Fred Drakeb94b8492001-12-06 20:51:35 +0000107
Greg Wardfe6462c2000-04-04 01:40:52 +0000108 def __init__ (self, attrs=None):
109 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000110 attributes of a Distribution, and then use 'attrs' (a dictionary
111 mapping attribute names to values) to assign some of those
112 attributes their "real" values. (Any attributes not mentioned in
113 'attrs' will be assigned to some null value: 0, None, an empty list
114 or dictionary, etc.) Most importantly, initialize the
115 'command_obj' attribute to the empty dictionary; this will be
116 filled in with real command objects by 'parse_command_line()'.
117 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000118
119 # Default values for our command-line options
120 self.verbose = 1
121 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000122 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000123 for attr in self.display_option_names:
124 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000125
Greg Ward82715e12000-04-21 02:28:14 +0000126 # Store the distribution meta-data (name, version, author, and so
127 # forth) in a separate object -- we're getting to have enough
128 # information here (and enough command-line options) that it's
129 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
130 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000131 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000132 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000133 method_name = "get_" + basename
134 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000135
136 # 'cmdclass' maps command names to class objects, so we
137 # can 1) quickly figure out which class to instantiate when
138 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000139 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000140 self.cmdclass = {}
141
Greg Ward9821bf42000-08-29 01:15:18 +0000142 # 'script_name' and 'script_args' are usually set to sys.argv[0]
143 # and sys.argv[1:], but they can be overridden when the caller is
144 # not necessarily a setup script run from the command-line.
145 self.script_name = None
146 self.script_args = None
147
Greg Wardd5d8a992000-05-23 01:42:17 +0000148 # 'command_options' is where we store command options between
149 # parsing them (from config files, the command-line, etc.) and when
150 # they are actually needed -- ie. when the command in question is
151 # instantiated. It is a dictionary of dictionaries of 2-tuples:
152 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000153 self.command_options = {}
154
Greg Wardfe6462c2000-04-04 01:40:52 +0000155 # These options are really the business of various commands, rather
156 # than of the Distribution itself. We provide aliases for them in
157 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000158 self.packages = None
159 self.package_dir = None
160 self.py_modules = None
161 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000162 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000163 self.ext_modules = None
164 self.ext_package = None
165 self.include_dirs = None
166 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000167 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000168 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000169
170 # And now initialize bookkeeping stuff that can't be supplied by
171 # the caller at all. 'command_obj' maps command names to
172 # Command instances -- that's how we enforce that every command
173 # class is a singleton.
174 self.command_obj = {}
175
176 # 'have_run' maps command names to boolean values; it keeps track
177 # of whether we have actually run a particular command, to make it
178 # cheap to "run" a command whenever we think we might need to -- if
179 # it's already been done, no need for expensive filesystem
180 # operations, we just check the 'have_run' dictionary and carry on.
181 # It's only safe to query 'have_run' for a command class that has
182 # been instantiated -- a false value will be inserted when the
183 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000184 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000185 # '.get()' rather than a straight lookup.
186 self.have_run = {}
187
188 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000189 # the setup script) to possibly override any or all of these
190 # distribution options.
191
Greg Wardfe6462c2000-04-04 01:40:52 +0000192 if attrs:
193
194 # Pull out the set of command options and work on them
195 # specifically. Note that this order guarantees that aliased
196 # command options will override any supplied redundantly
197 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000198 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000199 if options:
200 del attrs['options']
201 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000202 opt_dict = self.get_option_dict(command)
203 for (opt, val) in cmd_options.items():
204 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000205
206 # Now work on the rest of the attributes. Any attribute that's
207 # not already defined is invalid!
208 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000209 if hasattr(self.metadata, key):
210 setattr(self.metadata, key, val)
211 elif hasattr(self, key):
212 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000213 else:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000214 msg = "Unknown distribution option: %s" % repr(key)
215 if warnings is not None:
216 warnings.warn(msg)
217 else:
218 sys.stderr.write(msg + "\n")
Greg Wardfe6462c2000-04-04 01:40:52 +0000219
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000220 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000221
Greg Wardfe6462c2000-04-04 01:40:52 +0000222 # __init__ ()
223
224
Greg Ward0e48cfd2000-05-26 01:00:15 +0000225 def get_option_dict (self, command):
226 """Get the option dictionary for a given command. If that
227 command's option dictionary hasn't been created yet, then create it
228 and return the new dictionary; otherwise, return the existing
229 option dictionary.
230 """
231
232 dict = self.command_options.get(command)
233 if dict is None:
234 dict = self.command_options[command] = {}
235 return dict
236
237
Greg Wardc32d9a62000-05-28 23:53:06 +0000238 def dump_option_dicts (self, header=None, commands=None, indent=""):
239 from pprint import pformat
240
241 if commands is None: # dump all command option dicts
242 commands = self.command_options.keys()
243 commands.sort()
244
245 if header is not None:
246 print indent + header
247 indent = indent + " "
248
249 if not commands:
250 print indent + "no commands known yet"
251 return
252
253 for cmd_name in commands:
254 opt_dict = self.command_options.get(cmd_name)
255 if opt_dict is None:
256 print indent + "no option dict for '%s' command" % cmd_name
257 else:
258 print indent + "option dict for '%s' command:" % cmd_name
259 out = pformat(opt_dict)
260 for line in string.split(out, "\n"):
261 print indent + " " + line
262
263 # dump_option_dicts ()
Fred Drakeb94b8492001-12-06 20:51:35 +0000264
Greg Wardc32d9a62000-05-28 23:53:06 +0000265
266
Greg Wardd5d8a992000-05-23 01:42:17 +0000267 # -- Config file finding/parsing methods ---------------------------
268
Gregory P. Smith14263542000-05-12 00:41:33 +0000269 def find_config_files (self):
270 """Find as many configuration files as should be processed for this
271 platform, and return a list of filenames in the order in which they
272 should be parsed. The filenames returned are guaranteed to exist
273 (modulo nasty race conditions).
274
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000275 There are three possible config files: distutils.cfg in the
276 Distutils installation directory (ie. where the top-level
277 Distutils __inst__.py file lives), a file in the user's home
278 directory named .pydistutils.cfg on Unix and pydistutils.cfg
279 on Windows/Mac, and setup.cfg in the current directory.
Greg Wardd5d8a992000-05-23 01:42:17 +0000280 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000281 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000282 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000283
Greg Ward11696872000-06-07 02:29:03 +0000284 # Where to look for the system-wide Distutils config file
285 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
286
287 # Look for the system config file
288 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000289 if os.path.isfile(sys_file):
290 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000291
Greg Ward11696872000-06-07 02:29:03 +0000292 # What to call the per-user config file
293 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000294 user_filename = ".pydistutils.cfg"
295 else:
296 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000297
Greg Ward11696872000-06-07 02:29:03 +0000298 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000299 if os.environ.has_key('HOME'):
300 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000301 if os.path.isfile(user_file):
302 files.append(user_file)
303
Gregory P. Smith14263542000-05-12 00:41:33 +0000304 # All platforms support local setup.cfg
305 local_file = "setup.cfg"
306 if os.path.isfile(local_file):
307 files.append(local_file)
308
309 return files
310
311 # find_config_files ()
312
313
314 def parse_config_files (self, filenames=None):
315
316 from ConfigParser import ConfigParser
317
318 if filenames is None:
319 filenames = self.find_config_files()
320
Greg Ward2bd3f422000-06-02 01:59:33 +0000321 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000322
Gregory P. Smith14263542000-05-12 00:41:33 +0000323 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000324 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000325 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000326 parser.read(filename)
327 for section in parser.sections():
328 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000329 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000330
Greg Wardd5d8a992000-05-23 01:42:17 +0000331 for opt in options:
332 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000333 val = parser.get(section,opt)
334 opt = string.replace(opt, '-', '_')
335 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000336
Greg Ward47460772000-05-23 03:47:35 +0000337 # Make the ConfigParser forget everything (so we retain
338 # the original filenames that options come from) -- gag,
339 # retch, puke -- another good reason for a distutils-
340 # specific config parser (sigh...)
341 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000342
Greg Wardceb9e222000-09-25 01:23:52 +0000343 # If there was a "global" section in the config file, use it
344 # to set Distribution options.
345
346 if self.command_options.has_key('global'):
347 for (opt, (src, val)) in self.command_options['global'].items():
348 alias = self.negative_opt.get(opt)
349 try:
350 if alias:
351 setattr(self, alias, not strtobool(val))
352 elif opt in ('verbose', 'dry_run'): # ugh!
353 setattr(self, opt, strtobool(val))
354 except ValueError, msg:
355 raise DistutilsOptionError, msg
356
357 # parse_config_files ()
358
Gregory P. Smith14263542000-05-12 00:41:33 +0000359
Greg Wardd5d8a992000-05-23 01:42:17 +0000360 # -- Command-line parsing methods ----------------------------------
361
Greg Ward9821bf42000-08-29 01:15:18 +0000362 def parse_command_line (self):
363 """Parse the setup script's command line, taken from the
364 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
365 -- see 'setup()' in core.py). This list is first processed for
366 "global options" -- options that set attributes of the Distribution
367 instance. Then, it is alternately scanned for Distutils commands
368 and options for that command. Each new command terminates the
369 options for the previous command. The allowed options for a
370 command are determined by the 'user_options' attribute of the
371 command class -- thus, we have to be able to load command classes
372 in order to parse the command line. Any error in that 'options'
373 attribute raises DistutilsGetoptError; any error on the
374 command-line raises DistutilsArgError. If no Distutils commands
375 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000376 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000377 on with executing commands; false if no errors but we shouldn't
378 execute commands (currently, this only happens if user asks for
379 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000380 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000381 #
Fred Drake981a1782001-08-10 18:59:30 +0000382 # We now have enough information to show the Macintosh dialog
383 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000384 #
385 if sys.platform == 'mac':
386 import EasyDialogs
387 cmdlist = self.get_command_list()
388 self.script_args = EasyDialogs.GetArgv(
389 self.global_options + self.display_options, cmdlist)
Fred Drakeb94b8492001-12-06 20:51:35 +0000390
Greg Wardfe6462c2000-04-04 01:40:52 +0000391 # We have to parse the command line a bit at a time -- global
392 # options, then the first command, then its options, and so on --
393 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000394 # the options that are valid for a particular class aren't known
395 # until we have loaded the command class, which doesn't happen
396 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000397
398 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000399 parser = FancyGetopt(self.global_options + self.display_options)
400 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000401 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000402 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000403 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000404 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000405
Greg Ward82715e12000-04-21 02:28:14 +0000406 # for display options we return immediately
407 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000408 return
Fred Drakeb94b8492001-12-06 20:51:35 +0000409
Greg Wardfe6462c2000-04-04 01:40:52 +0000410 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000411 args = self._parse_command_opts(parser, args)
412 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000413 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000414
Greg Wardd5d8a992000-05-23 01:42:17 +0000415 # Handle the cases of --help as a "global" option, ie.
416 # "setup.py --help" and "setup.py --help command ...". For the
417 # former, we show global options (--verbose, --dry-run, etc.)
418 # and display-only options (--name, --version, etc.); for the
419 # latter, we omit the display-only options and show help for
420 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000421 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000422 self._show_help(parser,
423 display_options=len(self.commands) == 0,
424 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000425 return
426
427 # Oops, no commands found -- an end-user error
428 if not self.commands:
429 raise DistutilsArgError, "no commands supplied"
430
431 # All is well: return true
432 return 1
433
434 # parse_command_line()
435
Greg Wardd5d8a992000-05-23 01:42:17 +0000436 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000437 """Parse the command-line options for a single command.
438 'parser' must be a FancyGetopt instance; 'args' must be the list
439 of arguments, starting with the current command (whose options
440 we are about to parse). Returns a new version of 'args' with
441 the next command at the front of the list; will be the empty
442 list if there are no more commands on the command line. Returns
443 None if the user asked for help on this command.
444 """
445 # late import because of mutual dependence between these modules
446 from distutils.cmd import Command
447
448 # Pull the current command from the head of the command line
449 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000450 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000451 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000452 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000453
454 # Dig up the command class that implements this command, so we
455 # 1) know that it's a valid command, and 2) know which options
456 # it takes.
457 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000458 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000459 except DistutilsModuleError, msg:
460 raise DistutilsArgError, msg
461
462 # Require that the command class be derived from Command -- want
463 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000464 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000465 raise DistutilsClassError, \
466 "command class %s must subclass Command" % cmd_class
467
468 # Also make sure that the command object provides a list of its
469 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000470 if not (hasattr(cmd_class, 'user_options') and
471 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000472 raise DistutilsClassError, \
473 ("command class %s must provide " +
474 "'user_options' attribute (a list of tuples)") % \
475 cmd_class
476
477 # If the command class has a list of negative alias options,
478 # merge it in with the global negative aliases.
479 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000480 if hasattr(cmd_class, 'negative_opt'):
481 negative_opt = copy(negative_opt)
482 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000483
Greg Wardfa9ff762000-10-14 04:06:40 +0000484 # Check for help_options in command class. They have a different
485 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000486 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000487 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000488 help_options = fix_help_options(cmd_class.help_options)
489 else:
Greg Ward55fced32000-06-24 01:22:41 +0000490 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000491
Greg Ward9d17a7a2000-06-07 03:00:06 +0000492
Greg Wardd5d8a992000-05-23 01:42:17 +0000493 # All commands support the global options too, just by adding
494 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000495 parser.set_option_table(self.global_options +
496 cmd_class.user_options +
497 help_options)
498 parser.set_negative_aliases(negative_opt)
499 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000500 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000501 self._show_help(parser, display_options=0, commands=[cmd_class])
502 return
503
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000504 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000505 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000506 help_option_found=0
507 for (help_option, short, desc, func) in cmd_class.help_options:
508 if hasattr(opts, parser.get_attr_name(help_option)):
509 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000510 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000511 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000512
513 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000514 func()
Greg Ward55fced32000-06-24 01:22:41 +0000515 else:
Fred Drake981a1782001-08-10 18:59:30 +0000516 raise DistutilsClassError(
517 "invalid help function %s for help option '%s': "
518 "must be a callable object (function, etc.)"
519 % (`func`, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000520
Fred Drakeb94b8492001-12-06 20:51:35 +0000521 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000522 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000523
Greg Wardd5d8a992000-05-23 01:42:17 +0000524 # Put the options from the command-line into their official
525 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000526 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000527 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000528 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000529
530 return args
531
532 # _parse_command_opts ()
533
534
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000535 def finalize_options (self):
536 """Set final values for all the options on the Distribution
537 instance, analogous to the .finalize_options() method of Command
538 objects.
539 """
540
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000541 keywords = self.metadata.keywords
542 if keywords is not None:
543 if type(keywords) is StringType:
544 keywordlist = string.split(keywords, ',')
545 self.metadata.keywords = map(string.strip, keywordlist)
546
547 platforms = self.metadata.platforms
548 if platforms is not None:
549 if type(platforms) is StringType:
550 platformlist = string.split(platforms, ',')
551 self.metadata.platforms = map(string.strip, platformlist)
552
Greg Wardd5d8a992000-05-23 01:42:17 +0000553 def _show_help (self,
554 parser,
555 global_options=1,
556 display_options=1,
557 commands=[]):
558 """Show help for the setup script command-line in the form of
559 several lists of command-line options. 'parser' should be a
560 FancyGetopt instance; do not expect it to be returned in the
561 same state, as its option table will be reset to make it
562 generate the correct help text.
563
564 If 'global_options' is true, lists the global options:
565 --verbose, --dry-run, etc. If 'display_options' is true, lists
566 the "display-only" options: --name, --version, etc. Finally,
567 lists per-command help for every command name or command class
568 in 'commands'.
569 """
570 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000571 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000572 from distutils.cmd import Command
573
574 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000575 parser.set_option_table(self.global_options)
576 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000577 print
578
579 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000580 parser.set_option_table(self.display_options)
581 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000582 "Information display options (just display " +
583 "information, ignore any commands)")
584 print
585
586 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000587 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000588 klass = command
589 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000590 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000591 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000592 type(klass.help_options) is ListType):
593 parser.set_option_table(klass.user_options +
594 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000595 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000596 parser.set_option_table(klass.user_options)
597 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000598 print
599
Greg Ward9821bf42000-08-29 01:15:18 +0000600 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000601 return
602
603 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000604
Greg Wardd5d8a992000-05-23 01:42:17 +0000605
Greg Ward82715e12000-04-21 02:28:14 +0000606 def handle_display_options (self, option_order):
607 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000608 (--help-commands or the metadata display options) on the command
609 line, display the requested info and return true; else return
610 false.
611 """
Greg Ward9821bf42000-08-29 01:15:18 +0000612 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000613
614 # User just wants a list of commands -- we'll print it out and stop
615 # processing now (ie. if they ran "setup --help-commands foo bar",
616 # we ignore "foo bar").
617 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000618 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000619 print
Greg Ward9821bf42000-08-29 01:15:18 +0000620 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000621 return 1
622
623 # If user supplied any of the "display metadata" options, then
624 # display that metadata in the order in which the user supplied the
625 # metadata options.
626 any_display_options = 0
627 is_display_option = {}
628 for option in self.display_options:
629 is_display_option[option[0]] = 1
630
631 for (opt, val) in option_order:
632 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000633 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000634 value = getattr(self.metadata, "get_"+opt)()
635 if opt in ['keywords', 'platforms']:
636 print string.join(value, ',')
637 else:
638 print value
Greg Ward82715e12000-04-21 02:28:14 +0000639 any_display_options = 1
640
641 return any_display_options
642
643 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000644
645 def print_command_list (self, commands, header, max_length):
646 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000647 'print_commands()'.
648 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000649
650 print header + ":"
651
652 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000653 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000654 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000655 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000656 try:
657 description = klass.description
658 except AttributeError:
659 description = "(no description available)"
660
661 print " %-*s %s" % (max_length, cmd, description)
662
663 # print_command_list ()
664
665
666 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000667 """Print out a help message listing all available commands with a
668 description of each. The list is divided into "standard commands"
669 (listed in distutils.command.__all__) and "extra commands"
670 (mentioned in self.cmdclass, but not a standard command). The
671 descriptions come from the command class attribute
672 'description'.
673 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000674
675 import distutils.command
676 std_commands = distutils.command.__all__
677 is_std = {}
678 for cmd in std_commands:
679 is_std[cmd] = 1
680
681 extra_commands = []
682 for cmd in self.cmdclass.keys():
683 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000684 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000685
686 max_length = 0
687 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000688 if len(cmd) > max_length:
689 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000690
Greg Wardfd7b91e2000-09-26 01:52:25 +0000691 self.print_command_list(std_commands,
692 "Standard commands",
693 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000694 if extra_commands:
695 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000696 self.print_command_list(extra_commands,
697 "Extra commands",
698 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000699
700 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000701
Greg Wardf6fc8752000-11-11 02:47:11 +0000702 def get_command_list (self):
703 """Get a list of (command, description) tuples.
704 The list is divided into "standard commands" (listed in
705 distutils.command.__all__) and "extra commands" (mentioned in
706 self.cmdclass, but not a standard command). The descriptions come
707 from the command class attribute 'description'.
708 """
709 # Currently this is only used on Mac OS, for the Mac-only GUI
710 # Distutils interface (by Jack Jansen)
711
712 import distutils.command
713 std_commands = distutils.command.__all__
714 is_std = {}
715 for cmd in std_commands:
716 is_std[cmd] = 1
717
718 extra_commands = []
719 for cmd in self.cmdclass.keys():
720 if not is_std.get(cmd):
721 extra_commands.append(cmd)
722
723 rv = []
724 for cmd in (std_commands + extra_commands):
725 klass = self.cmdclass.get(cmd)
726 if not klass:
727 klass = self.get_command_class(cmd)
728 try:
729 description = klass.description
730 except AttributeError:
731 description = "(no description available)"
732 rv.append((cmd, description))
733 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000734
735 # -- Command class/object methods ----------------------------------
736
Greg Wardd5d8a992000-05-23 01:42:17 +0000737 def get_command_class (self, command):
738 """Return the class that implements the Distutils command named by
739 'command'. First we check the 'cmdclass' dictionary; if the
740 command is mentioned there, we fetch the class object from the
741 dictionary and return it. Otherwise we load the command module
742 ("distutils.command." + command) and fetch the command class from
743 the module. The loaded class is also stored in 'cmdclass'
744 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000745
Gregory P. Smith14263542000-05-12 00:41:33 +0000746 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000747 found, or if that module does not define the expected class.
748 """
749 klass = self.cmdclass.get(command)
750 if klass:
751 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000752
753 module_name = 'distutils.command.' + command
754 klass_name = command
755
756 try:
757 __import__ (module_name)
758 module = sys.modules[module_name]
759 except ImportError:
760 raise DistutilsModuleError, \
761 "invalid command '%s' (no module named '%s')" % \
762 (command, module_name)
763
764 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000765 klass = getattr(module, klass_name)
766 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000767 raise DistutilsModuleError, \
768 "invalid command '%s' (no class '%s' in module '%s')" \
769 % (command, klass_name, module_name)
770
Greg Wardd5d8a992000-05-23 01:42:17 +0000771 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000772 return klass
773
Greg Wardd5d8a992000-05-23 01:42:17 +0000774 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000775
Greg Wardd5d8a992000-05-23 01:42:17 +0000776 def get_command_obj (self, command, create=1):
777 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000778 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000779 object for 'command' is in the cache, then we either create and
780 return it (if 'create' is true) or return None.
781 """
782 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000783 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000784 if DEBUG:
785 print "Distribution.get_command_obj(): " \
786 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000787
Greg Wardd5d8a992000-05-23 01:42:17 +0000788 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000789 cmd_obj = self.command_obj[command] = klass(self)
790 self.have_run[command] = 0
791
792 # Set any options that were supplied in config files
793 # or on the command line. (NB. support for error
794 # reporting is lame here: any errors aren't reported
795 # until 'finalize_options()' is called, which means
796 # we won't report the source of the error.)
797 options = self.command_options.get(command)
798 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000799 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000800
801 return cmd_obj
802
Greg Wardc32d9a62000-05-28 23:53:06 +0000803 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000804 """Set the options for 'command_obj' from 'option_dict'. Basically
805 this means copying elements of a dictionary ('option_dict') to
806 attributes of an instance ('command').
807
Greg Wardceb9e222000-09-25 01:23:52 +0000808 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000809 supplied, uses the standard option dictionary for this command
810 (from 'self.command_options').
811 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000812 command_name = command_obj.get_command_name()
813 if option_dict is None:
814 option_dict = self.get_option_dict(command_name)
815
816 if DEBUG: print " setting options for '%s' command:" % command_name
817 for (option, (source, value)) in option_dict.items():
818 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000819 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000820 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000821 except AttributeError:
822 bool_opts = []
823 try:
824 neg_opt = command_obj.negative_opt
825 except AttributeError:
826 neg_opt = {}
827
828 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000829 is_string = type(value) is StringType
830 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000831 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000832 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000833 setattr(command_obj, option, strtobool(value))
834 elif hasattr(command_obj, option):
835 setattr(command_obj, option, value)
836 else:
837 raise DistutilsOptionError, \
838 ("error in %s: command '%s' has no such option '%s'"
839 % (source, command_name, option))
840 except ValueError, msg:
841 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000842
Greg Wardf449ea52000-09-16 15:23:28 +0000843 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000844 """Reinitializes a command to the state it was in when first
845 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000846 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000847 values in programmatically, overriding or supplementing
848 user-supplied values from the config files and command line.
849 You'll have to re-finalize the command object (by calling
850 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000851 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000852
Greg Wardf449ea52000-09-16 15:23:28 +0000853 'command' should be a command name (string) or command object. If
854 'reinit_subcommands' is true, also reinitializes the command's
855 sub-commands, as declared by the 'sub_commands' class attribute (if
856 it has one). See the "install" command for an example. Only
857 reinitializes the sub-commands that actually matter, ie. those
858 whose test predicates return true.
859
Greg Wardc32d9a62000-05-28 23:53:06 +0000860 Returns the reinitialized command object.
861 """
862 from distutils.cmd import Command
863 if not isinstance(command, Command):
864 command_name = command
865 command = self.get_command_obj(command_name)
866 else:
867 command_name = command.get_command_name()
868
869 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000870 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000871 command.initialize_options()
872 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000873 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000874 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000875
Greg Wardf449ea52000-09-16 15:23:28 +0000876 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000877 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000878 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000879
Greg Wardc32d9a62000-05-28 23:53:06 +0000880 return command
881
Fred Drakeb94b8492001-12-06 20:51:35 +0000882
Greg Wardfe6462c2000-04-04 01:40:52 +0000883 # -- Methods that operate on the Distribution ----------------------
884
885 def announce (self, msg, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000886 log.debug(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000887
888 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000889 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000890 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000891 created by 'get_command_obj()'.
892 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000893 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000894 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000895
896
Greg Wardfe6462c2000-04-04 01:40:52 +0000897 # -- Methods that operate on its Commands --------------------------
898
899 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000900 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000901 if the command has already been run). Specifically: if we have
902 already created and run the command named by 'command', return
903 silently without doing anything. If the command named by 'command'
904 doesn't even have a command object yet, create one. Then invoke
905 'run()' on that command object (or an existing one).
906 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000907 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000908 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000909 return
910
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000911 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000912 cmd_obj = self.get_command_obj(command)
913 cmd_obj.ensure_finalized()
914 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000915 self.have_run[command] = 1
916
917
Greg Wardfe6462c2000-04-04 01:40:52 +0000918 # -- Distribution query methods ------------------------------------
919
920 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000921 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000922
923 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000924 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000925
926 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000927 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000928
929 def has_modules (self):
930 return self.has_pure_modules() or self.has_ext_modules()
931
Greg Ward51def7d2000-05-27 01:36:14 +0000932 def has_headers (self):
933 return self.headers and len(self.headers) > 0
934
Greg Ward44a61bb2000-05-20 15:06:48 +0000935 def has_scripts (self):
936 return self.scripts and len(self.scripts) > 0
937
938 def has_data_files (self):
939 return self.data_files and len(self.data_files) > 0
940
Greg Wardfe6462c2000-04-04 01:40:52 +0000941 def is_pure (self):
942 return (self.has_pure_modules() and
943 not self.has_ext_modules() and
944 not self.has_c_libraries())
945
Greg Ward82715e12000-04-21 02:28:14 +0000946 # -- Metadata query methods ----------------------------------------
947
948 # If you're looking for 'get_name()', 'get_version()', and so forth,
949 # they are defined in a sneaky way: the constructor binds self.get_XXX
950 # to self.metadata.get_XXX. The actual code is in the
951 # DistributionMetadata class, below.
952
953# class Distribution
954
955
956class DistributionMetadata:
957 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000958 author, and so forth.
959 """
Greg Ward82715e12000-04-21 02:28:14 +0000960
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000961 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
962 "maintainer", "maintainer_email", "url",
963 "license", "description", "long_description",
964 "keywords", "platforms", "fullname", "contact",
965 "contact_email", "licence")
966
Greg Ward82715e12000-04-21 02:28:14 +0000967 def __init__ (self):
968 self.name = None
969 self.version = None
970 self.author = None
971 self.author_email = None
972 self.maintainer = None
973 self.maintainer_email = None
974 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000975 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000976 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000977 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000978 self.keywords = None
979 self.platforms = None
Fred Drakeb94b8492001-12-06 20:51:35 +0000980
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000981 def write_pkg_info (self, base_dir):
982 """Write the PKG-INFO file into the release tree.
983 """
984
985 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
986
987 pkg_info.write('Metadata-Version: 1.0\n')
988 pkg_info.write('Name: %s\n' % self.get_name() )
989 pkg_info.write('Version: %s\n' % self.get_version() )
990 pkg_info.write('Summary: %s\n' % self.get_description() )
991 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +0000992 pkg_info.write('Author: %s\n' % self.get_contact() )
993 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000994 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000995
996 long_desc = rfc822_escape( self.get_long_description() )
997 pkg_info.write('Description: %s\n' % long_desc)
998
999 keywords = string.join( self.get_keywords(), ',')
1000 if keywords:
1001 pkg_info.write('Keywords: %s\n' % keywords )
1002
1003 for platform in self.get_platforms():
1004 pkg_info.write('Platform: %s\n' % platform )
1005
1006 pkg_info.close()
Fred Drakeb94b8492001-12-06 20:51:35 +00001007
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001008 # write_pkg_info ()
Fred Drakeb94b8492001-12-06 20:51:35 +00001009
Greg Ward82715e12000-04-21 02:28:14 +00001010 # -- Metadata query methods ----------------------------------------
1011
Greg Wardfe6462c2000-04-04 01:40:52 +00001012 def get_name (self):
1013 return self.name or "UNKNOWN"
1014
Greg Ward82715e12000-04-21 02:28:14 +00001015 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001016 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001017
Greg Ward82715e12000-04-21 02:28:14 +00001018 def get_fullname (self):
1019 return "%s-%s" % (self.get_name(), self.get_version())
1020
1021 def get_author(self):
1022 return self.author or "UNKNOWN"
1023
1024 def get_author_email(self):
1025 return self.author_email or "UNKNOWN"
1026
1027 def get_maintainer(self):
1028 return self.maintainer or "UNKNOWN"
1029
1030 def get_maintainer_email(self):
1031 return self.maintainer_email or "UNKNOWN"
1032
1033 def get_contact(self):
1034 return (self.maintainer or
1035 self.author or
1036 "UNKNOWN")
1037
1038 def get_contact_email(self):
1039 return (self.maintainer_email or
1040 self.author_email or
1041 "UNKNOWN")
1042
1043 def get_url(self):
1044 return self.url or "UNKNOWN"
1045
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001046 def get_license(self):
1047 return self.license or "UNKNOWN"
1048 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001049
Greg Ward82715e12000-04-21 02:28:14 +00001050 def get_description(self):
1051 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001052
1053 def get_long_description(self):
1054 return self.long_description or "UNKNOWN"
1055
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001056 def get_keywords(self):
1057 return self.keywords or []
1058
1059 def get_platforms(self):
1060 return self.platforms or ["UNKNOWN"]
1061
Greg Ward82715e12000-04-21 02:28:14 +00001062# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001063
Greg Ward2ff78872000-06-24 00:23:20 +00001064
1065def fix_help_options (options):
1066 """Convert a 4-tuple 'help_options' list as found in various command
1067 classes to the 3-tuple form required by FancyGetopt.
1068 """
1069 new_options = []
1070 for help_tuple in options:
1071 new_options.append(help_tuple[0:3])
1072 return new_options
1073
1074
Greg Wardfe6462c2000-04-04 01:40:52 +00001075if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001076 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001077 print "ok"