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