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