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