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