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