Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 1 | """distutils.cmd |
| 2 | |
| 3 | Provides the Command class, the base class for the command classes |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 4 | in the distutils.command package. |
| 5 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 6 | |
| 7 | # created 2000/04/03, Greg Ward |
| 8 | # (extricated from core.py; actually dates back to the beginning) |
| 9 | |
| 10 | __revision__ = "$Id$" |
| 11 | |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 12 | import sys, os, string, re |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 13 | from types import * |
| 14 | from distutils.errors import * |
| 15 | from distutils import util |
| 16 | |
| 17 | |
| 18 | class Command: |
| 19 | """Abstract base class for defining command classes, the "worker bees" |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 20 | of the Distutils. A useful analogy for command classes is to think of |
| 21 | them as subroutines with local variables called "options". The options |
| 22 | are "declared" in 'initialize_options()' and "defined" (given their |
| 23 | final values, aka "finalized") in 'finalize_options()', both of which |
| 24 | must be defined by every command class. The distinction between the |
| 25 | two is necessary because option values might come from the outside |
| 26 | world (command line, config file, ...), and any options dependent on |
| 27 | other options must be computed *after* these outside influences have |
| 28 | been processed -- hence 'finalize_options()'. The "body" of the |
| 29 | subroutine, where it does all its work based on the values of its |
| 30 | options, is the 'run()' method, which must also be implemented by every |
| 31 | command class. |
| 32 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 33 | |
| 34 | # -- Creation/initialization methods ------------------------------- |
| 35 | |
| 36 | def __init__ (self, dist): |
| 37 | """Create and initialize a new Command object. Most importantly, |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 38 | invokes the 'initialize_options()' method, which is the real |
| 39 | initializer and depends on the actual command being |
| 40 | instantiated. |
| 41 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 42 | # late import because of mutual dependence between these classes |
| 43 | from distutils.dist import Distribution |
| 44 | |
| 45 | if not isinstance (dist, Distribution): |
| 46 | raise TypeError, "dist must be a Distribution instance" |
| 47 | if self.__class__ is Command: |
| 48 | raise RuntimeError, "Command is an abstract class" |
| 49 | |
| 50 | self.distribution = dist |
| 51 | self.initialize_options () |
| 52 | |
| 53 | # Per-command versions of the global flags, so that the user can |
| 54 | # customize Distutils' behaviour command-by-command and let some |
| 55 | # commands fallback on the Distribution's behaviour. None means |
| 56 | # "not defined, check self.distribution's copy", while 0 or 1 mean |
| 57 | # false and true (duh). Note that this means figuring out the real |
| 58 | # value of each flag is a touch complicatd -- hence "self.verbose" |
| 59 | # (etc.) will be handled by __getattr__, below. |
| 60 | self._verbose = None |
| 61 | self._dry_run = None |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 62 | |
Greg Ward | d197a3a | 2000-04-10 13:11:51 +0000 | [diff] [blame] | 63 | # Some commands define a 'self.force' option to ignore file |
| 64 | # timestamps, but methods defined *here* assume that |
| 65 | # 'self.force' exists for all commands. So define it here |
| 66 | # just to be safe. |
| 67 | self.force = None |
| 68 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 69 | # The 'help' flag is just used for command-line parsing, so |
| 70 | # none of that complicated bureaucracy is needed. |
| 71 | self.help = 0 |
| 72 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 73 | # 'finalized' records whether or not 'finalize_options()' has been |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 74 | # called. 'finalize_options()' itself should not pay attention to |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 75 | # this flag: it is the business of 'ensure_finalized()', which |
| 76 | # always calls 'finalize_options()', to respect/update it. |
| 77 | self.finalized = 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 78 | |
| 79 | # __init__ () |
| 80 | |
| 81 | |
| 82 | def __getattr__ (self, attr): |
Greg Ward | 68a0757 | 2000-04-10 00:18:16 +0000 | [diff] [blame] | 83 | if attr in ('verbose', 'dry_run'): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 84 | myval = getattr (self, "_" + attr) |
| 85 | if myval is None: |
| 86 | return getattr (self.distribution, attr) |
| 87 | else: |
| 88 | return myval |
| 89 | else: |
| 90 | raise AttributeError, attr |
| 91 | |
| 92 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 93 | def ensure_finalized (self): |
| 94 | if not self.finalized: |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 95 | self.finalize_options () |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 96 | self.finalized = 1 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | # Subclasses must define: |
| 100 | # initialize_options() |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 101 | # provide default values for all options; may be customized by |
| 102 | # setup script, by options from config file(s), or by command-line |
| 103 | # options |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 104 | # finalize_options() |
| 105 | # decide on the final values for all options; this is called |
| 106 | # after all possible intervention from the outside world |
| 107 | # (command-line, option file, etc.) has been processed |
| 108 | # run() |
| 109 | # run the command: do whatever it is we're here to do, |
| 110 | # controlled by the command's various option values |
| 111 | |
| 112 | def initialize_options (self): |
| 113 | """Set default values for all the options that this command |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 114 | supports. Note that these defaults may be overridden by other |
| 115 | commands, by the setup script, by config files, or by the |
| 116 | command-line. Thus, this is not the place to code dependencies |
| 117 | between options; generally, 'initialize_options()' implementations |
| 118 | are just a bunch of "self.foo = None" assignments. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 119 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 120 | This method must be implemented by all command classes. |
| 121 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 122 | raise RuntimeError, \ |
| 123 | "abstract method -- subclass %s must override" % self.__class__ |
| 124 | |
| 125 | def finalize_options (self): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 126 | """Set final values for all the options that this command supports. |
| 127 | This is always called as late as possible, ie. after any option |
| 128 | assignments from the command-line or from other commands have been |
| 129 | done. Thus, this is the place to to code option dependencies: if |
| 130 | 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as |
| 131 | long as 'foo' still has the same value it was assigned in |
| 132 | 'initialize_options()'. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 133 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 134 | This method must be implemented by all command classes. |
| 135 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 136 | raise RuntimeError, \ |
| 137 | "abstract method -- subclass %s must override" % self.__class__ |
| 138 | |
Greg Ward | adda156 | 2000-05-28 23:54:00 +0000 | [diff] [blame] | 139 | |
| 140 | def dump_options (self, header=None, indent=""): |
| 141 | from distutils.fancy_getopt import longopt_xlate |
| 142 | if header is None: |
| 143 | header = "command options for '%s':" % self.get_command_name() |
| 144 | print indent + header |
| 145 | indent = indent + " " |
| 146 | for (option, _, _) in self.user_options: |
| 147 | option = string.translate(option, longopt_xlate) |
| 148 | if option[-1] == "=": |
| 149 | option = option[:-1] |
| 150 | value = getattr(self, option) |
| 151 | print indent + "%s = %s" % (option, value) |
| 152 | |
| 153 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 154 | def run (self): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 155 | """A command's raison d'etre: carry out the action it exists to |
| 156 | perform, controlled by the options initialized in |
| 157 | 'initialize_options()', customized by other commands, the setup |
| 158 | script, the command-line, and config files, and finalized in |
| 159 | 'finalize_options()'. All terminal output and filesystem |
| 160 | interaction should be done by 'run()'. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 161 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 162 | This method must be implemented by all command classes. |
| 163 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 164 | |
| 165 | raise RuntimeError, \ |
| 166 | "abstract method -- subclass %s must override" % self.__class__ |
| 167 | |
| 168 | def announce (self, msg, level=1): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 169 | """If the current verbosity level is of greater than or equal to |
| 170 | 'level' print 'msg' to stdout. |
| 171 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 172 | if self.verbose >= level: |
| 173 | print msg |
| 174 | |
Greg Ward | ebec02a | 2000-06-08 00:02:36 +0000 | [diff] [blame] | 175 | def debug_print (self, msg): |
| 176 | """Print 'msg' to stdout if the global DEBUG (taken from the |
| 177 | DISTUTILS_DEBUG environment variable) flag is true. |
| 178 | """ |
| 179 | from distutils.core import DEBUG |
| 180 | if DEBUG: |
| 181 | print msg |
| 182 | |
| 183 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 184 | |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 185 | # -- Option validation methods ------------------------------------- |
| 186 | # (these are very handy in writing the 'finalize_options()' method) |
| 187 | # |
| 188 | # NB. the general philosophy here is to ensure that a particular option |
| 189 | # value meets certain type and value constraints. If not, we try to |
| 190 | # force it into conformance (eg. if we expect a list but have a string, |
| 191 | # split the string on comma and/or whitespace). If we can't force the |
| 192 | # option into conformance, raise DistutilsOptionError. Thus, command |
| 193 | # classes need do nothing more than (eg.) |
| 194 | # self.ensure_string_list('foo') |
| 195 | # and they can be guaranteed that thereafter, self.foo will be |
| 196 | # a list of strings. |
| 197 | |
| 198 | def _ensure_stringlike (self, option, what, default=None): |
| 199 | val = getattr(self, option) |
| 200 | if val is None: |
| 201 | setattr(self, option, default) |
| 202 | return default |
| 203 | elif type(val) is not StringType: |
| 204 | raise DistutilsOptionError, \ |
| 205 | "'%s' must be a %s (got `%s`)" % (option, what, val) |
| 206 | return val |
| 207 | |
| 208 | def ensure_string (self, option, default=None): |
| 209 | """Ensure that 'option' is a string; if not defined, set it to |
| 210 | 'default'. |
| 211 | """ |
| 212 | self._ensure_stringlike(option, "string", default) |
| 213 | |
| 214 | def ensure_string_list (self, option): |
| 215 | """Ensure that 'option' is a list of strings. If 'option' is |
| 216 | currently a string, we split it either on /,\s*/ or /\s+/, so |
| 217 | "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become |
| 218 | ["foo", "bar", "baz"]. |
| 219 | """ |
| 220 | val = getattr(self, option) |
| 221 | if val is None: |
| 222 | return |
| 223 | elif type(val) is StringType: |
| 224 | setattr(self, option, re.split(r',\s*|\s+', val)) |
| 225 | else: |
| 226 | if type(val) is ListType: |
| 227 | types = map(type, val) |
| 228 | ok = (types == [StringType] * len(val)) |
| 229 | else: |
| 230 | ok = 0 |
| 231 | |
| 232 | if not ok: |
| 233 | raise DistutilsOptionError, \ |
| 234 | "'%s' must be a list of strings (got %s)" % \ |
| 235 | (option, `val`) |
| 236 | |
| 237 | def _ensure_tested_string (self, option, tester, |
| 238 | what, error_fmt, default=None): |
| 239 | val = self._ensure_stringlike(option, what, default) |
| 240 | if val is not None and not tester(val): |
| 241 | raise DistutilsOptionError, \ |
| 242 | ("error in '%s' option: " + error_fmt) % (option, val) |
| 243 | |
| 244 | def ensure_filename (self, option): |
| 245 | """Ensure that 'option' is the name of an existing file.""" |
| 246 | self._ensure_tested_string(option, os.path.isfile, |
| 247 | "filename", |
| 248 | "'%s' does not exist or is not a file") |
| 249 | |
| 250 | def ensure_dirname (self, option): |
| 251 | self._ensure_tested_string(option, os.path.isdir, |
| 252 | "directory name", |
| 253 | "'%s' does not exist or is not a directory") |
| 254 | |
| 255 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 256 | # -- Convenience methods for commands ------------------------------ |
| 257 | |
| 258 | def get_command_name (self): |
| 259 | if hasattr (self, 'command_name'): |
| 260 | return self.command_name |
| 261 | else: |
| 262 | return self.__class__.__name__ |
| 263 | |
| 264 | |
| 265 | def set_undefined_options (self, src_cmd, *option_pairs): |
| 266 | """Set the values of any "undefined" options from corresponding |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 267 | option values in some other command object. "Undefined" here means |
| 268 | "is None", which is the convention used to indicate that an option |
| 269 | has not been changed between 'initialize_options()' and |
| 270 | 'finalize_options()'. Usually called from 'finalize_options()' for |
| 271 | options that depend on some other command rather than another |
| 272 | option of the same command. 'src_cmd' is the other command from |
| 273 | which option values will be taken (a command object will be created |
| 274 | for it if necessary); the remaining arguments are |
| 275 | '(src_option,dst_option)' tuples which mean "take the value of |
| 276 | 'src_option' in the 'src_cmd' command object, and copy it to |
| 277 | 'dst_option' in the current command object". |
| 278 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 279 | |
| 280 | # Option_pairs: list of (src_option, dst_option) tuples |
| 281 | |
Greg Ward | 5edcd90 | 2000-05-23 01:55:01 +0000 | [diff] [blame] | 282 | src_cmd_obj = self.distribution.get_command_obj (src_cmd) |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 283 | src_cmd_obj.ensure_finalized () |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 284 | for (src_option, dst_option) in option_pairs: |
| 285 | if getattr (self, dst_option) is None: |
Greg Ward | f4f8e64 | 2000-05-07 15:29:15 +0000 | [diff] [blame] | 286 | setattr (self, dst_option, |
| 287 | getattr (src_cmd_obj, src_option)) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 288 | |
| 289 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 290 | def get_finalized_command (self, command, create=1): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 291 | """Wrapper around Distribution's 'get_command_obj()' method: find |
| 292 | (create if necessary and 'create' is true) the command object for |
| 293 | 'command', call its 'ensure_finalized()' method, and return the |
| 294 | finalized command object. |
| 295 | """ |
Greg Ward | 5edcd90 | 2000-05-23 01:55:01 +0000 | [diff] [blame] | 296 | cmd_obj = self.distribution.get_command_obj (command, create) |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 297 | cmd_obj.ensure_finalized () |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 298 | return cmd_obj |
| 299 | |
Greg Ward | 308acf0 | 2000-06-01 01:08:52 +0000 | [diff] [blame] | 300 | # XXX rename to 'get_reinitialized_command()'? (should do the |
| 301 | # same in dist.py, if so) |
| 302 | def reinitialize_command (self, command): |
| 303 | return self.distribution.reinitialize_command(command) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 304 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 305 | def run_command (self, command): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 306 | """Run some other command: uses the 'run_command()' method of |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 307 | Distribution, which creates and finalizes the command object if |
| 308 | necessary and then invokes its 'run()' method. |
| 309 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 310 | self.distribution.run_command (command) |
| 311 | |
| 312 | |
| 313 | # -- External world manipulation ----------------------------------- |
| 314 | |
| 315 | def warn (self, msg): |
| 316 | sys.stderr.write ("warning: %s: %s\n" % |
| 317 | (self.get_command_name(), msg)) |
| 318 | |
| 319 | |
| 320 | def execute (self, func, args, msg=None, level=1): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 321 | """Perform some action that affects the outside world (eg. by |
| 322 | writing to the filesystem). Such actions are special because they |
| 323 | should be disabled by the "dry run" flag, and should announce |
| 324 | themselves if the current verbosity level is high enough. This |
| 325 | method takes care of all that bureaucracy for you; all you have to |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 326 | do is supply the function to call and an argument tuple for it (to |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 327 | embody the "external action" being performed), a message to print |
| 328 | if the verbosity level is high enough, and an optional verbosity |
| 329 | threshold. |
| 330 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 331 | |
| 332 | # Generate a message if we weren't passed one |
| 333 | if msg is None: |
| 334 | msg = "%s %s" % (func.__name__, `args`) |
| 335 | if msg[-2:] == ',)': # correct for singleton tuple |
| 336 | msg = msg[0:-2] + ')' |
| 337 | |
| 338 | # Print it if verbosity level is high enough |
| 339 | self.announce (msg, level) |
| 340 | |
| 341 | # And do it, as long as we're not in dry-run mode |
| 342 | if not self.dry_run: |
| 343 | apply (func, args) |
| 344 | |
| 345 | # execute() |
| 346 | |
| 347 | |
| 348 | def mkpath (self, name, mode=0777): |
| 349 | util.mkpath (name, mode, |
| 350 | self.verbose, self.dry_run) |
| 351 | |
| 352 | |
| 353 | def copy_file (self, infile, outfile, |
| 354 | preserve_mode=1, preserve_times=1, link=None, level=1): |
Greg Ward | e9613ae | 2000-04-10 01:30:44 +0000 | [diff] [blame] | 355 | """Copy a file respecting verbose, dry-run and force flags. (The |
| 356 | former two default to whatever is in the Distribution object, and |
| 357 | the latter defaults to false for commands that don't define it.)""" |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 358 | |
| 359 | return util.copy_file (infile, outfile, |
| 360 | preserve_mode, preserve_times, |
| 361 | not self.force, |
| 362 | link, |
| 363 | self.verbose >= level, |
| 364 | self.dry_run) |
| 365 | |
| 366 | |
| 367 | def copy_tree (self, infile, outfile, |
| 368 | preserve_mode=1, preserve_times=1, preserve_symlinks=0, |
| 369 | level=1): |
| 370 | """Copy an entire directory tree respecting verbose, dry-run, |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 371 | and force flags. |
| 372 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 373 | return util.copy_tree (infile, outfile, |
| 374 | preserve_mode,preserve_times,preserve_symlinks, |
| 375 | not self.force, |
| 376 | self.verbose >= level, |
| 377 | self.dry_run) |
| 378 | |
| 379 | |
| 380 | def move_file (self, src, dst, level=1): |
| 381 | """Move a file respecting verbose and dry-run flags.""" |
| 382 | return util.move_file (src, dst, |
| 383 | self.verbose >= level, |
| 384 | self.dry_run) |
| 385 | |
| 386 | |
| 387 | def spawn (self, cmd, search_path=1, level=1): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 388 | """Spawn an external command respecting verbose and dry-run flags.""" |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 389 | from distutils.spawn import spawn |
| 390 | spawn (cmd, search_path, |
| 391 | self.verbose >= level, |
| 392 | self.dry_run) |
| 393 | |
| 394 | |
| 395 | def make_archive (self, base_name, format, |
| 396 | root_dir=None, base_dir=None): |
Greg Ward | 308acf0 | 2000-06-01 01:08:52 +0000 | [diff] [blame] | 397 | return util.make_archive (base_name, format, root_dir, base_dir, |
| 398 | self.verbose, self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 399 | |
| 400 | |
| 401 | def make_file (self, infiles, outfile, func, args, |
Greg Ward | 68a0757 | 2000-04-10 00:18:16 +0000 | [diff] [blame] | 402 | exec_msg=None, skip_msg=None, level=1): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 403 | """Special case of 'execute()' for operations that process one or |
Greg Ward | 68a0757 | 2000-04-10 00:18:16 +0000 | [diff] [blame] | 404 | more input files and generate one output file. Works just like |
| 405 | 'execute()', except the operation is skipped and a different |
| 406 | message printed if 'outfile' already exists and is newer than all |
| 407 | files listed in 'infiles'. If the command defined 'self.force', |
| 408 | and it is true, then the command is unconditionally run -- does no |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 409 | timestamp checks. |
| 410 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 411 | if exec_msg is None: |
| 412 | exec_msg = "generating %s from %s" % \ |
| 413 | (outfile, string.join (infiles, ', ')) |
| 414 | if skip_msg is None: |
| 415 | skip_msg = "skipping %s (inputs unchanged)" % outfile |
| 416 | |
| 417 | |
| 418 | # Allow 'infiles' to be a single string |
| 419 | if type (infiles) is StringType: |
| 420 | infiles = (infiles,) |
| 421 | elif type (infiles) not in (ListType, TupleType): |
| 422 | raise TypeError, \ |
| 423 | "'infiles' must be a string, or a list or tuple of strings" |
| 424 | |
| 425 | # If 'outfile' must be regenerated (either because it doesn't |
| 426 | # exist, is out-of-date, or the 'force' flag is true) then |
| 427 | # perform the action that presumably regenerates it |
Greg Ward | e9613ae | 2000-04-10 01:30:44 +0000 | [diff] [blame] | 428 | if self.force or util.newer_group (infiles, outfile): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 429 | self.execute (func, args, exec_msg, level) |
| 430 | |
| 431 | # Otherwise, print the "skip" message |
| 432 | else: |
| 433 | self.announce (skip_msg, level) |
| 434 | |
| 435 | # make_file () |
| 436 | |
| 437 | # class Command |
Greg Ward | b361233 | 2000-04-09 03:48:37 +0000 | [diff] [blame] | 438 | |
| 439 | |
Greg Ward | 029e302 | 2000-05-25 01:26:23 +0000 | [diff] [blame] | 440 | # XXX 'install_misc' class not currently used -- it was the base class for |
| 441 | # both 'install_scripts' and 'install_data', but they outgrew it. It might |
| 442 | # still be useful for 'install_headers', though, so I'm keeping it around |
| 443 | # for the time being. |
| 444 | |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 445 | class install_misc (Command): |
| 446 | """Common base class for installing some files in a subdirectory. |
| 447 | Currently used by install_data and install_scripts. |
| 448 | """ |
| 449 | |
| 450 | user_options = [('install-dir=', 'd', "directory to install the files to")] |
| 451 | |
| 452 | def initialize_options (self): |
| 453 | self.install_dir = None |
Gregory P. Smith | 21b9e91 | 2000-05-13 03:10:30 +0000 | [diff] [blame] | 454 | self.outfiles = [] |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 455 | |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 456 | def _install_dir_from (self, dirname): |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 457 | self.set_undefined_options('install', (dirname, 'install_dir')) |
| 458 | |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 459 | def _copy_files (self, filelist): |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 460 | self.outfiles = [] |
| 461 | if not filelist: |
| 462 | return |
| 463 | self.mkpath(self.install_dir) |
| 464 | for f in filelist: |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 465 | self.copy_file(f, self.install_dir) |
| 466 | self.outfiles.append(os.path.join(self.install_dir, f)) |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 467 | |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 468 | def get_outputs (self): |
| 469 | return self.outfiles |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 470 | |
| 471 | |
Greg Ward | b361233 | 2000-04-09 03:48:37 +0000 | [diff] [blame] | 472 | if __name__ == "__main__": |
| 473 | print "ok" |