blob: 824a0d3e6a9dbf4a425ce37b74028c2949111f32 [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()
Greg Ward4982f982000-04-22 02:52:44 +0000125 method_basenames = dir(self.metadata) + \
126 ['fullname', 'contact', 'contact_email']
127 for basename in method_basenames:
128 method_name = "get_" + basename
129 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000130
131 # 'cmdclass' maps command names to class objects, so we
132 # can 1) quickly figure out which class to instantiate when
133 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000134 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000135 self.cmdclass = {}
136
Greg Ward9821bf42000-08-29 01:15:18 +0000137 # 'script_name' and 'script_args' are usually set to sys.argv[0]
138 # and sys.argv[1:], but they can be overridden when the caller is
139 # not necessarily a setup script run from the command-line.
140 self.script_name = None
141 self.script_args = None
142
Greg Wardd5d8a992000-05-23 01:42:17 +0000143 # 'command_options' is where we store command options between
144 # parsing them (from config files, the command-line, etc.) and when
145 # they are actually needed -- ie. when the command in question is
146 # instantiated. It is a dictionary of dictionaries of 2-tuples:
147 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000148 self.command_options = {}
149
Greg Wardfe6462c2000-04-04 01:40:52 +0000150 # These options are really the business of various commands, rather
151 # than of the Distribution itself. We provide aliases for them in
152 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000153 self.packages = None
154 self.package_dir = None
155 self.py_modules = None
156 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000157 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000158 self.ext_modules = None
159 self.ext_package = None
160 self.include_dirs = None
161 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000162 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000163 self.data_files = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000164
165 # And now initialize bookkeeping stuff that can't be supplied by
166 # the caller at all. 'command_obj' maps command names to
167 # Command instances -- that's how we enforce that every command
168 # class is a singleton.
169 self.command_obj = {}
170
171 # 'have_run' maps command names to boolean values; it keeps track
172 # of whether we have actually run a particular command, to make it
173 # cheap to "run" a command whenever we think we might need to -- if
174 # it's already been done, no need for expensive filesystem
175 # operations, we just check the 'have_run' dictionary and carry on.
176 # It's only safe to query 'have_run' for a command class that has
177 # been instantiated -- a false value will be inserted when the
178 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000179 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000180 # '.get()' rather than a straight lookup.
181 self.have_run = {}
182
183 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000184 # the setup script) to possibly override any or all of these
185 # distribution options.
186
Greg Wardfe6462c2000-04-04 01:40:52 +0000187 if attrs:
188
189 # Pull out the set of command options and work on them
190 # specifically. Note that this order guarantees that aliased
191 # command options will override any supplied redundantly
192 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000193 options = attrs.get('options')
Greg Wardfe6462c2000-04-04 01:40:52 +0000194 if options:
195 del attrs['options']
196 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000197 opt_dict = self.get_option_dict(command)
198 for (opt, val) in cmd_options.items():
199 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000200
201 # Now work on the rest of the attributes. Any attribute that's
202 # not already defined is invalid!
203 for (key,val) in attrs.items():
Greg Wardfd7b91e2000-09-26 01:52:25 +0000204 if hasattr(self.metadata, key):
205 setattr(self.metadata, key, val)
206 elif hasattr(self, key):
207 setattr(self, key, val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000208 else:
Greg Ward02a1a2b2000-04-15 22:15:07 +0000209 raise DistutilsSetupError, \
Greg Wardfe6462c2000-04-04 01:40:52 +0000210 "invalid distribution option '%s'" % key
211
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000212 self.finalize_options()
Andrew M. Kuchling898f0992001-03-17 19:59:26 +0000213
Greg Wardfe6462c2000-04-04 01:40:52 +0000214 # __init__ ()
215
216
Greg Ward0e48cfd2000-05-26 01:00:15 +0000217 def get_option_dict (self, command):
218 """Get the option dictionary for a given command. If that
219 command's option dictionary hasn't been created yet, then create it
220 and return the new dictionary; otherwise, return the existing
221 option dictionary.
222 """
223
224 dict = self.command_options.get(command)
225 if dict is None:
226 dict = self.command_options[command] = {}
227 return dict
228
229
Greg Wardc32d9a62000-05-28 23:53:06 +0000230 def dump_option_dicts (self, header=None, commands=None, indent=""):
231 from pprint import pformat
232
233 if commands is None: # dump all command option dicts
234 commands = self.command_options.keys()
235 commands.sort()
236
237 if header is not None:
238 print indent + header
239 indent = indent + " "
240
241 if not commands:
242 print indent + "no commands known yet"
243 return
244
245 for cmd_name in commands:
246 opt_dict = self.command_options.get(cmd_name)
247 if opt_dict is None:
248 print indent + "no option dict for '%s' command" % cmd_name
249 else:
250 print indent + "option dict for '%s' command:" % cmd_name
251 out = pformat(opt_dict)
252 for line in string.split(out, "\n"):
253 print indent + " " + line
254
255 # dump_option_dicts ()
256
257
258
Greg Wardd5d8a992000-05-23 01:42:17 +0000259 # -- Config file finding/parsing methods ---------------------------
260
Gregory P. Smith14263542000-05-12 00:41:33 +0000261 def find_config_files (self):
262 """Find as many configuration files as should be processed for this
263 platform, and return a list of filenames in the order in which they
264 should be parsed. The filenames returned are guaranteed to exist
265 (modulo nasty race conditions).
266
267 On Unix, there are three possible config files: pydistutils.cfg in
268 the Distutils installation directory (ie. where the top-level
269 Distutils __inst__.py file lives), .pydistutils.cfg in the user's
270 home directory, and setup.cfg in the current directory.
271
272 On Windows and Mac OS, there are two possible config files:
273 pydistutils.cfg in the Python installation directory (sys.prefix)
Greg Wardd5d8a992000-05-23 01:42:17 +0000274 and setup.cfg in the current directory.
275 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000276 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000277 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000278
Greg Ward11696872000-06-07 02:29:03 +0000279 # Where to look for the system-wide Distutils config file
280 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
281
282 # Look for the system config file
283 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000284 if os.path.isfile(sys_file):
285 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000286
Greg Ward11696872000-06-07 02:29:03 +0000287 # What to call the per-user config file
288 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000289 user_filename = ".pydistutils.cfg"
290 else:
291 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000292
Greg Ward11696872000-06-07 02:29:03 +0000293 # And look for the user config file
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000294 if os.environ.has_key('HOME'):
295 user_file = os.path.join(os.environ.get('HOME'), user_filename)
Gregory P. Smith14263542000-05-12 00:41:33 +0000296 if os.path.isfile(user_file):
297 files.append(user_file)
298
Gregory P. Smith14263542000-05-12 00:41:33 +0000299 # All platforms support local setup.cfg
300 local_file = "setup.cfg"
301 if os.path.isfile(local_file):
302 files.append(local_file)
303
304 return files
305
306 # find_config_files ()
307
308
309 def parse_config_files (self, filenames=None):
310
311 from ConfigParser import ConfigParser
Greg Ward2bd3f422000-06-02 01:59:33 +0000312 from distutils.core import DEBUG
Gregory P. Smith14263542000-05-12 00:41:33 +0000313
314 if filenames is None:
315 filenames = self.find_config_files()
316
Greg Ward2bd3f422000-06-02 01:59:33 +0000317 if DEBUG: print "Distribution.parse_config_files():"
Greg Ward47460772000-05-23 03:47:35 +0000318
Gregory P. Smith14263542000-05-12 00:41:33 +0000319 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000320 for filename in filenames:
Greg Ward2bd3f422000-06-02 01:59:33 +0000321 if DEBUG: print " reading", filename
Greg Wardd5d8a992000-05-23 01:42:17 +0000322 parser.read(filename)
323 for section in parser.sections():
324 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000325 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000326
Greg Wardd5d8a992000-05-23 01:42:17 +0000327 for opt in options:
328 if opt != '__name__':
Greg Wardceb9e222000-09-25 01:23:52 +0000329 val = parser.get(section,opt)
330 opt = string.replace(opt, '-', '_')
331 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000332
Greg Ward47460772000-05-23 03:47:35 +0000333 # Make the ConfigParser forget everything (so we retain
334 # the original filenames that options come from) -- gag,
335 # retch, puke -- another good reason for a distutils-
336 # specific config parser (sigh...)
337 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000338
Greg Wardceb9e222000-09-25 01:23:52 +0000339 # If there was a "global" section in the config file, use it
340 # to set Distribution options.
341
342 if self.command_options.has_key('global'):
343 for (opt, (src, val)) in self.command_options['global'].items():
344 alias = self.negative_opt.get(opt)
345 try:
346 if alias:
347 setattr(self, alias, not strtobool(val))
348 elif opt in ('verbose', 'dry_run'): # ugh!
349 setattr(self, opt, strtobool(val))
350 except ValueError, msg:
351 raise DistutilsOptionError, msg
352
353 # parse_config_files ()
354
Gregory P. Smith14263542000-05-12 00:41:33 +0000355
Greg Wardd5d8a992000-05-23 01:42:17 +0000356 # -- Command-line parsing methods ----------------------------------
357
Greg Ward9821bf42000-08-29 01:15:18 +0000358 def parse_command_line (self):
359 """Parse the setup script's command line, taken from the
360 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
361 -- see 'setup()' in core.py). This list is first processed for
362 "global options" -- options that set attributes of the Distribution
363 instance. Then, it is alternately scanned for Distutils commands
364 and options for that command. Each new command terminates the
365 options for the previous command. The allowed options for a
366 command are determined by the 'user_options' attribute of the
367 command class -- thus, we have to be able to load command classes
368 in order to parse the command line. Any error in that 'options'
369 attribute raises DistutilsGetoptError; any error on the
370 command-line raises DistutilsArgError. If no Distutils commands
371 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000372 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000373 on with executing commands; false if no errors but we shouldn't
374 execute commands (currently, this only happens if user asks for
375 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000376 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000377 #
378 # We now have enough information to show the Macintosh dialog that allows
379 # the user to interactively specify the "command line".
380 #
381 if sys.platform == 'mac':
382 import EasyDialogs
383 cmdlist = self.get_command_list()
384 self.script_args = EasyDialogs.GetArgv(
385 self.global_options + self.display_options, cmdlist)
386
Greg Wardfe6462c2000-04-04 01:40:52 +0000387 # We have to parse the command line a bit at a time -- global
388 # options, then the first command, then its options, and so on --
389 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000390 # the options that are valid for a particular class aren't known
391 # until we have loaded the command class, which doesn't happen
392 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000393
394 self.commands = []
Greg Wardfd7b91e2000-09-26 01:52:25 +0000395 parser = FancyGetopt(self.global_options + self.display_options)
396 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000397 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000398 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000399 option_order = parser.get_option_order()
Greg Wardfe6462c2000-04-04 01:40:52 +0000400
Greg Ward82715e12000-04-21 02:28:14 +0000401 # for display options we return immediately
402 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000403 return
404
405 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000406 args = self._parse_command_opts(parser, args)
407 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000408 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000409
Greg Wardd5d8a992000-05-23 01:42:17 +0000410 # Handle the cases of --help as a "global" option, ie.
411 # "setup.py --help" and "setup.py --help command ...". For the
412 # former, we show global options (--verbose, --dry-run, etc.)
413 # and display-only options (--name, --version, etc.); for the
414 # latter, we omit the display-only options and show help for
415 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000416 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000417 self._show_help(parser,
418 display_options=len(self.commands) == 0,
419 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000420 return
421
422 # Oops, no commands found -- an end-user error
423 if not self.commands:
424 raise DistutilsArgError, "no commands supplied"
425
426 # All is well: return true
427 return 1
428
429 # parse_command_line()
430
Greg Wardd5d8a992000-05-23 01:42:17 +0000431 def _parse_command_opts (self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000432 """Parse the command-line options for a single command.
433 'parser' must be a FancyGetopt instance; 'args' must be the list
434 of arguments, starting with the current command (whose options
435 we are about to parse). Returns a new version of 'args' with
436 the next command at the front of the list; will be the empty
437 list if there are no more commands on the command line. Returns
438 None if the user asked for help on this command.
439 """
440 # late import because of mutual dependence between these modules
441 from distutils.cmd import Command
442
443 # Pull the current command from the head of the command line
444 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000445 if not command_re.match(command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000446 raise SystemExit, "invalid command name '%s'" % command
Greg Wardfd7b91e2000-09-26 01:52:25 +0000447 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000448
449 # Dig up the command class that implements this command, so we
450 # 1) know that it's a valid command, and 2) know which options
451 # it takes.
452 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000453 cmd_class = self.get_command_class(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000454 except DistutilsModuleError, msg:
455 raise DistutilsArgError, msg
456
457 # Require that the command class be derived from Command -- want
458 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000459 if not issubclass(cmd_class, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000460 raise DistutilsClassError, \
461 "command class %s must subclass Command" % cmd_class
462
463 # Also make sure that the command object provides a list of its
464 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000465 if not (hasattr(cmd_class, 'user_options') and
466 type(cmd_class.user_options) is ListType):
Greg Wardd5d8a992000-05-23 01:42:17 +0000467 raise DistutilsClassError, \
468 ("command class %s must provide " +
469 "'user_options' attribute (a list of tuples)") % \
470 cmd_class
471
472 # If the command class has a list of negative alias options,
473 # merge it in with the global negative aliases.
474 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000475 if hasattr(cmd_class, 'negative_opt'):
476 negative_opt = copy(negative_opt)
477 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000478
Greg Wardfa9ff762000-10-14 04:06:40 +0000479 # Check for help_options in command class. They have a different
480 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000481 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000482 type(cmd_class.help_options) is ListType):
Greg Ward2ff78872000-06-24 00:23:20 +0000483 help_options = fix_help_options(cmd_class.help_options)
484 else:
Greg Ward55fced32000-06-24 01:22:41 +0000485 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000486
Greg Ward9d17a7a2000-06-07 03:00:06 +0000487
Greg Wardd5d8a992000-05-23 01:42:17 +0000488 # All commands support the global options too, just by adding
489 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000490 parser.set_option_table(self.global_options +
491 cmd_class.user_options +
492 help_options)
493 parser.set_negative_aliases(negative_opt)
494 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000495 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000496 self._show_help(parser, display_options=0, commands=[cmd_class])
497 return
498
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000499 if (hasattr(cmd_class, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000500 type(cmd_class.help_options) is ListType):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000501 help_option_found=0
502 for (help_option, short, desc, func) in cmd_class.help_options:
503 if hasattr(opts, parser.get_attr_name(help_option)):
504 help_option_found=1
Greg Wardfa9ff762000-10-14 04:06:40 +0000505 #print "showing help for option %s of command %s" % \
Greg Ward2ff78872000-06-24 00:23:20 +0000506 # (help_option[0],cmd_class)
Greg Ward55fced32000-06-24 01:22:41 +0000507
508 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000509 func()
Greg Ward55fced32000-06-24 01:22:41 +0000510 else:
511 raise DistutilsClassError, \
512 ("invalid help function %s for help option '%s': "
513 "must be a callable object (function, etc.)") % \
514 (`func`, help_option)
515
516 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000517 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000518
Greg Wardd5d8a992000-05-23 01:42:17 +0000519 # Put the options from the command-line into their official
520 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000521 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000522 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000523 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000524
525 return args
526
527 # _parse_command_opts ()
528
529
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000530 def finalize_options (self):
531 """Set final values for all the options on the Distribution
532 instance, analogous to the .finalize_options() method of Command
533 objects.
534 """
535
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000536 keywords = self.metadata.keywords
537 if keywords is not None:
538 if type(keywords) is StringType:
539 keywordlist = string.split(keywords, ',')
540 self.metadata.keywords = map(string.strip, keywordlist)
541
542 platforms = self.metadata.platforms
543 if platforms is not None:
544 if type(platforms) is StringType:
545 platformlist = string.split(platforms, ',')
546 self.metadata.platforms = map(string.strip, platformlist)
547
Greg Wardd5d8a992000-05-23 01:42:17 +0000548 def _show_help (self,
549 parser,
550 global_options=1,
551 display_options=1,
552 commands=[]):
553 """Show help for the setup script command-line in the form of
554 several lists of command-line options. 'parser' should be a
555 FancyGetopt instance; do not expect it to be returned in the
556 same state, as its option table will be reset to make it
557 generate the correct help text.
558
559 If 'global_options' is true, lists the global options:
560 --verbose, --dry-run, etc. If 'display_options' is true, lists
561 the "display-only" options: --name, --version, etc. Finally,
562 lists per-command help for every command name or command class
563 in 'commands'.
564 """
565 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000566 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000567 from distutils.cmd import Command
568
569 if global_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000570 parser.set_option_table(self.global_options)
571 parser.print_help("Global options:")
Greg Wardd5d8a992000-05-23 01:42:17 +0000572 print
573
574 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000575 parser.set_option_table(self.display_options)
576 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000577 "Information display options (just display " +
578 "information, ignore any commands)")
579 print
580
581 for command in self.commands:
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000582 if type(command) is ClassType and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000583 klass = command
584 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000585 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000586 if (hasattr(klass, 'help_options') and
Greg Wardfd7b91e2000-09-26 01:52:25 +0000587 type(klass.help_options) is ListType):
588 parser.set_option_table(klass.user_options +
589 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000590 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000591 parser.set_option_table(klass.user_options)
592 parser.print_help("Options for '%s' command:" % klass.__name__)
Greg Wardd5d8a992000-05-23 01:42:17 +0000593 print
594
Greg Ward9821bf42000-08-29 01:15:18 +0000595 print gen_usage(self.script_name)
Greg Wardd5d8a992000-05-23 01:42:17 +0000596 return
597
598 # _show_help ()
Greg Wardfa9ff762000-10-14 04:06:40 +0000599
Greg Wardd5d8a992000-05-23 01:42:17 +0000600
Greg Ward82715e12000-04-21 02:28:14 +0000601 def handle_display_options (self, option_order):
602 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000603 (--help-commands or the metadata display options) on the command
604 line, display the requested info and return true; else return
605 false.
606 """
Greg Ward9821bf42000-08-29 01:15:18 +0000607 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000608
609 # User just wants a list of commands -- we'll print it out and stop
610 # processing now (ie. if they ran "setup --help-commands foo bar",
611 # we ignore "foo bar").
612 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000613 self.print_commands()
Greg Ward82715e12000-04-21 02:28:14 +0000614 print
Greg Ward9821bf42000-08-29 01:15:18 +0000615 print gen_usage(self.script_name)
Greg Ward82715e12000-04-21 02:28:14 +0000616 return 1
617
618 # If user supplied any of the "display metadata" options, then
619 # display that metadata in the order in which the user supplied the
620 # metadata options.
621 any_display_options = 0
622 is_display_option = {}
623 for option in self.display_options:
624 is_display_option[option[0]] = 1
625
626 for (opt, val) in option_order:
627 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000628 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000629 value = getattr(self.metadata, "get_"+opt)()
630 if opt in ['keywords', 'platforms']:
631 print string.join(value, ',')
632 else:
633 print value
Greg Ward82715e12000-04-21 02:28:14 +0000634 any_display_options = 1
635
636 return any_display_options
637
638 # handle_display_options()
Greg Wardfe6462c2000-04-04 01:40:52 +0000639
640 def print_command_list (self, commands, header, max_length):
641 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000642 'print_commands()'.
643 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000644
645 print header + ":"
646
647 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000648 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000649 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000650 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000651 try:
652 description = klass.description
653 except AttributeError:
654 description = "(no description available)"
655
656 print " %-*s %s" % (max_length, cmd, description)
657
658 # print_command_list ()
659
660
661 def print_commands (self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000662 """Print out a help message listing all available commands with a
663 description of each. The list is divided into "standard commands"
664 (listed in distutils.command.__all__) and "extra commands"
665 (mentioned in self.cmdclass, but not a standard command). The
666 descriptions come from the command class attribute
667 'description'.
668 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000669
670 import distutils.command
671 std_commands = distutils.command.__all__
672 is_std = {}
673 for cmd in std_commands:
674 is_std[cmd] = 1
675
676 extra_commands = []
677 for cmd in self.cmdclass.keys():
678 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000679 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000680
681 max_length = 0
682 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000683 if len(cmd) > max_length:
684 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000685
Greg Wardfd7b91e2000-09-26 01:52:25 +0000686 self.print_command_list(std_commands,
687 "Standard commands",
688 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000689 if extra_commands:
690 print
Greg Wardfd7b91e2000-09-26 01:52:25 +0000691 self.print_command_list(extra_commands,
692 "Extra commands",
693 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000694
695 # print_commands ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000696
Greg Wardf6fc8752000-11-11 02:47:11 +0000697 def get_command_list (self):
698 """Get a list of (command, description) tuples.
699 The list is divided into "standard commands" (listed in
700 distutils.command.__all__) and "extra commands" (mentioned in
701 self.cmdclass, but not a standard command). The descriptions come
702 from the command class attribute 'description'.
703 """
704 # Currently this is only used on Mac OS, for the Mac-only GUI
705 # Distutils interface (by Jack Jansen)
706
707 import distutils.command
708 std_commands = distutils.command.__all__
709 is_std = {}
710 for cmd in std_commands:
711 is_std[cmd] = 1
712
713 extra_commands = []
714 for cmd in self.cmdclass.keys():
715 if not is_std.get(cmd):
716 extra_commands.append(cmd)
717
718 rv = []
719 for cmd in (std_commands + extra_commands):
720 klass = self.cmdclass.get(cmd)
721 if not klass:
722 klass = self.get_command_class(cmd)
723 try:
724 description = klass.description
725 except AttributeError:
726 description = "(no description available)"
727 rv.append((cmd, description))
728 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000729
730 # -- Command class/object methods ----------------------------------
731
Greg Wardd5d8a992000-05-23 01:42:17 +0000732 def get_command_class (self, command):
733 """Return the class that implements the Distutils command named by
734 'command'. First we check the 'cmdclass' dictionary; if the
735 command is mentioned there, we fetch the class object from the
736 dictionary and return it. Otherwise we load the command module
737 ("distutils.command." + command) and fetch the command class from
738 the module. The loaded class is also stored in 'cmdclass'
739 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000740
Gregory P. Smith14263542000-05-12 00:41:33 +0000741 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000742 found, or if that module does not define the expected class.
743 """
744 klass = self.cmdclass.get(command)
745 if klass:
746 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000747
748 module_name = 'distutils.command.' + command
749 klass_name = command
750
751 try:
752 __import__ (module_name)
753 module = sys.modules[module_name]
754 except ImportError:
755 raise DistutilsModuleError, \
756 "invalid command '%s' (no module named '%s')" % \
757 (command, module_name)
758
759 try:
Greg Wardd5d8a992000-05-23 01:42:17 +0000760 klass = getattr(module, klass_name)
761 except AttributeError:
Greg Wardfe6462c2000-04-04 01:40:52 +0000762 raise DistutilsModuleError, \
763 "invalid command '%s' (no class '%s' in module '%s')" \
764 % (command, klass_name, module_name)
765
Greg Wardd5d8a992000-05-23 01:42:17 +0000766 self.cmdclass[command] = klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000767 return klass
768
Greg Wardd5d8a992000-05-23 01:42:17 +0000769 # get_command_class ()
Greg Wardfe6462c2000-04-04 01:40:52 +0000770
Greg Wardd5d8a992000-05-23 01:42:17 +0000771 def get_command_obj (self, command, create=1):
772 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000773 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000774 object for 'command' is in the cache, then we either create and
775 return it (if 'create' is true) or return None.
776 """
Greg Ward2bd3f422000-06-02 01:59:33 +0000777 from distutils.core import DEBUG
Greg Wardd5d8a992000-05-23 01:42:17 +0000778 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000779 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000780 if DEBUG:
781 print "Distribution.get_command_obj(): " \
782 "creating '%s' command object" % command
Greg Ward47460772000-05-23 03:47:35 +0000783
Greg Wardd5d8a992000-05-23 01:42:17 +0000784 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000785 cmd_obj = self.command_obj[command] = klass(self)
786 self.have_run[command] = 0
787
788 # Set any options that were supplied in config files
789 # or on the command line. (NB. support for error
790 # reporting is lame here: any errors aren't reported
791 # until 'finalize_options()' is called, which means
792 # we won't report the source of the error.)
793 options = self.command_options.get(command)
794 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000795 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000796
797 return cmd_obj
798
Greg Wardc32d9a62000-05-28 23:53:06 +0000799 def _set_command_options (self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000800 """Set the options for 'command_obj' from 'option_dict'. Basically
801 this means copying elements of a dictionary ('option_dict') to
802 attributes of an instance ('command').
803
Greg Wardceb9e222000-09-25 01:23:52 +0000804 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000805 supplied, uses the standard option dictionary for this command
806 (from 'self.command_options').
807 """
808 from distutils.core import DEBUG
809
810 command_name = command_obj.get_command_name()
811 if option_dict is None:
812 option_dict = self.get_option_dict(command_name)
813
814 if DEBUG: print " setting options for '%s' command:" % command_name
815 for (option, (source, value)) in option_dict.items():
816 if DEBUG: print " %s = %s (from %s)" % (option, value, source)
Greg Wardceb9e222000-09-25 01:23:52 +0000817 try:
Greg Ward2f2b6c62000-09-25 01:58:07 +0000818 bool_opts = map(translate_longopt, command_obj.boolean_options)
Greg Wardceb9e222000-09-25 01:23:52 +0000819 except AttributeError:
820 bool_opts = []
821 try:
822 neg_opt = command_obj.negative_opt
823 except AttributeError:
824 neg_opt = {}
825
826 try:
Greg Ward2c08cf02000-09-27 00:15:37 +0000827 is_string = type(value) is StringType
828 if neg_opt.has_key(option) and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000829 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000830 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000831 setattr(command_obj, option, strtobool(value))
832 elif hasattr(command_obj, option):
833 setattr(command_obj, option, value)
834 else:
835 raise DistutilsOptionError, \
836 ("error in %s: command '%s' has no such option '%s'"
837 % (source, command_name, option))
838 except ValueError, msg:
839 raise DistutilsOptionError, msg
Greg Wardc32d9a62000-05-28 23:53:06 +0000840
Greg Wardf449ea52000-09-16 15:23:28 +0000841 def reinitialize_command (self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000842 """Reinitializes a command to the state it was in when first
843 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000844 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000845 values in programmatically, overriding or supplementing
846 user-supplied values from the config files and command line.
847 You'll have to re-finalize the command object (by calling
848 'finalize_options()' or 'ensure_finalized()') before using it for
849 real.
850
Greg Wardf449ea52000-09-16 15:23:28 +0000851 'command' should be a command name (string) or command object. If
852 'reinit_subcommands' is true, also reinitializes the command's
853 sub-commands, as declared by the 'sub_commands' class attribute (if
854 it has one). See the "install" command for an example. Only
855 reinitializes the sub-commands that actually matter, ie. those
856 whose test predicates return true.
857
Greg Wardc32d9a62000-05-28 23:53:06 +0000858 Returns the reinitialized command object.
859 """
860 from distutils.cmd import Command
861 if not isinstance(command, Command):
862 command_name = command
863 command = self.get_command_obj(command_name)
864 else:
865 command_name = command.get_command_name()
866
867 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000868 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000869 command.initialize_options()
870 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000871 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000872 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000873
Greg Wardf449ea52000-09-16 15:23:28 +0000874 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000875 for sub in command.get_sub_commands():
876 self.reinitialize_command(sub, reinit_subcommands)
877
Greg Wardc32d9a62000-05-28 23:53:06 +0000878 return command
879
Greg Wardfe6462c2000-04-04 01:40:52 +0000880
881 # -- Methods that operate on the Distribution ----------------------
882
883 def announce (self, msg, level=1):
884 """Print 'msg' if 'level' is greater than or equal to the verbosity
Greg Wardd5d8a992000-05-23 01:42:17 +0000885 level recorded in the 'verbose' attribute (which, currently, can be
886 only 0 or 1).
887 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000888 if self.verbose >= level:
889 print msg
890
891
892 def run_commands (self):
Greg Ward82715e12000-04-21 02:28:14 +0000893 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000894 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000895 created by 'get_command_obj()'.
896 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000897 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000898 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000899
900
Greg Wardfe6462c2000-04-04 01:40:52 +0000901 # -- Methods that operate on its Commands --------------------------
902
903 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000904 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000905 if the command has already been run). Specifically: if we have
906 already created and run the command named by 'command', return
907 silently without doing anything. If the command named by 'command'
908 doesn't even have a command object yet, create one. Then invoke
909 'run()' on that command object (or an existing one).
910 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000911 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000912 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000913 return
914
Greg Wardfd7b91e2000-09-26 01:52:25 +0000915 self.announce("running " + command)
916 cmd_obj = self.get_command_obj(command)
917 cmd_obj.ensure_finalized()
918 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000919 self.have_run[command] = 1
920
921
Greg Wardfe6462c2000-04-04 01:40:52 +0000922 # -- Distribution query methods ------------------------------------
923
924 def has_pure_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000925 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000926
927 def has_ext_modules (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000928 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000929
930 def has_c_libraries (self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000931 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000932
933 def has_modules (self):
934 return self.has_pure_modules() or self.has_ext_modules()
935
Greg Ward51def7d2000-05-27 01:36:14 +0000936 def has_headers (self):
937 return self.headers and len(self.headers) > 0
938
Greg Ward44a61bb2000-05-20 15:06:48 +0000939 def has_scripts (self):
940 return self.scripts and len(self.scripts) > 0
941
942 def has_data_files (self):
943 return self.data_files and len(self.data_files) > 0
944
Greg Wardfe6462c2000-04-04 01:40:52 +0000945 def is_pure (self):
946 return (self.has_pure_modules() and
947 not self.has_ext_modules() and
948 not self.has_c_libraries())
949
Greg Ward82715e12000-04-21 02:28:14 +0000950 # -- Metadata query methods ----------------------------------------
951
952 # If you're looking for 'get_name()', 'get_version()', and so forth,
953 # they are defined in a sneaky way: the constructor binds self.get_XXX
954 # to self.metadata.get_XXX. The actual code is in the
955 # DistributionMetadata class, below.
956
957# class Distribution
958
959
960class DistributionMetadata:
961 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +0000962 author, and so forth.
963 """
Greg Ward82715e12000-04-21 02:28:14 +0000964
965 def __init__ (self):
966 self.name = None
967 self.version = None
968 self.author = None
969 self.author_email = None
970 self.maintainer = None
971 self.maintainer_email = None
972 self.url = None
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000973 self.license = None
Greg Ward82715e12000-04-21 02:28:14 +0000974 self.description = None
Greg Warde5a584e2000-04-26 02:26:55 +0000975 self.long_description = None
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000976 self.keywords = None
977 self.platforms = None
Greg Ward82715e12000-04-21 02:28:14 +0000978
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000979 def write_pkg_info (self, base_dir):
980 """Write the PKG-INFO file into the release tree.
981 """
982
983 pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
984
985 pkg_info.write('Metadata-Version: 1.0\n')
986 pkg_info.write('Name: %s\n' % self.get_name() )
987 pkg_info.write('Version: %s\n' % self.get_version() )
988 pkg_info.write('Summary: %s\n' % self.get_description() )
989 pkg_info.write('Home-page: %s\n' % self.get_url() )
Andrew M. Kuchlingffb963c2001-03-22 15:32:23 +0000990 pkg_info.write('Author: %s\n' % self.get_contact() )
991 pkg_info.write('Author-email: %s\n' % self.get_contact_email() )
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000992 pkg_info.write('License: %s\n' % self.get_license() )
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000993
994 long_desc = rfc822_escape( self.get_long_description() )
995 pkg_info.write('Description: %s\n' % long_desc)
996
997 keywords = string.join( self.get_keywords(), ',')
998 if keywords:
999 pkg_info.write('Keywords: %s\n' % keywords )
1000
1001 for platform in self.get_platforms():
1002 pkg_info.write('Platform: %s\n' % platform )
1003
1004 pkg_info.close()
1005
1006 # write_pkg_info ()
1007
Greg Ward82715e12000-04-21 02:28:14 +00001008 # -- Metadata query methods ----------------------------------------
1009
Greg Wardfe6462c2000-04-04 01:40:52 +00001010 def get_name (self):
1011 return self.name or "UNKNOWN"
1012
Greg Ward82715e12000-04-21 02:28:14 +00001013 def get_version(self):
1014 return self.version or "???"
Greg Wardfe6462c2000-04-04 01:40:52 +00001015
Greg Ward82715e12000-04-21 02:28:14 +00001016 def get_fullname (self):
1017 return "%s-%s" % (self.get_name(), self.get_version())
1018
1019 def get_author(self):
1020 return self.author or "UNKNOWN"
1021
1022 def get_author_email(self):
1023 return self.author_email or "UNKNOWN"
1024
1025 def get_maintainer(self):
1026 return self.maintainer or "UNKNOWN"
1027
1028 def get_maintainer_email(self):
1029 return self.maintainer_email or "UNKNOWN"
1030
1031 def get_contact(self):
1032 return (self.maintainer or
1033 self.author or
1034 "UNKNOWN")
1035
1036 def get_contact_email(self):
1037 return (self.maintainer_email or
1038 self.author_email or
1039 "UNKNOWN")
1040
1041 def get_url(self):
1042 return self.url or "UNKNOWN"
1043
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001044 def get_license(self):
1045 return self.license or "UNKNOWN"
1046 get_licence = get_license
1047
Greg Ward82715e12000-04-21 02:28:14 +00001048 def get_description(self):
1049 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001050
1051 def get_long_description(self):
1052 return self.long_description or "UNKNOWN"
1053
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001054 def get_keywords(self):
1055 return self.keywords or []
1056
1057 def get_platforms(self):
1058 return self.platforms or ["UNKNOWN"]
1059
Greg Ward82715e12000-04-21 02:28:14 +00001060# class DistributionMetadata
Greg Wardfe6462c2000-04-04 01:40:52 +00001061
Greg Ward2ff78872000-06-24 00:23:20 +00001062
1063def fix_help_options (options):
1064 """Convert a 4-tuple 'help_options' list as found in various command
1065 classes to the 3-tuple form required by FancyGetopt.
1066 """
1067 new_options = []
1068 for help_tuple in options:
1069 new_options.append(help_tuple[0:3])
1070 return new_options
1071
1072
Greg Wardfe6462c2000-04-04 01:40:52 +00001073if __name__ == "__main__":
Greg Wardfd7b91e2000-09-26 01:52:25 +00001074 dist = Distribution()
Greg Wardfe6462c2000-04-04 01:40:52 +00001075 print "ok"