blob: 6cf0a0d6632dc7533b4d92d7ba07868912e4224c [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.dist
2
3Provides the Distribution class, which represents the module distribution
Greg Ward8ff5a3f2000-06-02 00:44:53 +00004being built/installed/distributed.
5"""
Greg Wardfe6462c2000-04-04 01:40:52 +00006
Jason R. Coombs7c456322014-07-02 08:36:19 -04007import sys
8import os
9import re
Jason R. Coombs3492e392013-11-10 18:15:03 -050010from email import message_from_file
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000011
12try:
13 import warnings
Andrew M. Kuchlingccf4e422002-10-31 13:39:33 +000014except ImportError:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +000015 warnings = None
16
Tarek Ziadé36797272010-07-22 12:50:05 +000017from distutils.errors import *
Greg Ward2f2b6c62000-09-25 01:58:07 +000018from distutils.fancy_getopt import FancyGetopt, translate_longopt
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +000019from distutils.util import check_environ, strtobool, rfc822_escape
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000020from distutils import log
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000021from distutils.debug import DEBUG
Greg Wardfe6462c2000-04-04 01:40:52 +000022
23# Regex to define acceptable Distutils command names. This is not *quite*
24# the same as a Python NAME -- I don't allow leading underscores. The fact
25# that they're very similar is no coincidence; the default naming scheme is
26# to look for a Python module named after the command.
Jason R. Coombs7c456322014-07-02 08:36:19 -040027command_re = re.compile(r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
Greg Wardfe6462c2000-04-04 01:40:52 +000028
29
Neil Schemenauer8837dd02017-12-04 18:58:12 -080030def _ensure_list(value, fieldname):
31 if isinstance(value, str):
32 # a string containing comma separated values is okay. It will
33 # be converted to a list by Distribution.finalize_options().
34 pass
35 elif not isinstance(value, list):
36 # passing a tuple or an iterator perhaps, warn and convert
37 typename = type(value).__name__
38 msg = f"Warning: '{fieldname}' should be a list, got type '{typename}'"
39 log.log(log.WARN, msg)
40 value = list(value)
41 return value
42
43
Greg Wardfe6462c2000-04-04 01:40:52 +000044class Distribution:
Greg Ward8ff5a3f2000-06-02 00:44:53 +000045 """The core of the Distutils. Most of the work hiding behind 'setup'
46 is really done within a Distribution instance, which farms the work out
47 to the Distutils commands specified on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +000048
Greg Ward8ff5a3f2000-06-02 00:44:53 +000049 Setup scripts will almost never instantiate Distribution directly,
50 unless the 'setup()' function is totally inadequate to their needs.
51 However, it is conceivable that a setup script might wish to subclass
52 Distribution for some specialized purpose, and then pass the subclass
53 to 'setup()' as the 'distclass' keyword argument. If so, it is
54 necessary to respect the expectations that 'setup' has of Distribution.
55 See the code for 'setup()', in core.py, for details.
56 """
Greg Wardfe6462c2000-04-04 01:40:52 +000057
Greg Wardfe6462c2000-04-04 01:40:52 +000058 # 'global_options' describes the command-line options that may be
Greg Ward82715e12000-04-21 02:28:14 +000059 # supplied to the setup script prior to any actual commands.
60 # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
Greg Wardfe6462c2000-04-04 01:40:52 +000061 # these global options. This list should be kept to a bare minimum,
62 # since every global option is also valid as a command option -- and we
63 # don't want to pollute the commands with too many options that they
64 # have minimal control over.
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000065 # The fourth entry for verbose means that it can be repeated.
Jason R. Coombs7c456322014-07-02 08:36:19 -040066 global_options = [
67 ('verbose', 'v', "run verbosely (default)", 1),
68 ('quiet', 'q', "run quietly (turns verbosity off)"),
69 ('dry-run', 'n', "don't actually do anything"),
70 ('help', 'h', "show detailed help message"),
71 ('no-user-cfg', None,
72 'ignore pydistutils.cfg in your home directory'),
Andrew Kuchling2a1838b2013-11-10 18:11:00 -050073 ]
Greg Ward82715e12000-04-21 02:28:14 +000074
Martin v. Löwis8ed338a2005-03-03 08:12:27 +000075 # 'common_usage' is a short (2-3 line) string describing the common
76 # usage of the setup script.
77 common_usage = """\
78Common commands: (see '--help-commands' for more)
79
80 setup.py build will build the package underneath 'build/'
81 setup.py install will install the package
82"""
83
Greg Ward82715e12000-04-21 02:28:14 +000084 # options that are not propagated to the commands
85 display_options = [
86 ('help-commands', None,
87 "list all available commands"),
88 ('name', None,
89 "print package name"),
90 ('version', 'V',
91 "print package version"),
92 ('fullname', None,
93 "print <package name>-<version>"),
94 ('author', None,
95 "print the author's name"),
96 ('author-email', None,
97 "print the author's email address"),
98 ('maintainer', None,
99 "print the maintainer's name"),
100 ('maintainer-email', None,
101 "print the maintainer's email address"),
102 ('contact', None,
Greg Wardd5d8a992000-05-23 01:42:17 +0000103 "print the maintainer's name if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +0000104 ('contact-email', None,
Greg Wardd5d8a992000-05-23 01:42:17 +0000105 "print the maintainer's email address if known, else the author's"),
Greg Ward82715e12000-04-21 02:28:14 +0000106 ('url', None,
107 "print the URL for this package"),
Greg Ward82715e12000-04-21 02:28:14 +0000108 ('license', None,
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000109 "print the license of the package"),
110 ('licence', None,
111 "alias for --license"),
Greg Ward82715e12000-04-21 02:28:14 +0000112 ('description', None,
113 "print the package description"),
Greg Warde5a584e2000-04-26 02:26:55 +0000114 ('long-description', None,
115 "print the long package description"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000116 ('platforms', None,
117 "print the list of platforms"),
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +0000118 ('classifiers', None,
119 "print the list of classifiers"),
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000120 ('keywords', None,
121 "print the list of keywords"),
Fred Drakedb7b0022005-03-20 22:19:47 +0000122 ('provides', None,
123 "print the list of packages/modules provided"),
124 ('requires', None,
125 "print the list of packages/modules required"),
126 ('obsoletes', None,
127 "print the list of packages/modules made obsolete")
Greg Ward82715e12000-04-21 02:28:14 +0000128 ]
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000129 display_option_names = [translate_longopt(x[0]) for x in display_options]
Greg Ward82715e12000-04-21 02:28:14 +0000130
131 # negative options are options that exclude other options
Greg Wardfe6462c2000-04-04 01:40:52 +0000132 negative_opt = {'quiet': 'verbose'}
133
Greg Wardfe6462c2000-04-04 01:40:52 +0000134 # -- Creation/initialization methods -------------------------------
Fred Drakeb94b8492001-12-06 20:51:35 +0000135
Jason R. Coombs7c456322014-07-02 08:36:19 -0400136 def __init__(self, attrs=None):
Greg Wardfe6462c2000-04-04 01:40:52 +0000137 """Construct a new Distribution instance: initialize all the
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000138 attributes of a Distribution, and then use 'attrs' (a dictionary
139 mapping attribute names to values) to assign some of those
140 attributes their "real" values. (Any attributes not mentioned in
141 'attrs' will be assigned to some null value: 0, None, an empty list
142 or dictionary, etc.) Most importantly, initialize the
143 'command_obj' attribute to the empty dictionary; this will be
144 filled in with real command objects by 'parse_command_line()'.
145 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000146
147 # Default values for our command-line options
148 self.verbose = 1
149 self.dry_run = 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000150 self.help = 0
Greg Ward82715e12000-04-21 02:28:14 +0000151 for attr in self.display_option_names:
152 setattr(self, attr, 0)
Greg Wardfe6462c2000-04-04 01:40:52 +0000153
Greg Ward82715e12000-04-21 02:28:14 +0000154 # Store the distribution meta-data (name, version, author, and so
155 # forth) in a separate object -- we're getting to have enough
156 # information here (and enough command-line options) that it's
157 # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
158 # object in a sneaky and underhanded (but efficient!) way.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000159 self.metadata = DistributionMetadata()
Neil Schemenauera8aefe52001-09-03 15:47:21 +0000160 for basename in self.metadata._METHOD_BASENAMES:
Greg Ward4982f982000-04-22 02:52:44 +0000161 method_name = "get_" + basename
162 setattr(self, method_name, getattr(self.metadata, method_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000163
164 # 'cmdclass' maps command names to class objects, so we
165 # can 1) quickly figure out which class to instantiate when
166 # we need to create a new command object, and 2) have a way
Greg Ward82715e12000-04-21 02:28:14 +0000167 # for the setup script to override command classes
Greg Wardfe6462c2000-04-04 01:40:52 +0000168 self.cmdclass = {}
169
Fred Draked04573f2004-08-03 16:37:40 +0000170 # 'command_packages' is a list of packages in which commands
171 # are searched for. The factory for command 'foo' is expected
172 # to be named 'foo' in the module 'foo' in one of the packages
173 # named here. This list is searched from the left; an error
174 # is raised if no named package provides the command being
175 # searched for. (Always access using get_command_packages().)
176 self.command_packages = None
177
Greg Ward9821bf42000-08-29 01:15:18 +0000178 # 'script_name' and 'script_args' are usually set to sys.argv[0]
179 # and sys.argv[1:], but they can be overridden when the caller is
180 # not necessarily a setup script run from the command-line.
181 self.script_name = None
182 self.script_args = None
183
Greg Wardd5d8a992000-05-23 01:42:17 +0000184 # 'command_options' is where we store command options between
185 # parsing them (from config files, the command-line, etc.) and when
186 # they are actually needed -- ie. when the command in question is
187 # instantiated. It is a dictionary of dictionaries of 2-tuples:
188 # command_options = { command_name : { option : (source, value) } }
Gregory P. Smith14263542000-05-12 00:41:33 +0000189 self.command_options = {}
190
Martin v. Löwis98da5622005-03-23 18:54:36 +0000191 # 'dist_files' is the list of (command, pyversion, file) that
192 # have been created by any dist commands run so far. This is
193 # filled regardless of whether the run is dry or not. pyversion
194 # gives sysconfig.get_python_version() if the dist file is
195 # specific to a Python version, 'any' if it is good for all
196 # Python versions on the target platform, and '' for a source
197 # file. pyversion should not be used to specify minimum or
198 # maximum required Python versions; use the metainfo for that
199 # instead.
Martin v. Löwis55f1bb82005-03-21 20:56:35 +0000200 self.dist_files = []
201
Greg Wardfe6462c2000-04-04 01:40:52 +0000202 # These options are really the business of various commands, rather
203 # than of the Distribution itself. We provide aliases for them in
204 # Distribution as a convenience to the developer.
Greg Wardfe6462c2000-04-04 01:40:52 +0000205 self.packages = None
Fred Drake0eb32a62004-06-11 21:50:33 +0000206 self.package_data = {}
Greg Wardfe6462c2000-04-04 01:40:52 +0000207 self.package_dir = None
208 self.py_modules = None
209 self.libraries = None
Greg Ward51def7d2000-05-27 01:36:14 +0000210 self.headers = None
Greg Wardfe6462c2000-04-04 01:40:52 +0000211 self.ext_modules = None
212 self.ext_package = None
213 self.include_dirs = None
214 self.extra_path = None
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000215 self.scripts = None
Gregory P. Smith6a901dd2000-05-13 03:09:50 +0000216 self.data_files = None
Tarek Ziadé13f7c3b2009-01-09 00:15:45 +0000217 self.password = ''
Greg Wardfe6462c2000-04-04 01:40:52 +0000218
219 # And now initialize bookkeeping stuff that can't be supplied by
220 # the caller at all. 'command_obj' maps command names to
221 # Command instances -- that's how we enforce that every command
222 # class is a singleton.
223 self.command_obj = {}
224
225 # 'have_run' maps command names to boolean values; it keeps track
226 # of whether we have actually run a particular command, to make it
227 # cheap to "run" a command whenever we think we might need to -- if
228 # it's already been done, no need for expensive filesystem
229 # operations, we just check the 'have_run' dictionary and carry on.
230 # It's only safe to query 'have_run' for a command class that has
231 # been instantiated -- a false value will be inserted when the
232 # command object is created, and replaced with a true value when
Greg Ward612eb9f2000-07-27 02:13:20 +0000233 # the command is successfully run. Thus it's probably best to use
Greg Wardfe6462c2000-04-04 01:40:52 +0000234 # '.get()' rather than a straight lookup.
235 self.have_run = {}
236
237 # Now we'll use the attrs dictionary (ultimately, keyword args from
Greg Ward82715e12000-04-21 02:28:14 +0000238 # the setup script) to possibly override any or all of these
239 # distribution options.
240
Greg Wardfe6462c2000-04-04 01:40:52 +0000241 if attrs:
Greg Wardfe6462c2000-04-04 01:40:52 +0000242 # Pull out the set of command options and work on them
243 # specifically. Note that this order guarantees that aliased
244 # command options will override any supplied redundantly
245 # through the general options dictionary.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000246 options = attrs.get('options')
Tarek Ziadé4450dcf2008-12-29 22:38:38 +0000247 if options is not None:
Greg Wardfe6462c2000-04-04 01:40:52 +0000248 del attrs['options']
249 for (command, cmd_options) in options.items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000250 opt_dict = self.get_option_dict(command)
251 for (opt, val) in cmd_options.items():
252 opt_dict[opt] = ("setup script", val)
Greg Wardfe6462c2000-04-04 01:40:52 +0000253
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000254 if 'licence' in attrs:
Andrew M. Kuchlinga52b8522003-03-03 20:07:27 +0000255 attrs['license'] = attrs['licence']
256 del attrs['licence']
257 msg = "'licence' distribution option is deprecated; use 'license'"
258 if warnings is not None:
259 warnings.warn(msg)
260 else:
261 sys.stderr.write(msg + "\n")
262
Greg Wardfe6462c2000-04-04 01:40:52 +0000263 # Now work on the rest of the attributes. Any attribute that's
264 # not already defined is invalid!
Tarek Ziadéf11be752009-06-01 22:36:26 +0000265 for (key, val) in attrs.items():
Fred Drakedb7b0022005-03-20 22:19:47 +0000266 if hasattr(self.metadata, "set_" + key):
267 getattr(self.metadata, "set_" + key)(val)
268 elif hasattr(self.metadata, key):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000269 setattr(self.metadata, key, val)
270 elif hasattr(self, key):
271 setattr(self, key, val)
Anthony Baxter73cc8472004-10-13 13:22:34 +0000272 else:
Andrew M. Kuchlingff4ad9a2002-10-31 13:22:41 +0000273 msg = "Unknown distribution option: %s" % repr(key)
Neil Schemenauer8837dd02017-12-04 18:58:12 -0800274 warnings.warn(msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000275
Andrew Kuchling2a1838b2013-11-10 18:11:00 -0500276 # no-user-cfg is handled before other command line args
277 # because other args override the config files, and this
278 # one is needed before we can load the config files.
279 # If attrs['script_args'] wasn't passed, assume false.
280 #
281 # This also make sure we just look at the global options
282 self.want_user_cfg = True
283
284 if self.script_args is not None:
285 for arg in self.script_args:
286 if not arg.startswith('-'):
287 break
288 if arg == '--no-user-cfg':
289 self.want_user_cfg = False
290 break
291
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000292 self.finalize_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000293
Tarek Ziadé188789d2009-05-16 18:37:32 +0000294 def get_option_dict(self, command):
Greg Ward0e48cfd2000-05-26 01:00:15 +0000295 """Get the option dictionary for a given command. If that
296 command's option dictionary hasn't been created yet, then create it
297 and return the new dictionary; otherwise, return the existing
298 option dictionary.
299 """
Greg Ward0e48cfd2000-05-26 01:00:15 +0000300 dict = self.command_options.get(command)
301 if dict is None:
302 dict = self.command_options[command] = {}
303 return dict
304
Tarek Ziadé188789d2009-05-16 18:37:32 +0000305 def dump_option_dicts(self, header=None, commands=None, indent=""):
Greg Wardc32d9a62000-05-28 23:53:06 +0000306 from pprint import pformat
307
308 if commands is None: # dump all command option dicts
Guido van Rossumd4ee1672007-10-15 01:27:53 +0000309 commands = sorted(self.command_options.keys())
Greg Wardc32d9a62000-05-28 23:53:06 +0000310
311 if header is not None:
Tarek Ziadéf11be752009-06-01 22:36:26 +0000312 self.announce(indent + header)
Greg Wardc32d9a62000-05-28 23:53:06 +0000313 indent = indent + " "
314
315 if not commands:
Tarek Ziadéf11be752009-06-01 22:36:26 +0000316 self.announce(indent + "no commands known yet")
Greg Wardc32d9a62000-05-28 23:53:06 +0000317 return
318
319 for cmd_name in commands:
320 opt_dict = self.command_options.get(cmd_name)
321 if opt_dict is None:
Tarek Ziadéf11be752009-06-01 22:36:26 +0000322 self.announce(indent +
323 "no option dict for '%s' command" % cmd_name)
Greg Wardc32d9a62000-05-28 23:53:06 +0000324 else:
Tarek Ziadéf11be752009-06-01 22:36:26 +0000325 self.announce(indent +
326 "option dict for '%s' command:" % cmd_name)
Greg Wardc32d9a62000-05-28 23:53:06 +0000327 out = pformat(opt_dict)
Tarek Ziadéf11be752009-06-01 22:36:26 +0000328 for line in out.split('\n'):
329 self.announce(indent + " " + line)
Greg Wardc32d9a62000-05-28 23:53:06 +0000330
Greg Wardd5d8a992000-05-23 01:42:17 +0000331 # -- Config file finding/parsing methods ---------------------------
332
Tarek Ziadé188789d2009-05-16 18:37:32 +0000333 def find_config_files(self):
Gregory P. Smith14263542000-05-12 00:41:33 +0000334 """Find as many configuration files as should be processed for this
335 platform, and return a list of filenames in the order in which they
336 should be parsed. The filenames returned are guaranteed to exist
337 (modulo nasty race conditions).
338
Andrew M. Kuchlingd303b612001-12-06 16:32:05 +0000339 There are three possible config files: distutils.cfg in the
340 Distutils installation directory (ie. where the top-level
341 Distutils __inst__.py file lives), a file in the user's home
342 directory named .pydistutils.cfg on Unix and pydistutils.cfg
Andrew Kuchling2a1838b2013-11-10 18:11:00 -0500343 on Windows/Mac; and setup.cfg in the current directory.
344
345 The file in the user's home directory can be disabled with the
346 --no-user-cfg option.
Greg Wardd5d8a992000-05-23 01:42:17 +0000347 """
Gregory P. Smith14263542000-05-12 00:41:33 +0000348 files = []
Greg Wardacf3f6a2000-06-07 02:26:19 +0000349 check_environ()
Gregory P. Smith14263542000-05-12 00:41:33 +0000350
Greg Ward11696872000-06-07 02:29:03 +0000351 # Where to look for the system-wide Distutils config file
352 sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
353
354 # Look for the system config file
355 sys_file = os.path.join(sys_dir, "distutils.cfg")
Greg Wardacf3f6a2000-06-07 02:26:19 +0000356 if os.path.isfile(sys_file):
357 files.append(sys_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000358
Greg Ward11696872000-06-07 02:29:03 +0000359 # What to call the per-user config file
360 if os.name == 'posix':
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000361 user_filename = ".pydistutils.cfg"
362 else:
363 user_filename = "pydistutils.cfg"
Greg Wardfa9ff762000-10-14 04:06:40 +0000364
Greg Ward11696872000-06-07 02:29:03 +0000365 # And look for the user config file
Andrew Kuchling2a1838b2013-11-10 18:11:00 -0500366 if self.want_user_cfg:
367 user_file = os.path.join(os.path.expanduser('~'), user_filename)
368 if os.path.isfile(user_file):
369 files.append(user_file)
Gregory P. Smith14263542000-05-12 00:41:33 +0000370
Gregory P. Smith14263542000-05-12 00:41:33 +0000371 # All platforms support local setup.cfg
372 local_file = "setup.cfg"
373 if os.path.isfile(local_file):
374 files.append(local_file)
375
Andrew Kuchling2a1838b2013-11-10 18:11:00 -0500376 if DEBUG:
377 self.announce("using config files: %s" % ', '.join(files))
378
Gregory P. Smith14263542000-05-12 00:41:33 +0000379 return files
380
Tarek Ziadé188789d2009-05-16 18:37:32 +0000381 def parse_config_files(self, filenames=None):
Alexandre Vassalotti1d1eaa42008-05-14 22:59:42 +0000382 from configparser import ConfigParser
Gregory P. Smith14263542000-05-12 00:41:33 +0000383
Georg Brandl521ed522013-05-12 12:36:07 +0200384 # Ignore install directory options if we have a venv
385 if sys.prefix != sys.base_prefix:
386 ignore_options = [
387 'install-base', 'install-platbase', 'install-lib',
388 'install-platlib', 'install-purelib', 'install-headers',
389 'install-scripts', 'install-data', 'prefix', 'exec-prefix',
390 'home', 'user', 'root']
391 else:
392 ignore_options = []
393
394 ignore_options = frozenset(ignore_options)
395
Gregory P. Smith14263542000-05-12 00:41:33 +0000396 if filenames is None:
397 filenames = self.find_config_files()
398
Tarek Ziadéf11be752009-06-01 22:36:26 +0000399 if DEBUG:
400 self.announce("Distribution.parse_config_files():")
Greg Ward47460772000-05-23 03:47:35 +0000401
Gregory P. Smith14263542000-05-12 00:41:33 +0000402 parser = ConfigParser()
Greg Wardd5d8a992000-05-23 01:42:17 +0000403 for filename in filenames:
Tarek Ziadéf11be752009-06-01 22:36:26 +0000404 if DEBUG:
Tarek Ziadé31d46072009-09-21 13:55:19 +0000405 self.announce(" reading %s" % filename)
Greg Wardd5d8a992000-05-23 01:42:17 +0000406 parser.read(filename)
407 for section in parser.sections():
408 options = parser.options(section)
Greg Ward0e48cfd2000-05-26 01:00:15 +0000409 opt_dict = self.get_option_dict(section)
Gregory P. Smith14263542000-05-12 00:41:33 +0000410
Greg Wardd5d8a992000-05-23 01:42:17 +0000411 for opt in options:
Georg Brandl521ed522013-05-12 12:36:07 +0200412 if opt != '__name__' and opt not in ignore_options:
Greg Wardceb9e222000-09-25 01:23:52 +0000413 val = parser.get(section,opt)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000414 opt = opt.replace('-', '_')
Greg Wardceb9e222000-09-25 01:23:52 +0000415 opt_dict[opt] = (filename, val)
Gregory P. Smith14263542000-05-12 00:41:33 +0000416
Greg Ward47460772000-05-23 03:47:35 +0000417 # Make the ConfigParser forget everything (so we retain
Fred Drakef06116d2004-02-17 22:35:19 +0000418 # the original filenames that options come from)
Greg Ward47460772000-05-23 03:47:35 +0000419 parser.__init__()
Gregory P. Smith14263542000-05-12 00:41:33 +0000420
Greg Wardceb9e222000-09-25 01:23:52 +0000421 # If there was a "global" section in the config file, use it
422 # to set Distribution options.
423
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000424 if 'global' in self.command_options:
Greg Wardceb9e222000-09-25 01:23:52 +0000425 for (opt, (src, val)) in self.command_options['global'].items():
426 alias = self.negative_opt.get(opt)
427 try:
428 if alias:
429 setattr(self, alias, not strtobool(val))
430 elif opt in ('verbose', 'dry_run'): # ugh!
431 setattr(self, opt, strtobool(val))
Fred Draked04573f2004-08-03 16:37:40 +0000432 else:
433 setattr(self, opt, val)
Guido van Rossumb940e112007-01-10 16:19:56 +0000434 except ValueError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000435 raise DistutilsOptionError(msg)
Greg Wardceb9e222000-09-25 01:23:52 +0000436
Greg Wardd5d8a992000-05-23 01:42:17 +0000437 # -- Command-line parsing methods ----------------------------------
438
Tarek Ziadé188789d2009-05-16 18:37:32 +0000439 def parse_command_line(self):
Greg Ward9821bf42000-08-29 01:15:18 +0000440 """Parse the setup script's command line, taken from the
441 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
442 -- see 'setup()' in core.py). This list is first processed for
443 "global options" -- options that set attributes of the Distribution
444 instance. Then, it is alternately scanned for Distutils commands
445 and options for that command. Each new command terminates the
446 options for the previous command. The allowed options for a
447 command are determined by the 'user_options' attribute of the
448 command class -- thus, we have to be able to load command classes
449 in order to parse the command line. Any error in that 'options'
450 attribute raises DistutilsGetoptError; any error on the
451 command-line raises DistutilsArgError. If no Distutils commands
452 were found on the command line, raises DistutilsArgError. Return
Greg Wardceb9e222000-09-25 01:23:52 +0000453 true if command-line was successfully parsed and we should carry
Greg Ward9821bf42000-08-29 01:15:18 +0000454 on with executing commands; false if no errors but we shouldn't
455 execute commands (currently, this only happens if user asks for
456 help).
Greg Wardd5d8a992000-05-23 01:42:17 +0000457 """
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000458 #
Fred Drake981a1782001-08-10 18:59:30 +0000459 # We now have enough information to show the Macintosh dialog
460 # that allows the user to interactively specify the "command line".
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000461 #
Fred Draked04573f2004-08-03 16:37:40 +0000462 toplevel_options = self._get_toplevel_options()
Fred Drakeb94b8492001-12-06 20:51:35 +0000463
Greg Wardfe6462c2000-04-04 01:40:52 +0000464 # We have to parse the command line a bit at a time -- global
465 # options, then the first command, then its options, and so on --
466 # because each command will be handled by a different class, and
Greg Wardd5d8a992000-05-23 01:42:17 +0000467 # the options that are valid for a particular class aren't known
468 # until we have loaded the command class, which doesn't happen
469 # until we know what the command is.
Greg Wardfe6462c2000-04-04 01:40:52 +0000470
471 self.commands = []
Fred Draked04573f2004-08-03 16:37:40 +0000472 parser = FancyGetopt(toplevel_options + self.display_options)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000473 parser.set_negative_aliases(self.negative_opt)
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +0000474 parser.set_aliases({'licence': 'license'})
Greg Wardfd7b91e2000-09-26 01:52:25 +0000475 args = parser.getopt(args=self.script_args, object=self)
Greg Ward82715e12000-04-21 02:28:14 +0000476 option_order = parser.get_option_order()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000477 log.set_verbosity(self.verbose)
Greg Wardfe6462c2000-04-04 01:40:52 +0000478
Greg Ward82715e12000-04-21 02:28:14 +0000479 # for display options we return immediately
480 if self.handle_display_options(option_order):
Greg Wardfe6462c2000-04-04 01:40:52 +0000481 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000482 while args:
Greg Wardd5d8a992000-05-23 01:42:17 +0000483 args = self._parse_command_opts(parser, args)
484 if args is None: # user asked for help (and got it)
Greg Wardfe6462c2000-04-04 01:40:52 +0000485 return
Greg Wardfe6462c2000-04-04 01:40:52 +0000486
Greg Wardd5d8a992000-05-23 01:42:17 +0000487 # Handle the cases of --help as a "global" option, ie.
488 # "setup.py --help" and "setup.py --help command ...". For the
489 # former, we show global options (--verbose, --dry-run, etc.)
490 # and display-only options (--name, --version, etc.); for the
491 # latter, we omit the display-only options and show help for
492 # each command listed on the command line.
Greg Wardfe6462c2000-04-04 01:40:52 +0000493 if self.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000494 self._show_help(parser,
495 display_options=len(self.commands) == 0,
496 commands=self.commands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000497 return
498
499 # Oops, no commands found -- an end-user error
500 if not self.commands:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000501 raise DistutilsArgError("no commands supplied")
Greg Wardfe6462c2000-04-04 01:40:52 +0000502
503 # All is well: return true
Collin Winter5b7e9d72007-08-30 03:52:21 +0000504 return True
Greg Wardfe6462c2000-04-04 01:40:52 +0000505
Tarek Ziadé188789d2009-05-16 18:37:32 +0000506 def _get_toplevel_options(self):
Fred Draked04573f2004-08-03 16:37:40 +0000507 """Return the non-display options recognized at the top level.
508
509 This includes options that are recognized *only* at the top
510 level as well as options recognized for commands.
511 """
512 return self.global_options + [
513 ("command-packages=", None,
514 "list of packages that provide distutils commands"),
515 ]
516
Tarek Ziadé188789d2009-05-16 18:37:32 +0000517 def _parse_command_opts(self, parser, args):
Greg Wardd5d8a992000-05-23 01:42:17 +0000518 """Parse the command-line options for a single command.
519 'parser' must be a FancyGetopt instance; 'args' must be the list
520 of arguments, starting with the current command (whose options
521 we are about to parse). Returns a new version of 'args' with
522 the next command at the front of the list; will be the empty
523 list if there are no more commands on the command line. Returns
524 None if the user asked for help on this command.
525 """
526 # late import because of mutual dependence between these modules
527 from distutils.cmd import Command
528
529 # Pull the current command from the head of the command line
530 command = args[0]
Greg Wardfd7b91e2000-09-26 01:52:25 +0000531 if not command_re.match(command):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000532 raise SystemExit("invalid command name '%s'" % command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000533 self.commands.append(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000534
535 # Dig up the command class that implements this command, so we
536 # 1) know that it's a valid command, and 2) know which options
537 # it takes.
538 try:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000539 cmd_class = self.get_command_class(command)
Guido van Rossumb940e112007-01-10 16:19:56 +0000540 except DistutilsModuleError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000541 raise DistutilsArgError(msg)
Greg Wardd5d8a992000-05-23 01:42:17 +0000542
543 # Require that the command class be derived from Command -- want
544 # to be sure that the basic "command" interface is implemented.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000545 if not issubclass(cmd_class, Command):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000546 raise DistutilsClassError(
Jason R. Coombs7c456322014-07-02 08:36:19 -0400547 "command class %s must subclass Command" % cmd_class)
Greg Wardd5d8a992000-05-23 01:42:17 +0000548
549 # Also make sure that the command object provides a list of its
550 # known options.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000551 if not (hasattr(cmd_class, 'user_options') and
Guido van Rossum13257902007-06-07 23:15:56 +0000552 isinstance(cmd_class.user_options, list)):
Jason R. Coombs7c456322014-07-02 08:36:19 -0400553 msg = ("command class %s must provide "
554 "'user_options' attribute (a list of tuples)")
555 raise DistutilsClassError(msg % cmd_class)
Greg Wardd5d8a992000-05-23 01:42:17 +0000556
557 # If the command class has a list of negative alias options,
558 # merge it in with the global negative aliases.
559 negative_opt = self.negative_opt
Greg Wardfd7b91e2000-09-26 01:52:25 +0000560 if hasattr(cmd_class, 'negative_opt'):
Antoine Pitrou56a00de2009-05-15 17:34:21 +0000561 negative_opt = negative_opt.copy()
Greg Wardfd7b91e2000-09-26 01:52:25 +0000562 negative_opt.update(cmd_class.negative_opt)
Greg Wardd5d8a992000-05-23 01:42:17 +0000563
Greg Wardfa9ff762000-10-14 04:06:40 +0000564 # Check for help_options in command class. They have a different
565 # format (tuple of four) so we need to preprocess them here.
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000566 if (hasattr(cmd_class, 'help_options') and
Jason R. Coombs7c456322014-07-02 08:36:19 -0400567 isinstance(cmd_class.help_options, list)):
Greg Ward2ff78872000-06-24 00:23:20 +0000568 help_options = fix_help_options(cmd_class.help_options)
569 else:
Greg Ward55fced32000-06-24 01:22:41 +0000570 help_options = []
Greg Ward2ff78872000-06-24 00:23:20 +0000571
Greg Wardd5d8a992000-05-23 01:42:17 +0000572 # All commands support the global options too, just by adding
573 # in 'global_options'.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000574 parser.set_option_table(self.global_options +
575 cmd_class.user_options +
576 help_options)
577 parser.set_negative_aliases(negative_opt)
578 (args, opts) = parser.getopt(args[1:])
Greg Ward47460772000-05-23 03:47:35 +0000579 if hasattr(opts, 'help') and opts.help:
Greg Wardd5d8a992000-05-23 01:42:17 +0000580 self._show_help(parser, display_options=0, commands=[cmd_class])
581 return
582
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000583 if (hasattr(cmd_class, 'help_options') and
Jason R. Coombs7c456322014-07-02 08:36:19 -0400584 isinstance(cmd_class.help_options, list)):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000585 help_option_found=0
586 for (help_option, short, desc, func) in cmd_class.help_options:
587 if hasattr(opts, parser.get_attr_name(help_option)):
588 help_option_found=1
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200589 if callable(func):
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000590 func()
Greg Ward55fced32000-06-24 01:22:41 +0000591 else:
Fred Drake981a1782001-08-10 18:59:30 +0000592 raise DistutilsClassError(
Walter Dörwald70a6b492004-02-12 17:35:32 +0000593 "invalid help function %r for help option '%s': "
Fred Drake981a1782001-08-10 18:59:30 +0000594 "must be a callable object (function, etc.)"
Walter Dörwald70a6b492004-02-12 17:35:32 +0000595 % (func, help_option))
Greg Ward55fced32000-06-24 01:22:41 +0000596
Fred Drakeb94b8492001-12-06 20:51:35 +0000597 if help_option_found:
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000598 return
Greg Ward9d17a7a2000-06-07 03:00:06 +0000599
Greg Wardd5d8a992000-05-23 01:42:17 +0000600 # Put the options from the command-line into their official
601 # holding pen, the 'command_options' dictionary.
Greg Ward0e48cfd2000-05-26 01:00:15 +0000602 opt_dict = self.get_option_dict(command)
Greg Wardd5d8a992000-05-23 01:42:17 +0000603 for (name, value) in vars(opts).items():
Greg Ward0e48cfd2000-05-26 01:00:15 +0000604 opt_dict[name] = ("command line", value)
Greg Wardd5d8a992000-05-23 01:42:17 +0000605
606 return args
607
Tarek Ziadé188789d2009-05-16 18:37:32 +0000608 def finalize_options(self):
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000609 """Set final values for all the options on the Distribution
610 instance, analogous to the .finalize_options() method of Command
611 objects.
612 """
Tarek Ziadéf11be752009-06-01 22:36:26 +0000613 for attr in ('keywords', 'platforms'):
614 value = getattr(self.metadata, attr)
615 if value is None:
616 continue
617 if isinstance(value, str):
618 value = [elm.strip() for elm in value.split(',')]
619 setattr(self.metadata, attr, value)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000620
Tarek Ziadé188789d2009-05-16 18:37:32 +0000621 def _show_help(self, parser, global_options=1, display_options=1,
622 commands=[]):
Greg Wardd5d8a992000-05-23 01:42:17 +0000623 """Show help for the setup script command-line in the form of
624 several lists of command-line options. 'parser' should be a
625 FancyGetopt instance; do not expect it to be returned in the
626 same state, as its option table will be reset to make it
627 generate the correct help text.
628
629 If 'global_options' is true, lists the global options:
630 --verbose, --dry-run, etc. If 'display_options' is true, lists
631 the "display-only" options: --name, --version, etc. Finally,
632 lists per-command help for every command name or command class
633 in 'commands'.
634 """
635 # late import because of mutual dependence between these modules
Greg Ward9821bf42000-08-29 01:15:18 +0000636 from distutils.core import gen_usage
Greg Wardd5d8a992000-05-23 01:42:17 +0000637 from distutils.cmd import Command
638
639 if global_options:
Fred Draked04573f2004-08-03 16:37:40 +0000640 if display_options:
641 options = self._get_toplevel_options()
642 else:
643 options = self.global_options
644 parser.set_option_table(options)
Martin v. Löwis8ed338a2005-03-03 08:12:27 +0000645 parser.print_help(self.common_usage + "\nGlobal options:")
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000646 print('')
Greg Wardd5d8a992000-05-23 01:42:17 +0000647
648 if display_options:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000649 parser.set_option_table(self.display_options)
650 parser.print_help(
Greg Wardd5d8a992000-05-23 01:42:17 +0000651 "Information display options (just display " +
652 "information, ignore any commands)")
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000653 print('')
Greg Wardd5d8a992000-05-23 01:42:17 +0000654
655 for command in self.commands:
Guido van Rossum13257902007-06-07 23:15:56 +0000656 if isinstance(command, type) and issubclass(command, Command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000657 klass = command
658 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000659 klass = self.get_command_class(command)
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000660 if (hasattr(klass, 'help_options') and
Jason R. Coombs7c456322014-07-02 08:36:19 -0400661 isinstance(klass.help_options, list)):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000662 parser.set_option_table(klass.user_options +
663 fix_help_options(klass.help_options))
Jeremy Hylton65d6edb2000-07-07 20:45:21 +0000664 else:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000665 parser.set_option_table(klass.user_options)
666 parser.print_help("Options for '%s' command:" % klass.__name__)
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000667 print('')
Greg Wardd5d8a992000-05-23 01:42:17 +0000668
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000669 print(gen_usage(self.script_name))
Greg Wardd5d8a992000-05-23 01:42:17 +0000670
Tarek Ziadé188789d2009-05-16 18:37:32 +0000671 def handle_display_options(self, option_order):
Greg Ward82715e12000-04-21 02:28:14 +0000672 """If there were any non-global "display-only" options
Greg Wardd5d8a992000-05-23 01:42:17 +0000673 (--help-commands or the metadata display options) on the command
674 line, display the requested info and return true; else return
675 false.
676 """
Greg Ward9821bf42000-08-29 01:15:18 +0000677 from distutils.core import gen_usage
Greg Ward82715e12000-04-21 02:28:14 +0000678
679 # User just wants a list of commands -- we'll print it out and stop
680 # processing now (ie. if they ran "setup --help-commands foo bar",
681 # we ignore "foo bar").
682 if self.help_commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000683 self.print_commands()
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000684 print('')
685 print(gen_usage(self.script_name))
Greg Ward82715e12000-04-21 02:28:14 +0000686 return 1
687
688 # If user supplied any of the "display metadata" options, then
689 # display that metadata in the order in which the user supplied the
690 # metadata options.
691 any_display_options = 0
692 is_display_option = {}
693 for option in self.display_options:
694 is_display_option[option[0]] = 1
695
696 for (opt, val) in option_order:
697 if val and is_display_option.get(opt):
Greg Ward2f2b6c62000-09-25 01:58:07 +0000698 opt = translate_longopt(opt)
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000699 value = getattr(self.metadata, "get_"+opt)()
700 if opt in ['keywords', 'platforms']:
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000701 print(','.join(value))
Fred Drakedb7b0022005-03-20 22:19:47 +0000702 elif opt in ('classifiers', 'provides', 'requires',
703 'obsoletes'):
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000704 print('\n'.join(value))
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +0000705 else:
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000706 print(value)
Greg Ward82715e12000-04-21 02:28:14 +0000707 any_display_options = 1
708
709 return any_display_options
710
Tarek Ziadé188789d2009-05-16 18:37:32 +0000711 def print_command_list(self, commands, header, max_length):
Greg Wardfe6462c2000-04-04 01:40:52 +0000712 """Print a subset of the list of all commands -- used by
Greg Wardd5d8a992000-05-23 01:42:17 +0000713 'print_commands()'.
714 """
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000715 print(header + ":")
Greg Wardfe6462c2000-04-04 01:40:52 +0000716
717 for cmd in commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000718 klass = self.cmdclass.get(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000719 if not klass:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000720 klass = self.get_command_class(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000721 try:
722 description = klass.description
723 except AttributeError:
724 description = "(no description available)"
725
Tarek Ziadé0d3fa832009-07-04 03:00:50 +0000726 print(" %-*s %s" % (max_length, cmd, description))
Greg Wardfe6462c2000-04-04 01:40:52 +0000727
Tarek Ziadé188789d2009-05-16 18:37:32 +0000728 def print_commands(self):
Greg Wardd5d8a992000-05-23 01:42:17 +0000729 """Print out a help message listing all available commands with a
730 description of each. The list is divided into "standard commands"
731 (listed in distutils.command.__all__) and "extra commands"
732 (mentioned in self.cmdclass, but not a standard command). The
733 descriptions come from the command class attribute
734 'description'.
735 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000736 import distutils.command
737 std_commands = distutils.command.__all__
738 is_std = {}
739 for cmd in std_commands:
740 is_std[cmd] = 1
741
742 extra_commands = []
743 for cmd in self.cmdclass.keys():
744 if not is_std.get(cmd):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000745 extra_commands.append(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000746
747 max_length = 0
748 for cmd in (std_commands + extra_commands):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000749 if len(cmd) > max_length:
750 max_length = len(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000751
Greg Wardfd7b91e2000-09-26 01:52:25 +0000752 self.print_command_list(std_commands,
753 "Standard commands",
754 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000755 if extra_commands:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000756 print()
Greg Wardfd7b91e2000-09-26 01:52:25 +0000757 self.print_command_list(extra_commands,
758 "Extra commands",
759 max_length)
Greg Wardfe6462c2000-04-04 01:40:52 +0000760
Tarek Ziadé188789d2009-05-16 18:37:32 +0000761 def get_command_list(self):
Greg Wardf6fc8752000-11-11 02:47:11 +0000762 """Get a list of (command, description) tuples.
763 The list is divided into "standard commands" (listed in
764 distutils.command.__all__) and "extra commands" (mentioned in
765 self.cmdclass, but not a standard command). The descriptions come
766 from the command class attribute 'description'.
767 """
768 # Currently this is only used on Mac OS, for the Mac-only GUI
769 # Distutils interface (by Jack Jansen)
Greg Wardf6fc8752000-11-11 02:47:11 +0000770 import distutils.command
771 std_commands = distutils.command.__all__
772 is_std = {}
773 for cmd in std_commands:
774 is_std[cmd] = 1
775
776 extra_commands = []
777 for cmd in self.cmdclass.keys():
778 if not is_std.get(cmd):
779 extra_commands.append(cmd)
780
781 rv = []
782 for cmd in (std_commands + extra_commands):
783 klass = self.cmdclass.get(cmd)
784 if not klass:
785 klass = self.get_command_class(cmd)
786 try:
787 description = klass.description
788 except AttributeError:
789 description = "(no description available)"
790 rv.append((cmd, description))
791 return rv
Greg Wardfe6462c2000-04-04 01:40:52 +0000792
793 # -- Command class/object methods ----------------------------------
794
Tarek Ziadé188789d2009-05-16 18:37:32 +0000795 def get_command_packages(self):
Fred Draked04573f2004-08-03 16:37:40 +0000796 """Return a list of packages from which commands are loaded."""
797 pkgs = self.command_packages
Tarek Ziadéf11be752009-06-01 22:36:26 +0000798 if not isinstance(pkgs, list):
799 if pkgs is None:
800 pkgs = ''
801 pkgs = [pkg.strip() for pkg in pkgs.split(',') if pkg != '']
Fred Draked04573f2004-08-03 16:37:40 +0000802 if "distutils.command" not in pkgs:
803 pkgs.insert(0, "distutils.command")
804 self.command_packages = pkgs
805 return pkgs
806
Tarek Ziadé188789d2009-05-16 18:37:32 +0000807 def get_command_class(self, command):
Greg Wardd5d8a992000-05-23 01:42:17 +0000808 """Return the class that implements the Distutils command named by
809 'command'. First we check the 'cmdclass' dictionary; if the
810 command is mentioned there, we fetch the class object from the
811 dictionary and return it. Otherwise we load the command module
812 ("distutils.command." + command) and fetch the command class from
813 the module. The loaded class is also stored in 'cmdclass'
814 to speed future calls to 'get_command_class()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000815
Gregory P. Smith14263542000-05-12 00:41:33 +0000816 Raises DistutilsModuleError if the expected module could not be
Greg Wardd5d8a992000-05-23 01:42:17 +0000817 found, or if that module does not define the expected class.
818 """
819 klass = self.cmdclass.get(command)
820 if klass:
821 return klass
Greg Wardfe6462c2000-04-04 01:40:52 +0000822
Fred Draked04573f2004-08-03 16:37:40 +0000823 for pkgname in self.get_command_packages():
824 module_name = "%s.%s" % (pkgname, command)
825 klass_name = command
Greg Wardfe6462c2000-04-04 01:40:52 +0000826
Fred Draked04573f2004-08-03 16:37:40 +0000827 try:
Jason R. Coombs7c456322014-07-02 08:36:19 -0400828 __import__(module_name)
Fred Draked04573f2004-08-03 16:37:40 +0000829 module = sys.modules[module_name]
830 except ImportError:
831 continue
Greg Wardfe6462c2000-04-04 01:40:52 +0000832
Fred Draked04573f2004-08-03 16:37:40 +0000833 try:
834 klass = getattr(module, klass_name)
835 except AttributeError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000836 raise DistutilsModuleError(
Jason R. Coombs7c456322014-07-02 08:36:19 -0400837 "invalid command '%s' (no class '%s' in module '%s')"
838 % (command, klass_name, module_name))
Greg Wardfe6462c2000-04-04 01:40:52 +0000839
Fred Draked04573f2004-08-03 16:37:40 +0000840 self.cmdclass[command] = klass
841 return klass
842
843 raise DistutilsModuleError("invalid command '%s'" % command)
844
Tarek Ziadé188789d2009-05-16 18:37:32 +0000845 def get_command_obj(self, command, create=1):
Greg Wardd5d8a992000-05-23 01:42:17 +0000846 """Return the command object for 'command'. Normally this object
Greg Ward612eb9f2000-07-27 02:13:20 +0000847 is cached on a previous call to 'get_command_obj()'; if no command
Greg Wardd5d8a992000-05-23 01:42:17 +0000848 object for 'command' is in the cache, then we either create and
849 return it (if 'create' is true) or return None.
850 """
851 cmd_obj = self.command_obj.get(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000852 if not cmd_obj and create:
Greg Ward2bd3f422000-06-02 01:59:33 +0000853 if DEBUG:
Jason R. Coombs7c456322014-07-02 08:36:19 -0400854 self.announce("Distribution.get_command_obj(): "
Tarek Ziadéf11be752009-06-01 22:36:26 +0000855 "creating '%s' command object" % command)
Greg Ward47460772000-05-23 03:47:35 +0000856
Greg Wardd5d8a992000-05-23 01:42:17 +0000857 klass = self.get_command_class(command)
Greg Ward47460772000-05-23 03:47:35 +0000858 cmd_obj = self.command_obj[command] = klass(self)
859 self.have_run[command] = 0
860
861 # Set any options that were supplied in config files
862 # or on the command line. (NB. support for error
863 # reporting is lame here: any errors aren't reported
864 # until 'finalize_options()' is called, which means
865 # we won't report the source of the error.)
866 options = self.command_options.get(command)
867 if options:
Greg Wardc32d9a62000-05-28 23:53:06 +0000868 self._set_command_options(cmd_obj, options)
Greg Wardfe6462c2000-04-04 01:40:52 +0000869
870 return cmd_obj
871
Tarek Ziadé188789d2009-05-16 18:37:32 +0000872 def _set_command_options(self, command_obj, option_dict=None):
Greg Wardc32d9a62000-05-28 23:53:06 +0000873 """Set the options for 'command_obj' from 'option_dict'. Basically
874 this means copying elements of a dictionary ('option_dict') to
875 attributes of an instance ('command').
876
Greg Wardceb9e222000-09-25 01:23:52 +0000877 'command_obj' must be a Command instance. If 'option_dict' is not
Greg Wardc32d9a62000-05-28 23:53:06 +0000878 supplied, uses the standard option dictionary for this command
879 (from 'self.command_options').
880 """
Greg Wardc32d9a62000-05-28 23:53:06 +0000881 command_name = command_obj.get_command_name()
882 if option_dict is None:
883 option_dict = self.get_option_dict(command_name)
884
Tarek Ziadéf11be752009-06-01 22:36:26 +0000885 if DEBUG:
886 self.announce(" setting options for '%s' command:" % command_name)
Greg Wardc32d9a62000-05-28 23:53:06 +0000887 for (option, (source, value)) in option_dict.items():
Tarek Ziadéf11be752009-06-01 22:36:26 +0000888 if DEBUG:
889 self.announce(" %s = %s (from %s)" % (option, value,
890 source))
Greg Wardceb9e222000-09-25 01:23:52 +0000891 try:
Amaury Forgeot d'Arc61cb0872008-07-26 20:09:45 +0000892 bool_opts = [translate_longopt(o)
893 for o in command_obj.boolean_options]
Greg Wardceb9e222000-09-25 01:23:52 +0000894 except AttributeError:
895 bool_opts = []
896 try:
897 neg_opt = command_obj.negative_opt
898 except AttributeError:
899 neg_opt = {}
900
901 try:
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000902 is_string = isinstance(value, str)
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000903 if option in neg_opt and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000904 setattr(command_obj, neg_opt[option], not strtobool(value))
Greg Ward2c08cf02000-09-27 00:15:37 +0000905 elif option in bool_opts and is_string:
Greg Wardceb9e222000-09-25 01:23:52 +0000906 setattr(command_obj, option, strtobool(value))
907 elif hasattr(command_obj, option):
908 setattr(command_obj, option, value)
909 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000910 raise DistutilsOptionError(
Jason R. Coombs7c456322014-07-02 08:36:19 -0400911 "error in %s: command '%s' has no such option '%s'"
912 % (source, command_name, option))
Guido van Rossumb940e112007-01-10 16:19:56 +0000913 except ValueError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000914 raise DistutilsOptionError(msg)
Greg Wardc32d9a62000-05-28 23:53:06 +0000915
Tarek Ziadé188789d2009-05-16 18:37:32 +0000916 def reinitialize_command(self, command, reinit_subcommands=0):
Greg Wardc32d9a62000-05-28 23:53:06 +0000917 """Reinitializes a command to the state it was in when first
918 returned by 'get_command_obj()': ie., initialized but not yet
Greg Ward7d9c7052000-06-28 01:25:27 +0000919 finalized. This provides the opportunity to sneak option
Greg Wardc32d9a62000-05-28 23:53:06 +0000920 values in programmatically, overriding or supplementing
921 user-supplied values from the config files and command line.
922 You'll have to re-finalize the command object (by calling
923 'finalize_options()' or 'ensure_finalized()') before using it for
Fred Drakeb94b8492001-12-06 20:51:35 +0000924 real.
Greg Wardc32d9a62000-05-28 23:53:06 +0000925
Greg Wardf449ea52000-09-16 15:23:28 +0000926 'command' should be a command name (string) or command object. If
927 'reinit_subcommands' is true, also reinitializes the command's
928 sub-commands, as declared by the 'sub_commands' class attribute (if
929 it has one). See the "install" command for an example. Only
930 reinitializes the sub-commands that actually matter, ie. those
931 whose test predicates return true.
932
Greg Wardc32d9a62000-05-28 23:53:06 +0000933 Returns the reinitialized command object.
934 """
935 from distutils.cmd import Command
936 if not isinstance(command, Command):
937 command_name = command
938 command = self.get_command_obj(command_name)
939 else:
940 command_name = command.get_command_name()
941
942 if not command.finalized:
Greg Ward282c7a02000-06-01 01:09:47 +0000943 return command
Greg Wardc32d9a62000-05-28 23:53:06 +0000944 command.initialize_options()
945 command.finalized = 0
Greg Ward43955c92000-06-06 02:52:36 +0000946 self.have_run[command_name] = 0
Greg Wardc32d9a62000-05-28 23:53:06 +0000947 self._set_command_options(command)
Greg Wardf449ea52000-09-16 15:23:28 +0000948
Greg Wardf449ea52000-09-16 15:23:28 +0000949 if reinit_subcommands:
Greg Wardf449ea52000-09-16 15:23:28 +0000950 for sub in command.get_sub_commands():
Fred Drakeb94b8492001-12-06 20:51:35 +0000951 self.reinitialize_command(sub, reinit_subcommands)
Greg Wardf449ea52000-09-16 15:23:28 +0000952
Greg Wardc32d9a62000-05-28 23:53:06 +0000953 return command
954
Greg Wardfe6462c2000-04-04 01:40:52 +0000955 # -- Methods that operate on the Distribution ----------------------
956
Tarek Ziadé05bf01a2009-07-04 02:04:21 +0000957 def announce(self, msg, level=log.INFO):
958 log.log(level, msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000959
Tarek Ziadé188789d2009-05-16 18:37:32 +0000960 def run_commands(self):
Greg Ward82715e12000-04-21 02:28:14 +0000961 """Run each command that was seen on the setup script command line.
Greg Wardd5d8a992000-05-23 01:42:17 +0000962 Uses the list of commands found and cache of command objects
Greg Wardfd7b91e2000-09-26 01:52:25 +0000963 created by 'get_command_obj()'.
964 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000965 for cmd in self.commands:
Greg Wardfd7b91e2000-09-26 01:52:25 +0000966 self.run_command(cmd)
Greg Wardfe6462c2000-04-04 01:40:52 +0000967
Greg Wardfe6462c2000-04-04 01:40:52 +0000968 # -- Methods that operate on its Commands --------------------------
969
Tarek Ziadé188789d2009-05-16 18:37:32 +0000970 def run_command(self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000971 """Do whatever it takes to run a command (including nothing at all,
Greg Wardd5d8a992000-05-23 01:42:17 +0000972 if the command has already been run). Specifically: if we have
973 already created and run the command named by 'command', return
974 silently without doing anything. If the command named by 'command'
975 doesn't even have a command object yet, create one. Then invoke
976 'run()' on that command object (or an existing one).
977 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000978 # Already been here, done that? then return silently.
Greg Wardfd7b91e2000-09-26 01:52:25 +0000979 if self.have_run.get(command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000980 return
981
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000982 log.info("running %s", command)
Greg Wardfd7b91e2000-09-26 01:52:25 +0000983 cmd_obj = self.get_command_obj(command)
984 cmd_obj.ensure_finalized()
985 cmd_obj.run()
Greg Wardfe6462c2000-04-04 01:40:52 +0000986 self.have_run[command] = 1
987
Greg Wardfe6462c2000-04-04 01:40:52 +0000988 # -- Distribution query methods ------------------------------------
989
Tarek Ziadé188789d2009-05-16 18:37:32 +0000990 def has_pure_modules(self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000991 return len(self.packages or self.py_modules or []) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000992
Tarek Ziadé188789d2009-05-16 18:37:32 +0000993 def has_ext_modules(self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000994 return self.ext_modules and len(self.ext_modules) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000995
Tarek Ziadé188789d2009-05-16 18:37:32 +0000996 def has_c_libraries(self):
Greg Wardfd7b91e2000-09-26 01:52:25 +0000997 return self.libraries and len(self.libraries) > 0
Greg Wardfe6462c2000-04-04 01:40:52 +0000998
Tarek Ziadé188789d2009-05-16 18:37:32 +0000999 def has_modules(self):
Greg Wardfe6462c2000-04-04 01:40:52 +00001000 return self.has_pure_modules() or self.has_ext_modules()
1001
Tarek Ziadé188789d2009-05-16 18:37:32 +00001002 def has_headers(self):
Greg Ward51def7d2000-05-27 01:36:14 +00001003 return self.headers and len(self.headers) > 0
1004
Tarek Ziadé188789d2009-05-16 18:37:32 +00001005 def has_scripts(self):
Greg Ward44a61bb2000-05-20 15:06:48 +00001006 return self.scripts and len(self.scripts) > 0
1007
Tarek Ziadé188789d2009-05-16 18:37:32 +00001008 def has_data_files(self):
Greg Ward44a61bb2000-05-20 15:06:48 +00001009 return self.data_files and len(self.data_files) > 0
1010
Tarek Ziadé188789d2009-05-16 18:37:32 +00001011 def is_pure(self):
Greg Wardfe6462c2000-04-04 01:40:52 +00001012 return (self.has_pure_modules() and
1013 not self.has_ext_modules() and
1014 not self.has_c_libraries())
1015
Greg Ward82715e12000-04-21 02:28:14 +00001016 # -- Metadata query methods ----------------------------------------
1017
1018 # If you're looking for 'get_name()', 'get_version()', and so forth,
1019 # they are defined in a sneaky way: the constructor binds self.get_XXX
1020 # to self.metadata.get_XXX. The actual code is in the
1021 # DistributionMetadata class, below.
1022
Greg Ward82715e12000-04-21 02:28:14 +00001023class DistributionMetadata:
1024 """Dummy class to hold the distribution meta-data: name, version,
Greg Wardfd7b91e2000-09-26 01:52:25 +00001025 author, and so forth.
1026 """
Greg Ward82715e12000-04-21 02:28:14 +00001027
Neil Schemenauera8aefe52001-09-03 15:47:21 +00001028 _METHOD_BASENAMES = ("name", "version", "author", "author_email",
1029 "maintainer", "maintainer_email", "url",
1030 "license", "description", "long_description",
1031 "keywords", "platforms", "fullname", "contact",
Berker Peksagce18d8c2016-04-24 01:55:09 +03001032 "contact_email", "classifiers", "download_url",
Fred Drakedb7b0022005-03-20 22:19:47 +00001033 # PEP 314
1034 "provides", "requires", "obsoletes",
1035 )
Neil Schemenauera8aefe52001-09-03 15:47:21 +00001036
Jason R. Coombs3492e392013-11-10 18:15:03 -05001037 def __init__(self, path=None):
1038 if path is not None:
1039 self.read_pkg_file(open(path))
1040 else:
1041 self.name = None
1042 self.version = None
1043 self.author = None
1044 self.author_email = None
1045 self.maintainer = None
1046 self.maintainer_email = None
1047 self.url = None
1048 self.license = None
1049 self.description = None
1050 self.long_description = None
1051 self.keywords = None
1052 self.platforms = None
1053 self.classifiers = None
1054 self.download_url = None
1055 # PEP 314
1056 self.provides = None
1057 self.requires = None
1058 self.obsoletes = None
1059
1060 def read_pkg_file(self, file):
1061 """Reads the metadata values from a file object."""
1062 msg = message_from_file(file)
1063
1064 def _read_field(name):
1065 value = msg[name]
1066 if value == 'UNKNOWN':
1067 return None
1068 return value
1069
1070 def _read_list(name):
1071 values = msg.get_all(name, None)
1072 if values == []:
1073 return None
1074 return values
1075
1076 metadata_version = msg['metadata-version']
1077 self.name = _read_field('name')
1078 self.version = _read_field('version')
1079 self.description = _read_field('summary')
1080 # we are filling author only.
1081 self.author = _read_field('author')
Greg Ward82715e12000-04-21 02:28:14 +00001082 self.maintainer = None
Jason R. Coombs3492e392013-11-10 18:15:03 -05001083 self.author_email = _read_field('author-email')
Greg Ward82715e12000-04-21 02:28:14 +00001084 self.maintainer_email = None
Jason R. Coombs3492e392013-11-10 18:15:03 -05001085 self.url = _read_field('home-page')
1086 self.license = _read_field('license')
1087
1088 if 'download-url' in msg:
1089 self.download_url = _read_field('download-url')
1090 else:
1091 self.download_url = None
1092
1093 self.long_description = _read_field('description')
1094 self.description = _read_field('summary')
1095
1096 if 'keywords' in msg:
1097 self.keywords = _read_field('keywords').split(',')
1098
1099 self.platforms = _read_list('platform')
1100 self.classifiers = _read_list('classifier')
1101
1102 # PEP 314 - these fields only exist in 1.1
1103 if metadata_version == '1.1':
1104 self.requires = _read_list('requires')
1105 self.provides = _read_list('provides')
1106 self.obsoletes = _read_list('obsoletes')
1107 else:
1108 self.requires = None
1109 self.provides = None
1110 self.obsoletes = None
Fred Drakeb94b8492001-12-06 20:51:35 +00001111
Tarek Ziadé188789d2009-05-16 18:37:32 +00001112 def write_pkg_info(self, base_dir):
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001113 """Write the PKG-INFO file into the release tree.
1114 """
Victor Stinnera1bea6e2011-09-05 23:44:56 +02001115 with open(os.path.join(base_dir, 'PKG-INFO'), 'w',
1116 encoding='UTF-8') as pkg_info:
Éric Araujobee5cef2010-11-05 23:51:56 +00001117 self.write_pkg_file(pkg_info)
Fred Drakeb94b8492001-12-06 20:51:35 +00001118
Tarek Ziadé188789d2009-05-16 18:37:32 +00001119 def write_pkg_file(self, file):
Fred Drakedb7b0022005-03-20 22:19:47 +00001120 """Write the PKG-INFO format data to a file object.
1121 """
1122 version = '1.0'
Éric Araujo13e8c8e2011-09-10 01:51:40 +02001123 if (self.provides or self.requires or self.obsoletes or
Jason R. Coombs7c456322014-07-02 08:36:19 -04001124 self.classifiers or self.download_url):
Fred Drakedb7b0022005-03-20 22:19:47 +00001125 version = '1.1'
1126
1127 file.write('Metadata-Version: %s\n' % version)
Jason R. Coombs7c456322014-07-02 08:36:19 -04001128 file.write('Name: %s\n' % self.get_name())
1129 file.write('Version: %s\n' % self.get_version())
1130 file.write('Summary: %s\n' % self.get_description())
1131 file.write('Home-page: %s\n' % self.get_url())
1132 file.write('Author: %s\n' % self.get_contact())
1133 file.write('Author-email: %s\n' % self.get_contact_email())
1134 file.write('License: %s\n' % self.get_license())
Fred Drakedb7b0022005-03-20 22:19:47 +00001135 if self.download_url:
1136 file.write('Download-URL: %s\n' % self.download_url)
1137
Tarek Ziadéf11be752009-06-01 22:36:26 +00001138 long_desc = rfc822_escape(self.get_long_description())
Fred Drakedb7b0022005-03-20 22:19:47 +00001139 file.write('Description: %s\n' % long_desc)
1140
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001141 keywords = ','.join(self.get_keywords())
Fred Drakedb7b0022005-03-20 22:19:47 +00001142 if keywords:
Jason R. Coombs7c456322014-07-02 08:36:19 -04001143 file.write('Keywords: %s\n' % keywords)
Fred Drakedb7b0022005-03-20 22:19:47 +00001144
1145 self._write_list(file, 'Platform', self.get_platforms())
1146 self._write_list(file, 'Classifier', self.get_classifiers())
1147
1148 # PEP 314
1149 self._write_list(file, 'Requires', self.get_requires())
1150 self._write_list(file, 'Provides', self.get_provides())
1151 self._write_list(file, 'Obsoletes', self.get_obsoletes())
1152
Tarek Ziadé188789d2009-05-16 18:37:32 +00001153 def _write_list(self, file, name, values):
Fred Drakedb7b0022005-03-20 22:19:47 +00001154 for value in values:
1155 file.write('%s: %s\n' % (name, value))
1156
Greg Ward82715e12000-04-21 02:28:14 +00001157 # -- Metadata query methods ----------------------------------------
1158
Tarek Ziadé188789d2009-05-16 18:37:32 +00001159 def get_name(self):
Greg Wardfe6462c2000-04-04 01:40:52 +00001160 return self.name or "UNKNOWN"
1161
Greg Ward82715e12000-04-21 02:28:14 +00001162 def get_version(self):
Thomas Hellerbcd89752001-12-06 20:44:19 +00001163 return self.version or "0.0.0"
Greg Wardfe6462c2000-04-04 01:40:52 +00001164
Tarek Ziadé188789d2009-05-16 18:37:32 +00001165 def get_fullname(self):
Greg Ward82715e12000-04-21 02:28:14 +00001166 return "%s-%s" % (self.get_name(), self.get_version())
1167
1168 def get_author(self):
1169 return self.author or "UNKNOWN"
1170
1171 def get_author_email(self):
1172 return self.author_email or "UNKNOWN"
1173
1174 def get_maintainer(self):
1175 return self.maintainer or "UNKNOWN"
1176
1177 def get_maintainer_email(self):
1178 return self.maintainer_email or "UNKNOWN"
1179
1180 def get_contact(self):
Tarek Ziadé188789d2009-05-16 18:37:32 +00001181 return self.maintainer or self.author or "UNKNOWN"
Greg Ward82715e12000-04-21 02:28:14 +00001182
1183 def get_contact_email(self):
Tarek Ziadé188789d2009-05-16 18:37:32 +00001184 return self.maintainer_email or self.author_email or "UNKNOWN"
Greg Ward82715e12000-04-21 02:28:14 +00001185
1186 def get_url(self):
1187 return self.url or "UNKNOWN"
1188
Andrew M. Kuchlingfa7dc572001-08-10 18:49:23 +00001189 def get_license(self):
1190 return self.license or "UNKNOWN"
1191 get_licence = get_license
Fred Drakeb94b8492001-12-06 20:51:35 +00001192
Greg Ward82715e12000-04-21 02:28:14 +00001193 def get_description(self):
1194 return self.description or "UNKNOWN"
Greg Warde5a584e2000-04-26 02:26:55 +00001195
1196 def get_long_description(self):
1197 return self.long_description or "UNKNOWN"
1198
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001199 def get_keywords(self):
1200 return self.keywords or []
1201
Berker Peksagdcaed6b2017-11-23 21:34:20 +03001202 def set_keywords(self, value):
Neil Schemenauer8837dd02017-12-04 18:58:12 -08001203 self.keywords = _ensure_list(value, 'keywords')
Berker Peksagdcaed6b2017-11-23 21:34:20 +03001204
Andrew M. Kuchlinga7210ed2001-03-22 03:06:52 +00001205 def get_platforms(self):
1206 return self.platforms or ["UNKNOWN"]
1207
Berker Peksagdcaed6b2017-11-23 21:34:20 +03001208 def set_platforms(self, value):
Neil Schemenauer8837dd02017-12-04 18:58:12 -08001209 self.platforms = _ensure_list(value, 'platforms')
Berker Peksagdcaed6b2017-11-23 21:34:20 +03001210
Andrew M. Kuchling282e2c32003-01-03 15:24:36 +00001211 def get_classifiers(self):
1212 return self.classifiers or []
1213
Berker Peksagdcaed6b2017-11-23 21:34:20 +03001214 def set_classifiers(self, value):
Neil Schemenauer8837dd02017-12-04 18:58:12 -08001215 self.classifiers = _ensure_list(value, 'classifiers')
Berker Peksagdcaed6b2017-11-23 21:34:20 +03001216
Andrew M. Kuchling188d85f2003-02-19 14:16:01 +00001217 def get_download_url(self):
1218 return self.download_url or "UNKNOWN"
1219
Fred Drakedb7b0022005-03-20 22:19:47 +00001220 # PEP 314
Fred Drakedb7b0022005-03-20 22:19:47 +00001221 def get_requires(self):
1222 return self.requires or []
1223
1224 def set_requires(self, value):
1225 import distutils.versionpredicate
1226 for v in value:
1227 distutils.versionpredicate.VersionPredicate(v)
Neil Schemenauer8837dd02017-12-04 18:58:12 -08001228 self.requires = list(value)
Fred Drakedb7b0022005-03-20 22:19:47 +00001229
1230 def get_provides(self):
1231 return self.provides or []
1232
1233 def set_provides(self, value):
1234 value = [v.strip() for v in value]
1235 for v in value:
1236 import distutils.versionpredicate
Fred Drake227e8ff2005-03-21 06:36:32 +00001237 distutils.versionpredicate.split_provision(v)
Fred Drakedb7b0022005-03-20 22:19:47 +00001238 self.provides = value
1239
1240 def get_obsoletes(self):
1241 return self.obsoletes or []
1242
1243 def set_obsoletes(self, value):
1244 import distutils.versionpredicate
1245 for v in value:
1246 distutils.versionpredicate.VersionPredicate(v)
Neil Schemenauer8837dd02017-12-04 18:58:12 -08001247 self.obsoletes = list(value)
Fred Drakedb7b0022005-03-20 22:19:47 +00001248
Tarek Ziadé188789d2009-05-16 18:37:32 +00001249def fix_help_options(options):
Greg Ward2ff78872000-06-24 00:23:20 +00001250 """Convert a 4-tuple 'help_options' list as found in various command
1251 classes to the 3-tuple form required by FancyGetopt.
1252 """
1253 new_options = []
1254 for help_tuple in options:
1255 new_options.append(help_tuple[0:3])
1256 return new_options