| 1 | n/a | """Base class for commands.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import re |
|---|
| 5 | n/a | from shutil import copyfile, move, make_archive |
|---|
| 6 | n/a | from packaging import util |
|---|
| 7 | n/a | from packaging import logger |
|---|
| 8 | n/a | from packaging.errors import PackagingOptionError |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | class Command: |
|---|
| 12 | n/a | """Abstract base class for defining command classes, the "worker bees" |
|---|
| 13 | n/a | of Packaging. A useful analogy for command classes is to think of |
|---|
| 14 | n/a | them as subroutines with local variables called "options". The options |
|---|
| 15 | n/a | are "declared" in 'initialize_options()' and "defined" (given their |
|---|
| 16 | n/a | final values, aka "finalized") in 'finalize_options()', both of which |
|---|
| 17 | n/a | must be defined by every command class. The distinction between the |
|---|
| 18 | n/a | two is necessary because option values might come from the outside |
|---|
| 19 | n/a | world (command line, config file, ...), and any options dependent on |
|---|
| 20 | n/a | other options must be computed *after* these outside influences have |
|---|
| 21 | n/a | been processed -- hence 'finalize_options()'. The "body" of the |
|---|
| 22 | n/a | subroutine, where it does all its work based on the values of its |
|---|
| 23 | n/a | options, is the 'run()' method, which must also be implemented by every |
|---|
| 24 | n/a | command class. |
|---|
| 25 | n/a | """ |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | # 'sub_commands' formalizes the notion of a "family" of commands, |
|---|
| 28 | n/a | # eg. "install_dist" as the parent with sub-commands "install_lib", |
|---|
| 29 | n/a | # "install_headers", etc. The parent of a family of commands |
|---|
| 30 | n/a | # defines 'sub_commands' as a class attribute; it's a list of |
|---|
| 31 | n/a | # (command_name : string, predicate : unbound_method | string | None) |
|---|
| 32 | n/a | # tuples, where 'predicate' is a method of the parent command that |
|---|
| 33 | n/a | # determines whether the corresponding command is applicable in the |
|---|
| 34 | n/a | # current situation. (Eg. we "install_headers" is only applicable if |
|---|
| 35 | n/a | # we have any C header files to install.) If 'predicate' is None, |
|---|
| 36 | n/a | # that command is always applicable. |
|---|
| 37 | n/a | # |
|---|
| 38 | n/a | # 'sub_commands' is usually defined at the *end* of a class, because |
|---|
| 39 | n/a | # predicates can be unbound methods, so they must already have been |
|---|
| 40 | n/a | # defined. The canonical example is the "install_dist" command. |
|---|
| 41 | n/a | sub_commands = [] |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | # Pre and post command hooks are run just before or just after the command |
|---|
| 44 | n/a | # itself. They are simple functions that receive the command instance. They |
|---|
| 45 | n/a | # are specified as callable objects or dotted strings (for lazy loading). |
|---|
| 46 | n/a | pre_hook = None |
|---|
| 47 | n/a | post_hook = None |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | # -- Creation/initialization methods ------------------------------- |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def __init__(self, dist): |
|---|
| 52 | n/a | """Create and initialize a new Command object. Most importantly, |
|---|
| 53 | n/a | invokes the 'initialize_options()' method, which is the real |
|---|
| 54 | n/a | initializer and depends on the actual command being instantiated. |
|---|
| 55 | n/a | """ |
|---|
| 56 | n/a | # late import because of mutual dependence between these classes |
|---|
| 57 | n/a | from packaging.dist import Distribution |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | if not isinstance(dist, Distribution): |
|---|
| 60 | n/a | raise TypeError("dist must be an instance of Distribution, not %r" |
|---|
| 61 | n/a | % type(dist)) |
|---|
| 62 | n/a | if self.__class__ is Command: |
|---|
| 63 | n/a | raise RuntimeError("Command is an abstract class") |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | self.distribution = dist |
|---|
| 66 | n/a | self.initialize_options() |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | # Per-command versions of the global flags, so that the user can |
|---|
| 69 | n/a | # customize Packaging' behaviour command-by-command and let some |
|---|
| 70 | n/a | # commands fall back on the Distribution's behaviour. None means |
|---|
| 71 | n/a | # "not defined, check self.distribution's copy", while 0 or 1 mean |
|---|
| 72 | n/a | # false and true (duh). Note that this means figuring out the real |
|---|
| 73 | n/a | # value of each flag is a touch complicated -- hence "self._dry_run" |
|---|
| 74 | n/a | # will be handled by a property, below. |
|---|
| 75 | n/a | # XXX This needs to be fixed. [I changed it to a property--does that |
|---|
| 76 | n/a | # "fix" it?] |
|---|
| 77 | n/a | self._dry_run = None |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | # Some commands define a 'self.force' option to ignore file |
|---|
| 80 | n/a | # timestamps, but methods defined *here* assume that |
|---|
| 81 | n/a | # 'self.force' exists for all commands. So define it here |
|---|
| 82 | n/a | # just to be safe. |
|---|
| 83 | n/a | self.force = None |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | # The 'help' flag is just used for command line parsing, so |
|---|
| 86 | n/a | # none of that complicated bureaucracy is needed. |
|---|
| 87 | n/a | self.help = False |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # 'finalized' records whether or not 'finalize_options()' has been |
|---|
| 90 | n/a | # called. 'finalize_options()' itself should not pay attention to |
|---|
| 91 | n/a | # this flag: it is the business of 'ensure_finalized()', which |
|---|
| 92 | n/a | # always calls 'finalize_options()', to respect/update it. |
|---|
| 93 | n/a | self.finalized = False |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | # XXX A more explicit way to customize dry_run would be better. |
|---|
| 96 | n/a | @property |
|---|
| 97 | n/a | def dry_run(self): |
|---|
| 98 | n/a | if self._dry_run is None: |
|---|
| 99 | n/a | return getattr(self.distribution, 'dry_run') |
|---|
| 100 | n/a | else: |
|---|
| 101 | n/a | return self._dry_run |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | def ensure_finalized(self): |
|---|
| 104 | n/a | if not self.finalized: |
|---|
| 105 | n/a | self.finalize_options() |
|---|
| 106 | n/a | self.finalized = True |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | # Subclasses must define: |
|---|
| 109 | n/a | # initialize_options() |
|---|
| 110 | n/a | # provide default values for all options; may be customized by |
|---|
| 111 | n/a | # setup script, by options from config file(s), or by command-line |
|---|
| 112 | n/a | # options |
|---|
| 113 | n/a | # finalize_options() |
|---|
| 114 | n/a | # decide on the final values for all options; this is called |
|---|
| 115 | n/a | # after all possible intervention from the outside world |
|---|
| 116 | n/a | # (command line, option file, etc.) has been processed |
|---|
| 117 | n/a | # run() |
|---|
| 118 | n/a | # run the command: do whatever it is we're here to do, |
|---|
| 119 | n/a | # controlled by the command's various option values |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def initialize_options(self): |
|---|
| 122 | n/a | """Set default values for all the options that this command |
|---|
| 123 | n/a | supports. Note that these defaults may be overridden by other |
|---|
| 124 | n/a | commands, by the setup script, by config files, or by the |
|---|
| 125 | n/a | command line. Thus, this is not the place to code dependencies |
|---|
| 126 | n/a | between options; generally, 'initialize_options()' implementations |
|---|
| 127 | n/a | are just a bunch of "self.foo = None" assignments. |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | This method must be implemented by all command classes. |
|---|
| 130 | n/a | """ |
|---|
| 131 | n/a | raise RuntimeError( |
|---|
| 132 | n/a | "abstract method -- subclass %s must override" % self.__class__) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def finalize_options(self): |
|---|
| 135 | n/a | """Set final values for all the options that this command supports. |
|---|
| 136 | n/a | This is always called as late as possible, ie. after any option |
|---|
| 137 | n/a | assignments from the command line or from other commands have been |
|---|
| 138 | n/a | done. Thus, this is the place to code option dependencies: if |
|---|
| 139 | n/a | 'foo' depends on 'bar', then it is safe to set 'foo' from 'bar' as |
|---|
| 140 | n/a | long as 'foo' still has the same value it was assigned in |
|---|
| 141 | n/a | 'initialize_options()'. |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | This method must be implemented by all command classes. |
|---|
| 144 | n/a | """ |
|---|
| 145 | n/a | raise RuntimeError( |
|---|
| 146 | n/a | "abstract method -- subclass %s must override" % self.__class__) |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | def dump_options(self, header=None, indent=""): |
|---|
| 149 | n/a | if header is None: |
|---|
| 150 | n/a | header = "command options for '%s':" % self.get_command_name() |
|---|
| 151 | n/a | logger.info(indent + header) |
|---|
| 152 | n/a | indent = indent + " " |
|---|
| 153 | n/a | negative_opt = getattr(self, 'negative_opt', ()) |
|---|
| 154 | n/a | for option, _, _ in self.user_options: |
|---|
| 155 | n/a | if option in negative_opt: |
|---|
| 156 | n/a | continue |
|---|
| 157 | n/a | option = option.replace('-', '_') |
|---|
| 158 | n/a | if option[-1] == "=": |
|---|
| 159 | n/a | option = option[:-1] |
|---|
| 160 | n/a | value = getattr(self, option) |
|---|
| 161 | n/a | logger.info(indent + "%s = %s", option, value) |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | def run(self): |
|---|
| 164 | n/a | """A command's raison d'etre: carry out the action it exists to |
|---|
| 165 | n/a | perform, controlled by the options initialized in |
|---|
| 166 | n/a | 'initialize_options()', customized by other commands, the setup |
|---|
| 167 | n/a | script, the command line and config files, and finalized in |
|---|
| 168 | n/a | 'finalize_options()'. All terminal output and filesystem |
|---|
| 169 | n/a | interaction should be done by 'run()'. |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | This method must be implemented by all command classes. |
|---|
| 172 | n/a | """ |
|---|
| 173 | n/a | raise RuntimeError( |
|---|
| 174 | n/a | "abstract method -- subclass %s must override" % self.__class__) |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | # -- External interface -------------------------------------------- |
|---|
| 177 | n/a | # (called by outsiders) |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def get_source_files(self): |
|---|
| 180 | n/a | """Return the list of files that are used as inputs to this command, |
|---|
| 181 | n/a | i.e. the files used to generate the output files. The result is used |
|---|
| 182 | n/a | by the `sdist` command in determining the set of default files. |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | Command classes should implement this method if they operate on files |
|---|
| 185 | n/a | from the source tree. |
|---|
| 186 | n/a | """ |
|---|
| 187 | n/a | return [] |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | def get_outputs(self): |
|---|
| 190 | n/a | """Return the list of files that would be produced if this command |
|---|
| 191 | n/a | were actually run. Not affected by the "dry-run" flag or whether |
|---|
| 192 | n/a | any other commands have been run. |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | Command classes should implement this method if they produce any |
|---|
| 195 | n/a | output files that get consumed by another command. e.g., `build_ext` |
|---|
| 196 | n/a | returns the list of built extension modules, but not any temporary |
|---|
| 197 | n/a | files used in the compilation process. |
|---|
| 198 | n/a | """ |
|---|
| 199 | n/a | return [] |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | # -- Option validation methods ------------------------------------- |
|---|
| 202 | n/a | # (these are very handy in writing the 'finalize_options()' method) |
|---|
| 203 | n/a | # |
|---|
| 204 | n/a | # NB. the general philosophy here is to ensure that a particular option |
|---|
| 205 | n/a | # value meets certain type and value constraints. If not, we try to |
|---|
| 206 | n/a | # force it into conformance (eg. if we expect a list but have a string, |
|---|
| 207 | n/a | # split the string on comma and/or whitespace). If we can't force the |
|---|
| 208 | n/a | # option into conformance, raise PackagingOptionError. Thus, command |
|---|
| 209 | n/a | # classes need do nothing more than (eg.) |
|---|
| 210 | n/a | # self.ensure_string_list('foo') |
|---|
| 211 | n/a | # and they can be guaranteed that thereafter, self.foo will be |
|---|
| 212 | n/a | # a list of strings. |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | def _ensure_stringlike(self, option, what, default=None): |
|---|
| 215 | n/a | val = getattr(self, option) |
|---|
| 216 | n/a | if val is None: |
|---|
| 217 | n/a | setattr(self, option, default) |
|---|
| 218 | n/a | return default |
|---|
| 219 | n/a | elif not isinstance(val, str): |
|---|
| 220 | n/a | raise PackagingOptionError("'%s' must be a %s (got `%s`)" % |
|---|
| 221 | n/a | (option, what, val)) |
|---|
| 222 | n/a | return val |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | def ensure_string(self, option, default=None): |
|---|
| 225 | n/a | """Ensure that 'option' is a string; if not defined, set it to |
|---|
| 226 | n/a | 'default'. |
|---|
| 227 | n/a | """ |
|---|
| 228 | n/a | self._ensure_stringlike(option, "string", default) |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | def ensure_string_list(self, option): |
|---|
| 231 | n/a | r"""Ensure that 'option' is a list of strings. If 'option' is |
|---|
| 232 | n/a | currently a string, we split it either on /,\s*/ or /\s+/, so |
|---|
| 233 | n/a | "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become |
|---|
| 234 | n/a | ["foo", "bar", "baz"]. |
|---|
| 235 | n/a | """ |
|---|
| 236 | n/a | val = getattr(self, option) |
|---|
| 237 | n/a | if val is None: |
|---|
| 238 | n/a | return |
|---|
| 239 | n/a | elif isinstance(val, str): |
|---|
| 240 | n/a | setattr(self, option, re.split(r',\s*|\s+', val)) |
|---|
| 241 | n/a | else: |
|---|
| 242 | n/a | if isinstance(val, list): |
|---|
| 243 | n/a | # checks if all elements are str |
|---|
| 244 | n/a | ok = True |
|---|
| 245 | n/a | for element in val: |
|---|
| 246 | n/a | if not isinstance(element, str): |
|---|
| 247 | n/a | ok = False |
|---|
| 248 | n/a | break |
|---|
| 249 | n/a | else: |
|---|
| 250 | n/a | ok = False |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | if not ok: |
|---|
| 253 | n/a | raise PackagingOptionError( |
|---|
| 254 | n/a | "'%s' must be a list of strings (got %r)" % (option, val)) |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | def _ensure_tested_string(self, option, tester, |
|---|
| 257 | n/a | what, error_fmt, default=None): |
|---|
| 258 | n/a | val = self._ensure_stringlike(option, what, default) |
|---|
| 259 | n/a | if val is not None and not tester(val): |
|---|
| 260 | n/a | raise PackagingOptionError( |
|---|
| 261 | n/a | ("error in '%s' option: " + error_fmt) % (option, val)) |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | def ensure_filename(self, option): |
|---|
| 264 | n/a | """Ensure that 'option' is the name of an existing file.""" |
|---|
| 265 | n/a | self._ensure_tested_string(option, os.path.isfile, |
|---|
| 266 | n/a | "filename", |
|---|
| 267 | n/a | "'%s' does not exist or is not a file") |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def ensure_dirname(self, option): |
|---|
| 270 | n/a | self._ensure_tested_string(option, os.path.isdir, |
|---|
| 271 | n/a | "directory name", |
|---|
| 272 | n/a | "'%s' does not exist or is not a directory") |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | # -- Convenience methods for commands ------------------------------ |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | @classmethod |
|---|
| 277 | n/a | def get_command_name(cls): |
|---|
| 278 | n/a | if hasattr(cls, 'command_name'): |
|---|
| 279 | n/a | return cls.command_name |
|---|
| 280 | n/a | else: |
|---|
| 281 | n/a | return cls.__name__ |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | def set_undefined_options(self, src_cmd, *options): |
|---|
| 284 | n/a | """Set values of undefined options from another command. |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | Undefined options are options set to None, which is the convention |
|---|
| 287 | n/a | used to indicate that an option has not been changed between |
|---|
| 288 | n/a | 'initialize_options()' and 'finalize_options()'. This method is |
|---|
| 289 | n/a | usually called from 'finalize_options()' for options that depend on |
|---|
| 290 | n/a | some other command rather than another option of the same command, |
|---|
| 291 | n/a | typically subcommands. |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | The 'src_cmd' argument is the other command from which option values |
|---|
| 294 | n/a | will be taken (a command object will be created for it if necessary); |
|---|
| 295 | n/a | the remaining positional arguments are strings that give the name of |
|---|
| 296 | n/a | the option to set. If the name is different on the source and target |
|---|
| 297 | n/a | command, you can pass a tuple with '(name_on_source, name_on_dest)' so |
|---|
| 298 | n/a | that 'self.name_on_dest' will be set from 'src_cmd.name_on_source'. |
|---|
| 299 | n/a | """ |
|---|
| 300 | n/a | src_cmd_obj = self.distribution.get_command_obj(src_cmd) |
|---|
| 301 | n/a | src_cmd_obj.ensure_finalized() |
|---|
| 302 | n/a | for obj in options: |
|---|
| 303 | n/a | if isinstance(obj, tuple): |
|---|
| 304 | n/a | src_option, dst_option = obj |
|---|
| 305 | n/a | else: |
|---|
| 306 | n/a | src_option, dst_option = obj, obj |
|---|
| 307 | n/a | if getattr(self, dst_option) is None: |
|---|
| 308 | n/a | setattr(self, dst_option, |
|---|
| 309 | n/a | getattr(src_cmd_obj, src_option)) |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | def get_finalized_command(self, command, create=True): |
|---|
| 312 | n/a | """Wrapper around Distribution's 'get_command_obj()' method: find |
|---|
| 313 | n/a | (create if necessary and 'create' is true) the command object for |
|---|
| 314 | n/a | 'command', call its 'ensure_finalized()' method, and return the |
|---|
| 315 | n/a | finalized command object. |
|---|
| 316 | n/a | """ |
|---|
| 317 | n/a | cmd_obj = self.distribution.get_command_obj(command, create) |
|---|
| 318 | n/a | cmd_obj.ensure_finalized() |
|---|
| 319 | n/a | return cmd_obj |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | def reinitialize_command(self, command, reinit_subcommands=False): |
|---|
| 322 | n/a | return self.distribution.reinitialize_command( |
|---|
| 323 | n/a | command, reinit_subcommands) |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | def run_command(self, command): |
|---|
| 326 | n/a | """Run some other command: uses the 'run_command()' method of |
|---|
| 327 | n/a | Distribution, which creates and finalizes the command object if |
|---|
| 328 | n/a | necessary and then invokes its 'run()' method. |
|---|
| 329 | n/a | """ |
|---|
| 330 | n/a | self.distribution.run_command(command) |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | def get_sub_commands(self): |
|---|
| 333 | n/a | """Determine the sub-commands that are relevant in the current |
|---|
| 334 | n/a | distribution (ie., that need to be run). This is based on the |
|---|
| 335 | n/a | 'sub_commands' class attribute: each tuple in that list may include |
|---|
| 336 | n/a | a method that we call to determine if the subcommand needs to be |
|---|
| 337 | n/a | run for the current distribution. Return a list of command names. |
|---|
| 338 | n/a | """ |
|---|
| 339 | n/a | commands = [] |
|---|
| 340 | n/a | for sub_command in self.sub_commands: |
|---|
| 341 | n/a | if len(sub_command) == 2: |
|---|
| 342 | n/a | cmd_name, method = sub_command |
|---|
| 343 | n/a | if method is None or method(self): |
|---|
| 344 | n/a | commands.append(cmd_name) |
|---|
| 345 | n/a | else: |
|---|
| 346 | n/a | commands.append(sub_command) |
|---|
| 347 | n/a | return commands |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | # -- External world manipulation ----------------------------------- |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | def execute(self, func, args, msg=None, level=1): |
|---|
| 352 | n/a | util.execute(func, args, msg, dry_run=self.dry_run) |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | def mkpath(self, name, mode=0o777, dry_run=None): |
|---|
| 355 | n/a | if dry_run is None: |
|---|
| 356 | n/a | dry_run = self.dry_run |
|---|
| 357 | n/a | name = os.path.normpath(name) |
|---|
| 358 | n/a | if os.path.isdir(name) or name == '': |
|---|
| 359 | n/a | return |
|---|
| 360 | n/a | if dry_run: |
|---|
| 361 | n/a | head = '' |
|---|
| 362 | n/a | for part in name.split(os.sep): |
|---|
| 363 | n/a | logger.info("created directory %s%s", head, part) |
|---|
| 364 | n/a | head += part + os.sep |
|---|
| 365 | n/a | return |
|---|
| 366 | n/a | os.makedirs(name, mode) |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | def copy_file(self, infile, outfile, |
|---|
| 369 | n/a | preserve_mode=True, preserve_times=True, link=None, level=1): |
|---|
| 370 | n/a | """Copy a file respecting dry-run and force flags. |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | (dry-run defaults to whatever is in the Distribution object, and |
|---|
| 373 | n/a | force to false for commands that don't define it.) |
|---|
| 374 | n/a | """ |
|---|
| 375 | n/a | if self.dry_run: |
|---|
| 376 | n/a | # XXX add a comment |
|---|
| 377 | n/a | return |
|---|
| 378 | n/a | if os.path.isdir(outfile): |
|---|
| 379 | n/a | outfile = os.path.join(outfile, os.path.split(infile)[-1]) |
|---|
| 380 | n/a | copyfile(infile, outfile) |
|---|
| 381 | n/a | return outfile, None # XXX |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | def copy_tree(self, infile, outfile, preserve_mode=True, |
|---|
| 384 | n/a | preserve_times=True, preserve_symlinks=False, level=1): |
|---|
| 385 | n/a | """Copy an entire directory tree respecting dry-run |
|---|
| 386 | n/a | and force flags. |
|---|
| 387 | n/a | """ |
|---|
| 388 | n/a | if self.dry_run: |
|---|
| 389 | n/a | # XXX should not return but let copy_tree log and decide to execute |
|---|
| 390 | n/a | # or not based on its dry_run argument |
|---|
| 391 | n/a | return |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | return util.copy_tree(infile, outfile, preserve_mode, preserve_times, |
|---|
| 394 | n/a | preserve_symlinks, not self.force, dry_run=self.dry_run) |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | def move_file(self, src, dst, level=1): |
|---|
| 397 | n/a | """Move a file respecting the dry-run flag.""" |
|---|
| 398 | n/a | if self.dry_run: |
|---|
| 399 | n/a | return # XXX same thing |
|---|
| 400 | n/a | return move(src, dst) |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | def spawn(self, cmd, search_path=True, level=1): |
|---|
| 403 | n/a | """Spawn an external command respecting dry-run flag.""" |
|---|
| 404 | n/a | from packaging.util import spawn |
|---|
| 405 | n/a | spawn(cmd, search_path, dry_run=self.dry_run) |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | def make_archive(self, base_name, format, root_dir=None, base_dir=None, |
|---|
| 408 | n/a | owner=None, group=None): |
|---|
| 409 | n/a | return make_archive(base_name, format, root_dir, |
|---|
| 410 | n/a | base_dir, dry_run=self.dry_run, |
|---|
| 411 | n/a | owner=owner, group=group) |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | def make_file(self, infiles, outfile, func, args, |
|---|
| 414 | n/a | exec_msg=None, skip_msg=None, level=1): |
|---|
| 415 | n/a | """Special case of 'execute()' for operations that process one or |
|---|
| 416 | n/a | more input files and generate one output file. Works just like |
|---|
| 417 | n/a | 'execute()', except the operation is skipped and a different |
|---|
| 418 | n/a | message printed if 'outfile' already exists and is newer than all |
|---|
| 419 | n/a | files listed in 'infiles'. If the command defined 'self.force', |
|---|
| 420 | n/a | and it is true, then the command is unconditionally run -- does no |
|---|
| 421 | n/a | timestamp checks. |
|---|
| 422 | n/a | """ |
|---|
| 423 | n/a | if skip_msg is None: |
|---|
| 424 | n/a | skip_msg = "skipping %s (inputs unchanged)" % outfile |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | # Allow 'infiles' to be a single string |
|---|
| 427 | n/a | if isinstance(infiles, str): |
|---|
| 428 | n/a | infiles = (infiles,) |
|---|
| 429 | n/a | elif not isinstance(infiles, (list, tuple)): |
|---|
| 430 | n/a | raise TypeError( |
|---|
| 431 | n/a | "'infiles' must be a string, or a list or tuple of strings") |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | if exec_msg is None: |
|---|
| 434 | n/a | exec_msg = "generating %s from %s" % (outfile, ', '.join(infiles)) |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | # If 'outfile' must be regenerated (either because it doesn't |
|---|
| 437 | n/a | # exist, is out-of-date, or the 'force' flag is true) then |
|---|
| 438 | n/a | # perform the action that presumably regenerates it |
|---|
| 439 | n/a | if self.force or util.newer_group(infiles, outfile): |
|---|
| 440 | n/a | self.execute(func, args, exec_msg, level) |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | # Otherwise, print the "skip" message |
|---|
| 443 | n/a | else: |
|---|
| 444 | n/a | logger.debug(skip_msg) |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | def byte_compile(self, files, prefix=None): |
|---|
| 447 | n/a | """Byte-compile files to pyc and/or pyo files. |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | This method requires that the calling class define compile and |
|---|
| 450 | n/a | optimize options, like build_py and install_lib. It also |
|---|
| 451 | n/a | automatically respects the force and dry-run options. |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | prefix, if given, is a string that will be stripped off the |
|---|
| 454 | n/a | filenames encoded in bytecode files. |
|---|
| 455 | n/a | """ |
|---|
| 456 | n/a | if self.compile: |
|---|
| 457 | n/a | util.byte_compile(files, optimize=False, prefix=prefix, |
|---|
| 458 | n/a | force=self.force, dry_run=self.dry_run) |
|---|
| 459 | n/a | if self.optimize: |
|---|
| 460 | n/a | util.byte_compile(files, optimize=self.optimize, prefix=prefix, |
|---|
| 461 | n/a | force=self.force, dry_run=self.dry_run) |
|---|