blob: 939f7959457934f2607d3289236f98132bd939bb [file] [log] [blame]
Greg Wardfe6462c2000-04-04 01:40:52 +00001"""distutils.cmd
2
3Provides the Command class, the base class for the command classes
Greg Ward8ff5a3f2000-06-02 00:44:53 +00004in the distutils.command package.
5"""
Greg Wardfe6462c2000-04-04 01:40:52 +00006
Neal Norwitz9d72bb42007-04-17 08:48:32 +00007import sys, os, re
Tarek Ziadé9b6ddb82009-02-06 09:03:10 +00008from distutils.errors import DistutilsOptionError
Greg Ward29124ff2000-08-13 00:36:47 +00009from distutils import util, dir_util, file_util, archive_util, dep_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000010from distutils import log
Greg Wardfe6462c2000-04-04 01:40:52 +000011
12class Command:
13 """Abstract base class for defining command classes, the "worker bees"
Greg Ward8ff5a3f2000-06-02 00:44:53 +000014 of the Distutils. A useful analogy for command classes is to think of
15 them as subroutines with local variables called "options". The options
16 are "declared" in 'initialize_options()' and "defined" (given their
17 final values, aka "finalized") in 'finalize_options()', both of which
18 must be defined by every command class. The distinction between the
19 two is necessary because option values might come from the outside
20 world (command line, config file, ...), and any options dependent on
21 other options must be computed *after* these outside influences have
22 been processed -- hence 'finalize_options()'. The "body" of the
23 subroutine, where it does all its work based on the values of its
24 options, is the 'run()' method, which must also be implemented by every
25 command class.
26 """
Greg Wardfe6462c2000-04-04 01:40:52 +000027
Greg Wardb3e0ad92000-09-16 15:09:17 +000028 # 'sub_commands' formalizes the notion of a "family" of commands,
29 # eg. "install" as the parent with sub-commands "install_lib",
30 # "install_headers", etc. The parent of a family of commands
31 # defines 'sub_commands' as a class attribute; it's a list of
32 # (command_name : string, predicate : unbound_method | string | None)
33 # tuples, where 'predicate' is a method of the parent command that
34 # determines whether the corresponding command is applicable in the
35 # current situation. (Eg. we "install_headers" is only applicable if
36 # we have any C header files to install.) If 'predicate' is None,
37 # that command is always applicable.
Fred Drakeb94b8492001-12-06 20:51:35 +000038 #
Greg Wardb3e0ad92000-09-16 15:09:17 +000039 # 'sub_commands' is usually defined at the *end* of a class, because
40 # predicates can be unbound methods, so they must already have been
41 # defined. The canonical example is the "install" command.
42 sub_commands = []
43
44
Greg Wardfe6462c2000-04-04 01:40:52 +000045 # -- Creation/initialization methods -------------------------------
46
Collin Winter5b7e9d72007-08-30 03:52:21 +000047 def __init__(self, dist):
Greg Wardfe6462c2000-04-04 01:40:52 +000048 """Create and initialize a new Command object. Most importantly,
Greg Ward8ff5a3f2000-06-02 00:44:53 +000049 invokes the 'initialize_options()' method, which is the real
50 initializer and depends on the actual command being
51 instantiated.
52 """
Greg Wardfe6462c2000-04-04 01:40:52 +000053 # late import because of mutual dependence between these classes
54 from distutils.dist import Distribution
55
Greg Ward071ed762000-09-26 02:12:31 +000056 if not isinstance(dist, Distribution):
Collin Winter5b7e9d72007-08-30 03:52:21 +000057 raise TypeError("dist must be a Distribution instance")
Greg Wardfe6462c2000-04-04 01:40:52 +000058 if self.__class__ is Command:
Collin Winter5b7e9d72007-08-30 03:52:21 +000059 raise RuntimeError("Command is an abstract class")
Greg Wardfe6462c2000-04-04 01:40:52 +000060
61 self.distribution = dist
Greg Ward071ed762000-09-26 02:12:31 +000062 self.initialize_options()
Greg Wardfe6462c2000-04-04 01:40:52 +000063
64 # Per-command versions of the global flags, so that the user can
65 # customize Distutils' behaviour command-by-command and let some
Fred Drake2b2fe942004-06-18 21:28:28 +000066 # commands fall back on the Distribution's behaviour. None means
Greg Wardfe6462c2000-04-04 01:40:52 +000067 # "not defined, check self.distribution's copy", while 0 or 1 mean
68 # false and true (duh). Note that this means figuring out the real
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000069 # value of each flag is a touch complicated -- hence "self._dry_run"
70 # will be handled by __getattr__, below.
71 # XXX This needs to be fixed.
Greg Wardfe6462c2000-04-04 01:40:52 +000072 self._dry_run = None
Greg Wardfe6462c2000-04-04 01:40:52 +000073
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000074 # verbose is largely ignored, but needs to be set for
75 # backwards compatibility (I think)?
76 self.verbose = dist.verbose
Tim Peters182b5ac2004-07-18 06:16:08 +000077
Greg Wardd197a3a2000-04-10 13:11:51 +000078 # Some commands define a 'self.force' option to ignore file
79 # timestamps, but methods defined *here* assume that
80 # 'self.force' exists for all commands. So define it here
81 # just to be safe.
82 self.force = None
83
Greg Wardfe6462c2000-04-04 01:40:52 +000084 # The 'help' flag is just used for command-line parsing, so
85 # none of that complicated bureaucracy is needed.
86 self.help = 0
87
Greg Ward4fb29e52000-05-27 17:27:23 +000088 # 'finalized' records whether or not 'finalize_options()' has been
Greg Wardfe6462c2000-04-04 01:40:52 +000089 # called. 'finalize_options()' itself should not pay attention to
Greg Ward4fb29e52000-05-27 17:27:23 +000090 # this flag: it is the business of 'ensure_finalized()', which
91 # always calls 'finalize_options()', to respect/update it.
92 self.finalized = 0
Greg Wardfe6462c2000-04-04 01:40:52 +000093
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000094 # XXX A more explicit way to customize dry_run would be better.
Tarek Ziadé3a794c42009-04-13 20:19:58 +000095 def __getattr__(self, attr):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000096 if attr == 'dry_run':
Greg Ward071ed762000-09-26 02:12:31 +000097 myval = getattr(self, "_" + attr)
Greg Wardfe6462c2000-04-04 01:40:52 +000098 if myval is None:
Greg Ward071ed762000-09-26 02:12:31 +000099 return getattr(self.distribution, attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000100 else:
101 return myval
102 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000103 raise AttributeError(attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000104
Tarek Ziadé3a794c42009-04-13 20:19:58 +0000105 def ensure_finalized(self):
Greg Ward4fb29e52000-05-27 17:27:23 +0000106 if not self.finalized:
Greg Ward071ed762000-09-26 02:12:31 +0000107 self.finalize_options()
Greg Ward4fb29e52000-05-27 17:27:23 +0000108 self.finalized = 1
Fred Drakeb94b8492001-12-06 20:51:35 +0000109
Greg Wardfe6462c2000-04-04 01:40:52 +0000110 # Subclasses must define:
111 # initialize_options()
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000112 # provide default values for all options; may be customized by
113 # setup script, by options from config file(s), or by command-line
114 # options
Greg Wardfe6462c2000-04-04 01:40:52 +0000115 # finalize_options()
116 # decide on the final values for all options; this is called
117 # after all possible intervention from the outside world
118 # (command-line, option file, etc.) has been processed
119 # run()
120 # run the command: do whatever it is we're here to do,
121 # controlled by the command's various option values
122
Collin Winter5b7e9d72007-08-30 03:52:21 +0000123 def initialize_options(self):
Greg Wardfe6462c2000-04-04 01:40:52 +0000124 """Set default values for all the options that this command
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000125 supports. Note that these defaults may be overridden by other
126 commands, by the setup script, by config files, or by the
127 command-line. Thus, this is not the place to code dependencies
128 between options; generally, 'initialize_options()' implementations
129 are just a bunch of "self.foo = None" assignments.
Fred Drakeb94b8492001-12-06 20:51:35 +0000130
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000131 This method must be implemented by all command classes.
132 """
Collin Winter5b7e9d72007-08-30 03:52:21 +0000133 raise RuntimeError("abstract method -- subclass %s must override"
134 % self.__class__)
Fred Drakeb94b8492001-12-06 20:51:35 +0000135
Collin Winter5b7e9d72007-08-30 03:52:21 +0000136 def finalize_options(self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000137 """Set final values for all the options that this command supports.
138 This is always called as late as possible, ie. after any option
139 assignments from the command-line or from other commands have been
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000140 done. Thus, this is the place to code option dependencies: if
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000141 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
142 long as 'foo' still has the same value it was assigned in
143 'initialize_options()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000144
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000145 This method must be implemented by all command classes.
146 """
Collin Winter5b7e9d72007-08-30 03:52:21 +0000147 raise RuntimeError("abstract method -- subclass %s must override"
148 % self.__class__)
Greg Wardfe6462c2000-04-04 01:40:52 +0000149
Greg Wardadda1562000-05-28 23:54:00 +0000150
Collin Winter5b7e9d72007-08-30 03:52:21 +0000151 def dump_options(self, header=None, indent=""):
Greg Wardadda1562000-05-28 23:54:00 +0000152 from distutils.fancy_getopt import longopt_xlate
153 if header is None:
154 header = "command options for '%s':" % self.get_command_name()
Tarek Ziadé48494d02009-02-07 00:10:48 +0000155 self.announce(indent + header, level=log.INFO)
Greg Wardadda1562000-05-28 23:54:00 +0000156 indent = indent + " "
157 for (option, _, _) in self.user_options:
Tarek Ziadé83496692009-09-21 13:10:05 +0000158 option = option.translate(longopt_xlate)
Greg Wardadda1562000-05-28 23:54:00 +0000159 if option[-1] == "=":
160 option = option[:-1]
161 value = getattr(self, option)
Tarek Ziadé48494d02009-02-07 00:10:48 +0000162 self.announce(indent + "%s = %s" % (option, value),
163 level=log.INFO)
Greg Wardadda1562000-05-28 23:54:00 +0000164
Collin Winter5b7e9d72007-08-30 03:52:21 +0000165 def run(self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000166 """A command's raison d'etre: carry out the action it exists to
167 perform, controlled by the options initialized in
168 'initialize_options()', customized by other commands, the setup
169 script, the command-line, and config files, and finalized in
170 'finalize_options()'. All terminal output and filesystem
171 interaction should be done by 'run()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000172
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000173 This method must be implemented by all command classes.
174 """
Collin Winter5b7e9d72007-08-30 03:52:21 +0000175 raise RuntimeError("abstract method -- subclass %s must override"
176 % self.__class__)
Greg Wardfe6462c2000-04-04 01:40:52 +0000177
Collin Winter5b7e9d72007-08-30 03:52:21 +0000178 def announce(self, msg, level=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000179 """If the current verbosity level is of greater than or equal to
180 'level' print 'msg' to stdout.
181 """
Guido van Rossumaf160652003-02-20 02:10:08 +0000182 log.log(level, msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000183
Collin Winter5b7e9d72007-08-30 03:52:21 +0000184 def debug_print(self, msg):
Greg Wardebec02a2000-06-08 00:02:36 +0000185 """Print 'msg' to stdout if the global DEBUG (taken from the
186 DISTUTILS_DEBUG environment variable) flag is true.
187 """
Jeremy Hyltonfcd73532002-09-11 16:31:53 +0000188 from distutils.debug import DEBUG
Greg Wardebec02a2000-06-08 00:02:36 +0000189 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000190 print(msg)
Neil Schemenauer69374e42001-08-29 23:57:22 +0000191 sys.stdout.flush()
Fred Drakeb94b8492001-12-06 20:51:35 +0000192
Greg Wardebec02a2000-06-08 00:02:36 +0000193
Greg Ward31413a72000-06-04 14:21:28 +0000194 # -- Option validation methods -------------------------------------
195 # (these are very handy in writing the 'finalize_options()' method)
Fred Drakeb94b8492001-12-06 20:51:35 +0000196 #
Greg Ward31413a72000-06-04 14:21:28 +0000197 # NB. the general philosophy here is to ensure that a particular option
198 # value meets certain type and value constraints. If not, we try to
199 # force it into conformance (eg. if we expect a list but have a string,
200 # split the string on comma and/or whitespace). If we can't force the
201 # option into conformance, raise DistutilsOptionError. Thus, command
202 # classes need do nothing more than (eg.)
203 # self.ensure_string_list('foo')
204 # and they can be guaranteed that thereafter, self.foo will be
205 # a list of strings.
206
Collin Winter5b7e9d72007-08-30 03:52:21 +0000207 def _ensure_stringlike(self, option, what, default=None):
Greg Ward31413a72000-06-04 14:21:28 +0000208 val = getattr(self, option)
209 if val is None:
210 setattr(self, option, default)
211 return default
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000212 elif not isinstance(val, str):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000213 raise DistutilsOptionError("'%s' must be a %s (got `%s`)"
214 % (option, what, val))
Greg Ward31413a72000-06-04 14:21:28 +0000215 return val
216
Collin Winter5b7e9d72007-08-30 03:52:21 +0000217 def ensure_string(self, option, default=None):
Greg Ward31413a72000-06-04 14:21:28 +0000218 """Ensure that 'option' is a string; if not defined, set it to
219 'default'.
220 """
221 self._ensure_stringlike(option, "string", default)
222
Collin Winter5b7e9d72007-08-30 03:52:21 +0000223 def ensure_string_list(self, option):
R David Murray44b548d2016-09-08 13:59:53 -0400224 r"""Ensure that 'option' is a list of strings. If 'option' is
Greg Ward31413a72000-06-04 14:21:28 +0000225 currently a string, we split it either on /,\s*/ or /\s+/, so
226 "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
227 ["foo", "bar", "baz"].
228 """
229 val = getattr(self, option)
230 if val is None:
231 return
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000232 elif isinstance(val, str):
Greg Ward31413a72000-06-04 14:21:28 +0000233 setattr(self, option, re.split(r',\s*|\s+', val))
234 else:
Guido van Rossum13257902007-06-07 23:15:56 +0000235 if isinstance(val, list):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000236 ok = all(isinstance(v, str) for v in val)
Greg Ward31413a72000-06-04 14:21:28 +0000237 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000238 ok = False
Greg Ward31413a72000-06-04 14:21:28 +0000239 if not ok:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000240 raise DistutilsOptionError(
241 "'%s' must be a list of strings (got %r)"
242 % (option, val))
Fred Drakeb94b8492001-12-06 20:51:35 +0000243
Collin Winter5b7e9d72007-08-30 03:52:21 +0000244 def _ensure_tested_string(self, option, tester, what, error_fmt,
245 default=None):
Greg Ward31413a72000-06-04 14:21:28 +0000246 val = self._ensure_stringlike(option, what, default)
247 if val is not None and not tester(val):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000248 raise DistutilsOptionError(("error in '%s' option: " + error_fmt)
249 % (option, val))
Greg Ward31413a72000-06-04 14:21:28 +0000250
Collin Winter5b7e9d72007-08-30 03:52:21 +0000251 def ensure_filename(self, option):
Greg Ward31413a72000-06-04 14:21:28 +0000252 """Ensure that 'option' is the name of an existing file."""
253 self._ensure_tested_string(option, os.path.isfile,
254 "filename",
255 "'%s' does not exist or is not a file")
256
Collin Winter5b7e9d72007-08-30 03:52:21 +0000257 def ensure_dirname(self, option):
Greg Ward31413a72000-06-04 14:21:28 +0000258 self._ensure_tested_string(option, os.path.isdir,
259 "directory name",
260 "'%s' does not exist or is not a directory")
261
262
Greg Wardfe6462c2000-04-04 01:40:52 +0000263 # -- Convenience methods for commands ------------------------------
264
Collin Winter5b7e9d72007-08-30 03:52:21 +0000265 def get_command_name(self):
Greg Ward071ed762000-09-26 02:12:31 +0000266 if hasattr(self, 'command_name'):
Greg Wardfe6462c2000-04-04 01:40:52 +0000267 return self.command_name
268 else:
269 return self.__class__.__name__
270
Collin Winter5b7e9d72007-08-30 03:52:21 +0000271 def set_undefined_options(self, src_cmd, *option_pairs):
Greg Wardfe6462c2000-04-04 01:40:52 +0000272 """Set the values of any "undefined" options from corresponding
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000273 option values in some other command object. "Undefined" here means
274 "is None", which is the convention used to indicate that an option
275 has not been changed between 'initialize_options()' and
276 'finalize_options()'. Usually called from 'finalize_options()' for
277 options that depend on some other command rather than another
278 option of the same command. 'src_cmd' is the other command from
279 which option values will be taken (a command object will be created
280 for it if necessary); the remaining arguments are
281 '(src_option,dst_option)' tuples which mean "take the value of
282 'src_option' in the 'src_cmd' command object, and copy it to
283 'dst_option' in the current command object".
284 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000285 # Option_pairs: list of (src_option, dst_option) tuples
Greg Ward071ed762000-09-26 02:12:31 +0000286 src_cmd_obj = self.distribution.get_command_obj(src_cmd)
287 src_cmd_obj.ensure_finalized()
Greg Ward02a1a2b2000-04-15 22:15:07 +0000288 for (src_option, dst_option) in option_pairs:
Greg Ward071ed762000-09-26 02:12:31 +0000289 if getattr(self, dst_option) is None:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000290 setattr(self, dst_option, getattr(src_cmd_obj, src_option))
Greg Wardfe6462c2000-04-04 01:40:52 +0000291
Collin Winter5b7e9d72007-08-30 03:52:21 +0000292 def get_finalized_command(self, command, create=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000293 """Wrapper around Distribution's 'get_command_obj()' method: find
294 (create if necessary and 'create' is true) the command object for
295 'command', call its 'ensure_finalized()' method, and return the
296 finalized command object.
297 """
Greg Ward071ed762000-09-26 02:12:31 +0000298 cmd_obj = self.distribution.get_command_obj(command, create)
299 cmd_obj.ensure_finalized()
Greg Wardfe6462c2000-04-04 01:40:52 +0000300 return cmd_obj
301
Greg Ward308acf02000-06-01 01:08:52 +0000302 # XXX rename to 'get_reinitialized_command()'? (should do the
303 # same in dist.py, if so)
Collin Winter5b7e9d72007-08-30 03:52:21 +0000304 def reinitialize_command(self, command, reinit_subcommands=0):
305 return self.distribution.reinitialize_command(command,
306 reinit_subcommands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000307
Collin Winter5b7e9d72007-08-30 03:52:21 +0000308 def run_command(self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000309 """Run some other command: uses the 'run_command()' method of
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000310 Distribution, which creates and finalizes the command object if
311 necessary and then invokes its 'run()' method.
312 """
Greg Ward071ed762000-09-26 02:12:31 +0000313 self.distribution.run_command(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000314
Collin Winter5b7e9d72007-08-30 03:52:21 +0000315 def get_sub_commands(self):
Greg Wardb3e0ad92000-09-16 15:09:17 +0000316 """Determine the sub-commands that are relevant in the current
317 distribution (ie., that need to be run). This is based on the
318 'sub_commands' class attribute: each tuple in that list may include
319 a method that we call to determine if the subcommand needs to be
320 run for the current distribution. Return a list of command names.
321 """
322 commands = []
323 for (cmd_name, method) in self.sub_commands:
324 if method is None or method(self):
325 commands.append(cmd_name)
326 return commands
327
328
Greg Wardfe6462c2000-04-04 01:40:52 +0000329 # -- External world manipulation -----------------------------------
330
Collin Winter5b7e9d72007-08-30 03:52:21 +0000331 def warn(self, msg):
Vinay Sajipdd917f82016-08-31 08:22:29 +0100332 log.warn("warning: %s: %s\n", self.get_command_name(), msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000333
Collin Winter5b7e9d72007-08-30 03:52:21 +0000334 def execute(self, func, args, msg=None, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000335 util.execute(func, args, msg, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000336
Collin Winter5b7e9d72007-08-30 03:52:21 +0000337 def mkpath(self, name, mode=0o777):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000338 dir_util.mkpath(name, mode, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000339
Collin Winter5b7e9d72007-08-30 03:52:21 +0000340 def copy_file(self, infile, outfile, preserve_mode=1, preserve_times=1,
341 link=None, level=1):
Greg Warde9613ae2000-04-10 01:30:44 +0000342 """Copy a file respecting verbose, dry-run and force flags. (The
343 former two default to whatever is in the Distribution object, and
344 the latter defaults to false for commands that don't define it.)"""
Collin Winter5b7e9d72007-08-30 03:52:21 +0000345 return file_util.copy_file(infile, outfile, preserve_mode,
346 preserve_times, not self.force, link,
347 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000348
Tarek Ziadé3a794c42009-04-13 20:19:58 +0000349 def copy_tree(self, infile, outfile, preserve_mode=1, preserve_times=1,
Collin Winter5b7e9d72007-08-30 03:52:21 +0000350 preserve_symlinks=0, level=1):
Greg Wardfe6462c2000-04-04 01:40:52 +0000351 """Copy an entire directory tree respecting verbose, dry-run,
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000352 and force flags.
353 """
Collin Winter5b7e9d72007-08-30 03:52:21 +0000354 return dir_util.copy_tree(infile, outfile, preserve_mode,
355 preserve_times, preserve_symlinks,
356 not self.force, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000357
358 def move_file (self, src, dst, level=1):
Ezio Melotti13925002011-03-16 11:05:33 +0200359 """Move a file respecting dry-run flag."""
Collin Winter5b7e9d72007-08-30 03:52:21 +0000360 return file_util.move_file(src, dst, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000361
Collin Winter5b7e9d72007-08-30 03:52:21 +0000362 def spawn(self, cmd, search_path=1, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000363 """Spawn an external command respecting dry-run flag."""
Greg Wardfe6462c2000-04-04 01:40:52 +0000364 from distutils.spawn import spawn
Collin Winter5b7e9d72007-08-30 03:52:21 +0000365 spawn(cmd, search_path, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000366
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500367 def make_archive(self, base_name, format, root_dir=None, base_dir=None,
368 owner=None, group=None):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000369 return archive_util.make_archive(base_name, format, root_dir, base_dir,
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500370 dry_run=self.dry_run,
371 owner=owner, group=group)
Greg Wardfe6462c2000-04-04 01:40:52 +0000372
Collin Winter5b7e9d72007-08-30 03:52:21 +0000373 def make_file(self, infiles, outfile, func, args,
374 exec_msg=None, skip_msg=None, level=1):
Greg Wardfe6462c2000-04-04 01:40:52 +0000375 """Special case of 'execute()' for operations that process one or
Greg Ward68a07572000-04-10 00:18:16 +0000376 more input files and generate one output file. Works just like
377 'execute()', except the operation is skipped and a different
378 message printed if 'outfile' already exists and is newer than all
379 files listed in 'infiles'. If the command defined 'self.force',
380 and it is true, then the command is unconditionally run -- does no
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000381 timestamp checks.
382 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000383 if skip_msg is None:
384 skip_msg = "skipping %s (inputs unchanged)" % outfile
Fred Drakeb94b8492001-12-06 20:51:35 +0000385
Greg Wardfe6462c2000-04-04 01:40:52 +0000386 # Allow 'infiles' to be a single string
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000387 if isinstance(infiles, str):
Greg Wardfe6462c2000-04-04 01:40:52 +0000388 infiles = (infiles,)
Guido van Rossum13257902007-06-07 23:15:56 +0000389 elif not isinstance(infiles, (list, tuple)):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000390 raise TypeError(
391 "'infiles' must be a string, or a list or tuple of strings")
Greg Wardfe6462c2000-04-04 01:40:52 +0000392
Tarek Ziadé48494d02009-02-07 00:10:48 +0000393 if exec_msg is None:
394 exec_msg = "generating %s from %s" % (outfile, ', '.join(infiles))
395
Greg Wardfe6462c2000-04-04 01:40:52 +0000396 # If 'outfile' must be regenerated (either because it doesn't
397 # exist, is out-of-date, or the 'force' flag is true) then
398 # perform the action that presumably regenerates it
Tarek Ziadé48494d02009-02-07 00:10:48 +0000399 if self.force or dep_util.newer_group(infiles, outfile):
Greg Ward071ed762000-09-26 02:12:31 +0000400 self.execute(func, args, exec_msg, level)
Greg Wardfe6462c2000-04-04 01:40:52 +0000401 # Otherwise, print the "skip" message
402 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000403 log.debug(skip_msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000404
Greg Ward029e3022000-05-25 01:26:23 +0000405# XXX 'install_misc' class not currently used -- it was the base class for
406# both 'install_scripts' and 'install_data', but they outgrew it. It might
407# still be useful for 'install_headers', though, so I'm keeping it around
408# for the time being.
409
Collin Winter5b7e9d72007-08-30 03:52:21 +0000410class install_misc(Command):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000411 """Common base class for installing some files in a subdirectory.
412 Currently used by install_data and install_scripts.
413 """
Fred Drakeb94b8492001-12-06 20:51:35 +0000414
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000415 user_options = [('install-dir=', 'd', "directory to install the files to")]
416
417 def initialize_options (self):
418 self.install_dir = None
Gregory P. Smith21b9e912000-05-13 03:10:30 +0000419 self.outfiles = []
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000420
Tarek Ziadé3a794c42009-04-13 20:19:58 +0000421 def _install_dir_from(self, dirname):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000422 self.set_undefined_options('install', (dirname, 'install_dir'))
423
Tarek Ziadé3a794c42009-04-13 20:19:58 +0000424 def _copy_files(self, filelist):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000425 self.outfiles = []
426 if not filelist:
427 return
428 self.mkpath(self.install_dir)
429 for f in filelist:
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000430 self.copy_file(f, self.install_dir)
431 self.outfiles.append(os.path.join(self.install_dir, f))
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000432
Tarek Ziadé3a794c42009-04-13 20:19:58 +0000433 def get_outputs(self):
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000434 return self.outfiles