blob: b2c952c38c8ac453afed3116fde0ec63d9509d61 [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
Martin v. Löwis5a6601c2004-11-10 22:23:15 +00007# This module should be kept compatible with Python 2.1.
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00008
Greg Wardfe6462c2000-04-04 01:40:52 +00009__revision__ = "$Id$"
10
Neal Norwitz9d72bb42007-04-17 08:48:32 +000011import sys, os, re
Greg Wardfe6462c2000-04-04 01:40:52 +000012from distutils.errors import *
Greg Ward29124ff2000-08-13 00:36:47 +000013from distutils import util, dir_util, file_util, archive_util, dep_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000014from distutils import log
Greg Wardfe6462c2000-04-04 01:40:52 +000015
16class Command:
17 """Abstract base class for defining command classes, the "worker bees"
Greg Ward8ff5a3f2000-06-02 00:44:53 +000018 of the Distutils. A useful analogy for command classes is to think of
19 them as subroutines with local variables called "options". The options
20 are "declared" in 'initialize_options()' and "defined" (given their
21 final values, aka "finalized") in 'finalize_options()', both of which
22 must be defined by every command class. The distinction between the
23 two is necessary because option values might come from the outside
24 world (command line, config file, ...), and any options dependent on
25 other options must be computed *after* these outside influences have
26 been processed -- hence 'finalize_options()'. The "body" of the
27 subroutine, where it does all its work based on the values of its
28 options, is the 'run()' method, which must also be implemented by every
29 command class.
30 """
Greg Wardfe6462c2000-04-04 01:40:52 +000031
Greg Wardb3e0ad92000-09-16 15:09:17 +000032 # 'sub_commands' formalizes the notion of a "family" of commands,
33 # eg. "install" as the parent with sub-commands "install_lib",
34 # "install_headers", etc. The parent of a family of commands
35 # defines 'sub_commands' as a class attribute; it's a list of
36 # (command_name : string, predicate : unbound_method | string | None)
37 # tuples, where 'predicate' is a method of the parent command that
38 # determines whether the corresponding command is applicable in the
39 # current situation. (Eg. we "install_headers" is only applicable if
40 # we have any C header files to install.) If 'predicate' is None,
41 # that command is always applicable.
Fred Drakeb94b8492001-12-06 20:51:35 +000042 #
Greg Wardb3e0ad92000-09-16 15:09:17 +000043 # 'sub_commands' is usually defined at the *end* of a class, because
44 # predicates can be unbound methods, so they must already have been
45 # defined. The canonical example is the "install" command.
46 sub_commands = []
47
48
Greg Wardfe6462c2000-04-04 01:40:52 +000049 # -- Creation/initialization methods -------------------------------
50
51 def __init__ (self, dist):
52 """Create and initialize a new Command object. Most importantly,
Greg Ward8ff5a3f2000-06-02 00:44:53 +000053 invokes the 'initialize_options()' method, which is the real
54 initializer and depends on the actual command being
55 instantiated.
56 """
Greg Wardfe6462c2000-04-04 01:40:52 +000057 # late import because of mutual dependence between these classes
58 from distutils.dist import Distribution
59
Greg Ward071ed762000-09-26 02:12:31 +000060 if not isinstance(dist, Distribution):
Greg Wardfe6462c2000-04-04 01:40:52 +000061 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
Greg Ward071ed762000-09-26 02:12:31 +000066 self.initialize_options()
Greg Wardfe6462c2000-04-04 01:40:52 +000067
68 # Per-command versions of the global flags, so that the user can
69 # customize Distutils' behaviour command-by-command and let some
Fred Drake2b2fe942004-06-18 21:28:28 +000070 # commands fall back on the Distribution's behaviour. None means
Greg Wardfe6462c2000-04-04 01:40:52 +000071 # "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
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000073 # 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.
Greg Wardfe6462c2000-04-04 01:40:52 +000076 self._dry_run = None
Greg Wardfe6462c2000-04-04 01:40:52 +000077
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000078 # verbose is largely ignored, but needs to be set for
79 # backwards compatibility (I think)?
80 self.verbose = dist.verbose
Tim Peters182b5ac2004-07-18 06:16:08 +000081
Greg Wardd197a3a2000-04-10 13:11:51 +000082 # 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
Greg Wardfe6462c2000-04-04 01:40:52 +000088 # The 'help' flag is just used for command-line parsing, so
89 # none of that complicated bureaucracy is needed.
90 self.help = 0
91
Greg Ward4fb29e52000-05-27 17:27:23 +000092 # 'finalized' records whether or not 'finalize_options()' has been
Greg Wardfe6462c2000-04-04 01:40:52 +000093 # called. 'finalize_options()' itself should not pay attention to
Greg Ward4fb29e52000-05-27 17:27:23 +000094 # this flag: it is the business of 'ensure_finalized()', which
95 # always calls 'finalize_options()', to respect/update it.
96 self.finalized = 0
Greg Wardfe6462c2000-04-04 01:40:52 +000097
98 # __init__ ()
99
100
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000101 # XXX A more explicit way to customize dry_run would be better.
Tim Peters182b5ac2004-07-18 06:16:08 +0000102
Greg Wardfe6462c2000-04-04 01:40:52 +0000103 def __getattr__ (self, attr):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000104 if attr == 'dry_run':
Greg Ward071ed762000-09-26 02:12:31 +0000105 myval = getattr(self, "_" + attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000106 if myval is None:
Greg Ward071ed762000-09-26 02:12:31 +0000107 return getattr(self.distribution, attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000108 else:
109 return myval
110 else:
111 raise AttributeError, attr
112
113
Greg Ward4fb29e52000-05-27 17:27:23 +0000114 def ensure_finalized (self):
115 if not self.finalized:
Greg Ward071ed762000-09-26 02:12:31 +0000116 self.finalize_options()
Greg Ward4fb29e52000-05-27 17:27:23 +0000117 self.finalized = 1
Fred Drakeb94b8492001-12-06 20:51:35 +0000118
Greg Wardfe6462c2000-04-04 01:40:52 +0000119
120 # Subclasses must define:
121 # initialize_options()
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000122 # provide default values for all options; may be customized by
123 # setup script, by options from config file(s), or by command-line
124 # options
Greg Wardfe6462c2000-04-04 01:40:52 +0000125 # finalize_options()
126 # decide on the final values for all options; this is called
127 # after all possible intervention from the outside world
128 # (command-line, option file, etc.) has been processed
129 # run()
130 # run the command: do whatever it is we're here to do,
131 # controlled by the command's various option values
132
133 def initialize_options (self):
134 """Set default values for all the options that this command
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000135 supports. Note that these defaults may be overridden by other
136 commands, by the setup script, by config files, or by the
137 command-line. Thus, this is not the place to code dependencies
138 between options; generally, 'initialize_options()' implementations
139 are just a bunch of "self.foo = None" assignments.
Fred Drakeb94b8492001-12-06 20:51:35 +0000140
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000141 This method must be implemented by all command classes.
142 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000143 raise RuntimeError, \
144 "abstract method -- subclass %s must override" % self.__class__
Fred Drakeb94b8492001-12-06 20:51:35 +0000145
Greg Wardfe6462c2000-04-04 01:40:52 +0000146 def finalize_options (self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000147 """Set final values for all the options that this command supports.
148 This is always called as late as possible, ie. after any option
149 assignments from the command-line or from other commands have been
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000150 done. Thus, this is the place to code option dependencies: if
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000151 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
152 long as 'foo' still has the same value it was assigned in
153 'initialize_options()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000154
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000155 This method must be implemented by all command classes.
156 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000157 raise RuntimeError, \
158 "abstract method -- subclass %s must override" % self.__class__
159
Greg Wardadda1562000-05-28 23:54:00 +0000160
161 def dump_options (self, header=None, indent=""):
162 from distutils.fancy_getopt import longopt_xlate
163 if header is None:
164 header = "command options for '%s':" % self.get_command_name()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000165 print(indent + header)
Greg Wardadda1562000-05-28 23:54:00 +0000166 indent = indent + " "
167 for (option, _, _) in self.user_options:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000168 option = option.translate(longopt_xlate)
Greg Wardadda1562000-05-28 23:54:00 +0000169 if option[-1] == "=":
170 option = option[:-1]
171 value = getattr(self, option)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000172 print(indent + "%s = %s" % (option, value))
Greg Wardadda1562000-05-28 23:54:00 +0000173
174
Greg Wardfe6462c2000-04-04 01:40:52 +0000175 def run (self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000176 """A command's raison d'etre: carry out the action it exists to
177 perform, controlled by the options initialized in
178 'initialize_options()', customized by other commands, the setup
179 script, the command-line, and config files, and finalized in
180 'finalize_options()'. All terminal output and filesystem
181 interaction should be done by 'run()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000182
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000183 This method must be implemented by all command classes.
184 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000185
186 raise RuntimeError, \
187 "abstract method -- subclass %s must override" % self.__class__
188
189 def announce (self, msg, level=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000190 """If the current verbosity level is of greater than or equal to
191 'level' print 'msg' to stdout.
192 """
Guido van Rossumaf160652003-02-20 02:10:08 +0000193 log.log(level, msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000194
Greg Wardebec02a2000-06-08 00:02:36 +0000195 def debug_print (self, msg):
196 """Print 'msg' to stdout if the global DEBUG (taken from the
197 DISTUTILS_DEBUG environment variable) flag is true.
198 """
Jeremy Hyltonfcd73532002-09-11 16:31:53 +0000199 from distutils.debug import DEBUG
Greg Wardebec02a2000-06-08 00:02:36 +0000200 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000201 print(msg)
Neil Schemenauer69374e42001-08-29 23:57:22 +0000202 sys.stdout.flush()
Fred Drakeb94b8492001-12-06 20:51:35 +0000203
Greg Wardebec02a2000-06-08 00:02:36 +0000204
Greg Wardfe6462c2000-04-04 01:40:52 +0000205
Greg Ward31413a72000-06-04 14:21:28 +0000206 # -- Option validation methods -------------------------------------
207 # (these are very handy in writing the 'finalize_options()' method)
Fred Drakeb94b8492001-12-06 20:51:35 +0000208 #
Greg Ward31413a72000-06-04 14:21:28 +0000209 # NB. the general philosophy here is to ensure that a particular option
210 # value meets certain type and value constraints. If not, we try to
211 # force it into conformance (eg. if we expect a list but have a string,
212 # split the string on comma and/or whitespace). If we can't force the
213 # option into conformance, raise DistutilsOptionError. Thus, command
214 # classes need do nothing more than (eg.)
215 # self.ensure_string_list('foo')
216 # and they can be guaranteed that thereafter, self.foo will be
217 # a list of strings.
218
219 def _ensure_stringlike (self, option, what, default=None):
220 val = getattr(self, option)
221 if val is None:
222 setattr(self, option, default)
223 return default
Guido van Rossum572dbf82007-04-27 23:53:51 +0000224 elif not isinstance(val, basestring):
Greg Ward31413a72000-06-04 14:21:28 +0000225 raise DistutilsOptionError, \
226 "'%s' must be a %s (got `%s`)" % (option, what, val)
227 return val
228
229 def ensure_string (self, option, default=None):
230 """Ensure that 'option' is a string; if not defined, set it to
231 'default'.
232 """
233 self._ensure_stringlike(option, "string", default)
234
235 def ensure_string_list (self, option):
236 """Ensure that 'option' is a list of strings. If 'option' is
237 currently a string, we split it either on /,\s*/ or /\s+/, so
238 "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
239 ["foo", "bar", "baz"].
240 """
241 val = getattr(self, option)
242 if val is None:
243 return
Guido van Rossum572dbf82007-04-27 23:53:51 +0000244 elif isinstance(val, basestring):
Greg Ward31413a72000-06-04 14:21:28 +0000245 setattr(self, option, re.split(r',\s*|\s+', val))
246 else:
Guido van Rossum13257902007-06-07 23:15:56 +0000247 if isinstance(val, list):
Guido van Rossum572dbf82007-04-27 23:53:51 +0000248 ok = all(isinstance(v, basestring) for v in val)
Greg Ward31413a72000-06-04 14:21:28 +0000249 else:
250 ok = 0
251
252 if not ok:
253 raise DistutilsOptionError, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000254 "'%s' must be a list of strings (got %r)" % \
255 (option, val)
Fred Drakeb94b8492001-12-06 20:51:35 +0000256
Greg Ward31413a72000-06-04 14:21:28 +0000257 def _ensure_tested_string (self, option, tester,
258 what, error_fmt, default=None):
259 val = self._ensure_stringlike(option, what, default)
260 if val is not None and not tester(val):
261 raise DistutilsOptionError, \
262 ("error in '%s' option: " + error_fmt) % (option, val)
263
264 def ensure_filename (self, option):
265 """Ensure that 'option' is the name of an existing file."""
266 self._ensure_tested_string(option, os.path.isfile,
267 "filename",
268 "'%s' does not exist or is not a file")
269
270 def ensure_dirname (self, option):
271 self._ensure_tested_string(option, os.path.isdir,
272 "directory name",
273 "'%s' does not exist or is not a directory")
274
275
Greg Wardfe6462c2000-04-04 01:40:52 +0000276 # -- Convenience methods for commands ------------------------------
277
278 def get_command_name (self):
Greg Ward071ed762000-09-26 02:12:31 +0000279 if hasattr(self, 'command_name'):
Greg Wardfe6462c2000-04-04 01:40:52 +0000280 return self.command_name
281 else:
282 return self.__class__.__name__
283
284
285 def set_undefined_options (self, src_cmd, *option_pairs):
286 """Set the values of any "undefined" options from corresponding
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000287 option values in some other command object. "Undefined" here means
288 "is None", which is the convention used to indicate that an option
289 has not been changed between 'initialize_options()' and
290 'finalize_options()'. Usually called from 'finalize_options()' for
291 options that depend on some other command rather than another
292 option of the same command. 'src_cmd' is the other command from
293 which option values will be taken (a command object will be created
294 for it if necessary); the remaining arguments are
295 '(src_option,dst_option)' tuples which mean "take the value of
296 'src_option' in the 'src_cmd' command object, and copy it to
297 'dst_option' in the current command object".
298 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000299
300 # Option_pairs: list of (src_option, dst_option) tuples
301
Greg Ward071ed762000-09-26 02:12:31 +0000302 src_cmd_obj = self.distribution.get_command_obj(src_cmd)
303 src_cmd_obj.ensure_finalized()
Greg Ward02a1a2b2000-04-15 22:15:07 +0000304 for (src_option, dst_option) in option_pairs:
Greg Ward071ed762000-09-26 02:12:31 +0000305 if getattr(self, dst_option) is None:
306 setattr(self, dst_option,
307 getattr(src_cmd_obj, src_option))
Greg Wardfe6462c2000-04-04 01:40:52 +0000308
309
Greg Ward4fb29e52000-05-27 17:27:23 +0000310 def get_finalized_command (self, command, create=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000311 """Wrapper around Distribution's 'get_command_obj()' method: find
312 (create if necessary and 'create' is true) the command object for
313 'command', call its 'ensure_finalized()' method, and return the
314 finalized command object.
315 """
Greg Ward071ed762000-09-26 02:12:31 +0000316 cmd_obj = self.distribution.get_command_obj(command, create)
317 cmd_obj.ensure_finalized()
Greg Wardfe6462c2000-04-04 01:40:52 +0000318 return cmd_obj
319
Greg Ward308acf02000-06-01 01:08:52 +0000320 # XXX rename to 'get_reinitialized_command()'? (should do the
321 # same in dist.py, if so)
Greg Wardecce1452000-09-16 15:25:55 +0000322 def reinitialize_command (self, command, reinit_subcommands=0):
323 return self.distribution.reinitialize_command(
324 command, reinit_subcommands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000325
Greg Ward4fb29e52000-05-27 17:27:23 +0000326 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000327 """Run some other command: uses the 'run_command()' method of
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000328 Distribution, which creates and finalizes the command object if
329 necessary and then invokes its 'run()' method.
330 """
Greg Ward071ed762000-09-26 02:12:31 +0000331 self.distribution.run_command(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000332
333
Greg Wardb3e0ad92000-09-16 15:09:17 +0000334 def get_sub_commands (self):
335 """Determine the sub-commands that are relevant in the current
336 distribution (ie., that need to be run). This is based on the
337 'sub_commands' class attribute: each tuple in that list may include
338 a method that we call to determine if the subcommand needs to be
339 run for the current distribution. Return a list of command names.
340 """
341 commands = []
342 for (cmd_name, method) in self.sub_commands:
343 if method is None or method(self):
344 commands.append(cmd_name)
345 return commands
346
347
Greg Wardfe6462c2000-04-04 01:40:52 +0000348 # -- External world manipulation -----------------------------------
349
350 def warn (self, msg):
Greg Ward071ed762000-09-26 02:12:31 +0000351 sys.stderr.write("warning: %s: %s\n" %
352 (self.get_command_name(), msg))
Greg Wardfe6462c2000-04-04 01:40:52 +0000353
354
355 def execute (self, func, args, msg=None, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000356 util.execute(func, args, msg, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000357
358
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000359 def mkpath (self, name, mode=0o777):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000360 dir_util.mkpath(name, mode, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000361
362
363 def copy_file (self, infile, outfile,
364 preserve_mode=1, preserve_times=1, link=None, level=1):
Greg Warde9613ae2000-04-10 01:30:44 +0000365 """Copy a file respecting verbose, dry-run and force flags. (The
366 former two default to whatever is in the Distribution object, and
367 the latter defaults to false for commands that don't define it.)"""
Greg Wardfe6462c2000-04-04 01:40:52 +0000368
Greg Ward29124ff2000-08-13 00:36:47 +0000369 return file_util.copy_file(
370 infile, outfile,
371 preserve_mode, preserve_times,
372 not self.force,
373 link,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000374 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000375
376
377 def copy_tree (self, infile, outfile,
378 preserve_mode=1, preserve_times=1, preserve_symlinks=0,
379 level=1):
380 """Copy an entire directory tree respecting verbose, dry-run,
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000381 and force flags.
382 """
Greg Ward29124ff2000-08-13 00:36:47 +0000383 return dir_util.copy_tree(
Fred Drakeb94b8492001-12-06 20:51:35 +0000384 infile, outfile,
Greg Ward29124ff2000-08-13 00:36:47 +0000385 preserve_mode,preserve_times,preserve_symlinks,
386 not self.force,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000387 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000388
389 def move_file (self, src, dst, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000390 """Move a file respectin dry-run flag."""
391 return file_util.move_file(src, dst, dry_run = self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000392
393 def spawn (self, cmd, search_path=1, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000394 """Spawn an external command respecting dry-run flag."""
Greg Wardfe6462c2000-04-04 01:40:52 +0000395 from distutils.spawn import spawn
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000396 spawn(cmd, search_path, dry_run= self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000397
398 def make_archive (self, base_name, format,
399 root_dir=None, base_dir=None):
Greg Ward29124ff2000-08-13 00:36:47 +0000400 return archive_util.make_archive(
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000401 base_name, format, root_dir, base_dir, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000402
403
404 def make_file (self, infiles, outfile, func, args,
Greg Ward68a07572000-04-10 00:18:16 +0000405 exec_msg=None, skip_msg=None, level=1):
Greg Wardfe6462c2000-04-04 01:40:52 +0000406 """Special case of 'execute()' for operations that process one or
Greg Ward68a07572000-04-10 00:18:16 +0000407 more input files and generate one output file. Works just like
408 'execute()', except the operation is skipped and a different
409 message printed if 'outfile' already exists and is newer than all
410 files listed in 'infiles'. If the command defined 'self.force',
411 and it is true, then the command is unconditionally run -- does no
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000412 timestamp checks.
413 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000414 if exec_msg is None:
415 exec_msg = "generating %s from %s" % \
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000416 (outfile, ', '.join(infiles))
Greg Wardfe6462c2000-04-04 01:40:52 +0000417 if skip_msg is None:
418 skip_msg = "skipping %s (inputs unchanged)" % outfile
Fred Drakeb94b8492001-12-06 20:51:35 +0000419
Greg Wardfe6462c2000-04-04 01:40:52 +0000420
421 # Allow 'infiles' to be a single string
Guido van Rossum572dbf82007-04-27 23:53:51 +0000422 if isinstance(infiles, basestring):
Greg Wardfe6462c2000-04-04 01:40:52 +0000423 infiles = (infiles,)
Guido van Rossum13257902007-06-07 23:15:56 +0000424 elif not isinstance(infiles, (list, tuple)):
Greg Wardfe6462c2000-04-04 01:40:52 +0000425 raise TypeError, \
426 "'infiles' must be a string, or a list or tuple of strings"
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
Greg Ward29124ff2000-08-13 00:36:47 +0000431 if self.force or dep_util.newer_group (infiles, outfile):
Greg Ward071ed762000-09-26 02:12:31 +0000432 self.execute(func, args, exec_msg, level)
Greg Wardfe6462c2000-04-04 01:40:52 +0000433
434 # Otherwise, print the "skip" message
435 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000436 log.debug(skip_msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000437
438 # make_file ()
439
440# class Command
Greg Wardb3612332000-04-09 03:48:37 +0000441
442
Greg Ward029e3022000-05-25 01:26:23 +0000443# XXX 'install_misc' class not currently used -- it was the base class for
444# both 'install_scripts' and 'install_data', but they outgrew it. It might
445# still be useful for 'install_headers', though, so I'm keeping it around
446# for the time being.
447
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000448class install_misc (Command):
449 """Common base class for installing some files in a subdirectory.
450 Currently used by install_data and install_scripts.
451 """
Fred Drakeb94b8492001-12-06 20:51:35 +0000452
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000453 user_options = [('install-dir=', 'd', "directory to install the files to")]
454
455 def initialize_options (self):
456 self.install_dir = None
Gregory P. Smith21b9e912000-05-13 03:10:30 +0000457 self.outfiles = []
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000458
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000459 def _install_dir_from (self, dirname):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000460 self.set_undefined_options('install', (dirname, 'install_dir'))
461
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000462 def _copy_files (self, filelist):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000463 self.outfiles = []
464 if not filelist:
465 return
466 self.mkpath(self.install_dir)
467 for f in filelist:
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000468 self.copy_file(f, self.install_dir)
469 self.outfiles.append(os.path.join(self.install_dir, f))
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000470
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000471 def get_outputs (self):
472 return self.outfiles
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000473
474
Greg Wardb3612332000-04-09 03:48:37 +0000475if __name__ == "__main__":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000476 print("ok")