Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1 | """distutils.dist |
| 2 | |
| 3 | Provides the Distribution class, which represents the module distribution |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 4 | being built/installed/distributed. |
| 5 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 6 | |
Andrew M. Kuchling | d448f66 | 2002-11-19 13:12:28 +0000 | [diff] [blame] | 7 | # This module should be kept compatible with Python 1.5.2. |
| 8 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 9 | __revision__ = "$Id$" |
| 10 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 11 | import sys, os, string, re |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 12 | from types import * |
| 13 | from copy import copy |
Andrew M. Kuchling | ff4ad9a | 2002-10-31 13:22:41 +0000 | [diff] [blame] | 14 | |
| 15 | try: |
| 16 | import warnings |
Andrew M. Kuchling | ccf4e42 | 2002-10-31 13:39:33 +0000 | [diff] [blame] | 17 | except ImportError: |
Andrew M. Kuchling | ff4ad9a | 2002-10-31 13:22:41 +0000 | [diff] [blame] | 18 | warnings = None |
| 19 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 20 | from distutils.errors import * |
Greg Ward | 2f2b6c6 | 2000-09-25 01:58:07 +0000 | [diff] [blame] | 21 | from distutils.fancy_getopt import FancyGetopt, translate_longopt |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 22 | from distutils.util import check_environ, strtobool, rfc822_escape |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 23 | from distutils import log |
Jeremy Hylton | fcd7353 | 2002-09-11 16:31:53 +0000 | [diff] [blame] | 24 | from distutils.debug import DEBUG |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 25 | |
| 26 | # Regex to define acceptable Distutils command names. This is not *quite* |
| 27 | # the same as a Python NAME -- I don't allow leading underscores. The fact |
| 28 | # that they're very similar is no coincidence; the default naming scheme is |
| 29 | # to look for a Python module named after the command. |
| 30 | command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$') |
| 31 | |
| 32 | |
| 33 | class Distribution: |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 34 | """The core of the Distutils. Most of the work hiding behind 'setup' |
| 35 | is really done within a Distribution instance, which farms the work out |
| 36 | to the Distutils commands specified on the command line. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 37 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 38 | Setup scripts will almost never instantiate Distribution directly, |
| 39 | unless the 'setup()' function is totally inadequate to their needs. |
| 40 | However, it is conceivable that a setup script might wish to subclass |
| 41 | Distribution for some specialized purpose, and then pass the subclass |
| 42 | to 'setup()' as the 'distclass' keyword argument. If so, it is |
| 43 | necessary to respect the expectations that 'setup' has of Distribution. |
| 44 | See the code for 'setup()', in core.py, for details. |
| 45 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | # 'global_options' describes the command-line options that may be |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 49 | # supplied to the setup script prior to any actual commands. |
| 50 | # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 51 | # these global options. This list should be kept to a bare minimum, |
| 52 | # since every global option is also valid as a command option -- and we |
| 53 | # don't want to pollute the commands with too many options that they |
| 54 | # have minimal control over. |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 55 | # The fourth entry for verbose means that it can be repeated. |
| 56 | global_options = [('verbose', 'v', "run verbosely (default)", 1), |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 57 | ('quiet', 'q', "run quietly (turns verbosity off)"), |
| 58 | ('dry-run', 'n', "don't actually do anything"), |
| 59 | ('help', 'h', "show detailed help message"), |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 60 | ] |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 61 | |
| 62 | # options that are not propagated to the commands |
| 63 | display_options = [ |
| 64 | ('help-commands', None, |
| 65 | "list all available commands"), |
| 66 | ('name', None, |
| 67 | "print package name"), |
| 68 | ('version', 'V', |
| 69 | "print package version"), |
| 70 | ('fullname', None, |
| 71 | "print <package name>-<version>"), |
| 72 | ('author', None, |
| 73 | "print the author's name"), |
| 74 | ('author-email', None, |
| 75 | "print the author's email address"), |
| 76 | ('maintainer', None, |
| 77 | "print the maintainer's name"), |
| 78 | ('maintainer-email', None, |
| 79 | "print the maintainer's email address"), |
| 80 | ('contact', None, |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 81 | "print the maintainer's name if known, else the author's"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 82 | ('contact-email', None, |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 83 | "print the maintainer's email address if known, else the author's"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 84 | ('url', None, |
| 85 | "print the URL for this package"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 86 | ('license', None, |
Andrew M. Kuchling | fa7dc57 | 2001-08-10 18:49:23 +0000 | [diff] [blame] | 87 | "print the license of the package"), |
| 88 | ('licence', None, |
| 89 | "alias for --license"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 90 | ('description', None, |
| 91 | "print the package description"), |
Greg Ward | e5a584e | 2000-04-26 02:26:55 +0000 | [diff] [blame] | 92 | ('long-description', None, |
| 93 | "print the long package description"), |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 94 | ('platforms', None, |
| 95 | "print the list of platforms"), |
Andrew M. Kuchling | 282e2c3 | 2003-01-03 15:24:36 +0000 | [diff] [blame] | 96 | ('classifiers', None, |
| 97 | "print the list of classifiers"), |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 98 | ('keywords', None, |
| 99 | "print the list of keywords"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 100 | ] |
Greg Ward | 2f2b6c6 | 2000-09-25 01:58:07 +0000 | [diff] [blame] | 101 | display_option_names = map(lambda x: translate_longopt(x[0]), |
| 102 | display_options) |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 103 | |
| 104 | # negative options are options that exclude other options |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 105 | negative_opt = {'quiet': 'verbose'} |
| 106 | |
| 107 | |
| 108 | # -- Creation/initialization methods ------------------------------- |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 109 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 110 | def __init__ (self, attrs=None): |
| 111 | """Construct a new Distribution instance: initialize all the |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 112 | attributes of a Distribution, and then use 'attrs' (a dictionary |
| 113 | mapping attribute names to values) to assign some of those |
| 114 | attributes their "real" values. (Any attributes not mentioned in |
| 115 | 'attrs' will be assigned to some null value: 0, None, an empty list |
| 116 | or dictionary, etc.) Most importantly, initialize the |
| 117 | 'command_obj' attribute to the empty dictionary; this will be |
| 118 | filled in with real command objects by 'parse_command_line()'. |
| 119 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 120 | |
| 121 | # Default values for our command-line options |
| 122 | self.verbose = 1 |
| 123 | self.dry_run = 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 124 | self.help = 0 |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 125 | for attr in self.display_option_names: |
| 126 | setattr(self, attr, 0) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 127 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 128 | # Store the distribution meta-data (name, version, author, and so |
| 129 | # forth) in a separate object -- we're getting to have enough |
| 130 | # information here (and enough command-line options) that it's |
| 131 | # worth it. Also delegate 'get_XXX()' methods to the 'metadata' |
| 132 | # object in a sneaky and underhanded (but efficient!) way. |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 133 | self.metadata = DistributionMetadata() |
Neil Schemenauer | a8aefe5 | 2001-09-03 15:47:21 +0000 | [diff] [blame] | 134 | for basename in self.metadata._METHOD_BASENAMES: |
Greg Ward | 4982f98 | 2000-04-22 02:52:44 +0000 | [diff] [blame] | 135 | method_name = "get_" + basename |
| 136 | setattr(self, method_name, getattr(self.metadata, method_name)) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 137 | |
| 138 | # 'cmdclass' maps command names to class objects, so we |
| 139 | # can 1) quickly figure out which class to instantiate when |
| 140 | # we need to create a new command object, and 2) have a way |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 141 | # for the setup script to override command classes |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 142 | self.cmdclass = {} |
| 143 | |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 144 | # 'script_name' and 'script_args' are usually set to sys.argv[0] |
| 145 | # and sys.argv[1:], but they can be overridden when the caller is |
| 146 | # not necessarily a setup script run from the command-line. |
| 147 | self.script_name = None |
| 148 | self.script_args = None |
| 149 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 150 | # 'command_options' is where we store command options between |
| 151 | # parsing them (from config files, the command-line, etc.) and when |
| 152 | # they are actually needed -- ie. when the command in question is |
| 153 | # instantiated. It is a dictionary of dictionaries of 2-tuples: |
| 154 | # command_options = { command_name : { option : (source, value) } } |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 155 | self.command_options = {} |
| 156 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 157 | # These options are really the business of various commands, rather |
| 158 | # than of the Distribution itself. We provide aliases for them in |
| 159 | # Distribution as a convenience to the developer. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 160 | self.packages = None |
| 161 | self.package_dir = None |
| 162 | self.py_modules = None |
| 163 | self.libraries = None |
Greg Ward | 51def7d | 2000-05-27 01:36:14 +0000 | [diff] [blame] | 164 | self.headers = None |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 165 | self.ext_modules = None |
| 166 | self.ext_package = None |
| 167 | self.include_dirs = None |
| 168 | self.extra_path = None |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 169 | self.scripts = None |
Gregory P. Smith | 6a901dd | 2000-05-13 03:09:50 +0000 | [diff] [blame] | 170 | self.data_files = None |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 171 | |
| 172 | # And now initialize bookkeeping stuff that can't be supplied by |
| 173 | # the caller at all. 'command_obj' maps command names to |
| 174 | # Command instances -- that's how we enforce that every command |
| 175 | # class is a singleton. |
| 176 | self.command_obj = {} |
| 177 | |
| 178 | # 'have_run' maps command names to boolean values; it keeps track |
| 179 | # of whether we have actually run a particular command, to make it |
| 180 | # cheap to "run" a command whenever we think we might need to -- if |
| 181 | # it's already been done, no need for expensive filesystem |
| 182 | # operations, we just check the 'have_run' dictionary and carry on. |
| 183 | # It's only safe to query 'have_run' for a command class that has |
| 184 | # been instantiated -- a false value will be inserted when the |
| 185 | # command object is created, and replaced with a true value when |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 186 | # the command is successfully run. Thus it's probably best to use |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 187 | # '.get()' rather than a straight lookup. |
| 188 | self.have_run = {} |
| 189 | |
| 190 | # Now we'll use the attrs dictionary (ultimately, keyword args from |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 191 | # the setup script) to possibly override any or all of these |
| 192 | # distribution options. |
| 193 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 194 | if attrs: |
| 195 | |
| 196 | # Pull out the set of command options and work on them |
| 197 | # specifically. Note that this order guarantees that aliased |
| 198 | # command options will override any supplied redundantly |
| 199 | # through the general options dictionary. |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 200 | options = attrs.get('options') |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 201 | if options: |
| 202 | del attrs['options'] |
| 203 | for (command, cmd_options) in options.items(): |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 204 | opt_dict = self.get_option_dict(command) |
| 205 | for (opt, val) in cmd_options.items(): |
| 206 | opt_dict[opt] = ("setup script", val) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 207 | |
Andrew M. Kuchling | a52b852 | 2003-03-03 20:07:27 +0000 | [diff] [blame] | 208 | if attrs.has_key('licence'): |
| 209 | attrs['license'] = attrs['licence'] |
| 210 | del attrs['licence'] |
| 211 | msg = "'licence' distribution option is deprecated; use 'license'" |
| 212 | if warnings is not None: |
| 213 | warnings.warn(msg) |
| 214 | else: |
| 215 | sys.stderr.write(msg + "\n") |
| 216 | |
Anthony Baxter | 05f842b | 2004-03-22 22:22:05 +0000 | [diff] [blame] | 217 | # Build up the requires sequence |
| 218 | from distutils.version import LooseVersion |
| 219 | requires = attrs.get('requires') |
| 220 | if requires: |
| 221 | if isinstance(requires, type('')): |
| 222 | raise DistutilsOptionError, 'requires should be a sequence' |
| 223 | newreq = [] |
| 224 | for req in requires: |
| 225 | if '-' not in req: |
| 226 | # We have a plain package name - any version will do |
| 227 | newreq.append((req,None)) |
| 228 | else: |
| 229 | pkg, ver = string.split(req, '-', 1) |
| 230 | newreq.append((pkg, LooseVersion(ver))) |
| 231 | attrs['requires'] = newreq |
| 232 | |
| 233 | # Build up the provides object. If the setup() has no |
| 234 | # provides line, we use packages or modules and the version |
| 235 | # to synthesise the provides. If no version is provided (no |
| 236 | # pun intended) we don't have a provides entry at all. |
| 237 | provides = attrs.get('provides') |
| 238 | if provides: |
| 239 | if isinstance(provides, type('')): |
| 240 | raise DistutilsOptionError, 'provides should be a sequence' |
| 241 | newprov = [] |
| 242 | for prov in provides: |
| 243 | if '-' not in prov: |
| 244 | # We have a plain package name - any version will do |
| 245 | newprov.append((prov,None)) |
| 246 | else: |
| 247 | pkg, ver = string.split(prov, '-', 1) |
| 248 | newprov.append((pkg, LooseVersion(ver))) |
| 249 | attrs['provides'] = newprov |
| 250 | elif attrs.get('version'): |
| 251 | # Build a provides line |
| 252 | prov = [] |
| 253 | if attrs.get('packages'): |
| 254 | for pkg in attrs['packages']: |
| 255 | pkg = string.replace(pkg, '/', '.') |
| 256 | prov.append('%s-%s'%(pkg, attrs['version'])) |
| 257 | elif attrs.get('modules'): |
| 258 | for mod in attrs['modules']: |
| 259 | prov.append('%s-%s'%(mod, attrs['version'])) |
| 260 | attrs['provides'] = prov |
| 261 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 262 | # Now work on the rest of the attributes. Any attribute that's |
| 263 | # not already defined is invalid! |
| 264 | for (key,val) in attrs.items(): |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 265 | if hasattr(self.metadata, key): |
| 266 | setattr(self.metadata, key, val) |
| 267 | elif hasattr(self, key): |
| 268 | setattr(self, key, val) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 269 | else: |
Andrew M. Kuchling | ff4ad9a | 2002-10-31 13:22:41 +0000 | [diff] [blame] | 270 | msg = "Unknown distribution option: %s" % repr(key) |
| 271 | if warnings is not None: |
| 272 | warnings.warn(msg) |
| 273 | else: |
| 274 | sys.stderr.write(msg + "\n") |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 275 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 276 | self.finalize_options() |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 277 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 278 | # __init__ () |
| 279 | |
| 280 | |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 281 | def get_option_dict (self, command): |
| 282 | """Get the option dictionary for a given command. If that |
| 283 | command's option dictionary hasn't been created yet, then create it |
| 284 | and return the new dictionary; otherwise, return the existing |
| 285 | option dictionary. |
| 286 | """ |
| 287 | |
| 288 | dict = self.command_options.get(command) |
| 289 | if dict is None: |
| 290 | dict = self.command_options[command] = {} |
| 291 | return dict |
| 292 | |
| 293 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 294 | def dump_option_dicts (self, header=None, commands=None, indent=""): |
| 295 | from pprint import pformat |
| 296 | |
| 297 | if commands is None: # dump all command option dicts |
| 298 | commands = self.command_options.keys() |
| 299 | commands.sort() |
| 300 | |
| 301 | if header is not None: |
| 302 | print indent + header |
| 303 | indent = indent + " " |
| 304 | |
| 305 | if not commands: |
| 306 | print indent + "no commands known yet" |
| 307 | return |
| 308 | |
| 309 | for cmd_name in commands: |
| 310 | opt_dict = self.command_options.get(cmd_name) |
| 311 | if opt_dict is None: |
| 312 | print indent + "no option dict for '%s' command" % cmd_name |
| 313 | else: |
| 314 | print indent + "option dict for '%s' command:" % cmd_name |
| 315 | out = pformat(opt_dict) |
| 316 | for line in string.split(out, "\n"): |
| 317 | print indent + " " + line |
| 318 | |
| 319 | # dump_option_dicts () |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 320 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 321 | |
| 322 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 323 | # -- Config file finding/parsing methods --------------------------- |
| 324 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 325 | def find_config_files (self): |
| 326 | """Find as many configuration files as should be processed for this |
| 327 | platform, and return a list of filenames in the order in which they |
| 328 | should be parsed. The filenames returned are guaranteed to exist |
| 329 | (modulo nasty race conditions). |
| 330 | |
Andrew M. Kuchling | d303b61 | 2001-12-06 16:32:05 +0000 | [diff] [blame] | 331 | There are three possible config files: distutils.cfg in the |
| 332 | Distutils installation directory (ie. where the top-level |
| 333 | Distutils __inst__.py file lives), a file in the user's home |
| 334 | directory named .pydistutils.cfg on Unix and pydistutils.cfg |
| 335 | on Windows/Mac, and setup.cfg in the current directory. |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 336 | """ |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 337 | files = [] |
Greg Ward | acf3f6a | 2000-06-07 02:26:19 +0000 | [diff] [blame] | 338 | check_environ() |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 339 | |
Greg Ward | 1169687 | 2000-06-07 02:29:03 +0000 | [diff] [blame] | 340 | # Where to look for the system-wide Distutils config file |
| 341 | sys_dir = os.path.dirname(sys.modules['distutils'].__file__) |
| 342 | |
| 343 | # Look for the system config file |
| 344 | sys_file = os.path.join(sys_dir, "distutils.cfg") |
Greg Ward | acf3f6a | 2000-06-07 02:26:19 +0000 | [diff] [blame] | 345 | if os.path.isfile(sys_file): |
| 346 | files.append(sys_file) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 347 | |
Greg Ward | 1169687 | 2000-06-07 02:29:03 +0000 | [diff] [blame] | 348 | # What to call the per-user config file |
| 349 | if os.name == 'posix': |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 350 | user_filename = ".pydistutils.cfg" |
| 351 | else: |
| 352 | user_filename = "pydistutils.cfg" |
Greg Ward | fa9ff76 | 2000-10-14 04:06:40 +0000 | [diff] [blame] | 353 | |
Greg Ward | 1169687 | 2000-06-07 02:29:03 +0000 | [diff] [blame] | 354 | # And look for the user config file |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 355 | if os.environ.has_key('HOME'): |
| 356 | user_file = os.path.join(os.environ.get('HOME'), user_filename) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 357 | if os.path.isfile(user_file): |
| 358 | files.append(user_file) |
| 359 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 360 | # All platforms support local setup.cfg |
| 361 | local_file = "setup.cfg" |
| 362 | if os.path.isfile(local_file): |
| 363 | files.append(local_file) |
| 364 | |
| 365 | return files |
| 366 | |
| 367 | # find_config_files () |
| 368 | |
| 369 | |
| 370 | def parse_config_files (self, filenames=None): |
| 371 | |
| 372 | from ConfigParser import ConfigParser |
| 373 | |
| 374 | if filenames is None: |
| 375 | filenames = self.find_config_files() |
| 376 | |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 377 | if DEBUG: print "Distribution.parse_config_files():" |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 378 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 379 | parser = ConfigParser() |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 380 | for filename in filenames: |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 381 | if DEBUG: print " reading", filename |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 382 | parser.read(filename) |
| 383 | for section in parser.sections(): |
| 384 | options = parser.options(section) |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 385 | opt_dict = self.get_option_dict(section) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 386 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 387 | for opt in options: |
| 388 | if opt != '__name__': |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 389 | val = parser.get(section,opt) |
| 390 | opt = string.replace(opt, '-', '_') |
| 391 | opt_dict[opt] = (filename, val) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 392 | |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 393 | # Make the ConfigParser forget everything (so we retain |
Fred Drake | f06116d | 2004-02-17 22:35:19 +0000 | [diff] [blame] | 394 | # the original filenames that options come from) |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 395 | parser.__init__() |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 396 | |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 397 | # If there was a "global" section in the config file, use it |
| 398 | # to set Distribution options. |
| 399 | |
| 400 | if self.command_options.has_key('global'): |
| 401 | for (opt, (src, val)) in self.command_options['global'].items(): |
| 402 | alias = self.negative_opt.get(opt) |
| 403 | try: |
| 404 | if alias: |
| 405 | setattr(self, alias, not strtobool(val)) |
| 406 | elif opt in ('verbose', 'dry_run'): # ugh! |
| 407 | setattr(self, opt, strtobool(val)) |
| 408 | except ValueError, msg: |
| 409 | raise DistutilsOptionError, msg |
| 410 | |
| 411 | # parse_config_files () |
| 412 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 413 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 414 | # -- Command-line parsing methods ---------------------------------- |
| 415 | |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 416 | def parse_command_line (self): |
| 417 | """Parse the setup script's command line, taken from the |
| 418 | 'script_args' instance attribute (which defaults to 'sys.argv[1:]' |
| 419 | -- see 'setup()' in core.py). This list is first processed for |
| 420 | "global options" -- options that set attributes of the Distribution |
| 421 | instance. Then, it is alternately scanned for Distutils commands |
| 422 | and options for that command. Each new command terminates the |
| 423 | options for the previous command. The allowed options for a |
| 424 | command are determined by the 'user_options' attribute of the |
| 425 | command class -- thus, we have to be able to load command classes |
| 426 | in order to parse the command line. Any error in that 'options' |
| 427 | attribute raises DistutilsGetoptError; any error on the |
| 428 | command-line raises DistutilsArgError. If no Distutils commands |
| 429 | were found on the command line, raises DistutilsArgError. Return |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 430 | true if command-line was successfully parsed and we should carry |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 431 | on with executing commands; false if no errors but we shouldn't |
| 432 | execute commands (currently, this only happens if user asks for |
| 433 | help). |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 434 | """ |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 435 | # |
Fred Drake | 981a178 | 2001-08-10 18:59:30 +0000 | [diff] [blame] | 436 | # We now have enough information to show the Macintosh dialog |
| 437 | # that allows the user to interactively specify the "command line". |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 438 | # |
| 439 | if sys.platform == 'mac': |
| 440 | import EasyDialogs |
| 441 | cmdlist = self.get_command_list() |
| 442 | self.script_args = EasyDialogs.GetArgv( |
| 443 | self.global_options + self.display_options, cmdlist) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 444 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 445 | # We have to parse the command line a bit at a time -- global |
| 446 | # options, then the first command, then its options, and so on -- |
| 447 | # because each command will be handled by a different class, and |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 448 | # the options that are valid for a particular class aren't known |
| 449 | # until we have loaded the command class, which doesn't happen |
| 450 | # until we know what the command is. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 451 | |
| 452 | self.commands = [] |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 453 | parser = FancyGetopt(self.global_options + self.display_options) |
| 454 | parser.set_negative_aliases(self.negative_opt) |
Andrew M. Kuchling | fa7dc57 | 2001-08-10 18:49:23 +0000 | [diff] [blame] | 455 | parser.set_aliases({'licence': 'license'}) |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 456 | args = parser.getopt(args=self.script_args, object=self) |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 457 | option_order = parser.get_option_order() |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 458 | log.set_verbosity(self.verbose) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 459 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 460 | # for display options we return immediately |
| 461 | if self.handle_display_options(option_order): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 462 | return |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 463 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 464 | while args: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 465 | args = self._parse_command_opts(parser, args) |
| 466 | if args is None: # user asked for help (and got it) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 467 | return |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 468 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 469 | # Handle the cases of --help as a "global" option, ie. |
| 470 | # "setup.py --help" and "setup.py --help command ...". For the |
| 471 | # former, we show global options (--verbose, --dry-run, etc.) |
| 472 | # and display-only options (--name, --version, etc.); for the |
| 473 | # latter, we omit the display-only options and show help for |
| 474 | # each command listed on the command line. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 475 | if self.help: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 476 | self._show_help(parser, |
| 477 | display_options=len(self.commands) == 0, |
| 478 | commands=self.commands) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 479 | return |
| 480 | |
| 481 | # Oops, no commands found -- an end-user error |
| 482 | if not self.commands: |
| 483 | raise DistutilsArgError, "no commands supplied" |
| 484 | |
| 485 | # All is well: return true |
| 486 | return 1 |
| 487 | |
| 488 | # parse_command_line() |
| 489 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 490 | def _parse_command_opts (self, parser, args): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 491 | """Parse the command-line options for a single command. |
| 492 | 'parser' must be a FancyGetopt instance; 'args' must be the list |
| 493 | of arguments, starting with the current command (whose options |
| 494 | we are about to parse). Returns a new version of 'args' with |
| 495 | the next command at the front of the list; will be the empty |
| 496 | list if there are no more commands on the command line. Returns |
| 497 | None if the user asked for help on this command. |
| 498 | """ |
| 499 | # late import because of mutual dependence between these modules |
| 500 | from distutils.cmd import Command |
| 501 | |
| 502 | # Pull the current command from the head of the command line |
| 503 | command = args[0] |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 504 | if not command_re.match(command): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 505 | raise SystemExit, "invalid command name '%s'" % command |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 506 | self.commands.append(command) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 507 | |
| 508 | # Dig up the command class that implements this command, so we |
| 509 | # 1) know that it's a valid command, and 2) know which options |
| 510 | # it takes. |
| 511 | try: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 512 | cmd_class = self.get_command_class(command) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 513 | except DistutilsModuleError, msg: |
| 514 | raise DistutilsArgError, msg |
| 515 | |
| 516 | # Require that the command class be derived from Command -- want |
| 517 | # to be sure that the basic "command" interface is implemented. |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 518 | if not issubclass(cmd_class, Command): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 519 | raise DistutilsClassError, \ |
| 520 | "command class %s must subclass Command" % cmd_class |
| 521 | |
| 522 | # Also make sure that the command object provides a list of its |
| 523 | # known options. |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 524 | if not (hasattr(cmd_class, 'user_options') and |
| 525 | type(cmd_class.user_options) is ListType): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 526 | raise DistutilsClassError, \ |
| 527 | ("command class %s must provide " + |
| 528 | "'user_options' attribute (a list of tuples)") % \ |
| 529 | cmd_class |
| 530 | |
| 531 | # If the command class has a list of negative alias options, |
| 532 | # merge it in with the global negative aliases. |
| 533 | negative_opt = self.negative_opt |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 534 | if hasattr(cmd_class, 'negative_opt'): |
| 535 | negative_opt = copy(negative_opt) |
| 536 | negative_opt.update(cmd_class.negative_opt) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 537 | |
Greg Ward | fa9ff76 | 2000-10-14 04:06:40 +0000 | [diff] [blame] | 538 | # Check for help_options in command class. They have a different |
| 539 | # format (tuple of four) so we need to preprocess them here. |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 540 | if (hasattr(cmd_class, 'help_options') and |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 541 | type(cmd_class.help_options) is ListType): |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 542 | help_options = fix_help_options(cmd_class.help_options) |
| 543 | else: |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 544 | help_options = [] |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 545 | |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 546 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 547 | # All commands support the global options too, just by adding |
| 548 | # in 'global_options'. |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 549 | parser.set_option_table(self.global_options + |
| 550 | cmd_class.user_options + |
| 551 | help_options) |
| 552 | parser.set_negative_aliases(negative_opt) |
| 553 | (args, opts) = parser.getopt(args[1:]) |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 554 | if hasattr(opts, 'help') and opts.help: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 555 | self._show_help(parser, display_options=0, commands=[cmd_class]) |
| 556 | return |
| 557 | |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 558 | if (hasattr(cmd_class, 'help_options') and |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 559 | type(cmd_class.help_options) is ListType): |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 560 | help_option_found=0 |
| 561 | for (help_option, short, desc, func) in cmd_class.help_options: |
| 562 | if hasattr(opts, parser.get_attr_name(help_option)): |
| 563 | help_option_found=1 |
Greg Ward | fa9ff76 | 2000-10-14 04:06:40 +0000 | [diff] [blame] | 564 | #print "showing help for option %s of command %s" % \ |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 565 | # (help_option[0],cmd_class) |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 566 | |
| 567 | if callable(func): |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 568 | func() |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 569 | else: |
Fred Drake | 981a178 | 2001-08-10 18:59:30 +0000 | [diff] [blame] | 570 | raise DistutilsClassError( |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 571 | "invalid help function %r for help option '%s': " |
Fred Drake | 981a178 | 2001-08-10 18:59:30 +0000 | [diff] [blame] | 572 | "must be a callable object (function, etc.)" |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 573 | % (func, help_option)) |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 574 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 575 | if help_option_found: |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 576 | return |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 577 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 578 | # Put the options from the command-line into their official |
| 579 | # holding pen, the 'command_options' dictionary. |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 580 | opt_dict = self.get_option_dict(command) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 581 | for (name, value) in vars(opts).items(): |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 582 | opt_dict[name] = ("command line", value) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 583 | |
| 584 | return args |
| 585 | |
| 586 | # _parse_command_opts () |
| 587 | |
| 588 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 589 | def finalize_options (self): |
| 590 | """Set final values for all the options on the Distribution |
| 591 | instance, analogous to the .finalize_options() method of Command |
| 592 | objects. |
| 593 | """ |
| 594 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 595 | keywords = self.metadata.keywords |
| 596 | if keywords is not None: |
| 597 | if type(keywords) is StringType: |
| 598 | keywordlist = string.split(keywords, ',') |
| 599 | self.metadata.keywords = map(string.strip, keywordlist) |
| 600 | |
| 601 | platforms = self.metadata.platforms |
| 602 | if platforms is not None: |
| 603 | if type(platforms) is StringType: |
| 604 | platformlist = string.split(platforms, ',') |
| 605 | self.metadata.platforms = map(string.strip, platformlist) |
| 606 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 607 | def _show_help (self, |
| 608 | parser, |
| 609 | global_options=1, |
| 610 | display_options=1, |
| 611 | commands=[]): |
| 612 | """Show help for the setup script command-line in the form of |
| 613 | several lists of command-line options. 'parser' should be a |
| 614 | FancyGetopt instance; do not expect it to be returned in the |
| 615 | same state, as its option table will be reset to make it |
| 616 | generate the correct help text. |
| 617 | |
| 618 | If 'global_options' is true, lists the global options: |
| 619 | --verbose, --dry-run, etc. If 'display_options' is true, lists |
| 620 | the "display-only" options: --name, --version, etc. Finally, |
| 621 | lists per-command help for every command name or command class |
| 622 | in 'commands'. |
| 623 | """ |
| 624 | # late import because of mutual dependence between these modules |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 625 | from distutils.core import gen_usage |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 626 | from distutils.cmd import Command |
| 627 | |
| 628 | if global_options: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 629 | parser.set_option_table(self.global_options) |
| 630 | parser.print_help("Global options:") |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 631 | print |
| 632 | |
| 633 | if display_options: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 634 | parser.set_option_table(self.display_options) |
| 635 | parser.print_help( |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 636 | "Information display options (just display " + |
| 637 | "information, ignore any commands)") |
| 638 | print |
| 639 | |
| 640 | for command in self.commands: |
Andrew M. Kuchling | fa7dc57 | 2001-08-10 18:49:23 +0000 | [diff] [blame] | 641 | if type(command) is ClassType and issubclass(command, Command): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 642 | klass = command |
| 643 | else: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 644 | klass = self.get_command_class(command) |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 645 | if (hasattr(klass, 'help_options') and |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 646 | type(klass.help_options) is ListType): |
| 647 | parser.set_option_table(klass.user_options + |
| 648 | fix_help_options(klass.help_options)) |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 649 | else: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 650 | parser.set_option_table(klass.user_options) |
| 651 | parser.print_help("Options for '%s' command:" % klass.__name__) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 652 | print |
| 653 | |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 654 | print gen_usage(self.script_name) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 655 | return |
| 656 | |
| 657 | # _show_help () |
Greg Ward | fa9ff76 | 2000-10-14 04:06:40 +0000 | [diff] [blame] | 658 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 659 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 660 | def handle_display_options (self, option_order): |
| 661 | """If there were any non-global "display-only" options |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 662 | (--help-commands or the metadata display options) on the command |
| 663 | line, display the requested info and return true; else return |
| 664 | false. |
| 665 | """ |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 666 | from distutils.core import gen_usage |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 667 | |
| 668 | # User just wants a list of commands -- we'll print it out and stop |
| 669 | # processing now (ie. if they ran "setup --help-commands foo bar", |
| 670 | # we ignore "foo bar"). |
| 671 | if self.help_commands: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 672 | self.print_commands() |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 673 | print |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 674 | print gen_usage(self.script_name) |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 675 | return 1 |
| 676 | |
| 677 | # If user supplied any of the "display metadata" options, then |
| 678 | # display that metadata in the order in which the user supplied the |
| 679 | # metadata options. |
| 680 | any_display_options = 0 |
| 681 | is_display_option = {} |
| 682 | for option in self.display_options: |
| 683 | is_display_option[option[0]] = 1 |
| 684 | |
| 685 | for (opt, val) in option_order: |
| 686 | if val and is_display_option.get(opt): |
Greg Ward | 2f2b6c6 | 2000-09-25 01:58:07 +0000 | [diff] [blame] | 687 | opt = translate_longopt(opt) |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 688 | value = getattr(self.metadata, "get_"+opt)() |
| 689 | if opt in ['keywords', 'platforms']: |
| 690 | print string.join(value, ',') |
Andrew M. Kuchling | 282e2c3 | 2003-01-03 15:24:36 +0000 | [diff] [blame] | 691 | elif opt == 'classifiers': |
| 692 | print string.join(value, '\n') |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 693 | else: |
| 694 | print value |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 695 | any_display_options = 1 |
| 696 | |
| 697 | return any_display_options |
| 698 | |
| 699 | # handle_display_options() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 700 | |
| 701 | def print_command_list (self, commands, header, max_length): |
| 702 | """Print a subset of the list of all commands -- used by |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 703 | 'print_commands()'. |
| 704 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 705 | |
| 706 | print header + ":" |
| 707 | |
| 708 | for cmd in commands: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 709 | klass = self.cmdclass.get(cmd) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 710 | if not klass: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 711 | klass = self.get_command_class(cmd) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 712 | try: |
| 713 | description = klass.description |
| 714 | except AttributeError: |
| 715 | description = "(no description available)" |
| 716 | |
| 717 | print " %-*s %s" % (max_length, cmd, description) |
| 718 | |
| 719 | # print_command_list () |
| 720 | |
| 721 | |
| 722 | def print_commands (self): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 723 | """Print out a help message listing all available commands with a |
| 724 | description of each. The list is divided into "standard commands" |
| 725 | (listed in distutils.command.__all__) and "extra commands" |
| 726 | (mentioned in self.cmdclass, but not a standard command). The |
| 727 | descriptions come from the command class attribute |
| 728 | 'description'. |
| 729 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 730 | |
| 731 | import distutils.command |
| 732 | std_commands = distutils.command.__all__ |
| 733 | is_std = {} |
| 734 | for cmd in std_commands: |
| 735 | is_std[cmd] = 1 |
| 736 | |
| 737 | extra_commands = [] |
| 738 | for cmd in self.cmdclass.keys(): |
| 739 | if not is_std.get(cmd): |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 740 | extra_commands.append(cmd) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 741 | |
| 742 | max_length = 0 |
| 743 | for cmd in (std_commands + extra_commands): |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 744 | if len(cmd) > max_length: |
| 745 | max_length = len(cmd) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 746 | |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 747 | self.print_command_list(std_commands, |
| 748 | "Standard commands", |
| 749 | max_length) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 750 | if extra_commands: |
| 751 | print |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 752 | self.print_command_list(extra_commands, |
| 753 | "Extra commands", |
| 754 | max_length) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 755 | |
| 756 | # print_commands () |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 757 | |
Greg Ward | f6fc875 | 2000-11-11 02:47:11 +0000 | [diff] [blame] | 758 | def get_command_list (self): |
| 759 | """Get a list of (command, description) tuples. |
| 760 | The list is divided into "standard commands" (listed in |
| 761 | distutils.command.__all__) and "extra commands" (mentioned in |
| 762 | self.cmdclass, but not a standard command). The descriptions come |
| 763 | from the command class attribute 'description'. |
| 764 | """ |
| 765 | # Currently this is only used on Mac OS, for the Mac-only GUI |
| 766 | # Distutils interface (by Jack Jansen) |
| 767 | |
| 768 | import distutils.command |
| 769 | std_commands = distutils.command.__all__ |
| 770 | is_std = {} |
| 771 | for cmd in std_commands: |
| 772 | is_std[cmd] = 1 |
| 773 | |
| 774 | extra_commands = [] |
| 775 | for cmd in self.cmdclass.keys(): |
| 776 | if not is_std.get(cmd): |
| 777 | extra_commands.append(cmd) |
| 778 | |
| 779 | rv = [] |
| 780 | for cmd in (std_commands + extra_commands): |
| 781 | klass = self.cmdclass.get(cmd) |
| 782 | if not klass: |
| 783 | klass = self.get_command_class(cmd) |
| 784 | try: |
| 785 | description = klass.description |
| 786 | except AttributeError: |
| 787 | description = "(no description available)" |
| 788 | rv.append((cmd, description)) |
| 789 | return rv |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 790 | |
| 791 | # -- Command class/object methods ---------------------------------- |
| 792 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 793 | def get_command_class (self, command): |
| 794 | """Return the class that implements the Distutils command named by |
| 795 | 'command'. First we check the 'cmdclass' dictionary; if the |
| 796 | command is mentioned there, we fetch the class object from the |
| 797 | dictionary and return it. Otherwise we load the command module |
| 798 | ("distutils.command." + command) and fetch the command class from |
| 799 | the module. The loaded class is also stored in 'cmdclass' |
| 800 | to speed future calls to 'get_command_class()'. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 801 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 802 | Raises DistutilsModuleError if the expected module could not be |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 803 | found, or if that module does not define the expected class. |
| 804 | """ |
| 805 | klass = self.cmdclass.get(command) |
| 806 | if klass: |
| 807 | return klass |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 808 | |
| 809 | module_name = 'distutils.command.' + command |
| 810 | klass_name = command |
| 811 | |
| 812 | try: |
| 813 | __import__ (module_name) |
| 814 | module = sys.modules[module_name] |
| 815 | except ImportError: |
| 816 | raise DistutilsModuleError, \ |
| 817 | "invalid command '%s' (no module named '%s')" % \ |
| 818 | (command, module_name) |
| 819 | |
| 820 | try: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 821 | klass = getattr(module, klass_name) |
| 822 | except AttributeError: |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 823 | raise DistutilsModuleError, \ |
| 824 | "invalid command '%s' (no class '%s' in module '%s')" \ |
| 825 | % (command, klass_name, module_name) |
| 826 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 827 | self.cmdclass[command] = klass |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 828 | return klass |
| 829 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 830 | # get_command_class () |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 831 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 832 | def get_command_obj (self, command, create=1): |
| 833 | """Return the command object for 'command'. Normally this object |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 834 | is cached on a previous call to 'get_command_obj()'; if no command |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 835 | object for 'command' is in the cache, then we either create and |
| 836 | return it (if 'create' is true) or return None. |
| 837 | """ |
| 838 | cmd_obj = self.command_obj.get(command) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 839 | if not cmd_obj and create: |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 840 | if DEBUG: |
| 841 | print "Distribution.get_command_obj(): " \ |
| 842 | "creating '%s' command object" % command |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 843 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 844 | klass = self.get_command_class(command) |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 845 | cmd_obj = self.command_obj[command] = klass(self) |
| 846 | self.have_run[command] = 0 |
| 847 | |
| 848 | # Set any options that were supplied in config files |
| 849 | # or on the command line. (NB. support for error |
| 850 | # reporting is lame here: any errors aren't reported |
| 851 | # until 'finalize_options()' is called, which means |
| 852 | # we won't report the source of the error.) |
| 853 | options = self.command_options.get(command) |
| 854 | if options: |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 855 | self._set_command_options(cmd_obj, options) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 856 | |
| 857 | return cmd_obj |
| 858 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 859 | def _set_command_options (self, command_obj, option_dict=None): |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 860 | """Set the options for 'command_obj' from 'option_dict'. Basically |
| 861 | this means copying elements of a dictionary ('option_dict') to |
| 862 | attributes of an instance ('command'). |
| 863 | |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 864 | 'command_obj' must be a Command instance. If 'option_dict' is not |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 865 | supplied, uses the standard option dictionary for this command |
| 866 | (from 'self.command_options'). |
| 867 | """ |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 868 | command_name = command_obj.get_command_name() |
| 869 | if option_dict is None: |
| 870 | option_dict = self.get_option_dict(command_name) |
| 871 | |
| 872 | if DEBUG: print " setting options for '%s' command:" % command_name |
| 873 | for (option, (source, value)) in option_dict.items(): |
| 874 | if DEBUG: print " %s = %s (from %s)" % (option, value, source) |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 875 | try: |
Greg Ward | 2f2b6c6 | 2000-09-25 01:58:07 +0000 | [diff] [blame] | 876 | bool_opts = map(translate_longopt, command_obj.boolean_options) |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 877 | except AttributeError: |
| 878 | bool_opts = [] |
| 879 | try: |
| 880 | neg_opt = command_obj.negative_opt |
| 881 | except AttributeError: |
| 882 | neg_opt = {} |
| 883 | |
| 884 | try: |
Greg Ward | 2c08cf0 | 2000-09-27 00:15:37 +0000 | [diff] [blame] | 885 | is_string = type(value) is StringType |
| 886 | if neg_opt.has_key(option) and is_string: |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 887 | setattr(command_obj, neg_opt[option], not strtobool(value)) |
Greg Ward | 2c08cf0 | 2000-09-27 00:15:37 +0000 | [diff] [blame] | 888 | elif option in bool_opts and is_string: |
Greg Ward | ceb9e22 | 2000-09-25 01:23:52 +0000 | [diff] [blame] | 889 | setattr(command_obj, option, strtobool(value)) |
| 890 | elif hasattr(command_obj, option): |
| 891 | setattr(command_obj, option, value) |
| 892 | else: |
| 893 | raise DistutilsOptionError, \ |
| 894 | ("error in %s: command '%s' has no such option '%s'" |
| 895 | % (source, command_name, option)) |
| 896 | except ValueError, msg: |
| 897 | raise DistutilsOptionError, msg |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 898 | |
Greg Ward | f449ea5 | 2000-09-16 15:23:28 +0000 | [diff] [blame] | 899 | def reinitialize_command (self, command, reinit_subcommands=0): |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 900 | """Reinitializes a command to the state it was in when first |
| 901 | returned by 'get_command_obj()': ie., initialized but not yet |
Greg Ward | 7d9c705 | 2000-06-28 01:25:27 +0000 | [diff] [blame] | 902 | finalized. This provides the opportunity to sneak option |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 903 | values in programmatically, overriding or supplementing |
| 904 | user-supplied values from the config files and command line. |
| 905 | You'll have to re-finalize the command object (by calling |
| 906 | 'finalize_options()' or 'ensure_finalized()') before using it for |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 907 | real. |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 908 | |
Greg Ward | f449ea5 | 2000-09-16 15:23:28 +0000 | [diff] [blame] | 909 | 'command' should be a command name (string) or command object. If |
| 910 | 'reinit_subcommands' is true, also reinitializes the command's |
| 911 | sub-commands, as declared by the 'sub_commands' class attribute (if |
| 912 | it has one). See the "install" command for an example. Only |
| 913 | reinitializes the sub-commands that actually matter, ie. those |
| 914 | whose test predicates return true. |
| 915 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 916 | Returns the reinitialized command object. |
| 917 | """ |
| 918 | from distutils.cmd import Command |
| 919 | if not isinstance(command, Command): |
| 920 | command_name = command |
| 921 | command = self.get_command_obj(command_name) |
| 922 | else: |
| 923 | command_name = command.get_command_name() |
| 924 | |
| 925 | if not command.finalized: |
Greg Ward | 282c7a0 | 2000-06-01 01:09:47 +0000 | [diff] [blame] | 926 | return command |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 927 | command.initialize_options() |
| 928 | command.finalized = 0 |
Greg Ward | 43955c9 | 2000-06-06 02:52:36 +0000 | [diff] [blame] | 929 | self.have_run[command_name] = 0 |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 930 | self._set_command_options(command) |
Greg Ward | f449ea5 | 2000-09-16 15:23:28 +0000 | [diff] [blame] | 931 | |
Greg Ward | f449ea5 | 2000-09-16 15:23:28 +0000 | [diff] [blame] | 932 | if reinit_subcommands: |
Greg Ward | f449ea5 | 2000-09-16 15:23:28 +0000 | [diff] [blame] | 933 | for sub in command.get_sub_commands(): |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 934 | self.reinitialize_command(sub, reinit_subcommands) |
Greg Ward | f449ea5 | 2000-09-16 15:23:28 +0000 | [diff] [blame] | 935 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 936 | return command |
| 937 | |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 938 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 939 | # -- Methods that operate on the Distribution ---------------------- |
| 940 | |
| 941 | def announce (self, msg, level=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 942 | log.debug(msg) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 943 | |
| 944 | def run_commands (self): |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 945 | """Run each command that was seen on the setup script command line. |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 946 | Uses the list of commands found and cache of command objects |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 947 | created by 'get_command_obj()'. |
| 948 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 949 | for cmd in self.commands: |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 950 | self.run_command(cmd) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 951 | |
| 952 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 953 | # -- Methods that operate on its Commands -------------------------- |
| 954 | |
| 955 | def run_command (self, command): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 956 | """Do whatever it takes to run a command (including nothing at all, |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 957 | if the command has already been run). Specifically: if we have |
| 958 | already created and run the command named by 'command', return |
| 959 | silently without doing anything. If the command named by 'command' |
| 960 | doesn't even have a command object yet, create one. Then invoke |
| 961 | 'run()' on that command object (or an existing one). |
| 962 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 963 | # Already been here, done that? then return silently. |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 964 | if self.have_run.get(command): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 965 | return |
| 966 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 967 | log.info("running %s", command) |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 968 | cmd_obj = self.get_command_obj(command) |
| 969 | cmd_obj.ensure_finalized() |
| 970 | cmd_obj.run() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 971 | self.have_run[command] = 1 |
| 972 | |
| 973 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 974 | # -- Distribution query methods ------------------------------------ |
| 975 | |
| 976 | def has_pure_modules (self): |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 977 | return len(self.packages or self.py_modules or []) > 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 978 | |
| 979 | def has_ext_modules (self): |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 980 | return self.ext_modules and len(self.ext_modules) > 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 981 | |
| 982 | def has_c_libraries (self): |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 983 | return self.libraries and len(self.libraries) > 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 984 | |
| 985 | def has_modules (self): |
| 986 | return self.has_pure_modules() or self.has_ext_modules() |
| 987 | |
Greg Ward | 51def7d | 2000-05-27 01:36:14 +0000 | [diff] [blame] | 988 | def has_headers (self): |
| 989 | return self.headers and len(self.headers) > 0 |
| 990 | |
Greg Ward | 44a61bb | 2000-05-20 15:06:48 +0000 | [diff] [blame] | 991 | def has_scripts (self): |
| 992 | return self.scripts and len(self.scripts) > 0 |
| 993 | |
| 994 | def has_data_files (self): |
| 995 | return self.data_files and len(self.data_files) > 0 |
| 996 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 997 | def is_pure (self): |
| 998 | return (self.has_pure_modules() and |
| 999 | not self.has_ext_modules() and |
| 1000 | not self.has_c_libraries()) |
| 1001 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1002 | # -- Metadata query methods ---------------------------------------- |
| 1003 | |
| 1004 | # If you're looking for 'get_name()', 'get_version()', and so forth, |
| 1005 | # they are defined in a sneaky way: the constructor binds self.get_XXX |
| 1006 | # to self.metadata.get_XXX. The actual code is in the |
| 1007 | # DistributionMetadata class, below. |
| 1008 | |
| 1009 | # class Distribution |
| 1010 | |
| 1011 | |
| 1012 | class DistributionMetadata: |
| 1013 | """Dummy class to hold the distribution meta-data: name, version, |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 1014 | author, and so forth. |
| 1015 | """ |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1016 | |
Neil Schemenauer | a8aefe5 | 2001-09-03 15:47:21 +0000 | [diff] [blame] | 1017 | _METHOD_BASENAMES = ("name", "version", "author", "author_email", |
| 1018 | "maintainer", "maintainer_email", "url", |
| 1019 | "license", "description", "long_description", |
| 1020 | "keywords", "platforms", "fullname", "contact", |
Andrew M. Kuchling | a52b852 | 2003-03-03 20:07:27 +0000 | [diff] [blame] | 1021 | "contact_email", "license", "classifiers", |
Anthony Baxter | 05f842b | 2004-03-22 22:22:05 +0000 | [diff] [blame] | 1022 | "download_url", "provides", "requires",) |
Neil Schemenauer | a8aefe5 | 2001-09-03 15:47:21 +0000 | [diff] [blame] | 1023 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1024 | def __init__ (self): |
| 1025 | self.name = None |
| 1026 | self.version = None |
| 1027 | self.author = None |
| 1028 | self.author_email = None |
| 1029 | self.maintainer = None |
| 1030 | self.maintainer_email = None |
| 1031 | self.url = None |
Andrew M. Kuchling | fa7dc57 | 2001-08-10 18:49:23 +0000 | [diff] [blame] | 1032 | self.license = None |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1033 | self.description = None |
Greg Ward | e5a584e | 2000-04-26 02:26:55 +0000 | [diff] [blame] | 1034 | self.long_description = None |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 1035 | self.keywords = None |
| 1036 | self.platforms = None |
Andrew M. Kuchling | 282e2c3 | 2003-01-03 15:24:36 +0000 | [diff] [blame] | 1037 | self.classifiers = None |
Andrew M. Kuchling | 188d85f | 2003-02-19 14:16:01 +0000 | [diff] [blame] | 1038 | self.download_url = None |
Anthony Baxter | 05f842b | 2004-03-22 22:22:05 +0000 | [diff] [blame] | 1039 | self.requires = [] |
| 1040 | self.provides = [] |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1041 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 1042 | def write_pkg_info (self, base_dir): |
| 1043 | """Write the PKG-INFO file into the release tree. |
| 1044 | """ |
| 1045 | |
| 1046 | pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w') |
| 1047 | |
| 1048 | pkg_info.write('Metadata-Version: 1.0\n') |
| 1049 | pkg_info.write('Name: %s\n' % self.get_name() ) |
| 1050 | pkg_info.write('Version: %s\n' % self.get_version() ) |
| 1051 | pkg_info.write('Summary: %s\n' % self.get_description() ) |
| 1052 | pkg_info.write('Home-page: %s\n' % self.get_url() ) |
Andrew M. Kuchling | ffb963c | 2001-03-22 15:32:23 +0000 | [diff] [blame] | 1053 | pkg_info.write('Author: %s\n' % self.get_contact() ) |
| 1054 | pkg_info.write('Author-email: %s\n' % self.get_contact_email() ) |
Andrew M. Kuchling | fa7dc57 | 2001-08-10 18:49:23 +0000 | [diff] [blame] | 1055 | pkg_info.write('License: %s\n' % self.get_license() ) |
Anthony Baxter | 05f842b | 2004-03-22 22:22:05 +0000 | [diff] [blame] | 1056 | for req in self.get_requires(): |
| 1057 | pkg_info.write('Requires: %s\n' % req ) |
| 1058 | for prov in self.get_provides(): |
| 1059 | pkg_info.write('Provides: %s\n' % prov ) |
Andrew M. Kuchling | 188d85f | 2003-02-19 14:16:01 +0000 | [diff] [blame] | 1060 | if self.download_url: |
| 1061 | pkg_info.write('Download-URL: %s\n' % self.download_url) |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 1062 | |
| 1063 | long_desc = rfc822_escape( self.get_long_description() ) |
| 1064 | pkg_info.write('Description: %s\n' % long_desc) |
| 1065 | |
| 1066 | keywords = string.join( self.get_keywords(), ',') |
| 1067 | if keywords: |
| 1068 | pkg_info.write('Keywords: %s\n' % keywords ) |
| 1069 | |
| 1070 | for platform in self.get_platforms(): |
| 1071 | pkg_info.write('Platform: %s\n' % platform ) |
| 1072 | |
Andrew M. Kuchling | 282e2c3 | 2003-01-03 15:24:36 +0000 | [diff] [blame] | 1073 | for classifier in self.get_classifiers(): |
| 1074 | pkg_info.write('Classifier: %s\n' % classifier ) |
| 1075 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 1076 | pkg_info.close() |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1077 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 1078 | # write_pkg_info () |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1079 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1080 | # -- Metadata query methods ---------------------------------------- |
| 1081 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1082 | def get_name (self): |
| 1083 | return self.name or "UNKNOWN" |
| 1084 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1085 | def get_version(self): |
Thomas Heller | bcd8975 | 2001-12-06 20:44:19 +0000 | [diff] [blame] | 1086 | return self.version or "0.0.0" |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1087 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1088 | def get_fullname (self): |
| 1089 | return "%s-%s" % (self.get_name(), self.get_version()) |
| 1090 | |
| 1091 | def get_author(self): |
| 1092 | return self.author or "UNKNOWN" |
| 1093 | |
| 1094 | def get_author_email(self): |
| 1095 | return self.author_email or "UNKNOWN" |
| 1096 | |
| 1097 | def get_maintainer(self): |
| 1098 | return self.maintainer or "UNKNOWN" |
| 1099 | |
| 1100 | def get_maintainer_email(self): |
| 1101 | return self.maintainer_email or "UNKNOWN" |
| 1102 | |
| 1103 | def get_contact(self): |
| 1104 | return (self.maintainer or |
| 1105 | self.author or |
| 1106 | "UNKNOWN") |
| 1107 | |
| 1108 | def get_contact_email(self): |
| 1109 | return (self.maintainer_email or |
| 1110 | self.author_email or |
| 1111 | "UNKNOWN") |
| 1112 | |
| 1113 | def get_url(self): |
| 1114 | return self.url or "UNKNOWN" |
| 1115 | |
Andrew M. Kuchling | fa7dc57 | 2001-08-10 18:49:23 +0000 | [diff] [blame] | 1116 | def get_license(self): |
| 1117 | return self.license or "UNKNOWN" |
| 1118 | get_licence = get_license |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 1119 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1120 | def get_description(self): |
| 1121 | return self.description or "UNKNOWN" |
Greg Ward | e5a584e | 2000-04-26 02:26:55 +0000 | [diff] [blame] | 1122 | |
| 1123 | def get_long_description(self): |
| 1124 | return self.long_description or "UNKNOWN" |
| 1125 | |
Andrew M. Kuchling | a7210ed | 2001-03-22 03:06:52 +0000 | [diff] [blame] | 1126 | def get_keywords(self): |
| 1127 | return self.keywords or [] |
| 1128 | |
| 1129 | def get_platforms(self): |
| 1130 | return self.platforms or ["UNKNOWN"] |
| 1131 | |
Andrew M. Kuchling | 282e2c3 | 2003-01-03 15:24:36 +0000 | [diff] [blame] | 1132 | def get_classifiers(self): |
| 1133 | return self.classifiers or [] |
| 1134 | |
Andrew M. Kuchling | 188d85f | 2003-02-19 14:16:01 +0000 | [diff] [blame] | 1135 | def get_download_url(self): |
| 1136 | return self.download_url or "UNKNOWN" |
| 1137 | |
Anthony Baxter | 05f842b | 2004-03-22 22:22:05 +0000 | [diff] [blame] | 1138 | def get_requires(self): |
| 1139 | return [ '%s%s%s'%(x, (y and '-') or '', y or '') |
| 1140 | for x,y in self.requires ] |
| 1141 | |
| 1142 | def get_provides(self): |
| 1143 | return self.provides |
| 1144 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 1145 | # class DistributionMetadata |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1146 | |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 1147 | |
| 1148 | def fix_help_options (options): |
| 1149 | """Convert a 4-tuple 'help_options' list as found in various command |
| 1150 | classes to the 3-tuple form required by FancyGetopt. |
| 1151 | """ |
| 1152 | new_options = [] |
| 1153 | for help_tuple in options: |
| 1154 | new_options.append(help_tuple[0:3]) |
| 1155 | return new_options |
| 1156 | |
| 1157 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1158 | if __name__ == "__main__": |
Greg Ward | fd7b91e | 2000-09-26 01:52:25 +0000 | [diff] [blame] | 1159 | dist = Distribution() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1160 | print "ok" |