blob: ea3799af16f65519f756028b982873236d4bcdff [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 types import *
13from distutils.errors import *
Greg Ward29124ff2000-08-13 00:36:47 +000014from distutils import util, dir_util, file_util, archive_util, dep_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Wardfe6462c2000-04-04 01:40:52 +000016
17class Command:
18 """Abstract base class for defining command classes, the "worker bees"
Greg Ward8ff5a3f2000-06-02 00:44:53 +000019 of the Distutils. A useful analogy for command classes is to think of
20 them as subroutines with local variables called "options". The options
21 are "declared" in 'initialize_options()' and "defined" (given their
22 final values, aka "finalized") in 'finalize_options()', both of which
23 must be defined by every command class. The distinction between the
24 two is necessary because option values might come from the outside
25 world (command line, config file, ...), and any options dependent on
26 other options must be computed *after* these outside influences have
27 been processed -- hence 'finalize_options()'. The "body" of the
28 subroutine, where it does all its work based on the values of its
29 options, is the 'run()' method, which must also be implemented by every
30 command class.
31 """
Greg Wardfe6462c2000-04-04 01:40:52 +000032
Greg Wardb3e0ad92000-09-16 15:09:17 +000033 # 'sub_commands' formalizes the notion of a "family" of commands,
34 # eg. "install" as the parent with sub-commands "install_lib",
35 # "install_headers", etc. The parent of a family of commands
36 # defines 'sub_commands' as a class attribute; it's a list of
37 # (command_name : string, predicate : unbound_method | string | None)
38 # tuples, where 'predicate' is a method of the parent command that
39 # determines whether the corresponding command is applicable in the
40 # current situation. (Eg. we "install_headers" is only applicable if
41 # we have any C header files to install.) If 'predicate' is None,
42 # that command is always applicable.
Fred Drakeb94b8492001-12-06 20:51:35 +000043 #
Greg Wardb3e0ad92000-09-16 15:09:17 +000044 # 'sub_commands' is usually defined at the *end* of a class, because
45 # predicates can be unbound methods, so they must already have been
46 # defined. The canonical example is the "install" command.
47 sub_commands = []
48
49
Greg Wardfe6462c2000-04-04 01:40:52 +000050 # -- Creation/initialization methods -------------------------------
51
52 def __init__ (self, dist):
53 """Create and initialize a new Command object. Most importantly,
Greg Ward8ff5a3f2000-06-02 00:44:53 +000054 invokes the 'initialize_options()' method, which is the real
55 initializer and depends on the actual command being
56 instantiated.
57 """
Greg Wardfe6462c2000-04-04 01:40:52 +000058 # late import because of mutual dependence between these classes
59 from distutils.dist import Distribution
60
Greg Ward071ed762000-09-26 02:12:31 +000061 if not isinstance(dist, Distribution):
Greg Wardfe6462c2000-04-04 01:40:52 +000062 raise TypeError, "dist must be a Distribution instance"
63 if self.__class__ is Command:
64 raise RuntimeError, "Command is an abstract class"
65
66 self.distribution = dist
Greg Ward071ed762000-09-26 02:12:31 +000067 self.initialize_options()
Greg Wardfe6462c2000-04-04 01:40:52 +000068
69 # Per-command versions of the global flags, so that the user can
70 # customize Distutils' behaviour command-by-command and let some
Fred Drake2b2fe942004-06-18 21:28:28 +000071 # commands fall back on the Distribution's behaviour. None means
Greg Wardfe6462c2000-04-04 01:40:52 +000072 # "not defined, check self.distribution's copy", while 0 or 1 mean
73 # false and true (duh). Note that this means figuring out the real
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000074 # value of each flag is a touch complicated -- hence "self._dry_run"
75 # will be handled by __getattr__, below.
76 # XXX This needs to be fixed.
Greg Wardfe6462c2000-04-04 01:40:52 +000077 self._dry_run = None
Greg Wardfe6462c2000-04-04 01:40:52 +000078
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000079 # verbose is largely ignored, but needs to be set for
80 # backwards compatibility (I think)?
81 self.verbose = dist.verbose
Tim Peters182b5ac2004-07-18 06:16:08 +000082
Greg Wardd197a3a2000-04-10 13:11:51 +000083 # Some commands define a 'self.force' option to ignore file
84 # timestamps, but methods defined *here* assume that
85 # 'self.force' exists for all commands. So define it here
86 # just to be safe.
87 self.force = None
88
Greg Wardfe6462c2000-04-04 01:40:52 +000089 # The 'help' flag is just used for command-line parsing, so
90 # none of that complicated bureaucracy is needed.
91 self.help = 0
92
Greg Ward4fb29e52000-05-27 17:27:23 +000093 # 'finalized' records whether or not 'finalize_options()' has been
Greg Wardfe6462c2000-04-04 01:40:52 +000094 # called. 'finalize_options()' itself should not pay attention to
Greg Ward4fb29e52000-05-27 17:27:23 +000095 # this flag: it is the business of 'ensure_finalized()', which
96 # always calls 'finalize_options()', to respect/update it.
97 self.finalized = 0
Greg Wardfe6462c2000-04-04 01:40:52 +000098
99 # __init__ ()
100
101
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000102 # XXX A more explicit way to customize dry_run would be better.
Tim Peters182b5ac2004-07-18 06:16:08 +0000103
Greg Wardfe6462c2000-04-04 01:40:52 +0000104 def __getattr__ (self, attr):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000105 if attr == 'dry_run':
Greg Ward071ed762000-09-26 02:12:31 +0000106 myval = getattr(self, "_" + attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000107 if myval is None:
Greg Ward071ed762000-09-26 02:12:31 +0000108 return getattr(self.distribution, attr)
Greg Wardfe6462c2000-04-04 01:40:52 +0000109 else:
110 return myval
111 else:
112 raise AttributeError, attr
113
114
Greg Ward4fb29e52000-05-27 17:27:23 +0000115 def ensure_finalized (self):
116 if not self.finalized:
Greg Ward071ed762000-09-26 02:12:31 +0000117 self.finalize_options()
Greg Ward4fb29e52000-05-27 17:27:23 +0000118 self.finalized = 1
Fred Drakeb94b8492001-12-06 20:51:35 +0000119
Greg Wardfe6462c2000-04-04 01:40:52 +0000120
121 # Subclasses must define:
122 # initialize_options()
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000123 # provide default values for all options; may be customized by
124 # setup script, by options from config file(s), or by command-line
125 # options
Greg Wardfe6462c2000-04-04 01:40:52 +0000126 # finalize_options()
127 # decide on the final values for all options; this is called
128 # after all possible intervention from the outside world
129 # (command-line, option file, etc.) has been processed
130 # run()
131 # run the command: do whatever it is we're here to do,
132 # controlled by the command's various option values
133
134 def initialize_options (self):
135 """Set default values for all the options that this command
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000136 supports. Note that these defaults may be overridden by other
137 commands, by the setup script, by config files, or by the
138 command-line. Thus, this is not the place to code dependencies
139 between options; generally, 'initialize_options()' implementations
140 are just a bunch of "self.foo = None" assignments.
Fred Drakeb94b8492001-12-06 20:51:35 +0000141
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000142 This method must be implemented by all command classes.
143 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000144 raise RuntimeError, \
145 "abstract method -- subclass %s must override" % self.__class__
Fred Drakeb94b8492001-12-06 20:51:35 +0000146
Greg Wardfe6462c2000-04-04 01:40:52 +0000147 def finalize_options (self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000148 """Set final values for all the options that this command supports.
149 This is always called as late as possible, ie. after any option
150 assignments from the command-line or from other commands have been
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000151 done. Thus, this is the place to code option dependencies: if
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000152 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as
153 long as 'foo' still has the same value it was assigned in
154 'initialize_options()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000155
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000156 This method must be implemented by all command classes.
157 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000158 raise RuntimeError, \
159 "abstract method -- subclass %s must override" % self.__class__
160
Greg Wardadda1562000-05-28 23:54:00 +0000161
162 def dump_options (self, header=None, indent=""):
163 from distutils.fancy_getopt import longopt_xlate
164 if header is None:
165 header = "command options for '%s':" % self.get_command_name()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000166 print(indent + header)
Greg Wardadda1562000-05-28 23:54:00 +0000167 indent = indent + " "
168 for (option, _, _) in self.user_options:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000169 option = option.translate(longopt_xlate)
Greg Wardadda1562000-05-28 23:54:00 +0000170 if option[-1] == "=":
171 option = option[:-1]
172 value = getattr(self, option)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000173 print(indent + "%s = %s" % (option, value))
Greg Wardadda1562000-05-28 23:54:00 +0000174
175
Greg Wardfe6462c2000-04-04 01:40:52 +0000176 def run (self):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000177 """A command's raison d'etre: carry out the action it exists to
178 perform, controlled by the options initialized in
179 'initialize_options()', customized by other commands, the setup
180 script, the command-line, and config files, and finalized in
181 'finalize_options()'. All terminal output and filesystem
182 interaction should be done by 'run()'.
Greg Wardfe6462c2000-04-04 01:40:52 +0000183
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000184 This method must be implemented by all command classes.
185 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000186
187 raise RuntimeError, \
188 "abstract method -- subclass %s must override" % self.__class__
189
190 def announce (self, msg, level=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000191 """If the current verbosity level is of greater than or equal to
192 'level' print 'msg' to stdout.
193 """
Guido van Rossumaf160652003-02-20 02:10:08 +0000194 log.log(level, msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000195
Greg Wardebec02a2000-06-08 00:02:36 +0000196 def debug_print (self, msg):
197 """Print 'msg' to stdout if the global DEBUG (taken from the
198 DISTUTILS_DEBUG environment variable) flag is true.
199 """
Jeremy Hyltonfcd73532002-09-11 16:31:53 +0000200 from distutils.debug import DEBUG
Greg Wardebec02a2000-06-08 00:02:36 +0000201 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000202 print(msg)
Neil Schemenauer69374e42001-08-29 23:57:22 +0000203 sys.stdout.flush()
Fred Drakeb94b8492001-12-06 20:51:35 +0000204
Greg Wardebec02a2000-06-08 00:02:36 +0000205
Greg Wardfe6462c2000-04-04 01:40:52 +0000206
Greg Ward31413a72000-06-04 14:21:28 +0000207 # -- Option validation methods -------------------------------------
208 # (these are very handy in writing the 'finalize_options()' method)
Fred Drakeb94b8492001-12-06 20:51:35 +0000209 #
Greg Ward31413a72000-06-04 14:21:28 +0000210 # NB. the general philosophy here is to ensure that a particular option
211 # value meets certain type and value constraints. If not, we try to
212 # force it into conformance (eg. if we expect a list but have a string,
213 # split the string on comma and/or whitespace). If we can't force the
214 # option into conformance, raise DistutilsOptionError. Thus, command
215 # classes need do nothing more than (eg.)
216 # self.ensure_string_list('foo')
217 # and they can be guaranteed that thereafter, self.foo will be
218 # a list of strings.
219
220 def _ensure_stringlike (self, option, what, default=None):
221 val = getattr(self, option)
222 if val is None:
223 setattr(self, option, default)
224 return default
Guido van Rossum572dbf82007-04-27 23:53:51 +0000225 elif not isinstance(val, basestring):
Greg Ward31413a72000-06-04 14:21:28 +0000226 raise DistutilsOptionError, \
227 "'%s' must be a %s (got `%s`)" % (option, what, val)
228 return val
229
230 def ensure_string (self, option, default=None):
231 """Ensure that 'option' is a string; if not defined, set it to
232 'default'.
233 """
234 self._ensure_stringlike(option, "string", default)
235
236 def ensure_string_list (self, option):
237 """Ensure that 'option' is a list of strings. If 'option' is
238 currently a string, we split it either on /,\s*/ or /\s+/, so
239 "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
240 ["foo", "bar", "baz"].
241 """
242 val = getattr(self, option)
243 if val is None:
244 return
Guido van Rossum572dbf82007-04-27 23:53:51 +0000245 elif isinstance(val, basestring):
Greg Ward31413a72000-06-04 14:21:28 +0000246 setattr(self, option, re.split(r',\s*|\s+', val))
247 else:
248 if type(val) is ListType:
Guido van Rossum572dbf82007-04-27 23:53:51 +0000249 ok = all(isinstance(v, basestring) for v in val)
Greg Ward31413a72000-06-04 14:21:28 +0000250 else:
251 ok = 0
252
253 if not ok:
254 raise DistutilsOptionError, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000255 "'%s' must be a list of strings (got %r)" % \
256 (option, val)
Fred Drakeb94b8492001-12-06 20:51:35 +0000257
Greg Ward31413a72000-06-04 14:21:28 +0000258 def _ensure_tested_string (self, option, tester,
259 what, error_fmt, default=None):
260 val = self._ensure_stringlike(option, what, default)
261 if val is not None and not tester(val):
262 raise DistutilsOptionError, \
263 ("error in '%s' option: " + error_fmt) % (option, val)
264
265 def ensure_filename (self, option):
266 """Ensure that 'option' is the name of an existing file."""
267 self._ensure_tested_string(option, os.path.isfile,
268 "filename",
269 "'%s' does not exist or is not a file")
270
271 def ensure_dirname (self, option):
272 self._ensure_tested_string(option, os.path.isdir,
273 "directory name",
274 "'%s' does not exist or is not a directory")
275
276
Greg Wardfe6462c2000-04-04 01:40:52 +0000277 # -- Convenience methods for commands ------------------------------
278
279 def get_command_name (self):
Greg Ward071ed762000-09-26 02:12:31 +0000280 if hasattr(self, 'command_name'):
Greg Wardfe6462c2000-04-04 01:40:52 +0000281 return self.command_name
282 else:
283 return self.__class__.__name__
284
285
286 def set_undefined_options (self, src_cmd, *option_pairs):
287 """Set the values of any "undefined" options from corresponding
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000288 option values in some other command object. "Undefined" here means
289 "is None", which is the convention used to indicate that an option
290 has not been changed between 'initialize_options()' and
291 'finalize_options()'. Usually called from 'finalize_options()' for
292 options that depend on some other command rather than another
293 option of the same command. 'src_cmd' is the other command from
294 which option values will be taken (a command object will be created
295 for it if necessary); the remaining arguments are
296 '(src_option,dst_option)' tuples which mean "take the value of
297 'src_option' in the 'src_cmd' command object, and copy it to
298 'dst_option' in the current command object".
299 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000300
301 # Option_pairs: list of (src_option, dst_option) tuples
302
Greg Ward071ed762000-09-26 02:12:31 +0000303 src_cmd_obj = self.distribution.get_command_obj(src_cmd)
304 src_cmd_obj.ensure_finalized()
Greg Ward02a1a2b2000-04-15 22:15:07 +0000305 for (src_option, dst_option) in option_pairs:
Greg Ward071ed762000-09-26 02:12:31 +0000306 if getattr(self, dst_option) is None:
307 setattr(self, dst_option,
308 getattr(src_cmd_obj, src_option))
Greg Wardfe6462c2000-04-04 01:40:52 +0000309
310
Greg Ward4fb29e52000-05-27 17:27:23 +0000311 def get_finalized_command (self, command, create=1):
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000312 """Wrapper around Distribution's 'get_command_obj()' method: find
313 (create if necessary and 'create' is true) the command object for
314 'command', call its 'ensure_finalized()' method, and return the
315 finalized command object.
316 """
Greg Ward071ed762000-09-26 02:12:31 +0000317 cmd_obj = self.distribution.get_command_obj(command, create)
318 cmd_obj.ensure_finalized()
Greg Wardfe6462c2000-04-04 01:40:52 +0000319 return cmd_obj
320
Greg Ward308acf02000-06-01 01:08:52 +0000321 # XXX rename to 'get_reinitialized_command()'? (should do the
322 # same in dist.py, if so)
Greg Wardecce1452000-09-16 15:25:55 +0000323 def reinitialize_command (self, command, reinit_subcommands=0):
324 return self.distribution.reinitialize_command(
325 command, reinit_subcommands)
Greg Wardfe6462c2000-04-04 01:40:52 +0000326
Greg Ward4fb29e52000-05-27 17:27:23 +0000327 def run_command (self, command):
Greg Wardfe6462c2000-04-04 01:40:52 +0000328 """Run some other command: uses the 'run_command()' method of
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000329 Distribution, which creates and finalizes the command object if
330 necessary and then invokes its 'run()' method.
331 """
Greg Ward071ed762000-09-26 02:12:31 +0000332 self.distribution.run_command(command)
Greg Wardfe6462c2000-04-04 01:40:52 +0000333
334
Greg Wardb3e0ad92000-09-16 15:09:17 +0000335 def get_sub_commands (self):
336 """Determine the sub-commands that are relevant in the current
337 distribution (ie., that need to be run). This is based on the
338 'sub_commands' class attribute: each tuple in that list may include
339 a method that we call to determine if the subcommand needs to be
340 run for the current distribution. Return a list of command names.
341 """
342 commands = []
343 for (cmd_name, method) in self.sub_commands:
344 if method is None or method(self):
345 commands.append(cmd_name)
346 return commands
347
348
Greg Wardfe6462c2000-04-04 01:40:52 +0000349 # -- External world manipulation -----------------------------------
350
351 def warn (self, msg):
Greg Ward071ed762000-09-26 02:12:31 +0000352 sys.stderr.write("warning: %s: %s\n" %
353 (self.get_command_name(), msg))
Greg Wardfe6462c2000-04-04 01:40:52 +0000354
355
356 def execute (self, func, args, msg=None, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000357 util.execute(func, args, msg, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000358
359
360 def mkpath (self, name, mode=0777):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000361 dir_util.mkpath(name, mode, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000362
363
364 def copy_file (self, infile, outfile,
365 preserve_mode=1, preserve_times=1, link=None, level=1):
Greg Warde9613ae2000-04-10 01:30:44 +0000366 """Copy a file respecting verbose, dry-run and force flags. (The
367 former two default to whatever is in the Distribution object, and
368 the latter defaults to false for commands that don't define it.)"""
Greg Wardfe6462c2000-04-04 01:40:52 +0000369
Greg Ward29124ff2000-08-13 00:36:47 +0000370 return file_util.copy_file(
371 infile, outfile,
372 preserve_mode, preserve_times,
373 not self.force,
374 link,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000375 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000376
377
378 def copy_tree (self, infile, outfile,
379 preserve_mode=1, preserve_times=1, preserve_symlinks=0,
380 level=1):
381 """Copy an entire directory tree respecting verbose, dry-run,
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000382 and force flags.
383 """
Greg Ward29124ff2000-08-13 00:36:47 +0000384 return dir_util.copy_tree(
Fred Drakeb94b8492001-12-06 20:51:35 +0000385 infile, outfile,
Greg Ward29124ff2000-08-13 00:36:47 +0000386 preserve_mode,preserve_times,preserve_symlinks,
387 not self.force,
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000388 dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000389
390 def move_file (self, src, dst, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000391 """Move a file respectin dry-run flag."""
392 return file_util.move_file(src, dst, dry_run = self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000393
394 def spawn (self, cmd, search_path=1, level=1):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000395 """Spawn an external command respecting dry-run flag."""
Greg Wardfe6462c2000-04-04 01:40:52 +0000396 from distutils.spawn import spawn
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000397 spawn(cmd, search_path, dry_run= self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000398
399 def make_archive (self, base_name, format,
400 root_dir=None, base_dir=None):
Greg Ward29124ff2000-08-13 00:36:47 +0000401 return archive_util.make_archive(
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000402 base_name, format, root_dir, base_dir, dry_run=self.dry_run)
Greg Wardfe6462c2000-04-04 01:40:52 +0000403
404
405 def make_file (self, infiles, outfile, func, args,
Greg Ward68a07572000-04-10 00:18:16 +0000406 exec_msg=None, skip_msg=None, level=1):
Greg Wardfe6462c2000-04-04 01:40:52 +0000407 """Special case of 'execute()' for operations that process one or
Greg Ward68a07572000-04-10 00:18:16 +0000408 more input files and generate one output file. Works just like
409 'execute()', except the operation is skipped and a different
410 message printed if 'outfile' already exists and is newer than all
411 files listed in 'infiles'. If the command defined 'self.force',
412 and it is true, then the command is unconditionally run -- does no
Greg Ward8ff5a3f2000-06-02 00:44:53 +0000413 timestamp checks.
414 """
Greg Wardfe6462c2000-04-04 01:40:52 +0000415 if exec_msg is None:
416 exec_msg = "generating %s from %s" % \
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000417 (outfile, ', '.join(infiles))
Greg Wardfe6462c2000-04-04 01:40:52 +0000418 if skip_msg is None:
419 skip_msg = "skipping %s (inputs unchanged)" % outfile
Fred Drakeb94b8492001-12-06 20:51:35 +0000420
Greg Wardfe6462c2000-04-04 01:40:52 +0000421
422 # Allow 'infiles' to be a single string
Guido van Rossum572dbf82007-04-27 23:53:51 +0000423 if isinstance(infiles, basestring):
Greg Wardfe6462c2000-04-04 01:40:52 +0000424 infiles = (infiles,)
Greg Ward071ed762000-09-26 02:12:31 +0000425 elif type(infiles) not in (ListType, TupleType):
Greg Wardfe6462c2000-04-04 01:40:52 +0000426 raise TypeError, \
427 "'infiles' must be a string, or a list or tuple of strings"
428
429 # If 'outfile' must be regenerated (either because it doesn't
430 # exist, is out-of-date, or the 'force' flag is true) then
431 # perform the action that presumably regenerates it
Greg Ward29124ff2000-08-13 00:36:47 +0000432 if self.force or dep_util.newer_group (infiles, outfile):
Greg Ward071ed762000-09-26 02:12:31 +0000433 self.execute(func, args, exec_msg, level)
Greg Wardfe6462c2000-04-04 01:40:52 +0000434
435 # Otherwise, print the "skip" message
436 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000437 log.debug(skip_msg)
Greg Wardfe6462c2000-04-04 01:40:52 +0000438
439 # make_file ()
440
441# class Command
Greg Wardb3612332000-04-09 03:48:37 +0000442
443
Greg Ward029e3022000-05-25 01:26:23 +0000444# XXX 'install_misc' class not currently used -- it was the base class for
445# both 'install_scripts' and 'install_data', but they outgrew it. It might
446# still be useful for 'install_headers', though, so I'm keeping it around
447# for the time being.
448
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000449class install_misc (Command):
450 """Common base class for installing some files in a subdirectory.
451 Currently used by install_data and install_scripts.
452 """
Fred Drakeb94b8492001-12-06 20:51:35 +0000453
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000454 user_options = [('install-dir=', 'd', "directory to install the files to")]
455
456 def initialize_options (self):
457 self.install_dir = None
Gregory P. Smith21b9e912000-05-13 03:10:30 +0000458 self.outfiles = []
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000459
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000460 def _install_dir_from (self, dirname):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000461 self.set_undefined_options('install', (dirname, 'install_dir'))
462
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000463 def _copy_files (self, filelist):
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000464 self.outfiles = []
465 if not filelist:
466 return
467 self.mkpath(self.install_dir)
468 for f in filelist:
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000469 self.copy_file(f, self.install_dir)
470 self.outfiles.append(os.path.join(self.install_dir, f))
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000471
Gregory P. Smithce2b6b82000-05-12 01:31:37 +0000472 def get_outputs (self):
473 return self.outfiles
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +0000474
475
Greg Wardb3612332000-04-09 03:48:37 +0000476if __name__ == "__main__":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000477 print("ok")