blob: dfcbe235373e11fa563b56b8c4dca859556a0121 [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
49 def __init__ (self, dist):
50 """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
96 # __init__ ()
97
98
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000099 # XXX A more explicit way to customize dry_run would be better.
Tim Peters182b5ac2004-07-18 06:16:08 +0000100
Greg Wardfe6462c2000-04-04 01:40:52 +0000101 def __getattr__ (self, attr):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000102 if attr == 'dry_run':
Greg Ward071ed762000-09-26 02:12:31 +0000103 myval = getattr(self, "_" + attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000104 if myval is None:
Greg Ward071ed762000-09-26 02:12:31 +0000105 return getattr(self.distribution, attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000106 else:
107 return myval
108 else:
109 raise AttributeError, attr
110
111
Greg Ward4fb29e52000-05-27 17:27:23 +0000112 def ensure_finalized (self):
113 if not self.finalized:
Greg Ward071ed762000-09-26 02:12:31 +0000114 self.finalize_options()
Greg Ward4fb29e52000-05-27 17:27:23 +0000115 self.finalized = 1
Fred Drakeb94b8492001-12-06 20:51:35 +0000116
Greg Wardfe6462c2000-04-04 01:40:52 +0000117
118 # Subclasses must define:
119 # initialize_options()
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000120 # provide default values for all options; may be customized by
121 # setup script, by options from config file(s), or by command-line
122 # options
Greg Wardfe6462c2000-04-04 01:40:52 +0000123 # finalize_options()
124 # decide on the final values for all options; this is called
125 # after all possible intervention from the outside world
126 # (command-line, option file, etc.) has been processed
127 # run()
128 # run the command: do whatever it is we're here to do,
129 # controlled by the command's various option values
130
131 def initialize_options (self):
132 """Set default values for all the options that this command
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000133 supports. Note that these defaults may be overridden by other
134 commands, by the setup script, by config files, or by the
135 command-line. Thus, this is not the place to code dependencies
136 between options; generally, 'initialize_options()' implementations
137 are just a bunch of "self.foo = None" assignments.
Fred Drakeb94b8492001-12-06 20:51:35 +0000138
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000139 This method must be implemented by all command classes.
140 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000141 raise RuntimeError, \
142 "abstract method -- subclass %s must override" % self.__class__
Fred Drakeb94b8492001-12-06 20:51:35 +0000143
Greg Wardfe6462c2000-04-04 01:40:52 +0000144 def finalize_options (self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000145 """Set final values for all the options that this command supports.
146 This is always called as late as possible, ie. after any option
147 assignments from the command-line or from other commands have been
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000148 done. Thus, this is the place to code option dependencies: if
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000149 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
150 long as 'foo' still has the same value it was assigned in
151 'initialize_options()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000152
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000153 This method must be implemented by all command classes.
154 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000155 raise RuntimeError, \
156 "abstract method -- subclass %s must override" % self.__class__
157
Greg Wardadda1562000-05-28 23:54:00 +0000158
Tarek Ziadé8be87652009-02-07 00:05:39 +0000159 def dump_options(self, header=None, indent=""):
Greg Wardadda1562000-05-28 23:54:00 +0000160 from distutils.fancy_getopt import longopt_xlate
161 if header is None:
162 header = "command options for '%s':" % self.get_command_name()
Tarek Ziadé8be87652009-02-07 00:05:39 +0000163 self.announce(indent + header, level=log.INFO)
Greg Wardadda1562000-05-28 23:54:00 +0000164 indent = indent + " "
165 for (option, _, _) in self.user_options:
Tarek Ziadé8be87652009-02-07 00:05:39 +0000166 option = option.translate(longopt_xlate)
Greg Wardadda1562000-05-28 23:54:00 +0000167 if option[-1] == "=":
168 option = option[:-1]
169 value = getattr(self, option)
Tarek Ziadé8be87652009-02-07 00:05:39 +0000170 self.announce(indent + "%s = %s" % (option, value),
171 level=log.INFO)
Greg Wardadda1562000-05-28 23:54:00 +0000172
Greg Wardfe6462c2000-04-04 01:40:52 +0000173 def run (self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000174 """A command's raison d'etre: carry out the action it exists to
175 perform, controlled by the options initialized in
176 'initialize_options()', customized by other commands, the setup
177 script, the command-line, and config files, and finalized in
178 'finalize_options()'. All terminal output and filesystem
179 interaction should be done by 'run()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000180
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000181 This method must be implemented by all command classes.
182 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000183
184 raise RuntimeError, \
185 "abstract method -- subclass %s must override" % self.__class__
186
187 def announce (self, msg, level=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000188 """If the current verbosity level is of greater than or equal to
189 'level' print 'msg' to stdout.
190 """
Guido van Rossumaf160652003-02-20 02:10:08 +0000191 log.log(level, msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000192
Greg Wardebec02a2000-06-08 00:02:36 +0000193 def debug_print (self, msg):
194 """Print 'msg' to stdout if the global DEBUG (taken from the
195 DISTUTILS_DEBUG environment variable) flag is true.
196 """
Jeremy Hyltonfcd73532002-09-11 16:31:53 +0000197 from distutils.debug import DEBUG
Greg Wardebec02a2000-06-08 00:02:36 +0000198 if DEBUG:
199 print msg
Neil Schemenauer69374e42001-08-29 23:57:22 +0000200 sys.stdout.flush()
Fred Drakeb94b8492001-12-06 20:51:35 +0000201
Greg Wardebec02a2000-06-08 00:02:36 +0000202
Greg Wardfe6462c2000-04-04 01:40:52 +0000203
Greg Ward31413a72000-06-04 14:21:28 +0000204 # -- Option validation methods -------------------------------------
205 # (these are very handy in writing the 'finalize_options()' method)
Fred Drakeb94b8492001-12-06 20:51:35 +0000206 #
Greg Ward31413a72000-06-04 14:21:28 +0000207 # NB. the general philosophy here is to ensure that a particular option
208 # value meets certain type and value constraints. If not, we try to
209 # force it into conformance (eg. if we expect a list but have a string,
210 # split the string on comma and/or whitespace). If we can't force the
211 # option into conformance, raise DistutilsOptionError. Thus, command
212 # classes need do nothing more than (eg.)
213 # self.ensure_string_list('foo')
214 # and they can be guaranteed that thereafter, self.foo will be
215 # a list of strings.
216
217 def _ensure_stringlike (self, option, what, default=None):
218 val = getattr(self, option)
219 if val is None:
220 setattr(self, option, default)
221 return default
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000222 elif not isinstance(val, str):
Greg Ward31413a72000-06-04 14:21:28 +0000223 raise DistutilsOptionError, \
224 "'%s' must be a %s (got `%s`)" % (option, what, val)
225 return val
226
227 def ensure_string (self, option, default=None):
228 """Ensure that 'option' is a string; if not defined, set it to
229 'default'.
230 """
231 self._ensure_stringlike(option, "string", default)
232
233 def ensure_string_list (self, option):
234 """Ensure that 'option' is a list of strings. If 'option' is
235 currently a string, we split it either on /,\s*/ or /\s+/, so
236 "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
237 ["foo", "bar", "baz"].
238 """
239 val = getattr(self, option)
240 if val is None:
241 return
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000242 elif isinstance(val, str):
Greg Ward31413a72000-06-04 14:21:28 +0000243 setattr(self, option, re.split(r',\s*|\s+', val))
244 else:
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000245 if isinstance(val, list):
246 # checks if all elements are str
247 ok = 1
248 for element in val:
249 if not isinstance(element, str):
250 ok = 0
251 break
Greg Ward31413a72000-06-04 14:21:28 +0000252 else:
253 ok = 0
254
255 if not ok:
256 raise DistutilsOptionError, \
Tarek Ziadé98da8e12009-02-06 08:55:23 +0000257 "'%s' must be a list of strings (got %r)" % \
258 (option, val)
259
Fred Drakeb94b8492001-12-06 20:51:35 +0000260
Greg Ward31413a72000-06-04 14:21:28 +0000261 def _ensure_tested_string (self, option, tester,
262 what, error_fmt, default=None):
263 val = self._ensure_stringlike(option, what, default)
264 if val is not None and not tester(val):
265 raise DistutilsOptionError, \
266 ("error in '%s' option: " + error_fmt) % (option, val)
267
268 def ensure_filename (self, option):
269 """Ensure that 'option' is the name of an existing file."""
270 self._ensure_tested_string(option, os.path.isfile,
271 "filename",
272 "'%s' does not exist or is not a file")
273
274 def ensure_dirname (self, option):
275 self._ensure_tested_string(option, os.path.isdir,
276 "directory name",
277 "'%s' does not exist or is not a directory")
278
279
Greg Wardfe6462c2000-04-04 01:40:52 +0000280 # -- Convenience methods for commands ------------------------------
281
282 def get_command_name (self):
Greg Ward071ed762000-09-26 02:12:31 +0000283 if hasattr(self, 'command_name'):
Greg Wardfe6462c2000-04-04 01:40:52 +0000284 return self.command_name
285 else:
286 return self.__class__.__name__
287
288
289 def set_undefined_options (self, src_cmd, *option_pairs):
290 """Set the values of any "undefined" options from corresponding
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000291 option values in some other command object. "Undefined" here means
292 "is None", which is the convention used to indicate that an option
293 has not been changed between 'initialize_options()' and
294 'finalize_options()'. Usually called from 'finalize_options()' for
295 options that depend on some other command rather than another
296 option of the same command. 'src_cmd' is the other command from
297 which option values will be taken (a command object will be created
298 for it if necessary); the remaining arguments are
299 '(src_option,dst_option)' tuples which mean "take the value of
300 'src_option' in the 'src_cmd' command object, and copy it to
301 'dst_option' in the current command object".
302 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000303
304 # Option_pairs: list of (src_option, dst_option) tuples
305
Greg Ward071ed762000-09-26 02:12:31 +0000306 src_cmd_obj = self.distribution.get_command_obj(src_cmd)
307 src_cmd_obj.ensure_finalized()
Greg Ward02a1a2b2000-04-15 22:15:07 +0000308 for (src_option, dst_option) in option_pairs:
Greg Ward071ed762000-09-26 02:12:31 +0000309 if getattr(self, dst_option) is None:
310 setattr(self, dst_option,
311 getattr(src_cmd_obj, src_option))
Greg Wardfe6462c2000-04-04 01:40:52 +0000312
313
Greg Ward4fb29e52000-05-27 17:27:23 +0000314 def get_finalized_command (self, command, create=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000315 """Wrapper around Distribution's 'get_command_obj()' method: find
316 (create if necessary and 'create' is true) the command object for
317 'command', call its 'ensure_finalized()' method, and return the
318 finalized command object.
319 """
Greg Ward071ed762000-09-26 02:12:31 +0000320 cmd_obj = self.distribution.get_command_obj(command, create)
321 cmd_obj.ensure_finalized()
Greg Wardfe6462c2000-04-04 01:40:52 +0000322 return cmd_obj
323
Greg Ward308acf02000-06-01 01:08:52 +0000324 # XXX rename to 'get_reinitialized_command()'? (should do the
325 # same in dist.py, if so)
Greg Wardecce1452000-09-16 15:25:55 +0000326 def reinitialize_command (self, command, reinit_subcommands=0):
327 return self.distribution.reinitialize_command(
328 command, reinit_subcommands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000329
Greg Ward4fb29e52000-05-27 17:27:23 +0000330 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000331 """Run some other command: uses the 'run_command()' method of
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000332 Distribution, which creates and finalizes the command object if
333 necessary and then invokes its 'run()' method.
334 """
Greg Ward071ed762000-09-26 02:12:31 +0000335 self.distribution.run_command(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000336
337
Greg Wardb3e0ad92000-09-16 15:09:17 +0000338 def get_sub_commands (self):
339 """Determine the sub-commands that are relevant in the current
340 distribution (ie., that need to be run). This is based on the
341 'sub_commands' class attribute: each tuple in that list may include
342 a method that we call to determine if the subcommand needs to be
343 run for the current distribution. Return a list of command names.
344 """
345 commands = []
346 for (cmd_name, method) in self.sub_commands:
347 if method is None or method(self):
348 commands.append(cmd_name)
349 return commands
350
351
Greg Wardfe6462c2000-04-04 01:40:52 +0000352 # -- External world manipulation -----------------------------------
353
354 def warn (self, msg):
Tarek Ziadéc7cd1382009-03-31 20:48:31 +0000355 log.warn("warning: %s: %s\n" %
356 (self.get_command_name(), msg))
Greg Wardfe6462c2000-04-04 01:40:52 +0000357
358 def execute (self, func, args, msg=None, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000359 util.execute(func, args, msg, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000360
361
362 def mkpath (self, name, mode=0777):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000363 dir_util.mkpath(name, mode, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000364
365
366 def copy_file (self, infile, outfile,
367 preserve_mode=1, preserve_times=1, link=None, level=1):
Greg Warde9613ae2000-04-10 01:30:44 +0000368 """Copy a file respecting verbose, dry-run and force flags. (The
369 former two default to whatever is in the Distribution object, and
370 the latter defaults to false for commands that don't define it.)"""
Greg Wardfe6462c2000-04-04 01:40:52 +0000371
Greg Ward29124ff2000-08-13 00:36:47 +0000372 return file_util.copy_file(
373 infile, outfile,
374 preserve_mode, preserve_times,
375 not self.force,
376 link,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000377 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000378
379
380 def copy_tree (self, infile, outfile,
381 preserve_mode=1, preserve_times=1, preserve_symlinks=0,
382 level=1):
383 """Copy an entire directory tree respecting verbose, dry-run,
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000384 and force flags.
385 """
Greg Ward29124ff2000-08-13 00:36:47 +0000386 return dir_util.copy_tree(
Fred Drakeb94b8492001-12-06 20:51:35 +0000387 infile, outfile,
Greg Ward29124ff2000-08-13 00:36:47 +0000388 preserve_mode,preserve_times,preserve_symlinks,
389 not self.force,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000390 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000391
392 def move_file (self, src, dst, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000393 """Move a file respectin dry-run flag."""
394 return file_util.move_file(src, dst, dry_run = self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000395
396 def spawn (self, cmd, search_path=1, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000397 """Spawn an external command respecting dry-run flag."""
Greg Wardfe6462c2000-04-04 01:40:52 +0000398 from distutils.spawn import spawn
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000399 spawn(cmd, search_path, dry_run= self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000400
401 def make_archive (self, base_name, format,
402 root_dir=None, base_dir=None):
Greg Ward29124ff2000-08-13 00:36:47 +0000403 return archive_util.make_archive(
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000404 base_name, format, root_dir, base_dir, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000405
406
Tarek Ziadé8be87652009-02-07 00:05:39 +0000407 def make_file(self, infiles, outfile, func, args,
408 exec_msg=None, skip_msg=None, level=1):
Greg Wardfe6462c2000-04-04 01:40:52 +0000409 """Special case of 'execute()' for operations that process one or
Greg Ward68a07572000-04-10 00:18:16 +0000410 more input files and generate one output file. Works just like
411 'execute()', except the operation is skipped and a different
412 message printed if 'outfile' already exists and is newer than all
413 files listed in 'infiles'. If the command defined 'self.force',
414 and it is true, then the command is unconditionally run -- does no
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000415 timestamp checks.
416 """
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 # Allow 'infiles' to be a single string
Tarek Ziadé8be87652009-02-07 00:05:39 +0000421 if isinstance(infiles, str):
Greg Wardfe6462c2000-04-04 01:40:52 +0000422 infiles = (infiles,)
Tarek Ziadé8be87652009-02-07 00:05:39 +0000423 elif not isinstance(infiles, (list, tuple)):
Greg Wardfe6462c2000-04-04 01:40:52 +0000424 raise TypeError, \
425 "'infiles' must be a string, or a list or tuple of strings"
426
Tarek Ziadé8be87652009-02-07 00:05:39 +0000427 if exec_msg is None:
428 exec_msg = "generating %s from %s" % \
429 (outfile, ', '.join(infiles))
430
Greg Wardfe6462c2000-04-04 01:40:52 +0000431 # If 'outfile' must be regenerated (either because it doesn't
432 # exist, is out-of-date, or the 'force' flag is true) then
433 # perform the action that presumably regenerates it
Tarek Ziadé8be87652009-02-07 00:05:39 +0000434 if self.force or dep_util.newer_group(infiles, outfile):
Greg Ward071ed762000-09-26 02:12:31 +0000435 self.execute(func, args, exec_msg, level)
Greg Wardfe6462c2000-04-04 01:40:52 +0000436
437 # Otherwise, print the "skip" message
438 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000439 log.debug(skip_msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000440
441 # make_file ()
442
443# class Command
Greg Wardb3612332000-04-09 03:48:37 +0000444
445
Greg Ward029e3022000-05-25 01:26:23 +0000446# XXX 'install_misc' class not currently used -- it was the base class for
447# both 'install_scripts' and 'install_data', but they outgrew it. It might
448# still be useful for 'install_headers', though, so I'm keeping it around
449# for the time being.
450
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000451class install_misc (Command):
452 """Common base class for installing some files in a subdirectory.
453 Currently used by install_data and install_scripts.
454 """
Fred Drakeb94b8492001-12-06 20:51:35 +0000455
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000456 user_options = [('install-dir=', 'd', "directory to install the files to")]
457
458 def initialize_options (self):
459 self.install_dir = None
Gregory P. Smith21b9e912000-05-13 03:10:30 +0000460 self.outfiles = []
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000461
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000462 def _install_dir_from (self, dirname):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000463 self.set_undefined_options('install', (dirname, 'install_dir'))
464
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000465 def _copy_files (self, filelist):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000466 self.outfiles = []
467 if not filelist:
468 return
469 self.mkpath(self.install_dir)
470 for f in filelist:
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000471 self.copy_file(f, self.install_dir)
472 self.outfiles.append(os.path.join(self.install_dir, f))
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000473
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000474 def get_outputs (self):
475 return self.outfiles