blob: 3fd29d9f48b3d3b3accf65316bb4b2492c332399 [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.dist
2
3Provides the Distribution class, which represents the module distribution
4being built/installed/distributed."""
5
6# created 2000/04/03, Greg Ward
7# (extricated from core.py; actually dates back to the beginning)
8
9__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
14from distutils.errors import *
Greg Ward36c36fe2000-05-20 14:07:59 +000015from distutils import sysconfig
Greg Ward82715e12000-04-21 02:28:14 +000016from distutils.fancy_getopt import FancyGetopt, longopt_xlate
Gregory P. Smith14263542000-05-12 00:41:33 +000017from distutils.util import check_environ
Greg Wardfe6462c2000-04-04 01:40:52 +000018
19
20# Regex to define acceptable Distutils command names. This is not *quite*
21# the same as a Python NAME -- I don't allow leading underscores. The fact
22# that they're very similar is no coincidence; the default naming scheme is
23# to look for a Python module named after the command.
24command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
25
26
27class Distribution:
28 """The core of the Distutils. Most of the work hiding behind
29 'setup' is really done within a Distribution instance, which
30 farms the work out to the Distutils commands specified on the
31 command line.
32
33 Clients will almost never instantiate Distribution directly,
34 unless the 'setup' function is totally inadequate to their needs.
35 However, it is conceivable that a client might wish to subclass
36 Distribution for some specialized purpose, and then pass the
37 subclass to 'setup' as the 'distclass' keyword argument. If so,
38 it is necessary to respect the expectations that 'setup' has of
39 Distribution: it must have a constructor and methods
40 'parse_command_line()' and 'run_commands()' with signatures like
41 those described below."""
42
43
44 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000045 # supplied to the setup script prior to any actual commands.
46 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000047 # these global options. This list should be kept to a bare minimum,
48 # since every global option is also valid as a command option -- and we
49 # don't want to pollute the commands with too many options that they
50 # have minimal control over.
Greg Wardd5d8a992000-05-23 01:42:17 +000051 global_options = [('verbose', 'v', "run verbosely (default)"),
52 ('quiet', 'q', "run quietly (turns verbosity off)"),
53 ('dry-run', 'n', "don't actually do anything"),
54 ('help', 'h', "show detailed help message"),
Greg Wardfe6462c2000-04-04 01:40:52 +000055 ]
Greg Ward82715e12000-04-21 02:28:14 +000056
57 # options that are not propagated to the commands
58 display_options = [
59 ('help-commands', None,
60 "list all available commands"),
61 ('name', None,
62 "print package name"),
63 ('version', 'V',
64 "print package version"),
65 ('fullname', None,
66 "print <package name>-<version>"),
67 ('author', None,
68 "print the author's name"),
69 ('author-email', None,
70 "print the author's email address"),
71 ('maintainer', None,
72 "print the maintainer's name"),
73 ('maintainer-email', None,
74 "print the maintainer's email address"),
75 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000076 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000077 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000078 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000079 ('url', None,
80 "print the URL for this package"),
81 ('licence', None,
82 "print the licence of the package"),
83 ('license', None,
84 "alias for --licence"),
85 ('description', None,
86 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +000087 ('long-description', None,
88 "print the long package description"),
Greg Ward82715e12000-04-21 02:28:14 +000089 ]
90 display_option_names = map(lambda x: string.translate(x[0], longopt_xlate),
91 display_options)
92
93 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +000094 negative_opt = {'quiet': 'verbose'}
95
96
97 # -- Creation/initialization methods -------------------------------
98
99 def __init__ (self, attrs=None):
100 """Construct a new Distribution instance: initialize all the
101 attributes of a Distribution, and then uses 'attrs' (a
102 dictionary mapping attribute names to values) to assign
103 some of those attributes their "real" values. (Any attributes
104 not mentioned in 'attrs' will be assigned to some null
105 value: 0, None, an empty list or dictionary, etc.) Most
106 importantly, initialize the 'command_obj' attribute
107 to the empty dictionary; this will be filled in with real
108 command objects by 'parse_command_line()'."""
109
110 # Default values for our command-line options
111 self.verbose = 1
112 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000113 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000114 for attr in self.display_option_names:
115 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000116
Greg Ward82715e12000-04-21 02:28:14 +0000117 # Store the distribution meta-data (name, version, author, and so
118 # forth) in a separate object -- we're getting to have enough
119 # information here (and enough command-line options) that it's
120 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
121 # object in a sneaky and underhanded (but efficient!) way.
122 self.metadata = DistributionMetadata ()
Greg Ward4982f982000-04-22 02:52:44 +0000123 method_basenames = dir(self.metadata) + \
124 ['fullname', 'contact', 'contact_email']
125 for basename in method_basenames:
126 method_name = "get_" + basename
127 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000128
129 # 'cmdclass' maps command names to class objects, so we
130 # can 1) quickly figure out which class to instantiate when
131 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000132 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000133 self.cmdclass = {}
134
Greg Wardd5d8a992000-05-23 01:42:17 +0000135 # 'command_options' is where we store command options between
136 # parsing them (from config files, the command-line, etc.) and when
137 # they are actually needed -- ie. when the command in question is
138 # instantiated. It is a dictionary of dictionaries of 2-tuples:
139 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000140 self.command_options = {}
141
Greg Wardfe6462c2000-04-04 01:40:52 +0000142 # These options are really the business of various commands, rather
143 # than of the Distribution itself. We provide aliases for them in
144 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000145 self.packages = None
146 self.package_dir = None
147 self.py_modules = None
148 self.libraries = None
149 self.ext_modules = None
150 self.ext_package = None
151 self.include_dirs = None
152 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000153 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000154 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000155
156 # And now initialize bookkeeping stuff that can't be supplied by
157 # the caller at all. 'command_obj' maps command names to
158 # Command instances -- that's how we enforce that every command
159 # class is a singleton.
160 self.command_obj = {}
161
162 # 'have_run' maps command names to boolean values; it keeps track
163 # of whether we have actually run a particular command, to make it
164 # cheap to "run" a command whenever we think we might need to -- if
165 # it's already been done, no need for expensive filesystem
166 # operations, we just check the 'have_run' dictionary and carry on.
167 # It's only safe to query 'have_run' for a command class that has
168 # been instantiated -- a false value will be inserted when the
169 # command object is created, and replaced with a true value when
170 # the command is succesfully run. Thus it's probably best to use
171 # '.get()' rather than a straight lookup.
172 self.have_run = {}
173
174 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000175 # the setup script) to possibly override any or all of these
176 # distribution options.
177
Greg Wardfe6462c2000-04-04 01:40:52 +0000178 if attrs:
179
180 # Pull out the set of command options and work on them
181 # specifically. Note that this order guarantees that aliased
182 # command options will override any supplied redundantly
183 # through the general options dictionary.
184 options = attrs.get ('options')
185 if options:
186 del attrs['options']
187 for (command, cmd_options) in options.items():
Greg Wardd5d8a992000-05-23 01:42:17 +0000188 cmd_obj = self.get_command_obj (command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000189 for (key, val) in cmd_options.items():
190 cmd_obj.set_option (key, val)
191 # loop over commands
192 # if any command options
193
194 # Now work on the rest of the attributes. Any attribute that's
195 # not already defined is invalid!
196 for (key,val) in attrs.items():
Greg Ward82715e12000-04-21 02:28:14 +0000197 if hasattr (self.metadata, key):
198 setattr (self.metadata, key, val)
199 elif hasattr (self, key):
Greg Wardfe6462c2000-04-04 01:40:52 +0000200 setattr (self, key, val)
201 else:
Greg Ward02a1a2b2000-04-15 22:15:07 +0000202 raise DistutilsSetupError, \
Greg Wardfe6462c2000-04-04 01:40:52 +0000203 "invalid distribution option '%s'" % key
204
205 # __init__ ()
206
207
Greg Wardd5d8a992000-05-23 01:42:17 +0000208 # -- Config file finding/parsing methods ---------------------------
209
Gregory P. Smith14263542000-05-12 00:41:33 +0000210 def find_config_files (self):
211 """Find as many configuration files as should be processed for this
212 platform, and return a list of filenames in the order in which they
213 should be parsed. The filenames returned are guaranteed to exist
214 (modulo nasty race conditions).
215
216 On Unix, there are three possible config files: pydistutils.cfg in
217 the Distutils installation directory (ie. where the top-level
218 Distutils __inst__.py file lives), .pydistutils.cfg in the user's
219 home directory, and setup.cfg in the current directory.
220
221 On Windows and Mac OS, there are two possible config files:
222 pydistutils.cfg in the Python installation directory (sys.prefix)
Greg Wardd5d8a992000-05-23 01:42:17 +0000223 and setup.cfg in the current directory.
224 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000225 files = []
226 if os.name == "posix":
227 check_environ()
228
229 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
230 sys_file = os.path.join(sys_dir, "pydistutils.cfg")
231 if os.path.isfile(sys_file):
232 files.append(sys_file)
233
234 user_file = os.path.join(os.environ.get('HOME'),
235 ".pydistutils.cfg")
236 if os.path.isfile(user_file):
237 files.append(user_file)
238
239 else:
240 sys_file = os.path.join (sysconfig.PREFIX, "pydistutils.cfg")
241 if os.path.isfile(sys_file):
242 files.append(sys_file)
243
244 # All platforms support local setup.cfg
245 local_file = "setup.cfg"
246 if os.path.isfile(local_file):
247 files.append(local_file)
248
249 return files
250
251 # find_config_files ()
252
253
254 def parse_config_files (self, filenames=None):
255
256 from ConfigParser import ConfigParser
257
258 if filenames is None:
259 filenames = self.find_config_files()
260
261 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000262 for filename in filenames:
263 parser.read(filename)
264 for section in parser.sections():
265 options = parser.options(section)
266 if not self.command_options.has_key(section):
267 self.command_options[section] = {}
268 opts = self.command_options[section]
Gregory P. Smith14263542000-05-12 00:41:33 +0000269
Greg Wardd5d8a992000-05-23 01:42:17 +0000270 for opt in options:
271 if opt != '__name__':
272 opts[opt] = (filename, parser.get(section,opt))
Gregory P. Smith14263542000-05-12 00:41:33 +0000273
274 from pprint import pprint
Greg Wardd5d8a992000-05-23 01:42:17 +0000275 print "options (after parsing config files):"
Gregory P. Smith14263542000-05-12 00:41:33 +0000276 pprint (self.command_options)
277
278
Greg Wardd5d8a992000-05-23 01:42:17 +0000279 # -- Command-line parsing methods ----------------------------------
280
Greg Wardfe6462c2000-04-04 01:40:52 +0000281 def parse_command_line (self, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000282 """Parse the setup script's command line. 'args' must be a list
283 of command-line arguments, most likely 'sys.argv[1:]' (see the
284 'setup()' function). This list is first processed for "global
285 options" -- options that set attributes of the Distribution
286 instance. Then, it is alternately scanned for Distutils
287 commands and options for that command. Each new command
288 terminates the options for the previous command. The allowed
289 options for a command are determined by the 'user_options'
290 attribute of the command class -- thus, we have to be able to
291 load command classes in order to parse the command line. Any
292 error in that 'options' attribute raises DistutilsGetoptError;
293 any error on the command-line raises DistutilsArgError. If no
294 Distutils commands were found on the command line, raises
295 DistutilsArgError. Return true if command-line were
296 successfully parsed and we should carry on with executing
297 commands; false if no errors but we shouldn't execute commands
298 (currently, this only happens if user asks for help).
299 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000300 # We have to parse the command line a bit at a time -- global
301 # options, then the first command, then its options, and so on --
302 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000303 # the options that are valid for a particular class aren't known
304 # until we have loaded the command class, which doesn't happen
305 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000306
307 self.commands = []
Greg Ward82715e12000-04-21 02:28:14 +0000308 parser = FancyGetopt (self.global_options + self.display_options)
309 parser.set_negative_aliases (self.negative_opt)
Greg Ward58ec6ed2000-04-21 04:22:49 +0000310 parser.set_aliases ({'license': 'licence'})
Greg Ward82715e12000-04-21 02:28:14 +0000311 args = parser.getopt (object=self)
312 option_order = parser.get_option_order()
Greg Wardfe6462c2000-04-04 01:40:52 +0000313
Greg Ward82715e12000-04-21 02:28:14 +0000314 # for display options we return immediately
315 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000316 return
317
318 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000319 args = self._parse_command_opts(parser, args)
320 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000321 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000322
Greg Wardd5d8a992000-05-23 01:42:17 +0000323 # Handle the cases of --help as a "global" option, ie.
324 # "setup.py --help" and "setup.py --help command ...". For the
325 # former, we show global options (--verbose, --dry-run, etc.)
326 # and display-only options (--name, --version, etc.); for the
327 # latter, we omit the display-only options and show help for
328 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000329 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000330 print "showing 'global' help; commands=", self.commands
331 self._show_help(parser,
332 display_options=len(self.commands) == 0,
333 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000334 return
335
336 # Oops, no commands found -- an end-user error
337 if not self.commands:
338 raise DistutilsArgError, "no commands supplied"
339
340 # All is well: return true
341 return 1
342
343 # parse_command_line()
344
Greg Wardd5d8a992000-05-23 01:42:17 +0000345 def _parse_command_opts (self, parser, args):
346
347 """Parse the command-line options for a single command.
348 'parser' must be a FancyGetopt instance; 'args' must be the list
349 of arguments, starting with the current command (whose options
350 we are about to parse). Returns a new version of 'args' with
351 the next command at the front of the list; will be the empty
352 list if there are no more commands on the command line. Returns
353 None if the user asked for help on this command.
354 """
355 # late import because of mutual dependence between these modules
356 from distutils.cmd import Command
357
358 # Pull the current command from the head of the command line
359 command = args[0]
360 if not command_re.match (command):
361 raise SystemExit, "invalid command name '%s'" % command
362 self.commands.append (command)
363
364 # Dig up the command class that implements this command, so we
365 # 1) know that it's a valid command, and 2) know which options
366 # it takes.
367 try:
368 cmd_class = self.get_command_class (command)
369 except DistutilsModuleError, msg:
370 raise DistutilsArgError, msg
371
372 # Require that the command class be derived from Command -- want
373 # to be sure that the basic "command" interface is implemented.
374 if not issubclass (cmd_class, Command):
375 raise DistutilsClassError, \
376 "command class %s must subclass Command" % cmd_class
377
378 # Also make sure that the command object provides a list of its
379 # known options.
380 if not (hasattr (cmd_class, 'user_options') and
381 type (cmd_class.user_options) is ListType):
382 raise DistutilsClassError, \
383 ("command class %s must provide " +
384 "'user_options' attribute (a list of tuples)") % \
385 cmd_class
386
387 # If the command class has a list of negative alias options,
388 # merge it in with the global negative aliases.
389 negative_opt = self.negative_opt
390 if hasattr (cmd_class, 'negative_opt'):
391 negative_opt = copy (negative_opt)
392 negative_opt.update (cmd_class.negative_opt)
393
394 # All commands support the global options too, just by adding
395 # in 'global_options'.
396 parser.set_option_table (self.global_options +
397 cmd_class.user_options)
398 parser.set_negative_aliases (negative_opt)
399 (args, opts) = parser.getopt (args[1:])
400 if opts.help:
401 print "showing help for command", cmd_class
402 self._show_help(parser, display_options=0, commands=[cmd_class])
403 return
404
405 # Put the options from the command-line into their official
406 # holding pen, the 'command_options' dictionary.
407 if not self.command_options.has_key(command):
408 self.command_options[command] = {}
409 cmd_opts = self.command_options[command]
410 for (name, value) in vars(opts).items():
411 cmd_opts[command] = ("command line", value)
412
413 return args
414
415 # _parse_command_opts ()
416
417
418 def _show_help (self,
419 parser,
420 global_options=1,
421 display_options=1,
422 commands=[]):
423 """Show help for the setup script command-line in the form of
424 several lists of command-line options. 'parser' should be a
425 FancyGetopt instance; do not expect it to be returned in the
426 same state, as its option table will be reset to make it
427 generate the correct help text.
428
429 If 'global_options' is true, lists the global options:
430 --verbose, --dry-run, etc. If 'display_options' is true, lists
431 the "display-only" options: --name, --version, etc. Finally,
432 lists per-command help for every command name or command class
433 in 'commands'.
434 """
435 # late import because of mutual dependence between these modules
436 from distutils.core import usage
437 from distutils.cmd import Command
438
439 if global_options:
440 parser.set_option_table (self.global_options)
441 parser.print_help ("Global options:")
442 print
443
444 if display_options:
445 parser.set_option_table (self.display_options)
446 parser.print_help (
447 "Information display options (just display " +
448 "information, ignore any commands)")
449 print
450
451 for command in self.commands:
452 if type(command) is ClassType and issubclass(klass, Command):
453 klass = command
454 else:
455 klass = self.get_command_class (command)
456 parser.set_option_table (klass.user_options)
457 parser.print_help ("Options for '%s' command:" % klass.__name__)
458 print
459
460 print usage
461 return
462
463 # _show_help ()
464
465
Greg Ward82715e12000-04-21 02:28:14 +0000466 def handle_display_options (self, option_order):
467 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000468 (--help-commands or the metadata display options) on the command
469 line, display the requested info and return true; else return
470 false.
471 """
Greg Ward82715e12000-04-21 02:28:14 +0000472 from distutils.core import usage
473
474 # User just wants a list of commands -- we'll print it out and stop
475 # processing now (ie. if they ran "setup --help-commands foo bar",
476 # we ignore "foo bar").
477 if self.help_commands:
478 self.print_commands ()
479 print
480 print usage
481 return 1
482
483 # If user supplied any of the "display metadata" options, then
484 # display that metadata in the order in which the user supplied the
485 # metadata options.
486 any_display_options = 0
487 is_display_option = {}
488 for option in self.display_options:
489 is_display_option[option[0]] = 1
490
491 for (opt, val) in option_order:
492 if val and is_display_option.get(opt):
493 opt = string.translate (opt, longopt_xlate)
494 print getattr(self.metadata, "get_"+opt)()
495 any_display_options = 1
496
497 return any_display_options
498
499 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000500
501 def print_command_list (self, commands, header, max_length):
502 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000503 'print_commands()'.
504 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000505
506 print header + ":"
507
508 for cmd in commands:
509 klass = self.cmdclass.get (cmd)
510 if not klass:
Greg Wardd5d8a992000-05-23 01:42:17 +0000511 klass = self.get_command_class (cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000512 try:
513 description = klass.description
514 except AttributeError:
515 description = "(no description available)"
516
517 print " %-*s %s" % (max_length, cmd, description)
518
519 # print_command_list ()
520
521
522 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000523 """Print out a help message listing all available commands with a
524 description of each. The list is divided into "standard commands"
525 (listed in distutils.command.__all__) and "extra commands"
526 (mentioned in self.cmdclass, but not a standard command). The
527 descriptions come from the command class attribute
528 'description'.
529 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000530
531 import distutils.command
532 std_commands = distutils.command.__all__
533 is_std = {}
534 for cmd in std_commands:
535 is_std[cmd] = 1
536
537 extra_commands = []
538 for cmd in self.cmdclass.keys():
539 if not is_std.get(cmd):
540 extra_commands.append (cmd)
541
542 max_length = 0
543 for cmd in (std_commands + extra_commands):
544 if len (cmd) > max_length:
545 max_length = len (cmd)
546
547 self.print_command_list (std_commands,
548 "Standard commands",
549 max_length)
550 if extra_commands:
551 print
552 self.print_command_list (extra_commands,
553 "Extra commands",
554 max_length)
555
556 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000557
558
559 # -- Command class/object methods ----------------------------------
560
Greg Wardd5d8a992000-05-23 01:42:17 +0000561 def get_command_class (self, command):
562 """Return the class that implements the Distutils command named by
563 'command'. First we check the 'cmdclass' dictionary; if the
564 command is mentioned there, we fetch the class object from the
565 dictionary and return it. Otherwise we load the command module
566 ("distutils.command." + command) and fetch the command class from
567 the module. The loaded class is also stored in 'cmdclass'
568 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000569
Gregory P. Smith14263542000-05-12 00:41:33 +0000570 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000571 found, or if that module does not define the expected class.
572 """
573 klass = self.cmdclass.get(command)
574 if klass:
575 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000576
577 module_name = 'distutils.command.' + command
578 klass_name = command
579
580 try:
581 __import__ (module_name)
582 module = sys.modules[module_name]
583 except ImportError:
584 raise DistutilsModuleError, \
585 "invalid command '%s' (no module named '%s')" % \
586 (command, module_name)
587
588 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000589 klass = getattr(module, klass_name)
590 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000591 raise DistutilsModuleError, \
592 "invalid command '%s' (no class '%s' in module '%s')" \
593 % (command, klass_name, module_name)
594
Greg Wardd5d8a992000-05-23 01:42:17 +0000595 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000596 return klass
597
Greg Wardd5d8a992000-05-23 01:42:17 +0000598 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000599
Greg Wardd5d8a992000-05-23 01:42:17 +0000600 def get_command_obj (self, command, create=1):
601 """Return the command object for 'command'. Normally this object
602 is cached on a previous call to 'get_command_obj()'; if no comand
603 object for 'command' is in the cache, then we either create and
604 return it (if 'create' is true) or return None.
605 """
606 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000607 if not cmd_obj and create:
Greg Wardd5d8a992000-05-23 01:42:17 +0000608 klass = self.get_command_class(command)
609 cmd_obj = self.command_obj[command] = klass()
610 self.command_run[command] = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000611
612 return cmd_obj
613
614
615 # -- Methods that operate on the Distribution ----------------------
616
617 def announce (self, msg, level=1):
618 """Print 'msg' if 'level' is greater than or equal to the verbosity
Greg Wardd5d8a992000-05-23 01:42:17 +0000619 level recorded in the 'verbose' attribute (which, currently, can be
620 only 0 or 1).
621 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000622 if self.verbose >= level:
623 print msg
624
625
626 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000627 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000628 Uses the list of commands found and cache of command objects
629 created by 'get_command_obj()'."""
Greg Wardfe6462c2000-04-04 01:40:52 +0000630
631 for cmd in self.commands:
632 self.run_command (cmd)
633
634
Greg Wardfe6462c2000-04-04 01:40:52 +0000635 # -- Methods that operate on its Commands --------------------------
636
637 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000638 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000639 if the command has already been run). Specifically: if we have
640 already created and run the command named by 'command', return
641 silently without doing anything. If the command named by 'command'
642 doesn't even have a command object yet, create one. Then invoke
643 'run()' on that command object (or an existing one).
644 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000645
646 # Already been here, done that? then return silently.
647 if self.have_run.get (command):
648 return
649
650 self.announce ("running " + command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000651 cmd_obj = self.get_command_obj (command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000652 cmd_obj.ensure_ready ()
653 cmd_obj.run ()
654 self.have_run[command] = 1
655
656
Greg Wardfe6462c2000-04-04 01:40:52 +0000657 # -- Distribution query methods ------------------------------------
658
659 def has_pure_modules (self):
660 return len (self.packages or self.py_modules or []) > 0
661
662 def has_ext_modules (self):
663 return self.ext_modules and len (self.ext_modules) > 0
664
665 def has_c_libraries (self):
666 return self.libraries and len (self.libraries) > 0
667
668 def has_modules (self):
669 return self.has_pure_modules() or self.has_ext_modules()
670
Greg Ward44a61bb2000-05-20 15:06:48 +0000671 def has_scripts (self):
672 return self.scripts and len(self.scripts) > 0
673
674 def has_data_files (self):
675 return self.data_files and len(self.data_files) > 0
676
Greg Wardfe6462c2000-04-04 01:40:52 +0000677 def is_pure (self):
678 return (self.has_pure_modules() and
679 not self.has_ext_modules() and
680 not self.has_c_libraries())
681
Greg Ward82715e12000-04-21 02:28:14 +0000682 # -- Metadata query methods ----------------------------------------
683
684 # If you're looking for 'get_name()', 'get_version()', and so forth,
685 # they are defined in a sneaky way: the constructor binds self.get_XXX
686 # to self.metadata.get_XXX. The actual code is in the
687 # DistributionMetadata class, below.
688
689# class Distribution
690
691
692class DistributionMetadata:
693 """Dummy class to hold the distribution meta-data: name, version,
694 author, and so forth."""
695
696 def __init__ (self):
697 self.name = None
698 self.version = None
699 self.author = None
700 self.author_email = None
701 self.maintainer = None
702 self.maintainer_email = None
703 self.url = None
704 self.licence = None
705 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000706 self.long_description = None
Greg Ward82715e12000-04-21 02:28:14 +0000707
708 # -- Metadata query methods ----------------------------------------
709
Greg Wardfe6462c2000-04-04 01:40:52 +0000710 def get_name (self):
711 return self.name or "UNKNOWN"
712
Greg Ward82715e12000-04-21 02:28:14 +0000713 def get_version(self):
714 return self.version or "???"
Greg Wardfe6462c2000-04-04 01:40:52 +0000715
Greg Ward82715e12000-04-21 02:28:14 +0000716 def get_fullname (self):
717 return "%s-%s" % (self.get_name(), self.get_version())
718
719 def get_author(self):
720 return self.author or "UNKNOWN"
721
722 def get_author_email(self):
723 return self.author_email or "UNKNOWN"
724
725 def get_maintainer(self):
726 return self.maintainer or "UNKNOWN"
727
728 def get_maintainer_email(self):
729 return self.maintainer_email or "UNKNOWN"
730
731 def get_contact(self):
732 return (self.maintainer or
733 self.author or
734 "UNKNOWN")
735
736 def get_contact_email(self):
737 return (self.maintainer_email or
738 self.author_email or
739 "UNKNOWN")
740
741 def get_url(self):
742 return self.url or "UNKNOWN"
743
744 def get_licence(self):
745 return self.licence or "UNKNOWN"
746
747 def get_description(self):
748 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +0000749
750 def get_long_description(self):
751 return self.long_description or "UNKNOWN"
752
Greg Ward82715e12000-04-21 02:28:14 +0000753# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +0000754
755if __name__ == "__main__":
756 dist = Distribution ()
757 print "ok"