blob: 40dcc96e27fd5742e2a642ba721e3c485cf7efee [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.dist
2
3Provides the Distribution class, which represents the module distribution
Greg Ward8ff5a3f2000-06-02 00:44:53 +00004being built/installed/distributed.
5"""
Greg Wardfe6462c2000-04-04 01:40:52 +00006
7# created 2000/04/03, Greg Ward
8# (extricated from core.py; actually dates back to the beginning)
9
10__revision__ = "$Id$"
11
Gregory P. Smith14263542000-05-12 00:41:33 +000012import sys, os, string, re
Greg Wardfe6462c2000-04-04 01:40:52 +000013from types import *
14from copy import copy
15from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000016from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000017from distutils.util import check_environ, strtobool, rfc822_escape
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:
Greg Ward8ff5a3f2000-06-02 00:44:53 +000028 """The core of the Distutils. Most of the work hiding behind 'setup'
29 is really done within a Distribution instance, which farms the work out
30 to the Distutils commands specified on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +000031
Greg Ward8ff5a3f2000-06-02 00:44:53 +000032 Setup scripts will almost never instantiate Distribution directly,
33 unless the 'setup()' function is totally inadequate to their needs.
34 However, it is conceivable that a setup script might wish to subclass
35 Distribution for some specialized purpose, and then pass the subclass
36 to 'setup()' as the 'distclass' keyword argument. If so, it is
37 necessary to respect the expectations that 'setup' has of Distribution.
38 See the code for 'setup()', in core.py, for details.
39 """
Greg Wardfe6462c2000-04-04 01:40:52 +000040
41
42 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000043 # supplied to the setup script prior to any actual commands.
44 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000045 # these global options. This list should be kept to a bare minimum,
46 # since every global option is also valid as a command option -- and we
47 # don't want to pollute the commands with too many options that they
48 # have minimal control over.
Greg Wardd5d8a992000-05-23 01:42:17 +000049 global_options = [('verbose', 'v', "run verbosely (default)"),
50 ('quiet', 'q', "run quietly (turns verbosity off)"),
51 ('dry-run', 'n', "don't actually do anything"),
52 ('help', 'h', "show detailed help message"),
Greg Wardfe6462c2000-04-04 01:40:52 +000053 ]
Greg Ward82715e12000-04-21 02:28:14 +000054
55 # options that are not propagated to the commands
56 display_options = [
57 ('help-commands', None,
58 "list all available commands"),
59 ('name', None,
60 "print package name"),
61 ('version', 'V',
62 "print package version"),
63 ('fullname', None,
64 "print <package name>-<version>"),
65 ('author', None,
66 "print the author's name"),
67 ('author-email', None,
68 "print the author's email address"),
69 ('maintainer', None,
70 "print the maintainer's name"),
71 ('maintainer-email', None,
72 "print the maintainer's email address"),
73 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000074 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000075 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +000076 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +000077 ('url', None,
78 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +000079 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +000080 "print the license of the package"),
81 ('licence', None,
82 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +000083 ('description', None,
84 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +000085 ('long-description', None,
86 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000087 ('platforms', None,
88 "print the list of platforms"),
89 ('keywords', None,
90 "print the list of keywords"),
Greg Ward82715e12000-04-21 02:28:14 +000091 ]
Greg Ward2f2b6c62000-09-25 01:58:07 +000092 display_option_names = map(lambda x: translate_longopt(x[0]),
93 display_options)
Greg Ward82715e12000-04-21 02:28:14 +000094
95 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +000096 negative_opt = {'quiet': 'verbose'}
97
98
99 # -- Creation/initialization methods -------------------------------
100
101 def __init__ (self, attrs=None):
102 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000103 attributes of a Distribution, and then use 'attrs' (a dictionary
104 mapping attribute names to values) to assign some of those
105 attributes their "real" values. (Any attributes not mentioned in
106 'attrs' will be assigned to some null value: 0, None, an empty list
107 or dictionary, etc.) Most importantly, initialize the
108 'command_obj' attribute to the empty dictionary; this will be
109 filled in with real command objects by 'parse_command_line()'.
110 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000111
112 # Default values for our command-line options
113 self.verbose = 1
114 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000115 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000116 for attr in self.display_option_names:
117 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000118
Greg Ward82715e12000-04-21 02:28:14 +0000119 # Store the distribution meta-data (name, version, author, and so
120 # forth) in a separate object -- we're getting to have enough
121 # information here (and enough command-line options) that it's
122 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
123 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000124 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000125 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000126 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 Ward9821bf42000-08-29 01:15:18 +0000135 # 'script_name' and 'script_args' are usually set to sys.argv[0]
136 # and sys.argv[1:], but they can be overridden when the caller is
137 # not necessarily a setup script run from the command-line.
138 self.script_name = None
139 self.script_args = None
140
Greg Wardd5d8a992000-05-23 01:42:17 +0000141 # 'command_options' is where we store command options between
142 # parsing them (from config files, the command-line, etc.) and when
143 # they are actually needed -- ie. when the command in question is
144 # instantiated. It is a dictionary of dictionaries of 2-tuples:
145 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000146 self.command_options = {}
147
Greg Wardfe6462c2000-04-04 01:40:52 +0000148 # These options are really the business of various commands, rather
149 # than of the Distribution itself. We provide aliases for them in
150 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000151 self.packages = None
152 self.package_dir = None
153 self.py_modules = None
154 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000155 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000156 self.ext_modules = None
157 self.ext_package = None
158 self.include_dirs = None
159 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000160 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000161 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000162
163 # And now initialize bookkeeping stuff that can't be supplied by
164 # the caller at all. 'command_obj' maps command names to
165 # Command instances -- that's how we enforce that every command
166 # class is a singleton.
167 self.command_obj = {}
168
169 # 'have_run' maps command names to boolean values; it keeps track
170 # of whether we have actually run a particular command, to make it
171 # cheap to "run" a command whenever we think we might need to -- if
172 # it's already been done, no need for expensive filesystem
173 # operations, we just check the 'have_run' dictionary and carry on.
174 # It's only safe to query 'have_run' for a command class that has
175 # been instantiated -- a false value will be inserted when the
176 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000177 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000178 # '.get()' rather than a straight lookup.
179 self.have_run = {}
180
181 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000182 # the setup script) to possibly override any or all of these
183 # distribution options.
184
Greg Wardfe6462c2000-04-04 01:40:52 +0000185 if attrs:
186
187 # Pull out the set of command options and work on them
188 # specifically. Note that this order guarantees that aliased
189 # command options will override any supplied redundantly
190 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000191 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000192 if options:
193 del attrs['options']
194 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000195 opt_dict = self.get_option_dict(command)
196 for (opt, val) in cmd_options.items():
197 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000198
199 # Now work on the rest of the attributes. Any attribute that's
200 # not already defined is invalid!
201 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000202 if hasattr(self.metadata, key):
203 setattr(self.metadata, key, val)
204 elif hasattr(self, key):
205 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000206 else:
Greg Ward02a1a2b2000-04-15 22:15:07 +0000207 raise DistutilsSetupError, \
Greg Wardfe6462c2000-04-04 01:40:52 +0000208 "invalid distribution option '%s'" % key
209
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000210 self.finalize_options()
Andrew M. Kuchling898f0992001-03-17 19:59:26 +0000211
Greg Wardfe6462c2000-04-04 01:40:52 +0000212 # __init__ ()
213
214
Greg Ward0e48cfd2000-05-26 01:00:15 +0000215 def get_option_dict (self, command):
216 """Get the option dictionary for a given command. If that
217 command's option dictionary hasn't been created yet, then create it
218 and return the new dictionary; otherwise, return the existing
219 option dictionary.
220 """
221
222 dict = self.command_options.get(command)
223 if dict is None:
224 dict = self.command_options[command] = {}
225 return dict
226
227
Greg Wardc32d9a62000-05-28 23:53:06 +0000228 def dump_option_dicts (self, header=None, commands=None, indent=""):
229 from pprint import pformat
230
231 if commands is None: # dump all command option dicts
232 commands = self.command_options.keys()
233 commands.sort()
234
235 if header is not None:
236 print indent + header
237 indent = indent + " "
238
239 if not commands:
240 print indent + "no commands known yet"
241 return
242
243 for cmd_name in commands:
244 opt_dict = self.command_options.get(cmd_name)
245 if opt_dict is None:
246 print indent + "no option dict for '%s' command" % cmd_name
247 else:
248 print indent + "option dict for '%s' command:" % cmd_name
249 out = pformat(opt_dict)
250 for line in string.split(out, "\n"):
251 print indent + " " + line
252
253 # dump_option_dicts ()
254
255
256
Greg Wardd5d8a992000-05-23 01:42:17 +0000257 # -- Config file finding/parsing methods ---------------------------
258
Gregory P. Smith14263542000-05-12 00:41:33 +0000259 def find_config_files (self):
260 """Find as many configuration files as should be processed for this
261 platform, and return a list of filenames in the order in which they
262 should be parsed. The filenames returned are guaranteed to exist
263 (modulo nasty race conditions).
264
265 On Unix, there are three possible config files: pydistutils.cfg in
266 the Distutils installation directory (ie. where the top-level
267 Distutils __inst__.py file lives), .pydistutils.cfg in the user's
268 home directory, and setup.cfg in the current directory.
269
270 On Windows and Mac OS, there are two possible config files:
271 pydistutils.cfg in the Python installation directory (sys.prefix)
Greg Wardd5d8a992000-05-23 01:42:17 +0000272 and setup.cfg in the current directory.
273 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000274 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000275 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000276
Greg Ward11696872000-06-07 02:29:03 +0000277 # Where to look for the system-wide Distutils config file
278 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
279
280 # Look for the system config file
281 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000282 if os.path.isfile(sys_file):
283 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000284
Greg Ward11696872000-06-07 02:29:03 +0000285 # What to call the per-user config file
286 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000287 user_filename = ".pydistutils.cfg"
288 else:
289 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000290
Greg Ward11696872000-06-07 02:29:03 +0000291 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000292 if os.environ.has_key('HOME'):
293 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000294 if os.path.isfile(user_file):
295 files.append(user_file)
296
Gregory P. Smith14263542000-05-12 00:41:33 +0000297 # All platforms support local setup.cfg
298 local_file = "setup.cfg"
299 if os.path.isfile(local_file):
300 files.append(local_file)
301
302 return files
303
304 # find_config_files ()
305
306
307 def parse_config_files (self, filenames=None):
308
309 from ConfigParser import ConfigParser
Greg Ward2bd3f422000-06-02 01:59:33 +0000310 from distutils.core import DEBUG
Gregory P. Smith14263542000-05-12 00:41:33 +0000311
312 if filenames is None:
313 filenames = self.find_config_files()
314
Greg Ward2bd3f422000-06-02 01:59:33 +0000315 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000316
Gregory P. Smith14263542000-05-12 00:41:33 +0000317 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000318 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000319 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000320 parser.read(filename)
321 for section in parser.sections():
322 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000323 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000324
Greg Wardd5d8a992000-05-23 01:42:17 +0000325 for opt in options:
326 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000327 val = parser.get(section,opt)
328 opt = string.replace(opt, '-', '_')
329 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000330
Greg Ward47460772000-05-23 03:47:35 +0000331 # Make the ConfigParser forget everything (so we retain
332 # the original filenames that options come from) -- gag,
333 # retch, puke -- another good reason for a distutils-
334 # specific config parser (sigh...)
335 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000336
Greg Wardceb9e222000-09-25 01:23:52 +0000337 # If there was a "global" section in the config file, use it
338 # to set Distribution options.
339
340 if self.command_options.has_key('global'):
341 for (opt, (src, val)) in self.command_options['global'].items():
342 alias = self.negative_opt.get(opt)
343 try:
344 if alias:
345 setattr(self, alias, not strtobool(val))
346 elif opt in ('verbose', 'dry_run'): # ugh!
347 setattr(self, opt, strtobool(val))
348 except ValueError, msg:
349 raise DistutilsOptionError, msg
350
351 # parse_config_files ()
352
Gregory P. Smith14263542000-05-12 00:41:33 +0000353
Greg Wardd5d8a992000-05-23 01:42:17 +0000354 # -- Command-line parsing methods ----------------------------------
355
Greg Ward9821bf42000-08-29 01:15:18 +0000356 def parse_command_line (self):
357 """Parse the setup script's command line, taken from the
358 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
359 -- see 'setup()' in core.py). This list is first processed for
360 "global options" -- options that set attributes of the Distribution
361 instance. Then, it is alternately scanned for Distutils commands
362 and options for that command. Each new command terminates the
363 options for the previous command. The allowed options for a
364 command are determined by the 'user_options' attribute of the
365 command class -- thus, we have to be able to load command classes
366 in order to parse the command line. Any error in that 'options'
367 attribute raises DistutilsGetoptError; any error on the
368 command-line raises DistutilsArgError. If no Distutils commands
369 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000370 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000371 on with executing commands; false if no errors but we shouldn't
372 execute commands (currently, this only happens if user asks for
373 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000374 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000375 #
Fred Drake981a1782001-08-10 18:59:30 +0000376 # We now have enough information to show the Macintosh dialog
377 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000378 #
379 if sys.platform == 'mac':
380 import EasyDialogs
381 cmdlist = self.get_command_list()
382 self.script_args = EasyDialogs.GetArgv(
383 self.global_options + self.display_options, cmdlist)
384
Greg Wardfe6462c2000-04-04 01:40:52 +0000385 # We have to parse the command line a bit at a time -- global
386 # options, then the first command, then its options, and so on --
387 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000388 # the options that are valid for a particular class aren't known
389 # until we have loaded the command class, which doesn't happen
390 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000391
392 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000393 parser = FancyGetopt(self.global_options + self.display_options)
394 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000395 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000396 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000397 option_order = parser.get_option_order()
Greg Wardfe6462c2000-04-04 01:40:52 +0000398
Greg Ward82715e12000-04-21 02:28:14 +0000399 # for display options we return immediately
400 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000401 return
402
403 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000404 args = self._parse_command_opts(parser, args)
405 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000406 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000407
Greg Wardd5d8a992000-05-23 01:42:17 +0000408 # Handle the cases of --help as a "global" option, ie.
409 # "setup.py --help" and "setup.py --help command ...". For the
410 # former, we show global options (--verbose, --dry-run, etc.)
411 # and display-only options (--name, --version, etc.); for the
412 # latter, we omit the display-only options and show help for
413 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000414 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000415 self._show_help(parser,
416 display_options=len(self.commands) == 0,
417 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000418 return
419
420 # Oops, no commands found -- an end-user error
421 if not self.commands:
422 raise DistutilsArgError, "no commands supplied"
423
424 # All is well: return true
425 return 1
426
427 # parse_command_line()
428
Greg Wardd5d8a992000-05-23 01:42:17 +0000429 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000430 """Parse the command-line options for a single command.
431 'parser' must be a FancyGetopt instance; 'args' must be the list
432 of arguments, starting with the current command (whose options
433 we are about to parse). Returns a new version of 'args' with
434 the next command at the front of the list; will be the empty
435 list if there are no more commands on the command line. Returns
436 None if the user asked for help on this command.
437 """
438 # late import because of mutual dependence between these modules
439 from distutils.cmd import Command
440
441 # Pull the current command from the head of the command line
442 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000443 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000444 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000445 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000446
447 # Dig up the command class that implements this command, so we
448 # 1) know that it's a valid command, and 2) know which options
449 # it takes.
450 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000451 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000452 except DistutilsModuleError, msg:
453 raise DistutilsArgError, msg
454
455 # Require that the command class be derived from Command -- want
456 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000457 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000458 raise DistutilsClassError, \
459 "command class %s must subclass Command" % cmd_class
460
461 # Also make sure that the command object provides a list of its
462 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000463 if not (hasattr(cmd_class, 'user_options') and
464 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000465 raise DistutilsClassError, \
466 ("command class %s must provide " +
467 "'user_options' attribute (a list of tuples)") % \
468 cmd_class
469
470 # If the command class has a list of negative alias options,
471 # merge it in with the global negative aliases.
472 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000473 if hasattr(cmd_class, 'negative_opt'):
474 negative_opt = copy(negative_opt)
475 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000476
Greg Wardfa9ff762000-10-14 04:06:40 +0000477 # Check for help_options in command class. They have a different
478 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000479 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000480 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000481 help_options = fix_help_options(cmd_class.help_options)
482 else:
Greg Ward55fced32000-06-24 01:22:41 +0000483 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000484
Greg Ward9d17a7a2000-06-07 03:00:06 +0000485
Greg Wardd5d8a992000-05-23 01:42:17 +0000486 # All commands support the global options too, just by adding
487 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000488 parser.set_option_table(self.global_options +
489 cmd_class.user_options +
490 help_options)
491 parser.set_negative_aliases(negative_opt)
492 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000493 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000494 self._show_help(parser, display_options=0, commands=[cmd_class])
495 return
496
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000497 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000498 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000499 help_option_found=0
500 for (help_option, short, desc, func) in cmd_class.help_options:
501 if hasattr(opts, parser.get_attr_name(help_option)):
502 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000503 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000504 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000505
506 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000507 func()
Greg Ward55fced32000-06-24 01:22:41 +0000508 else:
Fred Drake981a1782001-08-10 18:59:30 +0000509 raise DistutilsClassError(
510 "invalid help function %s for help option '%s': "
511 "must be a callable object (function, etc.)"
512 % (`func`, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000513
514 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000515 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000516
Greg Wardd5d8a992000-05-23 01:42:17 +0000517 # Put the options from the command-line into their official
518 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000519 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000520 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000521 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000522
523 return args
524
525 # _parse_command_opts ()
526
527
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000528 def finalize_options (self):
529 """Set final values for all the options on the Distribution
530 instance, analogous to the .finalize_options() method of Command
531 objects.
532 """
533
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000534 keywords = self.metadata.keywords
535 if keywords is not None:
536 if type(keywords) is StringType:
537 keywordlist = string.split(keywords, ',')
538 self.metadata.keywords = map(string.strip, keywordlist)
539
540 platforms = self.metadata.platforms
541 if platforms is not None:
542 if type(platforms) is StringType:
543 platformlist = string.split(platforms, ',')
544 self.metadata.platforms = map(string.strip, platformlist)
545
Greg Wardd5d8a992000-05-23 01:42:17 +0000546 def _show_help (self,
547 parser,
548 global_options=1,
549 display_options=1,
550 commands=[]):
551 """Show help for the setup script command-line in the form of
552 several lists of command-line options. 'parser' should be a
553 FancyGetopt instance; do not expect it to be returned in the
554 same state, as its option table will be reset to make it
555 generate the correct help text.
556
557 If 'global_options' is true, lists the global options:
558 --verbose, --dry-run, etc. If 'display_options' is true, lists
559 the "display-only" options: --name, --version, etc. Finally,
560 lists per-command help for every command name or command class
561 in 'commands'.
562 """
563 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000564 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000565 from distutils.cmd import Command
566
567 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000568 parser.set_option_table(self.global_options)
569 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000570 print
571
572 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000573 parser.set_option_table(self.display_options)
574 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000575 "Information display options (just display " +
576 "information, ignore any commands)")
577 print
578
579 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000580 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000581 klass = command
582 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000583 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000584 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000585 type(klass.help_options) is ListType):
586 parser.set_option_table(klass.user_options +
587 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000588 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000589 parser.set_option_table(klass.user_options)
590 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000591 print
592
Greg Ward9821bf42000-08-29 01:15:18 +0000593 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000594 return
595
596 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000597
Greg Wardd5d8a992000-05-23 01:42:17 +0000598
Greg Ward82715e12000-04-21 02:28:14 +0000599 def handle_display_options (self, option_order):
600 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000601 (--help-commands or the metadata display options) on the command
602 line, display the requested info and return true; else return
603 false.
604 """
Greg Ward9821bf42000-08-29 01:15:18 +0000605 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000606
607 # User just wants a list of commands -- we'll print it out and stop
608 # processing now (ie. if they ran "setup --help-commands foo bar",
609 # we ignore "foo bar").
610 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000611 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000612 print
Greg Ward9821bf42000-08-29 01:15:18 +0000613 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000614 return 1
615
616 # If user supplied any of the "display metadata" options, then
617 # display that metadata in the order in which the user supplied the
618 # metadata options.
619 any_display_options = 0
620 is_display_option = {}
621 for option in self.display_options:
622 is_display_option[option[0]] = 1
623
624 for (opt, val) in option_order:
625 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000626 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000627 value = getattr(self.metadata, "get_"+opt)()
628 if opt in ['keywords', 'platforms']:
629 print string.join(value, ',')
630 else:
631 print value
Greg Ward82715e12000-04-21 02:28:14 +0000632 any_display_options = 1
633
634 return any_display_options
635
636 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000637
638 def print_command_list (self, commands, header, max_length):
639 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000640 'print_commands()'.
641 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000642
643 print header + ":"
644
645 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000646 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000647 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000648 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000649 try:
650 description = klass.description
651 except AttributeError:
652 description = "(no description available)"
653
654 print " %-*s %s" % (max_length, cmd, description)
655
656 # print_command_list ()
657
658
659 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000660 """Print out a help message listing all available commands with a
661 description of each. The list is divided into "standard commands"
662 (listed in distutils.command.__all__) and "extra commands"
663 (mentioned in self.cmdclass, but not a standard command). The
664 descriptions come from the command class attribute
665 'description'.
666 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000667
668 import distutils.command
669 std_commands = distutils.command.__all__
670 is_std = {}
671 for cmd in std_commands:
672 is_std[cmd] = 1
673
674 extra_commands = []
675 for cmd in self.cmdclass.keys():
676 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000677 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000678
679 max_length = 0
680 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000681 if len(cmd) > max_length:
682 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000683
Greg Wardfd7b91e2000-09-26 01:52:25 +0000684 self.print_command_list(std_commands,
685 "Standard commands",
686 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000687 if extra_commands:
688 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000689 self.print_command_list(extra_commands,
690 "Extra commands",
691 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000692
693 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000694
Greg Wardf6fc8752000-11-11 02:47:11 +0000695 def get_command_list (self):
696 """Get a list of (command, description) tuples.
697 The list is divided into "standard commands" (listed in
698 distutils.command.__all__) and "extra commands" (mentioned in
699 self.cmdclass, but not a standard command). The descriptions come
700 from the command class attribute 'description'.
701 """
702 # Currently this is only used on Mac OS, for the Mac-only GUI
703 # Distutils interface (by Jack Jansen)
704
705 import distutils.command
706 std_commands = distutils.command.__all__
707 is_std = {}
708 for cmd in std_commands:
709 is_std[cmd] = 1
710
711 extra_commands = []
712 for cmd in self.cmdclass.keys():
713 if not is_std.get(cmd):
714 extra_commands.append(cmd)
715
716 rv = []
717 for cmd in (std_commands + extra_commands):
718 klass = self.cmdclass.get(cmd)
719 if not klass:
720 klass = self.get_command_class(cmd)
721 try:
722 description = klass.description
723 except AttributeError:
724 description = "(no description available)"
725 rv.append((cmd, description))
726 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000727
728 # -- Command class/object methods ----------------------------------
729
Greg Wardd5d8a992000-05-23 01:42:17 +0000730 def get_command_class (self, command):
731 """Return the class that implements the Distutils command named by
732 'command'. First we check the 'cmdclass' dictionary; if the
733 command is mentioned there, we fetch the class object from the
734 dictionary and return it. Otherwise we load the command module
735 ("distutils.command." + command) and fetch the command class from
736 the module. The loaded class is also stored in 'cmdclass'
737 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000738
Gregory P. Smith14263542000-05-12 00:41:33 +0000739 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000740 found, or if that module does not define the expected class.
741 """
742 klass = self.cmdclass.get(command)
743 if klass:
744 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000745
746 module_name = 'distutils.command.' + command
747 klass_name = command
748
749 try:
750 __import__ (module_name)
751 module = sys.modules[module_name]
752 except ImportError:
753 raise DistutilsModuleError, \
754 "invalid command '%s' (no module named '%s')" % \
755 (command, module_name)
756
757 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000758 klass = getattr(module, klass_name)
759 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000760 raise DistutilsModuleError, \
761 "invalid command '%s' (no class '%s' in module '%s')" \
762 % (command, klass_name, module_name)
763
Greg Wardd5d8a992000-05-23 01:42:17 +0000764 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000765 return klass
766
Greg Wardd5d8a992000-05-23 01:42:17 +0000767 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000768
Greg Wardd5d8a992000-05-23 01:42:17 +0000769 def get_command_obj (self, command, create=1):
770 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000771 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000772 object for 'command' is in the cache, then we either create and
773 return it (if 'create' is true) or return None.
774 """
Greg Ward2bd3f422000-06-02 01:59:33 +0000775 from distutils.core import DEBUG
Greg Wardd5d8a992000-05-23 01:42:17 +0000776 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000777 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000778 if DEBUG:
779 print "Distribution.get_command_obj(): " \
780 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000781
Greg Wardd5d8a992000-05-23 01:42:17 +0000782 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000783 cmd_obj = self.command_obj[command] = klass(self)
784 self.have_run[command] = 0
785
786 # Set any options that were supplied in config files
787 # or on the command line. (NB. support for error
788 # reporting is lame here: any errors aren't reported
789 # until 'finalize_options()' is called, which means
790 # we won't report the source of the error.)
791 options = self.command_options.get(command)
792 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000793 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000794
795 return cmd_obj
796
Greg Wardc32d9a62000-05-28 23:53:06 +0000797 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000798 """Set the options for 'command_obj' from 'option_dict'. Basically
799 this means copying elements of a dictionary ('option_dict') to
800 attributes of an instance ('command').
801
Greg Wardceb9e222000-09-25 01:23:52 +0000802 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000803 supplied, uses the standard option dictionary for this command
804 (from 'self.command_options').
805 """
806 from distutils.core import DEBUG
807
808 command_name = command_obj.get_command_name()
809 if option_dict is None:
810 option_dict = self.get_option_dict(command_name)
811
812 if DEBUG: print " setting options for '%s' command:" % command_name
813 for (option, (source, value)) in option_dict.items():
814 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000815 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000816 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000817 except AttributeError:
818 bool_opts = []
819 try:
820 neg_opt = command_obj.negative_opt
821 except AttributeError:
822 neg_opt = {}
823
824 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000825 is_string = type(value) is StringType
826 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000827 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000828 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000829 setattr(command_obj, option, strtobool(value))
830 elif hasattr(command_obj, option):
831 setattr(command_obj, option, value)
832 else:
833 raise DistutilsOptionError, \
834 ("error in %s: command '%s' has no such option '%s'"
835 % (source, command_name, option))
836 except ValueError, msg:
837 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000838
Greg Wardf449ea52000-09-16 15:23:28 +0000839 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000840 """Reinitializes a command to the state it was in when first
841 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000842 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000843 values in programmatically, overriding or supplementing
844 user-supplied values from the config files and command line.
845 You'll have to re-finalize the command object (by calling
846 'finalize_options()' or 'ensure_finalized()') before using it for
847 real.
848
Greg Wardf449ea52000-09-16 15:23:28 +0000849 'command' should be a command name (string) or command object. If
850 'reinit_subcommands' is true, also reinitializes the command's
851 sub-commands, as declared by the 'sub_commands' class attribute (if
852 it has one). See the "install" command for an example. Only
853 reinitializes the sub-commands that actually matter, ie. those
854 whose test predicates return true.
855
Greg Wardc32d9a62000-05-28 23:53:06 +0000856 Returns the reinitialized command object.
857 """
858 from distutils.cmd import Command
859 if not isinstance(command, Command):
860 command_name = command
861 command = self.get_command_obj(command_name)
862 else:
863 command_name = command.get_command_name()
864
865 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000866 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000867 command.initialize_options()
868 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000869 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000870 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000871
Greg Wardf449ea52000-09-16 15:23:28 +0000872 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000873 for sub in command.get_sub_commands():
874 self.reinitialize_command(sub, reinit_subcommands)
875
Greg Wardc32d9a62000-05-28 23:53:06 +0000876 return command
877
Greg Wardfe6462c2000-04-04 01:40:52 +0000878
879 # -- Methods that operate on the Distribution ----------------------
880
881 def announce (self, msg, level=1):
882 """Print 'msg' if 'level' is greater than or equal to the verbosity
Greg Wardd5d8a992000-05-23 01:42:17 +0000883 level recorded in the 'verbose' attribute (which, currently, can be
884 only 0 or 1).
885 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000886 if self.verbose >= level:
887 print msg
888
889
890 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000891 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000892 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000893 created by 'get_command_obj()'.
894 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000895 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000896 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000897
898
Greg Wardfe6462c2000-04-04 01:40:52 +0000899 # -- Methods that operate on its Commands --------------------------
900
901 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000902 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000903 if the command has already been run). Specifically: if we have
904 already created and run the command named by 'command', return
905 silently without doing anything. If the command named by 'command'
906 doesn't even have a command object yet, create one. Then invoke
907 'run()' on that command object (or an existing one).
908 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000909 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000910 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000911 return
912
Greg Wardfd7b91e2000-09-26 01:52:25 +0000913 self.announce("running " + command)
914 cmd_obj = self.get_command_obj(command)
915 cmd_obj.ensure_finalized()
916 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000917 self.have_run[command] = 1
918
919
Greg Wardfe6462c2000-04-04 01:40:52 +0000920 # -- Distribution query methods ------------------------------------
921
922 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000923 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000924
925 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000926 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000927
928 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000929 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000930
931 def has_modules (self):
932 return self.has_pure_modules() or self.has_ext_modules()
933
Greg Ward51def7d2000-05-27 01:36:14 +0000934 def has_headers (self):
935 return self.headers and len(self.headers) > 0
936
Greg Ward44a61bb2000-05-20 15:06:48 +0000937 def has_scripts (self):
938 return self.scripts and len(self.scripts) > 0
939
940 def has_data_files (self):
941 return self.data_files and len(self.data_files) > 0
942
Greg Wardfe6462c2000-04-04 01:40:52 +0000943 def is_pure (self):
944 return (self.has_pure_modules() and
945 not self.has_ext_modules() and
946 not self.has_c_libraries())
947
Greg Ward82715e12000-04-21 02:28:14 +0000948 # -- Metadata query methods ----------------------------------------
949
950 # If you're looking for 'get_name()', 'get_version()', and so forth,
951 # they are defined in a sneaky way: the constructor binds self.get_XXX
952 # to self.metadata.get_XXX. The actual code is in the
953 # DistributionMetadata class, below.
954
955# class Distribution
956
957
958class DistributionMetadata:
959 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000960 author, and so forth.
961 """
Greg Ward82715e12000-04-21 02:28:14 +0000962
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000963 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
964 "maintainer", "maintainer_email", "url",
965 "license", "description", "long_description",
966 "keywords", "platforms", "fullname", "contact",
967 "contact_email", "licence")
968
Greg Ward82715e12000-04-21 02:28:14 +0000969 def __init__ (self):
970 self.name = None
971 self.version = None
972 self.author = None
973 self.author_email = None
974 self.maintainer = None
975 self.maintainer_email = None
976 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000977 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000978 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000979 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000980 self.keywords = None
981 self.platforms = None
Greg Ward82715e12000-04-21 02:28:14 +0000982
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000983 def write_pkg_info (self, base_dir):
984 """Write the PKG-INFO file into the release tree.
985 """
986
987 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
988
989 pkg_info.write('Metadata-Version: 1.0\n')
990 pkg_info.write('Name: %s\n' % self.get_name() )
991 pkg_info.write('Version: %s\n' % self.get_version() )
992 pkg_info.write('Summary: %s\n' % self.get_description() )
993 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +0000994 pkg_info.write('Author: %s\n' % self.get_contact() )
995 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000996 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000997
998 long_desc = rfc822_escape( self.get_long_description() )
999 pkg_info.write('Description: %s\n' % long_desc)
1000
1001 keywords = string.join( self.get_keywords(), ',')
1002 if keywords:
1003 pkg_info.write('Keywords: %s\n' % keywords )
1004
1005 for platform in self.get_platforms():
1006 pkg_info.write('Platform: %s\n' % platform )
1007
1008 pkg_info.close()
1009
1010 # write_pkg_info ()
1011
Greg Ward82715e12000-04-21 02:28:14 +00001012 # -- Metadata query methods ----------------------------------------
1013
Greg Wardfe6462c2000-04-04 01:40:52 +00001014 def get_name (self):
1015 return self.name or "UNKNOWN"
1016
Greg Ward82715e12000-04-21 02:28:14 +00001017 def get_version(self):
1018 return self.version or "???"
Greg Wardfe6462c2000-04-04 01:40:52 +00001019
Greg Ward82715e12000-04-21 02:28:14 +00001020 def get_fullname (self):
1021 return "%s-%s" % (self.get_name(), self.get_version())
1022
1023 def get_author(self):
1024 return self.author or "UNKNOWN"
1025
1026 def get_author_email(self):
1027 return self.author_email or "UNKNOWN"
1028
1029 def get_maintainer(self):
1030 return self.maintainer or "UNKNOWN"
1031
1032 def get_maintainer_email(self):
1033 return self.maintainer_email or "UNKNOWN"
1034
1035 def get_contact(self):
1036 return (self.maintainer or
1037 self.author or
1038 "UNKNOWN")
1039
1040 def get_contact_email(self):
1041 return (self.maintainer_email or
1042 self.author_email or
1043 "UNKNOWN")
1044
1045 def get_url(self):
1046 return self.url or "UNKNOWN"
1047
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001048 def get_license(self):
1049 return self.license or "UNKNOWN"
1050 get_licence = get_license
1051
Greg Ward82715e12000-04-21 02:28:14 +00001052 def get_description(self):
1053 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001054
1055 def get_long_description(self):
1056 return self.long_description or "UNKNOWN"
1057
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001058 def get_keywords(self):
1059 return self.keywords or []
1060
1061 def get_platforms(self):
1062 return self.platforms or ["UNKNOWN"]
1063
Greg Ward82715e12000-04-21 02:28:14 +00001064# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001065
Greg Ward2ff78872000-06-24 00:23:20 +00001066
1067def fix_help_options (options):
1068 """Convert a 4-tuple 'help_options' list as found in various command
1069 classes to the 3-tuple form required by FancyGetopt.
1070 """
1071 new_options = []
1072 for help_tuple in options:
1073 new_options.append(help_tuple[0:3])
1074 return new_options
1075
1076
Greg Wardfe6462c2000-04-04 01:40:52 +00001077if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001078 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001079 print "ok"