]> crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/setuptools/_distutils/cmd.py
Configuracion en desarrollo PC pega
[config.git] / djavu-asus / elpy / rpc-venv / lib / python3.11 / site-packages / setuptools / _distutils / cmd.py
1 """distutils.cmd
2
3 Provides the Command class, the base class for the command classes
4 in the distutils.command package.
5 """
6
7 import sys
8 import os
9 import re
10 import logging
11
12 from .errors import DistutilsOptionError
13 from . import util, dir_util, file_util, archive_util, dep_util
14 from ._log import log
15
16
17 class Command:
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
30 command class.
31 """
32
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.
43 #
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.
47 sub_commands = []
48
49 # -- Creation/initialization methods -------------------------------
50
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
55 instantiated.
56 """
57 # late import because of mutual dependence between these classes
58 from distutils.dist import Distribution
59
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")
64
65 self.distribution = dist
66 self.initialize_options()
67
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.
76 self._dry_run = None
77
78 # verbose is largely ignored, but needs to be set for
79 # backwards compatibility (I think)?
80 self.verbose = dist.verbose
81
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
85 # just to be safe.
86 self.force = None
87
88 # The 'help' flag is just used for command-line parsing, so
89 # none of that complicated bureaucracy is needed.
90 self.help = 0
91
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.
96 self.finalized = 0
97
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)
102 if myval is None:
103 return getattr(self.distribution, attr)
104 else:
105 return myval
106 else:
107 raise AttributeError(attr)
108
109 def ensure_finalized(self):
110 if not self.finalized:
111 self.finalize_options()
112 self.finalized = 1
113
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
118 # options
119 # finalize_options()
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
123 # run()
124 # run the command: do whatever it is we're here to do,
125 # controlled by the command's various option values
126
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.
134
135 This method must be implemented by all command classes.
136 """
137 raise RuntimeError(
138 "abstract method -- subclass %s must override" % self.__class__
139 )
140
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()'.
149
150 This method must be implemented by all command classes.
151 """
152 raise RuntimeError(
153 "abstract method -- subclass %s must override" % self.__class__
154 )
155
156 def dump_options(self, header=None, indent=""):
157 from distutils.fancy_getopt import longopt_xlate
158
159 if header is None:
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] == "=":
166 option = option[:-1]
167 value = getattr(self, option)
168 self.announce(indent + "{} = {}".format(option, value), level=logging.INFO)
169
170 def run(self):
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()'.
177
178 This method must be implemented by all command classes.
179 """
180 raise RuntimeError(
181 "abstract method -- subclass %s must override" % self.__class__
182 )
183
184 def announce(self, msg, level=logging.DEBUG):
185 log.log(level, msg)
186
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.
190 """
191 from distutils.debug import DEBUG
192
193 if DEBUG:
194 print(msg)
195 sys.stdout.flush()
196
197 # -- Option validation methods -------------------------------------
198 # (these are very handy in writing the 'finalize_options()' method)
199 #
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
208 # a list of strings.
209
210 def _ensure_stringlike(self, option, what, default=None):
211 val = getattr(self, option)
212 if val is None:
213 setattr(self, option, default)
214 return default
215 elif not isinstance(val, str):
216 raise DistutilsOptionError(
217 "'{}' must be a {} (got `{}`)".format(option, what, val)
218 )
219 return val
220
221 def ensure_string(self, option, default=None):
222 """Ensure that 'option' is a string; if not defined, set it to
223 'default'.
224 """
225 self._ensure_stringlike(option, "string", default)
226
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"].
232 """
233 val = getattr(self, option)
234 if val is None:
235 return
236 elif isinstance(val, str):
237 setattr(self, option, re.split(r',\s*|\s+', val))
238 else:
239 if isinstance(val, list):
240 ok = all(isinstance(v, str) for v in val)
241 else:
242 ok = False
243 if not ok:
244 raise DistutilsOptionError(
245 "'{}' must be a list of strings (got {!r})".format(option, val)
246 )
247
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)
253 )
254
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"
259 )
260
261 def ensure_dirname(self, option):
262 self._ensure_tested_string(
263 option,
264 os.path.isdir,
265 "directory name",
266 "'%s' does not exist or is not a directory",
267 )
268
269 # -- Convenience methods for commands ------------------------------
270
271 def get_command_name(self):
272 if hasattr(self, 'command_name'):
273 return self.command_name
274 else:
275 return self.__class__.__name__
276
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".
290 """
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))
297
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.
303 """
304 cmd_obj = self.distribution.get_command_obj(command, create)
305 cmd_obj.ensure_finalized()
306 return cmd_obj
307
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)
312
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.
317 """
318 self.distribution.run_command(command)
319
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.
326 """
327 commands = []
328 for (cmd_name, method) in self.sub_commands:
329 if method is None or method(self):
330 commands.append(cmd_name)
331 return commands
332
333 # -- External world manipulation -----------------------------------
334
335 def warn(self, msg):
336 log.warning("warning: %s: %s\n", self.get_command_name(), msg)
337
338 def execute(self, func, args, msg=None, level=1):
339 util.execute(func, args, msg, dry_run=self.dry_run)
340
341 def mkpath(self, name, mode=0o777):
342 dir_util.mkpath(name, mode, dry_run=self.dry_run)
343
344 def copy_file(
345 self, infile, outfile, preserve_mode=1, preserve_times=1, link=None, level=1
346 ):
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(
351 infile,
352 outfile,
353 preserve_mode,
354 preserve_times,
355 not self.force,
356 link,
357 dry_run=self.dry_run,
358 )
359
360 def copy_tree(
361 self,
362 infile,
363 outfile,
364 preserve_mode=1,
365 preserve_times=1,
366 preserve_symlinks=0,
367 level=1,
368 ):
369 """Copy an entire directory tree respecting verbose, dry-run,
370 and force flags.
371 """
372 return dir_util.copy_tree(
373 infile,
374 outfile,
375 preserve_mode,
376 preserve_times,
377 preserve_symlinks,
378 not self.force,
379 dry_run=self.dry_run,
380 )
381
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)
385
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
389
390 spawn(cmd, search_path, dry_run=self.dry_run)
391
392 def make_archive(
393 self, base_name, format, root_dir=None, base_dir=None, owner=None, group=None
394 ):
395 return archive_util.make_archive(
396 base_name,
397 format,
398 root_dir,
399 base_dir,
400 dry_run=self.dry_run,
401 owner=owner,
402 group=group,
403 )
404
405 def make_file(
406 self, infiles, outfile, func, args, exec_msg=None, skip_msg=None, level=1
407 ):
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
414 timestamp checks.
415 """
416 if skip_msg is None:
417 skip_msg = "skipping %s (inputs unchanged)" % outfile
418
419 # Allow 'infiles' to be a single string
420 if isinstance(infiles, str):
421 infiles = (infiles,)
422 elif not isinstance(infiles, (list, tuple)):
423 raise TypeError("'infiles' must be a string, or a list or tuple of strings")
424
425 if exec_msg is None:
426 exec_msg = "generating {} from {}".format(outfile, ', '.join(infiles))
427
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
434 else:
435 log.debug(skip_msg)