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 | |
| 7 | # created 2000/04/03, Greg Ward |
| 8 | # (extricated from core.py; actually dates back to the beginning) |
| 9 | |
| 10 | __revision__ = "$Id$" |
| 11 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 12 | import sys, os, string, re |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 13 | from types import * |
| 14 | from copy import copy |
| 15 | from distutils.errors import * |
Greg Ward | 36c36fe | 2000-05-20 14:07:59 +0000 | [diff] [blame] | 16 | from distutils import sysconfig |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 17 | from distutils.fancy_getopt import FancyGetopt, longopt_xlate |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 18 | from distutils.util import check_environ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | # Regex to define acceptable Distutils command names. This is not *quite* |
| 22 | # the same as a Python NAME -- I don't allow leading underscores. The fact |
| 23 | # that they're very similar is no coincidence; the default naming scheme is |
| 24 | # to look for a Python module named after the command. |
| 25 | command_re = re.compile (r'^[a-zA-Z]([a-zA-Z0-9_]*)$') |
| 26 | |
| 27 | |
| 28 | class Distribution: |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 29 | """The core of the Distutils. Most of the work hiding behind 'setup' |
| 30 | is really done within a Distribution instance, which farms the work out |
| 31 | to the Distutils commands specified on the command line. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 32 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 33 | Setup scripts will almost never instantiate Distribution directly, |
| 34 | unless the 'setup()' function is totally inadequate to their needs. |
| 35 | However, it is conceivable that a setup script might wish to subclass |
| 36 | Distribution for some specialized purpose, and then pass the subclass |
| 37 | to 'setup()' as the 'distclass' keyword argument. If so, it is |
| 38 | necessary to respect the expectations that 'setup' has of Distribution. |
| 39 | See the code for 'setup()', in core.py, for details. |
| 40 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | # 'global_options' describes the command-line options that may be |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 44 | # supplied to the setup script prior to any actual commands. |
| 45 | # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 46 | # these global options. This list should be kept to a bare minimum, |
| 47 | # since every global option is also valid as a command option -- and we |
| 48 | # don't want to pollute the commands with too many options that they |
| 49 | # have minimal control over. |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 50 | global_options = [('verbose', 'v', "run verbosely (default)"), |
| 51 | ('quiet', 'q', "run quietly (turns verbosity off)"), |
| 52 | ('dry-run', 'n', "don't actually do anything"), |
| 53 | ('help', 'h', "show detailed help message"), |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 54 | ] |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 55 | |
| 56 | # options that are not propagated to the commands |
| 57 | display_options = [ |
| 58 | ('help-commands', None, |
| 59 | "list all available commands"), |
| 60 | ('name', None, |
| 61 | "print package name"), |
| 62 | ('version', 'V', |
| 63 | "print package version"), |
| 64 | ('fullname', None, |
| 65 | "print <package name>-<version>"), |
| 66 | ('author', None, |
| 67 | "print the author's name"), |
| 68 | ('author-email', None, |
| 69 | "print the author's email address"), |
| 70 | ('maintainer', None, |
| 71 | "print the maintainer's name"), |
| 72 | ('maintainer-email', None, |
| 73 | "print the maintainer's email address"), |
| 74 | ('contact', None, |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 75 | "print the maintainer's name if known, else the author's"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 76 | ('contact-email', None, |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 77 | "print the maintainer's email address if known, else the author's"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 78 | ('url', None, |
| 79 | "print the URL for this package"), |
| 80 | ('licence', None, |
| 81 | "print the licence of the package"), |
| 82 | ('license', None, |
| 83 | "alias for --licence"), |
| 84 | ('description', None, |
| 85 | "print the package description"), |
Greg Ward | e5a584e | 2000-04-26 02:26:55 +0000 | [diff] [blame] | 86 | ('long-description', None, |
| 87 | "print the long package description"), |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 88 | ] |
| 89 | display_option_names = map(lambda x: string.translate(x[0], longopt_xlate), |
| 90 | display_options) |
| 91 | |
| 92 | # negative options are options that exclude other options |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 93 | negative_opt = {'quiet': 'verbose'} |
| 94 | |
| 95 | |
| 96 | # -- Creation/initialization methods ------------------------------- |
| 97 | |
| 98 | def __init__ (self, attrs=None): |
| 99 | """Construct a new Distribution instance: initialize all the |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 100 | attributes of a Distribution, and then use 'attrs' (a dictionary |
| 101 | mapping attribute names to values) to assign some of those |
| 102 | attributes their "real" values. (Any attributes not mentioned in |
| 103 | 'attrs' will be assigned to some null value: 0, None, an empty list |
| 104 | or dictionary, etc.) Most importantly, initialize the |
| 105 | 'command_obj' attribute to the empty dictionary; this will be |
| 106 | filled in with real command objects by 'parse_command_line()'. |
| 107 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 108 | |
| 109 | # Default values for our command-line options |
| 110 | self.verbose = 1 |
| 111 | self.dry_run = 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 112 | self.help = 0 |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 113 | for attr in self.display_option_names: |
| 114 | setattr(self, attr, 0) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 115 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 116 | # Store the distribution meta-data (name, version, author, and so |
| 117 | # forth) in a separate object -- we're getting to have enough |
| 118 | # information here (and enough command-line options) that it's |
| 119 | # worth it. Also delegate 'get_XXX()' methods to the 'metadata' |
| 120 | # object in a sneaky and underhanded (but efficient!) way. |
| 121 | self.metadata = DistributionMetadata () |
Greg Ward | 4982f98 | 2000-04-22 02:52:44 +0000 | [diff] [blame] | 122 | method_basenames = dir(self.metadata) + \ |
| 123 | ['fullname', 'contact', 'contact_email'] |
| 124 | for basename in method_basenames: |
| 125 | method_name = "get_" + basename |
| 126 | setattr(self, method_name, getattr(self.metadata, method_name)) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 127 | |
| 128 | # 'cmdclass' maps command names to class objects, so we |
| 129 | # can 1) quickly figure out which class to instantiate when |
| 130 | # 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] | 131 | # for the setup script to override command classes |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 132 | self.cmdclass = {} |
| 133 | |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 134 | # 'script_name' and 'script_args' are usually set to sys.argv[0] |
| 135 | # and sys.argv[1:], but they can be overridden when the caller is |
| 136 | # not necessarily a setup script run from the command-line. |
| 137 | self.script_name = None |
| 138 | self.script_args = None |
| 139 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 140 | # 'command_options' is where we store command options between |
| 141 | # parsing them (from config files, the command-line, etc.) and when |
| 142 | # they are actually needed -- ie. when the command in question is |
| 143 | # instantiated. It is a dictionary of dictionaries of 2-tuples: |
| 144 | # command_options = { command_name : { option : (source, value) } } |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 145 | self.command_options = {} |
| 146 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 147 | # These options are really the business of various commands, rather |
| 148 | # than of the Distribution itself. We provide aliases for them in |
| 149 | # Distribution as a convenience to the developer. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 150 | self.packages = None |
| 151 | self.package_dir = None |
| 152 | self.py_modules = None |
| 153 | self.libraries = None |
Greg Ward | 51def7d | 2000-05-27 01:36:14 +0000 | [diff] [blame] | 154 | self.headers = None |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 155 | self.ext_modules = None |
| 156 | self.ext_package = None |
| 157 | self.include_dirs = None |
| 158 | self.extra_path = None |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 159 | self.scripts = None |
Gregory P. Smith | 6a901dd | 2000-05-13 03:09:50 +0000 | [diff] [blame] | 160 | self.data_files = None |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 161 | |
| 162 | # And now initialize bookkeeping stuff that can't be supplied by |
| 163 | # the caller at all. 'command_obj' maps command names to |
| 164 | # Command instances -- that's how we enforce that every command |
| 165 | # class is a singleton. |
| 166 | self.command_obj = {} |
| 167 | |
| 168 | # 'have_run' maps command names to boolean values; it keeps track |
| 169 | # of whether we have actually run a particular command, to make it |
| 170 | # cheap to "run" a command whenever we think we might need to -- if |
| 171 | # it's already been done, no need for expensive filesystem |
| 172 | # operations, we just check the 'have_run' dictionary and carry on. |
| 173 | # It's only safe to query 'have_run' for a command class that has |
| 174 | # been instantiated -- a false value will be inserted when the |
| 175 | # command object is created, and replaced with a true value when |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 176 | # the command is successfully run. Thus it's probably best to use |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 177 | # '.get()' rather than a straight lookup. |
| 178 | self.have_run = {} |
| 179 | |
| 180 | # Now we'll use the attrs dictionary (ultimately, keyword args from |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 181 | # the setup script) to possibly override any or all of these |
| 182 | # distribution options. |
| 183 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 184 | if attrs: |
| 185 | |
| 186 | # Pull out the set of command options and work on them |
| 187 | # specifically. Note that this order guarantees that aliased |
| 188 | # command options will override any supplied redundantly |
| 189 | # through the general options dictionary. |
| 190 | options = attrs.get ('options') |
| 191 | if options: |
| 192 | del attrs['options'] |
| 193 | for (command, cmd_options) in options.items(): |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 194 | opt_dict = self.get_option_dict(command) |
| 195 | for (opt, val) in cmd_options.items(): |
| 196 | opt_dict[opt] = ("setup script", val) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 197 | |
| 198 | # Now work on the rest of the attributes. Any attribute that's |
| 199 | # not already defined is invalid! |
| 200 | for (key,val) in attrs.items(): |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 201 | if hasattr (self.metadata, key): |
| 202 | setattr (self.metadata, key, val) |
| 203 | elif hasattr (self, key): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 204 | setattr (self, key, val) |
| 205 | else: |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 206 | raise DistutilsSetupError, \ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 207 | "invalid distribution option '%s'" % key |
| 208 | |
| 209 | # __init__ () |
| 210 | |
| 211 | |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 212 | def get_option_dict (self, command): |
| 213 | """Get the option dictionary for a given command. If that |
| 214 | command's option dictionary hasn't been created yet, then create it |
| 215 | and return the new dictionary; otherwise, return the existing |
| 216 | option dictionary. |
| 217 | """ |
| 218 | |
| 219 | dict = self.command_options.get(command) |
| 220 | if dict is None: |
| 221 | dict = self.command_options[command] = {} |
| 222 | return dict |
| 223 | |
| 224 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 225 | def dump_option_dicts (self, header=None, commands=None, indent=""): |
| 226 | from pprint import pformat |
| 227 | |
| 228 | if commands is None: # dump all command option dicts |
| 229 | commands = self.command_options.keys() |
| 230 | commands.sort() |
| 231 | |
| 232 | if header is not None: |
| 233 | print indent + header |
| 234 | indent = indent + " " |
| 235 | |
| 236 | if not commands: |
| 237 | print indent + "no commands known yet" |
| 238 | return |
| 239 | |
| 240 | for cmd_name in commands: |
| 241 | opt_dict = self.command_options.get(cmd_name) |
| 242 | if opt_dict is None: |
| 243 | print indent + "no option dict for '%s' command" % cmd_name |
| 244 | else: |
| 245 | print indent + "option dict for '%s' command:" % cmd_name |
| 246 | out = pformat(opt_dict) |
| 247 | for line in string.split(out, "\n"): |
| 248 | print indent + " " + line |
| 249 | |
| 250 | # dump_option_dicts () |
| 251 | |
| 252 | |
| 253 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 254 | # -- Config file finding/parsing methods --------------------------- |
| 255 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 256 | def find_config_files (self): |
| 257 | """Find as many configuration files as should be processed for this |
| 258 | platform, and return a list of filenames in the order in which they |
| 259 | should be parsed. The filenames returned are guaranteed to exist |
| 260 | (modulo nasty race conditions). |
| 261 | |
| 262 | On Unix, there are three possible config files: pydistutils.cfg in |
| 263 | the Distutils installation directory (ie. where the top-level |
| 264 | Distutils __inst__.py file lives), .pydistutils.cfg in the user's |
| 265 | home directory, and setup.cfg in the current directory. |
| 266 | |
| 267 | On Windows and Mac OS, there are two possible config files: |
| 268 | pydistutils.cfg in the Python installation directory (sys.prefix) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 269 | and setup.cfg in the current directory. |
| 270 | """ |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 271 | files = [] |
Greg Ward | acf3f6a | 2000-06-07 02:26:19 +0000 | [diff] [blame] | 272 | check_environ() |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 273 | |
Greg Ward | 1169687 | 2000-06-07 02:29:03 +0000 | [diff] [blame] | 274 | # Where to look for the system-wide Distutils config file |
| 275 | sys_dir = os.path.dirname(sys.modules['distutils'].__file__) |
| 276 | |
| 277 | # Look for the system config file |
| 278 | sys_file = os.path.join(sys_dir, "distutils.cfg") |
Greg Ward | acf3f6a | 2000-06-07 02:26:19 +0000 | [diff] [blame] | 279 | if os.path.isfile(sys_file): |
| 280 | files.append(sys_file) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 281 | |
Greg Ward | 1169687 | 2000-06-07 02:29:03 +0000 | [diff] [blame] | 282 | # What to call the per-user config file |
| 283 | if os.name == 'posix': |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 284 | user_filename = ".pydistutils.cfg" |
| 285 | else: |
| 286 | user_filename = "pydistutils.cfg" |
Greg Ward | 1169687 | 2000-06-07 02:29:03 +0000 | [diff] [blame] | 287 | |
| 288 | # And look for the user config file |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 289 | if os.environ.has_key('HOME'): |
| 290 | user_file = os.path.join(os.environ.get('HOME'), user_filename) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 291 | if os.path.isfile(user_file): |
| 292 | files.append(user_file) |
| 293 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 294 | # All platforms support local setup.cfg |
| 295 | local_file = "setup.cfg" |
| 296 | if os.path.isfile(local_file): |
| 297 | files.append(local_file) |
| 298 | |
| 299 | return files |
| 300 | |
| 301 | # find_config_files () |
| 302 | |
| 303 | |
| 304 | def parse_config_files (self, filenames=None): |
| 305 | |
| 306 | from ConfigParser import ConfigParser |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 307 | from distutils.core import DEBUG |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 308 | |
| 309 | if filenames is None: |
| 310 | filenames = self.find_config_files() |
| 311 | |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 312 | if DEBUG: print "Distribution.parse_config_files():" |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 313 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 314 | parser = ConfigParser() |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 315 | for filename in filenames: |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 316 | if DEBUG: print " reading", filename |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 317 | parser.read(filename) |
| 318 | for section in parser.sections(): |
| 319 | options = parser.options(section) |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 320 | opt_dict = self.get_option_dict(section) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 321 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 322 | for opt in options: |
| 323 | if opt != '__name__': |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 324 | opt_dict[opt] = (filename, parser.get(section,opt)) |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 325 | |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 326 | # Make the ConfigParser forget everything (so we retain |
| 327 | # the original filenames that options come from) -- gag, |
| 328 | # retch, puke -- another good reason for a distutils- |
| 329 | # specific config parser (sigh...) |
| 330 | parser.__init__() |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 331 | |
| 332 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 333 | # -- Command-line parsing methods ---------------------------------- |
| 334 | |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 335 | def parse_command_line (self): |
| 336 | """Parse the setup script's command line, taken from the |
| 337 | 'script_args' instance attribute (which defaults to 'sys.argv[1:]' |
| 338 | -- see 'setup()' in core.py). This list is first processed for |
| 339 | "global options" -- options that set attributes of the Distribution |
| 340 | instance. Then, it is alternately scanned for Distutils commands |
| 341 | and options for that command. Each new command terminates the |
| 342 | options for the previous command. The allowed options for a |
| 343 | command are determined by the 'user_options' attribute of the |
| 344 | command class -- thus, we have to be able to load command classes |
| 345 | in order to parse the command line. Any error in that 'options' |
| 346 | attribute raises DistutilsGetoptError; any error on the |
| 347 | command-line raises DistutilsArgError. If no Distutils commands |
| 348 | were found on the command line, raises DistutilsArgError. Return |
| 349 | true if command-line were successfully parsed and we should carry |
| 350 | on with executing commands; false if no errors but we shouldn't |
| 351 | execute commands (currently, this only happens if user asks for |
| 352 | help). |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 353 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 354 | # We have to parse the command line a bit at a time -- global |
| 355 | # options, then the first command, then its options, and so on -- |
| 356 | # because each command will be handled by a different class, and |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 357 | # the options that are valid for a particular class aren't known |
| 358 | # until we have loaded the command class, which doesn't happen |
| 359 | # until we know what the command is. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 360 | |
| 361 | self.commands = [] |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 362 | parser = FancyGetopt (self.global_options + self.display_options) |
| 363 | parser.set_negative_aliases (self.negative_opt) |
Greg Ward | 58ec6ed | 2000-04-21 04:22:49 +0000 | [diff] [blame] | 364 | parser.set_aliases ({'license': 'licence'}) |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 365 | args = parser.getopt (args=self.script_args, object=self) |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 366 | option_order = parser.get_option_order() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 367 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 368 | # for display options we return immediately |
| 369 | if self.handle_display_options(option_order): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 370 | return |
| 371 | |
| 372 | while args: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 373 | args = self._parse_command_opts(parser, args) |
| 374 | if args is None: # user asked for help (and got it) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 375 | return |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 376 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 377 | # Handle the cases of --help as a "global" option, ie. |
| 378 | # "setup.py --help" and "setup.py --help command ...". For the |
| 379 | # former, we show global options (--verbose, --dry-run, etc.) |
| 380 | # and display-only options (--name, --version, etc.); for the |
| 381 | # latter, we omit the display-only options and show help for |
| 382 | # each command listed on the command line. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 383 | if self.help: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 384 | self._show_help(parser, |
| 385 | display_options=len(self.commands) == 0, |
| 386 | commands=self.commands) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 387 | return |
| 388 | |
| 389 | # Oops, no commands found -- an end-user error |
| 390 | if not self.commands: |
| 391 | raise DistutilsArgError, "no commands supplied" |
| 392 | |
| 393 | # All is well: return true |
| 394 | return 1 |
| 395 | |
| 396 | # parse_command_line() |
| 397 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 398 | def _parse_command_opts (self, parser, args): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 399 | """Parse the command-line options for a single command. |
| 400 | 'parser' must be a FancyGetopt instance; 'args' must be the list |
| 401 | of arguments, starting with the current command (whose options |
| 402 | we are about to parse). Returns a new version of 'args' with |
| 403 | the next command at the front of the list; will be the empty |
| 404 | list if there are no more commands on the command line. Returns |
| 405 | None if the user asked for help on this command. |
| 406 | """ |
| 407 | # late import because of mutual dependence between these modules |
| 408 | from distutils.cmd import Command |
| 409 | |
| 410 | # Pull the current command from the head of the command line |
| 411 | command = args[0] |
| 412 | if not command_re.match (command): |
| 413 | raise SystemExit, "invalid command name '%s'" % command |
| 414 | self.commands.append (command) |
| 415 | |
| 416 | # Dig up the command class that implements this command, so we |
| 417 | # 1) know that it's a valid command, and 2) know which options |
| 418 | # it takes. |
| 419 | try: |
| 420 | cmd_class = self.get_command_class (command) |
| 421 | except DistutilsModuleError, msg: |
| 422 | raise DistutilsArgError, msg |
| 423 | |
| 424 | # Require that the command class be derived from Command -- want |
| 425 | # to be sure that the basic "command" interface is implemented. |
| 426 | if not issubclass (cmd_class, Command): |
| 427 | raise DistutilsClassError, \ |
| 428 | "command class %s must subclass Command" % cmd_class |
| 429 | |
| 430 | # Also make sure that the command object provides a list of its |
| 431 | # known options. |
| 432 | if not (hasattr (cmd_class, 'user_options') and |
| 433 | type (cmd_class.user_options) is ListType): |
| 434 | raise DistutilsClassError, \ |
| 435 | ("command class %s must provide " + |
| 436 | "'user_options' attribute (a list of tuples)") % \ |
| 437 | cmd_class |
| 438 | |
| 439 | # If the command class has a list of negative alias options, |
| 440 | # merge it in with the global negative aliases. |
| 441 | negative_opt = self.negative_opt |
| 442 | if hasattr (cmd_class, 'negative_opt'): |
| 443 | negative_opt = copy (negative_opt) |
| 444 | negative_opt.update (cmd_class.negative_opt) |
| 445 | |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 446 | # Check for help_options in command class. They have a different |
| 447 | # format (tuple of four) so we need to preprocess them here. |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 448 | if (hasattr(cmd_class, 'help_options') and |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 449 | type (cmd_class.help_options) is ListType): |
| 450 | help_options = fix_help_options(cmd_class.help_options) |
| 451 | else: |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 452 | help_options = [] |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 453 | |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 454 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 455 | # All commands support the global options too, just by adding |
| 456 | # in 'global_options'. |
| 457 | parser.set_option_table (self.global_options + |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 458 | cmd_class.user_options + |
| 459 | help_options) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 460 | parser.set_negative_aliases (negative_opt) |
| 461 | (args, opts) = parser.getopt (args[1:]) |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 462 | if hasattr(opts, 'help') and opts.help: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 463 | self._show_help(parser, display_options=0, commands=[cmd_class]) |
| 464 | return |
| 465 | |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 466 | if (hasattr(cmd_class, 'help_options') and |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 467 | type (cmd_class.help_options) is ListType): |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 468 | help_option_found=0 |
| 469 | for (help_option, short, desc, func) in cmd_class.help_options: |
| 470 | if hasattr(opts, parser.get_attr_name(help_option)): |
| 471 | help_option_found=1 |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 472 | #print "showing help for option %s of command %s" % \ |
| 473 | # (help_option[0],cmd_class) |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 474 | |
| 475 | if callable(func): |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 476 | func() |
Greg Ward | 55fced3 | 2000-06-24 01:22:41 +0000 | [diff] [blame] | 477 | else: |
| 478 | raise DistutilsClassError, \ |
| 479 | ("invalid help function %s for help option '%s': " |
| 480 | "must be a callable object (function, etc.)") % \ |
| 481 | (`func`, help_option) |
| 482 | |
| 483 | if help_option_found: |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 484 | return |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 485 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 486 | # Put the options from the command-line into their official |
| 487 | # holding pen, the 'command_options' dictionary. |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 488 | opt_dict = self.get_option_dict(command) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 489 | for (name, value) in vars(opts).items(): |
Greg Ward | 0e48cfd | 2000-05-26 01:00:15 +0000 | [diff] [blame] | 490 | opt_dict[name] = ("command line", value) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 491 | |
| 492 | return args |
| 493 | |
| 494 | # _parse_command_opts () |
| 495 | |
| 496 | |
| 497 | def _show_help (self, |
| 498 | parser, |
| 499 | global_options=1, |
| 500 | display_options=1, |
| 501 | commands=[]): |
| 502 | """Show help for the setup script command-line in the form of |
| 503 | several lists of command-line options. 'parser' should be a |
| 504 | FancyGetopt instance; do not expect it to be returned in the |
| 505 | same state, as its option table will be reset to make it |
| 506 | generate the correct help text. |
| 507 | |
| 508 | If 'global_options' is true, lists the global options: |
| 509 | --verbose, --dry-run, etc. If 'display_options' is true, lists |
| 510 | the "display-only" options: --name, --version, etc. Finally, |
| 511 | lists per-command help for every command name or command class |
| 512 | in 'commands'. |
| 513 | """ |
| 514 | # late import because of mutual dependence between these modules |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 515 | from distutils.core import gen_usage |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 516 | from distutils.cmd import Command |
| 517 | |
| 518 | if global_options: |
| 519 | parser.set_option_table (self.global_options) |
| 520 | parser.print_help ("Global options:") |
| 521 | print |
| 522 | |
| 523 | if display_options: |
| 524 | parser.set_option_table (self.display_options) |
| 525 | parser.print_help ( |
| 526 | "Information display options (just display " + |
| 527 | "information, ignore any commands)") |
| 528 | print |
| 529 | |
| 530 | for command in self.commands: |
| 531 | if type(command) is ClassType and issubclass(klass, Command): |
| 532 | klass = command |
| 533 | else: |
| 534 | klass = self.get_command_class (command) |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 535 | if (hasattr(klass, 'help_options') and |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 536 | type (klass.help_options) is ListType): |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 537 | parser.set_option_table (klass.user_options + |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 538 | fix_help_options(klass.help_options)) |
Jeremy Hylton | 65d6edb | 2000-07-07 20:45:21 +0000 | [diff] [blame] | 539 | else: |
| 540 | parser.set_option_table (klass.user_options) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 541 | parser.print_help ("Options for '%s' command:" % klass.__name__) |
| 542 | print |
| 543 | |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 544 | print gen_usage(self.script_name) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 545 | return |
| 546 | |
| 547 | # _show_help () |
Greg Ward | 9d17a7a | 2000-06-07 03:00:06 +0000 | [diff] [blame] | 548 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 549 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 550 | def handle_display_options (self, option_order): |
| 551 | """If there were any non-global "display-only" options |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 552 | (--help-commands or the metadata display options) on the command |
| 553 | line, display the requested info and return true; else return |
| 554 | false. |
| 555 | """ |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 556 | from distutils.core import gen_usage |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 557 | |
| 558 | # User just wants a list of commands -- we'll print it out and stop |
| 559 | # processing now (ie. if they ran "setup --help-commands foo bar", |
| 560 | # we ignore "foo bar"). |
| 561 | if self.help_commands: |
| 562 | self.print_commands () |
| 563 | print |
Greg Ward | 9821bf4 | 2000-08-29 01:15:18 +0000 | [diff] [blame] | 564 | print gen_usage(self.script_name) |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 565 | return 1 |
| 566 | |
| 567 | # If user supplied any of the "display metadata" options, then |
| 568 | # display that metadata in the order in which the user supplied the |
| 569 | # metadata options. |
| 570 | any_display_options = 0 |
| 571 | is_display_option = {} |
| 572 | for option in self.display_options: |
| 573 | is_display_option[option[0]] = 1 |
| 574 | |
| 575 | for (opt, val) in option_order: |
| 576 | if val and is_display_option.get(opt): |
| 577 | opt = string.translate (opt, longopt_xlate) |
| 578 | print getattr(self.metadata, "get_"+opt)() |
| 579 | any_display_options = 1 |
| 580 | |
| 581 | return any_display_options |
| 582 | |
| 583 | # handle_display_options() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 584 | |
| 585 | def print_command_list (self, commands, header, max_length): |
| 586 | """Print a subset of the list of all commands -- used by |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 587 | 'print_commands()'. |
| 588 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 589 | |
| 590 | print header + ":" |
| 591 | |
| 592 | for cmd in commands: |
| 593 | klass = self.cmdclass.get (cmd) |
| 594 | if not klass: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 595 | klass = self.get_command_class (cmd) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 596 | try: |
| 597 | description = klass.description |
| 598 | except AttributeError: |
| 599 | description = "(no description available)" |
| 600 | |
| 601 | print " %-*s %s" % (max_length, cmd, description) |
| 602 | |
| 603 | # print_command_list () |
| 604 | |
| 605 | |
| 606 | def print_commands (self): |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 607 | """Print out a help message listing all available commands with a |
| 608 | description of each. The list is divided into "standard commands" |
| 609 | (listed in distutils.command.__all__) and "extra commands" |
| 610 | (mentioned in self.cmdclass, but not a standard command). The |
| 611 | descriptions come from the command class attribute |
| 612 | 'description'. |
| 613 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 614 | |
| 615 | import distutils.command |
| 616 | std_commands = distutils.command.__all__ |
| 617 | is_std = {} |
| 618 | for cmd in std_commands: |
| 619 | is_std[cmd] = 1 |
| 620 | |
| 621 | extra_commands = [] |
| 622 | for cmd in self.cmdclass.keys(): |
| 623 | if not is_std.get(cmd): |
| 624 | extra_commands.append (cmd) |
| 625 | |
| 626 | max_length = 0 |
| 627 | for cmd in (std_commands + extra_commands): |
| 628 | if len (cmd) > max_length: |
| 629 | max_length = len (cmd) |
| 630 | |
| 631 | self.print_command_list (std_commands, |
| 632 | "Standard commands", |
| 633 | max_length) |
| 634 | if extra_commands: |
| 635 | print |
| 636 | self.print_command_list (extra_commands, |
| 637 | "Extra commands", |
| 638 | max_length) |
| 639 | |
| 640 | # print_commands () |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 641 | |
| 642 | |
| 643 | # -- Command class/object methods ---------------------------------- |
| 644 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 645 | def get_command_class (self, command): |
| 646 | """Return the class that implements the Distutils command named by |
| 647 | 'command'. First we check the 'cmdclass' dictionary; if the |
| 648 | command is mentioned there, we fetch the class object from the |
| 649 | dictionary and return it. Otherwise we load the command module |
| 650 | ("distutils.command." + command) and fetch the command class from |
| 651 | the module. The loaded class is also stored in 'cmdclass' |
| 652 | to speed future calls to 'get_command_class()'. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 653 | |
Gregory P. Smith | 1426354 | 2000-05-12 00:41:33 +0000 | [diff] [blame] | 654 | Raises DistutilsModuleError if the expected module could not be |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 655 | found, or if that module does not define the expected class. |
| 656 | """ |
| 657 | klass = self.cmdclass.get(command) |
| 658 | if klass: |
| 659 | return klass |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 660 | |
| 661 | module_name = 'distutils.command.' + command |
| 662 | klass_name = command |
| 663 | |
| 664 | try: |
| 665 | __import__ (module_name) |
| 666 | module = sys.modules[module_name] |
| 667 | except ImportError: |
| 668 | raise DistutilsModuleError, \ |
| 669 | "invalid command '%s' (no module named '%s')" % \ |
| 670 | (command, module_name) |
| 671 | |
| 672 | try: |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 673 | klass = getattr(module, klass_name) |
| 674 | except AttributeError: |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 675 | raise DistutilsModuleError, \ |
| 676 | "invalid command '%s' (no class '%s' in module '%s')" \ |
| 677 | % (command, klass_name, module_name) |
| 678 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 679 | self.cmdclass[command] = klass |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 680 | return klass |
| 681 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 682 | # get_command_class () |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 683 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 684 | def get_command_obj (self, command, create=1): |
| 685 | """Return the command object for 'command'. Normally this object |
Greg Ward | 612eb9f | 2000-07-27 02:13:20 +0000 | [diff] [blame] | 686 | 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] | 687 | object for 'command' is in the cache, then we either create and |
| 688 | return it (if 'create' is true) or return None. |
| 689 | """ |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 690 | from distutils.core import DEBUG |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 691 | cmd_obj = self.command_obj.get(command) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 692 | if not cmd_obj and create: |
Greg Ward | 2bd3f42 | 2000-06-02 01:59:33 +0000 | [diff] [blame] | 693 | if DEBUG: |
| 694 | print "Distribution.get_command_obj(): " \ |
| 695 | "creating '%s' command object" % command |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 696 | |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 697 | klass = self.get_command_class(command) |
Greg Ward | 4746077 | 2000-05-23 03:47:35 +0000 | [diff] [blame] | 698 | cmd_obj = self.command_obj[command] = klass(self) |
| 699 | self.have_run[command] = 0 |
| 700 | |
| 701 | # Set any options that were supplied in config files |
| 702 | # or on the command line. (NB. support for error |
| 703 | # reporting is lame here: any errors aren't reported |
| 704 | # until 'finalize_options()' is called, which means |
| 705 | # we won't report the source of the error.) |
| 706 | options = self.command_options.get(command) |
| 707 | if options: |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 708 | self._set_command_options(cmd_obj, options) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 709 | |
| 710 | return cmd_obj |
| 711 | |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 712 | def _set_command_options (self, command_obj, option_dict=None): |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 713 | """Set the options for 'command_obj' from 'option_dict'. Basically |
| 714 | this means copying elements of a dictionary ('option_dict') to |
| 715 | attributes of an instance ('command'). |
| 716 | |
| 717 | 'command_obj' must be a Commnd instance. If 'option_dict' is not |
| 718 | supplied, uses the standard option dictionary for this command |
| 719 | (from 'self.command_options'). |
| 720 | """ |
| 721 | from distutils.core import DEBUG |
| 722 | |
| 723 | command_name = command_obj.get_command_name() |
| 724 | if option_dict is None: |
| 725 | option_dict = self.get_option_dict(command_name) |
| 726 | |
| 727 | if DEBUG: print " setting options for '%s' command:" % command_name |
| 728 | for (option, (source, value)) in option_dict.items(): |
| 729 | if DEBUG: print " %s = %s (from %s)" % (option, value, source) |
| 730 | if not hasattr(command_obj, option): |
| 731 | raise DistutilsOptionError, \ |
| 732 | ("error in %s: command '%s' has no such option '%s'") % \ |
| 733 | (source, command_name, option) |
| 734 | setattr(command_obj, option, value) |
| 735 | |
| 736 | def reinitialize_command (self, command): |
| 737 | """Reinitializes a command to the state it was in when first |
| 738 | returned by 'get_command_obj()': ie., initialized but not yet |
Greg Ward | 7d9c705 | 2000-06-28 01:25:27 +0000 | [diff] [blame] | 739 | finalized. This provides the opportunity to sneak option |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 740 | values in programmatically, overriding or supplementing |
| 741 | user-supplied values from the config files and command line. |
| 742 | You'll have to re-finalize the command object (by calling |
| 743 | 'finalize_options()' or 'ensure_finalized()') before using it for |
| 744 | real. |
| 745 | |
| 746 | 'command' should be a command name (string) or command object. |
| 747 | Returns the reinitialized command object. |
| 748 | """ |
| 749 | from distutils.cmd import Command |
| 750 | if not isinstance(command, Command): |
| 751 | command_name = command |
| 752 | command = self.get_command_obj(command_name) |
| 753 | else: |
| 754 | command_name = command.get_command_name() |
| 755 | |
| 756 | if not command.finalized: |
Greg Ward | 282c7a0 | 2000-06-01 01:09:47 +0000 | [diff] [blame] | 757 | return command |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 758 | command.initialize_options() |
| 759 | command.finalized = 0 |
Greg Ward | 43955c9 | 2000-06-06 02:52:36 +0000 | [diff] [blame] | 760 | self.have_run[command_name] = 0 |
Greg Ward | c32d9a6 | 2000-05-28 23:53:06 +0000 | [diff] [blame] | 761 | self._set_command_options(command) |
| 762 | return command |
| 763 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 764 | |
| 765 | # -- Methods that operate on the Distribution ---------------------- |
| 766 | |
| 767 | def announce (self, msg, level=1): |
| 768 | """Print 'msg' if 'level' is greater than or equal to the verbosity |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 769 | level recorded in the 'verbose' attribute (which, currently, can be |
| 770 | only 0 or 1). |
| 771 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 772 | if self.verbose >= level: |
| 773 | print msg |
| 774 | |
| 775 | |
| 776 | def run_commands (self): |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 777 | """Run each command that was seen on the setup script command line. |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 778 | Uses the list of commands found and cache of command objects |
| 779 | created by 'get_command_obj()'.""" |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 780 | |
| 781 | for cmd in self.commands: |
| 782 | self.run_command (cmd) |
| 783 | |
| 784 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 785 | # -- Methods that operate on its Commands -------------------------- |
| 786 | |
| 787 | def run_command (self, command): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 788 | """Do whatever it takes to run a command (including nothing at all, |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 789 | if the command has already been run). Specifically: if we have |
| 790 | already created and run the command named by 'command', return |
| 791 | silently without doing anything. If the command named by 'command' |
| 792 | doesn't even have a command object yet, create one. Then invoke |
| 793 | 'run()' on that command object (or an existing one). |
| 794 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 795 | |
| 796 | # Already been here, done that? then return silently. |
| 797 | if self.have_run.get (command): |
| 798 | return |
| 799 | |
| 800 | self.announce ("running " + command) |
Greg Ward | d5d8a99 | 2000-05-23 01:42:17 +0000 | [diff] [blame] | 801 | cmd_obj = self.get_command_obj (command) |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 802 | cmd_obj.ensure_finalized () |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 803 | cmd_obj.run () |
| 804 | self.have_run[command] = 1 |
| 805 | |
| 806 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 807 | # -- Distribution query methods ------------------------------------ |
| 808 | |
| 809 | def has_pure_modules (self): |
| 810 | return len (self.packages or self.py_modules or []) > 0 |
| 811 | |
| 812 | def has_ext_modules (self): |
| 813 | return self.ext_modules and len (self.ext_modules) > 0 |
| 814 | |
| 815 | def has_c_libraries (self): |
| 816 | return self.libraries and len (self.libraries) > 0 |
| 817 | |
| 818 | def has_modules (self): |
| 819 | return self.has_pure_modules() or self.has_ext_modules() |
| 820 | |
Greg Ward | 51def7d | 2000-05-27 01:36:14 +0000 | [diff] [blame] | 821 | def has_headers (self): |
| 822 | return self.headers and len(self.headers) > 0 |
| 823 | |
Greg Ward | 44a61bb | 2000-05-20 15:06:48 +0000 | [diff] [blame] | 824 | def has_scripts (self): |
| 825 | return self.scripts and len(self.scripts) > 0 |
| 826 | |
| 827 | def has_data_files (self): |
| 828 | return self.data_files and len(self.data_files) > 0 |
| 829 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 830 | def is_pure (self): |
| 831 | return (self.has_pure_modules() and |
| 832 | not self.has_ext_modules() and |
| 833 | not self.has_c_libraries()) |
| 834 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 835 | # -- Metadata query methods ---------------------------------------- |
| 836 | |
| 837 | # If you're looking for 'get_name()', 'get_version()', and so forth, |
| 838 | # they are defined in a sneaky way: the constructor binds self.get_XXX |
| 839 | # to self.metadata.get_XXX. The actual code is in the |
| 840 | # DistributionMetadata class, below. |
| 841 | |
| 842 | # class Distribution |
| 843 | |
| 844 | |
| 845 | class DistributionMetadata: |
| 846 | """Dummy class to hold the distribution meta-data: name, version, |
| 847 | author, and so forth.""" |
| 848 | |
| 849 | def __init__ (self): |
| 850 | self.name = None |
| 851 | self.version = None |
| 852 | self.author = None |
| 853 | self.author_email = None |
| 854 | self.maintainer = None |
| 855 | self.maintainer_email = None |
| 856 | self.url = None |
| 857 | self.licence = None |
| 858 | self.description = None |
Greg Ward | e5a584e | 2000-04-26 02:26:55 +0000 | [diff] [blame] | 859 | self.long_description = None |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 860 | |
| 861 | # -- Metadata query methods ---------------------------------------- |
| 862 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 863 | def get_name (self): |
| 864 | return self.name or "UNKNOWN" |
| 865 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 866 | def get_version(self): |
| 867 | return self.version or "???" |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 868 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 869 | def get_fullname (self): |
| 870 | return "%s-%s" % (self.get_name(), self.get_version()) |
| 871 | |
| 872 | def get_author(self): |
| 873 | return self.author or "UNKNOWN" |
| 874 | |
| 875 | def get_author_email(self): |
| 876 | return self.author_email or "UNKNOWN" |
| 877 | |
| 878 | def get_maintainer(self): |
| 879 | return self.maintainer or "UNKNOWN" |
| 880 | |
| 881 | def get_maintainer_email(self): |
| 882 | return self.maintainer_email or "UNKNOWN" |
| 883 | |
| 884 | def get_contact(self): |
| 885 | return (self.maintainer or |
| 886 | self.author or |
| 887 | "UNKNOWN") |
| 888 | |
| 889 | def get_contact_email(self): |
| 890 | return (self.maintainer_email or |
| 891 | self.author_email or |
| 892 | "UNKNOWN") |
| 893 | |
| 894 | def get_url(self): |
| 895 | return self.url or "UNKNOWN" |
| 896 | |
| 897 | def get_licence(self): |
| 898 | return self.licence or "UNKNOWN" |
| 899 | |
| 900 | def get_description(self): |
| 901 | return self.description or "UNKNOWN" |
Greg Ward | e5a584e | 2000-04-26 02:26:55 +0000 | [diff] [blame] | 902 | |
| 903 | def get_long_description(self): |
| 904 | return self.long_description or "UNKNOWN" |
| 905 | |
Greg Ward | 82715e1 | 2000-04-21 02:28:14 +0000 | [diff] [blame] | 906 | # class DistributionMetadata |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 907 | |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 908 | |
| 909 | def fix_help_options (options): |
| 910 | """Convert a 4-tuple 'help_options' list as found in various command |
| 911 | classes to the 3-tuple form required by FancyGetopt. |
| 912 | """ |
| 913 | new_options = [] |
| 914 | for help_tuple in options: |
| 915 | new_options.append(help_tuple[0:3]) |
| 916 | return new_options |
| 917 | |
| 918 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 919 | if __name__ == "__main__": |
| 920 | dist = Distribution () |
| 921 | print "ok" |