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