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 | |
Martin v. Löwis | 5a6601c | 2004-11-10 22:23:15 +0000 | [diff] [blame] | 7 | # This module should be kept compatible with Python 2.1. |
Andrew M. Kuchling | d448f66 | 2002-11-19 13:12:28 +0000 | [diff] [blame] | 8 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 9 | __revision__ = "$Id$" |
| 10 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 11 | import sys, os, re |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 12 | from distutils.errors import * |
Greg Ward | 29124ff | 2000-08-13 00:36:47 +0000 | [diff] [blame] | 13 | from distutils import util, dir_util, file_util, archive_util, dep_util |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 14 | from distutils import log |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 15 | |
| 16 | class Command: |
| 17 | """Abstract base class for defining command classes, the "worker bees" |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 18 | 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 31 | |
Greg Ward | b3e0ad9 | 2000-09-16 15:09:17 +0000 | [diff] [blame] | 32 | # '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 Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 42 | # |
Greg Ward | b3e0ad9 | 2000-09-16 15:09:17 +0000 | [diff] [blame] | 43 | # '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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 49 | # -- Creation/initialization methods ------------------------------- |
| 50 | |
| 51 | def __init__ (self, dist): |
| 52 | """Create and initialize a new Command object. Most importantly, |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 53 | invokes the 'initialize_options()' method, which is the real |
| 54 | initializer and depends on the actual command being |
| 55 | instantiated. |
| 56 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 57 | # late import because of mutual dependence between these classes |
| 58 | from distutils.dist import Distribution |
| 59 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 60 | if not isinstance(dist, Distribution): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 61 | 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 Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 66 | self.initialize_options() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 67 | |
| 68 | # Per-command versions of the global flags, so that the user can |
| 69 | # customize Distutils' behaviour command-by-command and let some |
Fred Drake | 2b2fe94 | 2004-06-18 21:28:28 +0000 | [diff] [blame] | 70 | # commands fall back on the Distribution's behaviour. None means |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 71 | # "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 Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 73 | # 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 76 | self._dry_run = None |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 77 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 78 | # verbose is largely ignored, but needs to be set for |
| 79 | # backwards compatibility (I think)? |
| 80 | self.verbose = dist.verbose |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 81 | |
Greg Ward | d197a3a | 2000-04-10 13:11:51 +0000 | [diff] [blame] | 82 | # 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 88 | # 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 Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 92 | # 'finalized' records whether or not 'finalize_options()' has been |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 93 | # called. 'finalize_options()' itself should not pay attention to |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 94 | # this flag: it is the business of 'ensure_finalized()', which |
| 95 | # always calls 'finalize_options()', to respect/update it. |
| 96 | self.finalized = 0 |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 97 | |
| 98 | # __init__ () |
| 99 | |
| 100 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 101 | # XXX A more explicit way to customize dry_run would be better. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 102 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 103 | def __getattr__ (self, attr): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 104 | if attr == 'dry_run': |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 105 | myval = getattr(self, "_" + attr) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 106 | if myval is None: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 107 | return getattr(self.distribution, attr) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 108 | else: |
| 109 | return myval |
| 110 | else: |
| 111 | raise AttributeError, attr |
| 112 | |
| 113 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 114 | def ensure_finalized (self): |
| 115 | if not self.finalized: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 116 | self.finalize_options() |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 117 | self.finalized = 1 |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 118 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 119 | |
| 120 | # Subclasses must define: |
| 121 | # initialize_options() |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 122 | # 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 125 | # 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 Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 135 | 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 Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 140 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 141 | This method must be implemented by all command classes. |
| 142 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 143 | raise RuntimeError, \ |
| 144 | "abstract method -- subclass %s must override" % self.__class__ |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 145 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 146 | def finalize_options (self): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 147 | """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örwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 150 | done. Thus, this is the place to code option dependencies: if |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 151 | '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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 154 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 155 | This method must be implemented by all command classes. |
| 156 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 157 | raise RuntimeError, \ |
| 158 | "abstract method -- subclass %s must override" % self.__class__ |
| 159 | |
Greg Ward | adda156 | 2000-05-28 23:54:00 +0000 | [diff] [blame] | 160 | |
| 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 Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 165 | print(indent + header) |
Greg Ward | adda156 | 2000-05-28 23:54:00 +0000 | [diff] [blame] | 166 | indent = indent + " " |
| 167 | for (option, _, _) in self.user_options: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 168 | option = option.translate(longopt_xlate) |
Greg Ward | adda156 | 2000-05-28 23:54:00 +0000 | [diff] [blame] | 169 | if option[-1] == "=": |
| 170 | option = option[:-1] |
| 171 | value = getattr(self, option) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 172 | print(indent + "%s = %s" % (option, value)) |
Greg Ward | adda156 | 2000-05-28 23:54:00 +0000 | [diff] [blame] | 173 | |
| 174 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 175 | def run (self): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 176 | """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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 182 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 183 | This method must be implemented by all command classes. |
| 184 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 185 | |
| 186 | raise RuntimeError, \ |
| 187 | "abstract method -- subclass %s must override" % self.__class__ |
| 188 | |
| 189 | def announce (self, msg, level=1): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 190 | """If the current verbosity level is of greater than or equal to |
| 191 | 'level' print 'msg' to stdout. |
| 192 | """ |
Guido van Rossum | af16065 | 2003-02-20 02:10:08 +0000 | [diff] [blame] | 193 | log.log(level, msg) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 194 | |
Greg Ward | ebec02a | 2000-06-08 00:02:36 +0000 | [diff] [blame] | 195 | 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 Hylton | fcd7353 | 2002-09-11 16:31:53 +0000 | [diff] [blame] | 199 | from distutils.debug import DEBUG |
Greg Ward | ebec02a | 2000-06-08 00:02:36 +0000 | [diff] [blame] | 200 | if DEBUG: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 201 | print(msg) |
Neil Schemenauer | 69374e4 | 2001-08-29 23:57:22 +0000 | [diff] [blame] | 202 | sys.stdout.flush() |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 203 | |
Greg Ward | ebec02a | 2000-06-08 00:02:36 +0000 | [diff] [blame] | 204 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 205 | |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 206 | # -- Option validation methods ------------------------------------- |
| 207 | # (these are very handy in writing the 'finalize_options()' method) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 208 | # |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 209 | # 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 Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 224 | elif not isinstance(val, basestring): |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 225 | 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 Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 244 | elif isinstance(val, basestring): |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 245 | setattr(self, option, re.split(r',\s*|\s+', val)) |
| 246 | else: |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 247 | if isinstance(val, list): |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 248 | ok = all(isinstance(v, basestring) for v in val) |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 249 | else: |
| 250 | ok = 0 |
| 251 | |
| 252 | if not ok: |
| 253 | raise DistutilsOptionError, \ |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 254 | "'%s' must be a list of strings (got %r)" % \ |
| 255 | (option, val) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 256 | |
Greg Ward | 31413a7 | 2000-06-04 14:21:28 +0000 | [diff] [blame] | 257 | 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 276 | # -- Convenience methods for commands ------------------------------ |
| 277 | |
| 278 | def get_command_name (self): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 279 | if hasattr(self, 'command_name'): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 280 | 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 Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 287 | 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 299 | |
| 300 | # Option_pairs: list of (src_option, dst_option) tuples |
| 301 | |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 302 | src_cmd_obj = self.distribution.get_command_obj(src_cmd) |
| 303 | src_cmd_obj.ensure_finalized() |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 304 | for (src_option, dst_option) in option_pairs: |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 305 | if getattr(self, dst_option) is None: |
| 306 | setattr(self, dst_option, |
| 307 | getattr(src_cmd_obj, src_option)) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 308 | |
| 309 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 310 | def get_finalized_command (self, command, create=1): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 311 | """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 Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 316 | cmd_obj = self.distribution.get_command_obj(command, create) |
| 317 | cmd_obj.ensure_finalized() |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 318 | return cmd_obj |
| 319 | |
Greg Ward | 308acf0 | 2000-06-01 01:08:52 +0000 | [diff] [blame] | 320 | # XXX rename to 'get_reinitialized_command()'? (should do the |
| 321 | # same in dist.py, if so) |
Greg Ward | ecce145 | 2000-09-16 15:25:55 +0000 | [diff] [blame] | 322 | def reinitialize_command (self, command, reinit_subcommands=0): |
| 323 | return self.distribution.reinitialize_command( |
| 324 | command, reinit_subcommands) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 325 | |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 326 | def run_command (self, command): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 327 | """Run some other command: uses the 'run_command()' method of |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 328 | Distribution, which creates and finalizes the command object if |
| 329 | necessary and then invokes its 'run()' method. |
| 330 | """ |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 331 | self.distribution.run_command(command) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 332 | |
| 333 | |
Greg Ward | b3e0ad9 | 2000-09-16 15:09:17 +0000 | [diff] [blame] | 334 | 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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 348 | # -- External world manipulation ----------------------------------- |
| 349 | |
| 350 | def warn (self, msg): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 351 | sys.stderr.write("warning: %s: %s\n" % |
| 352 | (self.get_command_name(), msg)) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 353 | |
| 354 | |
| 355 | def execute (self, func, args, msg=None, level=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 356 | util.execute(func, args, msg, dry_run=self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 357 | |
| 358 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 359 | def mkpath (self, name, mode=0o777): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 360 | dir_util.mkpath(name, mode, dry_run=self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 361 | |
| 362 | |
| 363 | def copy_file (self, infile, outfile, |
| 364 | preserve_mode=1, preserve_times=1, link=None, level=1): |
Greg Ward | e9613ae | 2000-04-10 01:30:44 +0000 | [diff] [blame] | 365 | """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 Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 368 | |
Greg Ward | 29124ff | 2000-08-13 00:36:47 +0000 | [diff] [blame] | 369 | return file_util.copy_file( |
| 370 | infile, outfile, |
| 371 | preserve_mode, preserve_times, |
| 372 | not self.force, |
| 373 | link, |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 374 | dry_run=self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 375 | |
| 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 Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 381 | and force flags. |
| 382 | """ |
Greg Ward | 29124ff | 2000-08-13 00:36:47 +0000 | [diff] [blame] | 383 | return dir_util.copy_tree( |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 384 | infile, outfile, |
Greg Ward | 29124ff | 2000-08-13 00:36:47 +0000 | [diff] [blame] | 385 | preserve_mode,preserve_times,preserve_symlinks, |
| 386 | not self.force, |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 387 | dry_run=self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 388 | |
| 389 | def move_file (self, src, dst, level=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 390 | """Move a file respectin dry-run flag.""" |
| 391 | return file_util.move_file(src, dst, dry_run = self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 392 | |
| 393 | def spawn (self, cmd, search_path=1, level=1): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 394 | """Spawn an external command respecting dry-run flag.""" |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 395 | from distutils.spawn import spawn |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 396 | spawn(cmd, search_path, dry_run= self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 397 | |
| 398 | def make_archive (self, base_name, format, |
| 399 | root_dir=None, base_dir=None): |
Greg Ward | 29124ff | 2000-08-13 00:36:47 +0000 | [diff] [blame] | 400 | return archive_util.make_archive( |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 401 | base_name, format, root_dir, base_dir, dry_run=self.dry_run) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 402 | |
| 403 | |
| 404 | def make_file (self, infiles, outfile, func, args, |
Greg Ward | 68a0757 | 2000-04-10 00:18:16 +0000 | [diff] [blame] | 405 | exec_msg=None, skip_msg=None, level=1): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 406 | """Special case of 'execute()' for operations that process one or |
Greg Ward | 68a0757 | 2000-04-10 00:18:16 +0000 | [diff] [blame] | 407 | 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 Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 412 | timestamp checks. |
| 413 | """ |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 414 | if exec_msg is None: |
| 415 | exec_msg = "generating %s from %s" % \ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 416 | (outfile, ', '.join(infiles)) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 417 | if skip_msg is None: |
| 418 | skip_msg = "skipping %s (inputs unchanged)" % outfile |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 419 | |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 420 | |
| 421 | # Allow 'infiles' to be a single string |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 422 | if isinstance(infiles, basestring): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 423 | infiles = (infiles,) |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 424 | elif not isinstance(infiles, (list, tuple)): |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 425 | 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 Ward | 29124ff | 2000-08-13 00:36:47 +0000 | [diff] [blame] | 431 | if self.force or dep_util.newer_group (infiles, outfile): |
Greg Ward | 071ed76 | 2000-09-26 02:12:31 +0000 | [diff] [blame] | 432 | self.execute(func, args, exec_msg, level) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 433 | |
| 434 | # Otherwise, print the "skip" message |
| 435 | else: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 436 | log.debug(skip_msg) |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 437 | |
| 438 | # make_file () |
| 439 | |
| 440 | # class Command |
Greg Ward | b361233 | 2000-04-09 03:48:37 +0000 | [diff] [blame] | 441 | |
| 442 | |
Greg Ward | 029e302 | 2000-05-25 01:26:23 +0000 | [diff] [blame] | 443 | # 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. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 448 | class 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 Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 452 | |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 453 | user_options = [('install-dir=', 'd', "directory to install the files to")] |
| 454 | |
| 455 | def initialize_options (self): |
| 456 | self.install_dir = None |
Gregory P. Smith | 21b9e91 | 2000-05-13 03:10:30 +0000 | [diff] [blame] | 457 | self.outfiles = [] |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 458 | |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 459 | def _install_dir_from (self, dirname): |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 460 | self.set_undefined_options('install', (dirname, 'install_dir')) |
| 461 | |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 462 | def _copy_files (self, filelist): |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 463 | self.outfiles = [] |
| 464 | if not filelist: |
| 465 | return |
| 466 | self.mkpath(self.install_dir) |
| 467 | for f in filelist: |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 468 | self.copy_file(f, self.install_dir) |
| 469 | self.outfiles.append(os.path.join(self.install_dir, f)) |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 470 | |
Gregory P. Smith | ce2b6b8 | 2000-05-12 01:31:37 +0000 | [diff] [blame] | 471 | def get_outputs (self): |
| 472 | return self.outfiles |
Gregory P. Smith | b2e3bb3 | 2000-05-12 00:52:23 +0000 | [diff] [blame] | 473 | |
| 474 | |
Greg Ward | b361233 | 2000-04-09 03:48:37 +0000 | [diff] [blame] | 475 | if __name__ == "__main__": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 476 | print("ok") |