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