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