]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/setuptools/_distutils/cmd.py
3 Provides the Command class, the base class for the command classes
4 in the distutils.command package.
12 from .errors
import DistutilsOptionError
13 from . import util
, dir_util
, file_util
, archive_util
, dep_util
18 """Abstract base class for defining command classes, the "worker bees"
19 of the Distutils. A useful analogy for command classes is to think of
20 them as subroutines with local variables called "options". The options
21 are "declared" in 'initialize_options()' and "defined" (given their
22 final values, aka "finalized") in 'finalize_options()', both of which
23 must be defined by every command class. The distinction between the
24 two is necessary because option values might come from the outside
25 world (command line, config file, ...), and any options dependent on
26 other options must be computed *after* these outside influences have
27 been processed -- hence 'finalize_options()'. The "body" of the
28 subroutine, where it does all its work based on the values of its
29 options, is the 'run()' method, which must also be implemented by every
33 # 'sub_commands' formalizes the notion of a "family" of commands,
34 # eg. "install" as the parent with sub-commands "install_lib",
35 # "install_headers", etc. The parent of a family of commands
36 # defines 'sub_commands' as a class attribute; it's a list of
37 # (command_name : string, predicate : unbound_method | string | None)
38 # tuples, where 'predicate' is a method of the parent command that
39 # determines whether the corresponding command is applicable in the
40 # current situation. (Eg. we "install_headers" is only applicable if
41 # we have any C header files to install.) If 'predicate' is None,
42 # that command is always applicable.
44 # 'sub_commands' is usually defined at the *end* of a class, because
45 # predicates can be unbound methods, so they must already have been
46 # defined. The canonical example is the "install" command.
49 # -- Creation/initialization methods -------------------------------
51 def __init__(self
, dist
):
52 """Create and initialize a new Command object. Most importantly,
53 invokes the 'initialize_options()' method, which is the real
54 initializer and depends on the actual command being
57 # late import because of mutual dependence between these classes
58 from distutils
.dist
import Distribution
60 if not isinstance(dist
, Distribution
):
61 raise TypeError("dist must be a Distribution instance")
62 if self
.__class
__ is Command
:
63 raise RuntimeError("Command is an abstract class")
65 self
.distribution
= dist
66 self
.initialize_options()
68 # Per-command versions of the global flags, so that the user can
69 # customize Distutils' behaviour command-by-command and let some
70 # commands fall back on the Distribution's behaviour. None means
71 # "not defined, check self.distribution's copy", while 0 or 1 mean
72 # false and true (duh). Note that this means figuring out the real
73 # value of each flag is a touch complicated -- hence "self._dry_run"
74 # will be handled by __getattr__, below.
75 # XXX This needs to be fixed.
78 # verbose is largely ignored, but needs to be set for
79 # backwards compatibility (I think)?
80 self
.verbose
= dist
.verbose
82 # Some commands define a 'self.force' option to ignore file
83 # timestamps, but methods defined *here* assume that
84 # 'self.force' exists for all commands. So define it here
88 # The 'help' flag is just used for command-line parsing, so
89 # none of that complicated bureaucracy is needed.
92 # 'finalized' records whether or not 'finalize_options()' has been
93 # called. 'finalize_options()' itself should not pay attention to
94 # this flag: it is the business of 'ensure_finalized()', which
95 # always calls 'finalize_options()', to respect/update it.
98 # XXX A more explicit way to customize dry_run would be better.
99 def __getattr__(self
, attr
):
100 if attr
== 'dry_run':
101 myval
= getattr(self
, "_" + attr
)
103 return getattr(self
.distribution
, attr
)
107 raise AttributeError(attr
)
109 def ensure_finalized(self
):
110 if not self
.finalized
:
111 self
.finalize_options()
114 # Subclasses must define:
115 # initialize_options()
116 # provide default values for all options; may be customized by
117 # setup script, by options from config file(s), or by command-line
120 # decide on the final values for all options; this is called
121 # after all possible intervention from the outside world
122 # (command-line, option file, etc.) has been processed
124 # run the command: do whatever it is we're here to do,
125 # controlled by the command's various option values
127 def initialize_options(self
):
128 """Set default values for all the options that this command
129 supports. Note that these defaults may be overridden by other
130 commands, by the setup script, by config files, or by the
131 command-line. Thus, this is not the place to code dependencies
132 between options; generally, 'initialize_options()' implementations
133 are just a bunch of "self.foo = None" assignments.
135 This method must be implemented by all command classes.
138 "abstract method -- subclass %s must override" % self
.__class
__
141 def finalize_options(self
):
142 """Set final values for all the options that this command supports.
143 This is always called as late as possible, ie. after any option
144 assignments from the command-line or from other commands have been
145 done. Thus, this is the place to code option dependencies: if
146 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
147 long as 'foo' still has the same value it was assigned in
148 'initialize_options()'.
150 This method must be implemented by all command classes.
153 "abstract method -- subclass %s must override" % self
.__class
__
156 def dump_options(self
, header
=None, indent
=""):
157 from distutils
.fancy_getopt
import longopt_xlate
160 header
= "command options for '%s':" % self
.get_command_name()
161 self
.announce(indent
+ header
, level
=logging
.INFO
)
162 indent
= indent
+ " "
163 for (option
, _
, _
) in self
.user_options
:
164 option
= option
.translate(longopt_xlate
)
165 if option
[-1] == "=":
167 value
= getattr(self
, option
)
168 self
.announce(indent
+ "{} = {}".format(option
, value
), level
=logging
.INFO
)
171 """A command's raison d'etre: carry out the action it exists to
172 perform, controlled by the options initialized in
173 'initialize_options()', customized by other commands, the setup
174 script, the command-line, and config files, and finalized in
175 'finalize_options()'. All terminal output and filesystem
176 interaction should be done by 'run()'.
178 This method must be implemented by all command classes.
181 "abstract method -- subclass %s must override" % self
.__class
__
184 def announce(self
, msg
, level
=logging
.DEBUG
):
187 def debug_print(self
, msg
):
188 """Print 'msg' to stdout if the global DEBUG (taken from the
189 DISTUTILS_DEBUG environment variable) flag is true.
191 from distutils
.debug
import DEBUG
197 # -- Option validation methods -------------------------------------
198 # (these are very handy in writing the 'finalize_options()' method)
200 # NB. the general philosophy here is to ensure that a particular option
201 # value meets certain type and value constraints. If not, we try to
202 # force it into conformance (eg. if we expect a list but have a string,
203 # split the string on comma and/or whitespace). If we can't force the
204 # option into conformance, raise DistutilsOptionError. Thus, command
205 # classes need do nothing more than (eg.)
206 # self.ensure_string_list('foo')
207 # and they can be guaranteed that thereafter, self.foo will be
210 def _ensure_stringlike(self
, option
, what
, default
=None):
211 val
= getattr(self
, option
)
213 setattr(self
, option
, default
)
215 elif not isinstance(val
, str):
216 raise DistutilsOptionError(
217 "'{}' must be a {} (got `{}`)".format(option
, what
, val
)
221 def ensure_string(self
, option
, default
=None):
222 """Ensure that 'option' is a string; if not defined, set it to
225 self
._ensure
_stringlike
(option
, "string", default
)
227 def ensure_string_list(self
, option
):
228 r
"""Ensure that 'option' is a list of strings. If 'option' is
229 currently a string, we split it either on /,\s*/ or /\s+/, so
230 "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
231 ["foo", "bar", "baz"].
233 val
= getattr(self
, option
)
236 elif isinstance(val
, str):
237 setattr(self
, option
, re
.split(r
',\s*|\s+', val
))
239 if isinstance(val
, list):
240 ok
= all(isinstance(v
, str) for v
in val
)
244 raise DistutilsOptionError(
245 "'{}' must be a list of strings (got {!r})".format(option
, val
)
248 def _ensure_tested_string(self
, option
, tester
, what
, error_fmt
, default
=None):
249 val
= self
._ensure
_stringlike
(option
, what
, default
)
250 if val
is not None and not tester(val
):
251 raise DistutilsOptionError(
252 ("error in '%s' option: " + error_fmt
) % (option
, val
)
255 def ensure_filename(self
, option
):
256 """Ensure that 'option' is the name of an existing file."""
257 self
._ensure
_tested
_string
(
258 option
, os
.path
.isfile
, "filename", "'%s' does not exist or is not a file"
261 def ensure_dirname(self
, option
):
262 self
._ensure
_tested
_string
(
266 "'%s' does not exist or is not a directory",
269 # -- Convenience methods for commands ------------------------------
271 def get_command_name(self
):
272 if hasattr(self
, 'command_name'):
273 return self
.command_name
275 return self
.__class
__.__name
__
277 def set_undefined_options(self
, src_cmd
, *option_pairs
):
278 """Set the values of any "undefined" options from corresponding
279 option values in some other command object. "Undefined" here means
280 "is None", which is the convention used to indicate that an option
281 has not been changed between 'initialize_options()' and
282 'finalize_options()'. Usually called from 'finalize_options()' for
283 options that depend on some other command rather than another
284 option of the same command. 'src_cmd' is the other command from
285 which option values will be taken (a command object will be created
286 for it if necessary); the remaining arguments are
287 '(src_option,dst_option)' tuples which mean "take the value of
288 'src_option' in the 'src_cmd' command object, and copy it to
289 'dst_option' in the current command object".
291 # Option_pairs: list of (src_option, dst_option) tuples
292 src_cmd_obj
= self
.distribution
.get_command_obj(src_cmd
)
293 src_cmd_obj
.ensure_finalized()
294 for (src_option
, dst_option
) in option_pairs
:
295 if getattr(self
, dst_option
) is None:
296 setattr(self
, dst_option
, getattr(src_cmd_obj
, src_option
))
298 def get_finalized_command(self
, command
, create
=1):
299 """Wrapper around Distribution's 'get_command_obj()' method: find
300 (create if necessary and 'create' is true) the command object for
301 'command', call its 'ensure_finalized()' method, and return the
302 finalized command object.
304 cmd_obj
= self
.distribution
.get_command_obj(command
, create
)
305 cmd_obj
.ensure_finalized()
308 # XXX rename to 'get_reinitialized_command()'? (should do the
309 # same in dist.py, if so)
310 def reinitialize_command(self
, command
, reinit_subcommands
=0):
311 return self
.distribution
.reinitialize_command(command
, reinit_subcommands
)
313 def run_command(self
, command
):
314 """Run some other command: uses the 'run_command()' method of
315 Distribution, which creates and finalizes the command object if
316 necessary and then invokes its 'run()' method.
318 self
.distribution
.run_command(command
)
320 def get_sub_commands(self
):
321 """Determine the sub-commands that are relevant in the current
322 distribution (ie., that need to be run). This is based on the
323 'sub_commands' class attribute: each tuple in that list may include
324 a method that we call to determine if the subcommand needs to be
325 run for the current distribution. Return a list of command names.
328 for (cmd_name
, method
) in self
.sub_commands
:
329 if method
is None or method(self
):
330 commands
.append(cmd_name
)
333 # -- External world manipulation -----------------------------------
336 log
.warning("warning: %s: %s\n", self
.get_command_name(), msg
)
338 def execute(self
, func
, args
, msg
=None, level
=1):
339 util
.execute(func
, args
, msg
, dry_run
=self
.dry_run
)
341 def mkpath(self
, name
, mode
=0o777):
342 dir_util
.mkpath(name
, mode
, dry_run
=self
.dry_run
)
345 self
, infile
, outfile
, preserve_mode
=1, preserve_times
=1, link
=None, level
=1
347 """Copy a file respecting verbose, dry-run and force flags. (The
348 former two default to whatever is in the Distribution object, and
349 the latter defaults to false for commands that don't define it.)"""
350 return file_util
.copy_file(
357 dry_run
=self
.dry_run
,
369 """Copy an entire directory tree respecting verbose, dry-run,
372 return dir_util
.copy_tree(
379 dry_run
=self
.dry_run
,
382 def move_file(self
, src
, dst
, level
=1):
383 """Move a file respecting dry-run flag."""
384 return file_util
.move_file(src
, dst
, dry_run
=self
.dry_run
)
386 def spawn(self
, cmd
, search_path
=1, level
=1):
387 """Spawn an external command respecting dry-run flag."""
388 from distutils
.spawn
import spawn
390 spawn(cmd
, search_path
, dry_run
=self
.dry_run
)
393 self
, base_name
, format
, root_dir
=None, base_dir
=None, owner
=None, group
=None
395 return archive_util
.make_archive(
400 dry_run
=self
.dry_run
,
406 self
, infiles
, outfile
, func
, args
, exec_msg
=None, skip_msg
=None, level
=1
408 """Special case of 'execute()' for operations that process one or
409 more input files and generate one output file. Works just like
410 'execute()', except the operation is skipped and a different
411 message printed if 'outfile' already exists and is newer than all
412 files listed in 'infiles'. If the command defined 'self.force',
413 and it is true, then the command is unconditionally run -- does no
417 skip_msg
= "skipping %s (inputs unchanged)" % outfile
419 # Allow 'infiles' to be a single string
420 if isinstance(infiles
, str):
422 elif not isinstance(infiles
, (list, tuple)):
423 raise TypeError("'infiles' must be a string, or a list or tuple of strings")
426 exec_msg
= "generating {} from {}".format(outfile
, ', '.join(infiles
))
428 # If 'outfile' must be regenerated (either because it doesn't
429 # exist, is out-of-date, or the 'force' flag is true) then
430 # perform the action that presumably regenerates it
431 if self
.force
or dep_util
.newer_group(infiles
, outfile
):
432 self
.execute(func
, args
, exec_msg
, level
)
433 # Otherwise, print the "skip" message