blob: 9ad5657e40a59c081f70052f3fe43814f45cdd98 [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
Greg Wardfe6462c2000-04-04 01:40:52 +00007__revision__ = "$Id$"
8
Tarek Ziadé8be87652009-02-07 00:05:39 +00009import sys, os, re
Tarek Ziadé98da8e12009-02-06 08:55:23 +000010from distutils.errors import DistutilsOptionError
Greg Ward29124ff2000-08-13 00:36:47 +000011from distutils import util, dir_util, file_util, archive_util, dep_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000012from distutils import log
Greg Wardfe6462c2000-04-04 01:40:52 +000013
14class Command:
15 """Abstract base class for defining command classes, the "worker bees"
Greg Ward8ff5a3f2000-06-02 00:44:53 +000016 of the Distutils. A useful analogy for command classes is to think of
17 them as subroutines with local variables called "options". The options
18 are "declared" in 'initialize_options()' and "defined" (given their
19 final values, aka "finalized") in 'finalize_options()', both of which
20 must be defined by every command class. The distinction between the
21 two is necessary because option values might come from the outside
22 world (command line, config file, ...), and any options dependent on
23 other options must be computed *after* these outside influences have
24 been processed -- hence 'finalize_options()'. The "body" of the
25 subroutine, where it does all its work based on the values of its
26 options, is the 'run()' method, which must also be implemented by every
27 command class.
28 """
Greg Wardfe6462c2000-04-04 01:40:52 +000029
Greg Wardb3e0ad92000-09-16 15:09:17 +000030 # 'sub_commands' formalizes the notion of a "family" of commands,
31 # eg. "install" as the parent with sub-commands "install_lib",
32 # "install_headers", etc. The parent of a family of commands
33 # defines 'sub_commands' as a class attribute; it's a list of
34 # (command_name : string, predicate : unbound_method | string | None)
35 # tuples, where 'predicate' is a method of the parent command that
36 # determines whether the corresponding command is applicable in the
37 # current situation. (Eg. we "install_headers" is only applicable if
38 # we have any C header files to install.) If 'predicate' is None,
39 # that command is always applicable.
Fred Drakeb94b8492001-12-06 20:51:35 +000040 #
Greg Wardb3e0ad92000-09-16 15:09:17 +000041 # 'sub_commands' is usually defined at the *end* of a class, because
42 # predicates can be unbound methods, so they must already have been
43 # defined. The canonical example is the "install" command.
44 sub_commands = []
45
46
Greg Wardfe6462c2000-04-04 01:40:52 +000047 # -- Creation/initialization methods -------------------------------
48
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +000049 def __init__(self, dist):
Greg Wardfe6462c2000-04-04 01:40:52 +000050 """Create and initialize a new Command object. Most importantly,
Greg Ward8ff5a3f2000-06-02 00:44:53 +000051 invokes the 'initialize_options()' method, which is the real
52 initializer and depends on the actual command being
53 instantiated.
54 """
Greg Wardfe6462c2000-04-04 01:40:52 +000055 # late import because of mutual dependence between these classes
56 from distutils.dist import Distribution
57
Greg Ward071ed762000-09-26 02:12:31 +000058 if not isinstance(dist, Distribution):
Greg Wardfe6462c2000-04-04 01:40:52 +000059 raise TypeError, "dist must be a Distribution instance"
60 if self.__class__ is Command:
61 raise RuntimeError, "Command is an abstract class"
62
63 self.distribution = dist
Greg Ward071ed762000-09-26 02:12:31 +000064 self.initialize_options()
Greg Wardfe6462c2000-04-04 01:40:52 +000065
66 # Per-command versions of the global flags, so that the user can
67 # customize Distutils' behaviour command-by-command and let some
Fred Drake2b2fe942004-06-18 21:28:28 +000068 # commands fall back on the Distribution's behaviour. None means
Greg Wardfe6462c2000-04-04 01:40:52 +000069 # "not defined, check self.distribution's copy", while 0 or 1 mean
70 # false and true (duh). Note that this means figuring out the real
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000071 # value of each flag is a touch complicated -- hence "self._dry_run"
72 # will be handled by __getattr__, below.
73 # XXX This needs to be fixed.
Greg Wardfe6462c2000-04-04 01:40:52 +000074 self._dry_run = None
Greg Wardfe6462c2000-04-04 01:40:52 +000075
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000076 # verbose is largely ignored, but needs to be set for
77 # backwards compatibility (I think)?
78 self.verbose = dist.verbose
Tim Peters182b5ac2004-07-18 06:16:08 +000079
Greg Wardd197a3a2000-04-10 13:11:51 +000080 # Some commands define a 'self.force' option to ignore file
81 # timestamps, but methods defined *here* assume that
82 # 'self.force' exists for all commands. So define it here
83 # just to be safe.
84 self.force = None
85
Greg Wardfe6462c2000-04-04 01:40:52 +000086 # The 'help' flag is just used for command-line parsing, so
87 # none of that complicated bureaucracy is needed.
88 self.help = 0
89
Greg Ward4fb29e52000-05-27 17:27:23 +000090 # 'finalized' records whether or not 'finalize_options()' has been
Greg Wardfe6462c2000-04-04 01:40:52 +000091 # called. 'finalize_options()' itself should not pay attention to
Greg Ward4fb29e52000-05-27 17:27:23 +000092 # this flag: it is the business of 'ensure_finalized()', which
93 # always calls 'finalize_options()', to respect/update it.
94 self.finalized = 0
Greg Wardfe6462c2000-04-04 01:40:52 +000095
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000096 # XXX A more explicit way to customize dry_run would be better.
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +000097 def __getattr__(self, attr):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000098 if attr == 'dry_run':
Greg Ward071ed762000-09-26 02:12:31 +000099 myval = getattr(self, "_" + attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000100 if myval is None:
Greg Ward071ed762000-09-26 02:12:31 +0000101 return getattr(self.distribution, attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000102 else:
103 return myval
104 else:
105 raise AttributeError, attr
106
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000107 def ensure_finalized(self):
Greg Ward4fb29e52000-05-27 17:27:23 +0000108 if not self.finalized:
Greg Ward071ed762000-09-26 02:12:31 +0000109 self.finalize_options()
Greg Ward4fb29e52000-05-27 17:27:23 +0000110 self.finalized = 1
Fred Drakeb94b8492001-12-06 20:51:35 +0000111
Greg Wardfe6462c2000-04-04 01:40:52 +0000112 # Subclasses must define:
113 # initialize_options()
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000114 # provide default values for all options; may be customized by
115 # setup script, by options from config file(s), or by command-line
116 # options
Greg Wardfe6462c2000-04-04 01:40:52 +0000117 # finalize_options()
118 # decide on the final values for all options; this is called
119 # after all possible intervention from the outside world
120 # (command-line, option file, etc.) has been processed
121 # run()
122 # run the command: do whatever it is we're here to do,
123 # controlled by the command's various option values
124
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000125 def initialize_options(self):
Greg Wardfe6462c2000-04-04 01:40:52 +0000126 """Set default values for all the options that this command
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000127 supports. Note that these defaults may be overridden by other
128 commands, by the setup script, by config files, or by the
129 command-line. Thus, this is not the place to code dependencies
130 between options; generally, 'initialize_options()' implementations
131 are just a bunch of "self.foo = None" assignments.
Fred Drakeb94b8492001-12-06 20:51:35 +0000132
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000133 This method must be implemented by all command classes.
134 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000135 raise RuntimeError, \
136 "abstract method -- subclass %s must override" % self.__class__
Fred Drakeb94b8492001-12-06 20:51:35 +0000137
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000138 def finalize_options(self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000139 """Set final values for all the options that this command supports.
140 This is always called as late as possible, ie. after any option
141 assignments from the command-line or from other commands have been
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000142 done. Thus, this is the place to code option dependencies: if
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000143 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
144 long as 'foo' still has the same value it was assigned in
145 'initialize_options()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000146
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000147 This method must be implemented by all command classes.
148 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000149 raise RuntimeError, \
150 "abstract method -- subclass %s must override" % self.__class__
151
Greg Wardadda1562000-05-28 23:54:00 +0000152
Tarek Ziadé8be87652009-02-07 00:05:39 +0000153 def dump_options(self, header=None, indent=""):
Greg Wardadda1562000-05-28 23:54:00 +0000154 from distutils.fancy_getopt import longopt_xlate
155 if header is None:
156 header = "command options for '%s':" % self.get_command_name()
Tarek Ziadé8be87652009-02-07 00:05:39 +0000157 self.announce(indent + header, level=log.INFO)
Greg Wardadda1562000-05-28 23:54:00 +0000158 indent = indent + " "
159 for (option, _, _) in self.user_options:
Tarek Ziadé8be87652009-02-07 00:05:39 +0000160 option = option.translate(longopt_xlate)
Greg Wardadda1562000-05-28 23:54:00 +0000161 if option[-1] == "=":
162 option = option[:-1]
163 value = getattr(self, option)
Tarek Ziadé8be87652009-02-07 00:05:39 +0000164 self.announce(indent + "%s = %s" % (option, value),
165 level=log.INFO)
Greg Wardadda1562000-05-28 23:54:00 +0000166
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000167 def run(self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000168 """A command's raison d'etre: carry out the action it exists to
169 perform, controlled by the options initialized in
170 'initialize_options()', customized by other commands, the setup
171 script, the command-line, and config files, and finalized in
172 'finalize_options()'. All terminal output and filesystem
173 interaction should be done by 'run()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000174
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000175 This method must be implemented by all command classes.
176 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000177 raise RuntimeError, \
178 "abstract method -- subclass %s must override" % self.__class__
179
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000180 def announce(self, msg, level=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000181 """If the current verbosity level is of greater than or equal to
182 'level' print 'msg' to stdout.
183 """
Guido van Rossumaf160652003-02-20 02:10:08 +0000184 log.log(level, msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000185
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000186 def debug_print(self, msg):
Greg Wardebec02a2000-06-08 00:02:36 +0000187 """Print 'msg' to stdout if the global DEBUG (taken from the
188 DISTUTILS_DEBUG environment variable) flag is true.
189 """
Jeremy Hyltonfcd73532002-09-11 16:31:53 +0000190 from distutils.debug import DEBUG
Greg Wardebec02a2000-06-08 00:02:36 +0000191 if DEBUG:
192 print msg
Neil Schemenauer69374e42001-08-29 23:57:22 +0000193 sys.stdout.flush()
Fred Drakeb94b8492001-12-06 20:51:35 +0000194
Greg Wardebec02a2000-06-08 00:02:36 +0000195
Greg Ward31413a72000-06-04 14:21:28 +0000196 # -- Option validation methods -------------------------------------
197 # (these are very handy in writing the 'finalize_options()' method)
Fred Drakeb94b8492001-12-06 20:51:35 +0000198 #
Greg Ward31413a72000-06-04 14:21:28 +0000199 # NB. the general philosophy here is to ensure that a particular option
200 # value meets certain type and value constraints. If not, we try to
201 # force it into conformance (eg. if we expect a list but have a string,
202 # split the string on comma and/or whitespace). If we can't force the
203 # option into conformance, raise DistutilsOptionError. Thus, command
204 # classes need do nothing more than (eg.)
205 # self.ensure_string_list('foo')
206 # and they can be guaranteed that thereafter, self.foo will be
207 # a list of strings.
208
Tarek Ziadé2fdd0d52009-04-13 20:03:44 +0000209 def _ensure_stringlike(self, option, what, default=None):
Greg Ward31413a72000-06-04 14:21:28 +0000210 val = getattr(self, option)
211 if val is None:
212 setattr(self, option, default)
213 return default
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000214 elif not isinstance(val, str):
Greg Ward31413a72000-06-04 14:21:28 +0000215 raise DistutilsOptionError, \
216 "'%s' must be a %s (got `%s`)" % (option, what, val)
217 return val
218
Tarek Ziadé2fdd0d52009-04-13 20:03:44 +0000219 def ensure_string(self, option, default=None):
Greg Ward31413a72000-06-04 14:21:28 +0000220 """Ensure that 'option' is a string; if not defined, set it to
221 'default'.
222 """
223 self._ensure_stringlike(option, "string", default)
224
Tarek Ziadé2fdd0d52009-04-13 20:03:44 +0000225 def ensure_string_list(self, option):
Greg Ward31413a72000-06-04 14:21:28 +0000226 """Ensure that 'option' is a list of strings. If 'option' is
227 currently a string, we split it either on /,\s*/ or /\s+/, so
228 "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
229 ["foo", "bar", "baz"].
230 """
231 val = getattr(self, option)
232 if val is None:
233 return
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000234 elif isinstance(val, str):
Greg Ward31413a72000-06-04 14:21:28 +0000235 setattr(self, option, re.split(r',\s*|\s+', val))
236 else:
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000237 if isinstance(val, list):
238 # checks if all elements are str
239 ok = 1
240 for element in val:
241 if not isinstance(element, str):
242 ok = 0
243 break
Greg Ward31413a72000-06-04 14:21:28 +0000244 else:
245 ok = 0
246
247 if not ok:
248 raise DistutilsOptionError, \
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000249 "'%s' must be a list of strings (got %r)" % \
250 (option, val)
251
Fred Drakeb94b8492001-12-06 20:51:35 +0000252
Tarek Ziadé2fdd0d52009-04-13 20:03:44 +0000253 def _ensure_tested_string(self, option, tester,
254 what, error_fmt, default=None):
Greg Ward31413a72000-06-04 14:21:28 +0000255 val = self._ensure_stringlike(option, what, default)
256 if val is not None and not tester(val):
257 raise DistutilsOptionError, \
258 ("error in '%s' option: " + error_fmt) % (option, val)
259
Tarek Ziadé2fdd0d52009-04-13 20:03:44 +0000260 def ensure_filename(self, option):
Greg Ward31413a72000-06-04 14:21:28 +0000261 """Ensure that 'option' is the name of an existing file."""
262 self._ensure_tested_string(option, os.path.isfile,
263 "filename",
264 "'%s' does not exist or is not a file")
265
Tarek Ziadé2fdd0d52009-04-13 20:03:44 +0000266 def ensure_dirname(self, option):
Greg Ward31413a72000-06-04 14:21:28 +0000267 self._ensure_tested_string(option, os.path.isdir,
268 "directory name",
269 "'%s' does not exist or is not a directory")
270
271
Greg Wardfe6462c2000-04-04 01:40:52 +0000272 # -- Convenience methods for commands ------------------------------
273
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000274 def get_command_name(self):
Greg Ward071ed762000-09-26 02:12:31 +0000275 if hasattr(self, 'command_name'):
Greg Wardfe6462c2000-04-04 01:40:52 +0000276 return self.command_name
277 else:
278 return self.__class__.__name__
279
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000280 def set_undefined_options(self, src_cmd, *option_pairs):
Greg Wardfe6462c2000-04-04 01:40:52 +0000281 """Set the values of any "undefined" options from corresponding
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000282 option values in some other command object. "Undefined" here means
283 "is None", which is the convention used to indicate that an option
284 has not been changed between 'initialize_options()' and
285 'finalize_options()'. Usually called from 'finalize_options()' for
286 options that depend on some other command rather than another
287 option of the same command. 'src_cmd' is the other command from
288 which option values will be taken (a command object will be created
289 for it if necessary); the remaining arguments are
290 '(src_option,dst_option)' tuples which mean "take the value of
291 'src_option' in the 'src_cmd' command object, and copy it to
292 'dst_option' in the current command object".
293 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000294
295 # Option_pairs: list of (src_option, dst_option) tuples
296
Greg Ward071ed762000-09-26 02:12:31 +0000297 src_cmd_obj = self.distribution.get_command_obj(src_cmd)
298 src_cmd_obj.ensure_finalized()
Greg Ward02a1a2b2000-04-15 22:15:07 +0000299 for (src_option, dst_option) in option_pairs:
Greg Ward071ed762000-09-26 02:12:31 +0000300 if getattr(self, dst_option) is None:
301 setattr(self, dst_option,
302 getattr(src_cmd_obj, src_option))
Greg Wardfe6462c2000-04-04 01:40:52 +0000303
304
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000305 def get_finalized_command(self, command, create=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000306 """Wrapper around Distribution's 'get_command_obj()' method: find
307 (create if necessary and 'create' is true) the command object for
308 'command', call its 'ensure_finalized()' method, and return the
309 finalized command object.
310 """
Greg Ward071ed762000-09-26 02:12:31 +0000311 cmd_obj = self.distribution.get_command_obj(command, create)
312 cmd_obj.ensure_finalized()
Greg Wardfe6462c2000-04-04 01:40:52 +0000313 return cmd_obj
314
Greg Ward308acf02000-06-01 01:08:52 +0000315 # XXX rename to 'get_reinitialized_command()'? (should do the
316 # same in dist.py, if so)
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000317 def reinitialize_command(self, command, reinit_subcommands=0):
Greg Wardecce1452000-09-16 15:25:55 +0000318 return self.distribution.reinitialize_command(
319 command, reinit_subcommands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000320
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000321 def run_command(self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000322 """Run some other command: uses the 'run_command()' method of
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000323 Distribution, which creates and finalizes the command object if
324 necessary and then invokes its 'run()' method.
325 """
Greg Ward071ed762000-09-26 02:12:31 +0000326 self.distribution.run_command(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000327
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000328 def get_sub_commands(self):
Greg Wardb3e0ad92000-09-16 15:09:17 +0000329 """Determine the sub-commands that are relevant in the current
330 distribution (ie., that need to be run). This is based on the
331 'sub_commands' class attribute: each tuple in that list may include
332 a method that we call to determine if the subcommand needs to be
333 run for the current distribution. Return a list of command names.
334 """
335 commands = []
336 for (cmd_name, method) in self.sub_commands:
337 if method is None or method(self):
338 commands.append(cmd_name)
339 return commands
340
341
Greg Wardfe6462c2000-04-04 01:40:52 +0000342 # -- External world manipulation -----------------------------------
343
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000344 def warn(self, msg):
Tarek Ziadéc7cd1382009-03-31 20:48:31 +0000345 log.warn("warning: %s: %s\n" %
346 (self.get_command_name(), msg))
Greg Wardfe6462c2000-04-04 01:40:52 +0000347
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000348 def execute(self, func, args, msg=None, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000349 util.execute(func, args, msg, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000350
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000351 def mkpath(self, name, mode=0777):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000352 dir_util.mkpath(name, mode, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000353
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000354 def copy_file(self, infile, outfile,
Greg Wardfe6462c2000-04-04 01:40:52 +0000355 preserve_mode=1, preserve_times=1, link=None, level=1):
Greg Warde9613ae2000-04-10 01:30:44 +0000356 """Copy a file respecting verbose, dry-run and force flags. (The
357 former two default to whatever is in the Distribution object, and
358 the latter defaults to false for commands that don't define it.)"""
Greg Wardfe6462c2000-04-04 01:40:52 +0000359
Greg Ward29124ff2000-08-13 00:36:47 +0000360 return file_util.copy_file(
361 infile, outfile,
362 preserve_mode, preserve_times,
363 not self.force,
364 link,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000365 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000366
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000367 def copy_tree(self, infile, outfile,
Greg Wardfe6462c2000-04-04 01:40:52 +0000368 preserve_mode=1, preserve_times=1, preserve_symlinks=0,
369 level=1):
370 """Copy an entire directory tree respecting verbose, dry-run,
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000371 and force flags.
372 """
Greg Ward29124ff2000-08-13 00:36:47 +0000373 return dir_util.copy_tree(
Fred Drakeb94b8492001-12-06 20:51:35 +0000374 infile, outfile,
Greg Ward29124ff2000-08-13 00:36:47 +0000375 preserve_mode,preserve_times,preserve_symlinks,
376 not self.force,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000377 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000378
379 def move_file (self, src, dst, level=1):
Ezio Melottic2077b02011-03-16 12:34:31 +0200380 """Move a file respecting dry-run flag."""
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000381 return file_util.move_file(src, dst, dry_run = self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000382
383 def spawn (self, cmd, search_path=1, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000384 """Spawn an external command respecting dry-run flag."""
Greg Wardfe6462c2000-04-04 01:40:52 +0000385 from distutils.spawn import spawn
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000386 spawn(cmd, search_path, dry_run= self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000387
Tarek Ziadé1b486712009-10-02 23:49:48 +0000388 def make_archive(self, base_name, format, root_dir=None, base_dir=None,
389 owner=None, group=None):
390 return archive_util.make_archive(base_name, format, root_dir,
391 base_dir, dry_run=self.dry_run,
392 owner=owner, group=group)
Greg Wardfe6462c2000-04-04 01:40:52 +0000393
Tarek Ziadé8be87652009-02-07 00:05:39 +0000394 def make_file(self, infiles, outfile, func, args,
395 exec_msg=None, skip_msg=None, level=1):
Greg Wardfe6462c2000-04-04 01:40:52 +0000396 """Special case of 'execute()' for operations that process one or
Greg Ward68a07572000-04-10 00:18:16 +0000397 more input files and generate one output file. Works just like
398 'execute()', except the operation is skipped and a different
399 message printed if 'outfile' already exists and is newer than all
400 files listed in 'infiles'. If the command defined 'self.force',
401 and it is true, then the command is unconditionally run -- does no
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000402 timestamp checks.
403 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000404 if skip_msg is None:
405 skip_msg = "skipping %s (inputs unchanged)" % outfile
Fred Drakeb94b8492001-12-06 20:51:35 +0000406
Greg Wardfe6462c2000-04-04 01:40:52 +0000407 # Allow 'infiles' to be a single string
Tarek Ziadé8be87652009-02-07 00:05:39 +0000408 if isinstance(infiles, str):
Greg Wardfe6462c2000-04-04 01:40:52 +0000409 infiles = (infiles,)
Tarek Ziadé8be87652009-02-07 00:05:39 +0000410 elif not isinstance(infiles, (list, tuple)):
Greg Wardfe6462c2000-04-04 01:40:52 +0000411 raise TypeError, \
412 "'infiles' must be a string, or a list or tuple of strings"
413
Tarek Ziadé8be87652009-02-07 00:05:39 +0000414 if exec_msg is None:
415 exec_msg = "generating %s from %s" % \
416 (outfile, ', '.join(infiles))
417
Greg Wardfe6462c2000-04-04 01:40:52 +0000418 # If 'outfile' must be regenerated (either because it doesn't
419 # exist, is out-of-date, or the 'force' flag is true) then
420 # perform the action that presumably regenerates it
Tarek Ziadé8be87652009-02-07 00:05:39 +0000421 if self.force or dep_util.newer_group(infiles, outfile):
Greg Ward071ed762000-09-26 02:12:31 +0000422 self.execute(func, args, exec_msg, level)
Greg Wardfe6462c2000-04-04 01:40:52 +0000423
424 # Otherwise, print the "skip" message
425 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000426 log.debug(skip_msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000427
Greg Ward029e3022000-05-25 01:26:23 +0000428# XXX 'install_misc' class not currently used -- it was the base class for
429# both 'install_scripts' and 'install_data', but they outgrew it. It might
430# still be useful for 'install_headers', though, so I'm keeping it around
431# for the time being.
432
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000433class install_misc(Command):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000434 """Common base class for installing some files in a subdirectory.
435 Currently used by install_data and install_scripts.
436 """
Fred Drakeb94b8492001-12-06 20:51:35 +0000437
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000438 user_options = [('install-dir=', 'd', "directory to install the files to")]
439
440 def initialize_options (self):
441 self.install_dir = None
Gregory P. Smith21b9e912000-05-13 03:10:30 +0000442 self.outfiles = []
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000443
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000444 def _install_dir_from(self, dirname):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000445 self.set_undefined_options('install', (dirname, 'install_dir'))
446
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000447 def _copy_files(self, filelist):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000448 self.outfiles = []
449 if not filelist:
450 return
451 self.mkpath(self.install_dir)
452 for f in filelist:
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000453 self.copy_file(f, self.install_dir)
454 self.outfiles.append(os.path.join(self.install_dir, f))
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000455
Tarek Ziadéeb6e0f52009-04-13 20:14:54 +0000456 def get_outputs(self):
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000457 return self.outfiles