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