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