| 1 | n/a | """Utility functions for copying and archiving files and directory trees. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | XXX The functions here don't copy the resource fork or other metadata on Mac. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | import os |
|---|
| 8 | n/a | import sys |
|---|
| 9 | n/a | import stat |
|---|
| 10 | n/a | import fnmatch |
|---|
| 11 | n/a | import collections |
|---|
| 12 | n/a | import errno |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | try: |
|---|
| 15 | n/a | import zlib |
|---|
| 16 | n/a | del zlib |
|---|
| 17 | n/a | _ZLIB_SUPPORTED = True |
|---|
| 18 | n/a | except ImportError: |
|---|
| 19 | n/a | _ZLIB_SUPPORTED = False |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | try: |
|---|
| 22 | n/a | import bz2 |
|---|
| 23 | n/a | del bz2 |
|---|
| 24 | n/a | _BZ2_SUPPORTED = True |
|---|
| 25 | n/a | except ImportError: |
|---|
| 26 | n/a | _BZ2_SUPPORTED = False |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | try: |
|---|
| 29 | n/a | import lzma |
|---|
| 30 | n/a | del lzma |
|---|
| 31 | n/a | _LZMA_SUPPORTED = True |
|---|
| 32 | n/a | except ImportError: |
|---|
| 33 | n/a | _LZMA_SUPPORTED = False |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | try: |
|---|
| 36 | n/a | from pwd import getpwnam |
|---|
| 37 | n/a | except ImportError: |
|---|
| 38 | n/a | getpwnam = None |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | try: |
|---|
| 41 | n/a | from grp import getgrnam |
|---|
| 42 | n/a | except ImportError: |
|---|
| 43 | n/a | getgrnam = None |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | __all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2", |
|---|
| 46 | n/a | "copytree", "move", "rmtree", "Error", "SpecialFileError", |
|---|
| 47 | n/a | "ExecError", "make_archive", "get_archive_formats", |
|---|
| 48 | n/a | "register_archive_format", "unregister_archive_format", |
|---|
| 49 | n/a | "get_unpack_formats", "register_unpack_format", |
|---|
| 50 | n/a | "unregister_unpack_format", "unpack_archive", |
|---|
| 51 | n/a | "ignore_patterns", "chown", "which", "get_terminal_size", |
|---|
| 52 | n/a | "SameFileError"] |
|---|
| 53 | n/a | # disk_usage is added later, if available on the platform |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | class Error(OSError): |
|---|
| 56 | n/a | pass |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | class SameFileError(Error): |
|---|
| 59 | n/a | """Raised when source and destination are the same file.""" |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | class SpecialFileError(OSError): |
|---|
| 62 | n/a | """Raised when trying to do a kind of operation (e.g. copying) which is |
|---|
| 63 | n/a | not supported on a special file (e.g. a named pipe)""" |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | class ExecError(OSError): |
|---|
| 66 | n/a | """Raised when a command could not be executed""" |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | class ReadError(OSError): |
|---|
| 69 | n/a | """Raised when an archive cannot be read""" |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | class RegistryError(Exception): |
|---|
| 72 | n/a | """Raised when a registry operation with the archiving |
|---|
| 73 | n/a | and unpacking registries fails""" |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def copyfileobj(fsrc, fdst, length=16*1024): |
|---|
| 77 | n/a | """copy data from file-like object fsrc to file-like object fdst""" |
|---|
| 78 | n/a | while 1: |
|---|
| 79 | n/a | buf = fsrc.read(length) |
|---|
| 80 | n/a | if not buf: |
|---|
| 81 | n/a | break |
|---|
| 82 | n/a | fdst.write(buf) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def _samefile(src, dst): |
|---|
| 85 | n/a | # Macintosh, Unix. |
|---|
| 86 | n/a | if hasattr(os.path, 'samefile'): |
|---|
| 87 | n/a | try: |
|---|
| 88 | n/a | return os.path.samefile(src, dst) |
|---|
| 89 | n/a | except OSError: |
|---|
| 90 | n/a | return False |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | # All other platforms: check for same pathname. |
|---|
| 93 | n/a | return (os.path.normcase(os.path.abspath(src)) == |
|---|
| 94 | n/a | os.path.normcase(os.path.abspath(dst))) |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | def copyfile(src, dst, *, follow_symlinks=True): |
|---|
| 97 | n/a | """Copy data from src to dst. |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | If follow_symlinks is not set and src is a symbolic link, a new |
|---|
| 100 | n/a | symlink will be created instead of copying the file it points to. |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | """ |
|---|
| 103 | n/a | if _samefile(src, dst): |
|---|
| 104 | n/a | raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | for fn in [src, dst]: |
|---|
| 107 | n/a | try: |
|---|
| 108 | n/a | st = os.stat(fn) |
|---|
| 109 | n/a | except OSError: |
|---|
| 110 | n/a | # File most likely does not exist |
|---|
| 111 | n/a | pass |
|---|
| 112 | n/a | else: |
|---|
| 113 | n/a | # XXX What about other special files? (sockets, devices...) |
|---|
| 114 | n/a | if stat.S_ISFIFO(st.st_mode): |
|---|
| 115 | n/a | raise SpecialFileError("`%s` is a named pipe" % fn) |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | if not follow_symlinks and os.path.islink(src): |
|---|
| 118 | n/a | os.symlink(os.readlink(src), dst) |
|---|
| 119 | n/a | else: |
|---|
| 120 | n/a | with open(src, 'rb') as fsrc: |
|---|
| 121 | n/a | with open(dst, 'wb') as fdst: |
|---|
| 122 | n/a | copyfileobj(fsrc, fdst) |
|---|
| 123 | n/a | return dst |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | def copymode(src, dst, *, follow_symlinks=True): |
|---|
| 126 | n/a | """Copy mode bits from src to dst. |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | If follow_symlinks is not set, symlinks aren't followed if and only |
|---|
| 129 | n/a | if both `src` and `dst` are symlinks. If `lchmod` isn't available |
|---|
| 130 | n/a | (e.g. Linux) this method does nothing. |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | """ |
|---|
| 133 | n/a | if not follow_symlinks and os.path.islink(src) and os.path.islink(dst): |
|---|
| 134 | n/a | if hasattr(os, 'lchmod'): |
|---|
| 135 | n/a | stat_func, chmod_func = os.lstat, os.lchmod |
|---|
| 136 | n/a | else: |
|---|
| 137 | n/a | return |
|---|
| 138 | n/a | elif hasattr(os, 'chmod'): |
|---|
| 139 | n/a | stat_func, chmod_func = os.stat, os.chmod |
|---|
| 140 | n/a | else: |
|---|
| 141 | n/a | return |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | st = stat_func(src) |
|---|
| 144 | n/a | chmod_func(dst, stat.S_IMODE(st.st_mode)) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | if hasattr(os, 'listxattr'): |
|---|
| 147 | n/a | def _copyxattr(src, dst, *, follow_symlinks=True): |
|---|
| 148 | n/a | """Copy extended filesystem attributes from `src` to `dst`. |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | Overwrite existing attributes. |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | If `follow_symlinks` is false, symlinks won't be followed. |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | """ |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | try: |
|---|
| 157 | n/a | names = os.listxattr(src, follow_symlinks=follow_symlinks) |
|---|
| 158 | n/a | except OSError as e: |
|---|
| 159 | n/a | if e.errno not in (errno.ENOTSUP, errno.ENODATA): |
|---|
| 160 | n/a | raise |
|---|
| 161 | n/a | return |
|---|
| 162 | n/a | for name in names: |
|---|
| 163 | n/a | try: |
|---|
| 164 | n/a | value = os.getxattr(src, name, follow_symlinks=follow_symlinks) |
|---|
| 165 | n/a | os.setxattr(dst, name, value, follow_symlinks=follow_symlinks) |
|---|
| 166 | n/a | except OSError as e: |
|---|
| 167 | n/a | if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA): |
|---|
| 168 | n/a | raise |
|---|
| 169 | n/a | else: |
|---|
| 170 | n/a | def _copyxattr(*args, **kwargs): |
|---|
| 171 | n/a | pass |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | def copystat(src, dst, *, follow_symlinks=True): |
|---|
| 174 | n/a | """Copy all stat info (mode bits, atime, mtime, flags) from src to dst. |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | If the optional flag `follow_symlinks` is not set, symlinks aren't followed if and |
|---|
| 177 | n/a | only if both `src` and `dst` are symlinks. |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | """ |
|---|
| 180 | n/a | def _nop(*args, ns=None, follow_symlinks=None): |
|---|
| 181 | n/a | pass |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | # follow symlinks (aka don't not follow symlinks) |
|---|
| 184 | n/a | follow = follow_symlinks or not (os.path.islink(src) and os.path.islink(dst)) |
|---|
| 185 | n/a | if follow: |
|---|
| 186 | n/a | # use the real function if it exists |
|---|
| 187 | n/a | def lookup(name): |
|---|
| 188 | n/a | return getattr(os, name, _nop) |
|---|
| 189 | n/a | else: |
|---|
| 190 | n/a | # use the real function only if it exists |
|---|
| 191 | n/a | # *and* it supports follow_symlinks |
|---|
| 192 | n/a | def lookup(name): |
|---|
| 193 | n/a | fn = getattr(os, name, _nop) |
|---|
| 194 | n/a | if fn in os.supports_follow_symlinks: |
|---|
| 195 | n/a | return fn |
|---|
| 196 | n/a | return _nop |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | st = lookup("stat")(src, follow_symlinks=follow) |
|---|
| 199 | n/a | mode = stat.S_IMODE(st.st_mode) |
|---|
| 200 | n/a | lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns), |
|---|
| 201 | n/a | follow_symlinks=follow) |
|---|
| 202 | n/a | try: |
|---|
| 203 | n/a | lookup("chmod")(dst, mode, follow_symlinks=follow) |
|---|
| 204 | n/a | except NotImplementedError: |
|---|
| 205 | n/a | # if we got a NotImplementedError, it's because |
|---|
| 206 | n/a | # * follow_symlinks=False, |
|---|
| 207 | n/a | # * lchown() is unavailable, and |
|---|
| 208 | n/a | # * either |
|---|
| 209 | n/a | # * fchownat() is unavailable or |
|---|
| 210 | n/a | # * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW. |
|---|
| 211 | n/a | # (it returned ENOSUP.) |
|---|
| 212 | n/a | # therefore we're out of options--we simply cannot chown the |
|---|
| 213 | n/a | # symlink. give up, suppress the error. |
|---|
| 214 | n/a | # (which is what shutil always did in this circumstance.) |
|---|
| 215 | n/a | pass |
|---|
| 216 | n/a | if hasattr(st, 'st_flags'): |
|---|
| 217 | n/a | try: |
|---|
| 218 | n/a | lookup("chflags")(dst, st.st_flags, follow_symlinks=follow) |
|---|
| 219 | n/a | except OSError as why: |
|---|
| 220 | n/a | for err in 'EOPNOTSUPP', 'ENOTSUP': |
|---|
| 221 | n/a | if hasattr(errno, err) and why.errno == getattr(errno, err): |
|---|
| 222 | n/a | break |
|---|
| 223 | n/a | else: |
|---|
| 224 | n/a | raise |
|---|
| 225 | n/a | _copyxattr(src, dst, follow_symlinks=follow) |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def copy(src, dst, *, follow_symlinks=True): |
|---|
| 228 | n/a | """Copy data and mode bits ("cp src dst"). Return the file's destination. |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | The destination may be a directory. |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | If follow_symlinks is false, symlinks won't be followed. This |
|---|
| 233 | n/a | resembles GNU's "cp -P src dst". |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | If source and destination are the same file, a SameFileError will be |
|---|
| 236 | n/a | raised. |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | """ |
|---|
| 239 | n/a | if os.path.isdir(dst): |
|---|
| 240 | n/a | dst = os.path.join(dst, os.path.basename(src)) |
|---|
| 241 | n/a | copyfile(src, dst, follow_symlinks=follow_symlinks) |
|---|
| 242 | n/a | copymode(src, dst, follow_symlinks=follow_symlinks) |
|---|
| 243 | n/a | return dst |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | def copy2(src, dst, *, follow_symlinks=True): |
|---|
| 246 | n/a | """Copy data and all stat info ("cp -p src dst"). Return the file's |
|---|
| 247 | n/a | destination." |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | The destination may be a directory. |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | If follow_symlinks is false, symlinks won't be followed. This |
|---|
| 252 | n/a | resembles GNU's "cp -P src dst". |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | """ |
|---|
| 255 | n/a | if os.path.isdir(dst): |
|---|
| 256 | n/a | dst = os.path.join(dst, os.path.basename(src)) |
|---|
| 257 | n/a | copyfile(src, dst, follow_symlinks=follow_symlinks) |
|---|
| 258 | n/a | copystat(src, dst, follow_symlinks=follow_symlinks) |
|---|
| 259 | n/a | return dst |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | def ignore_patterns(*patterns): |
|---|
| 262 | n/a | """Function that can be used as copytree() ignore parameter. |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | Patterns is a sequence of glob-style patterns |
|---|
| 265 | n/a | that are used to exclude files""" |
|---|
| 266 | n/a | def _ignore_patterns(path, names): |
|---|
| 267 | n/a | ignored_names = [] |
|---|
| 268 | n/a | for pattern in patterns: |
|---|
| 269 | n/a | ignored_names.extend(fnmatch.filter(names, pattern)) |
|---|
| 270 | n/a | return set(ignored_names) |
|---|
| 271 | n/a | return _ignore_patterns |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, |
|---|
| 274 | n/a | ignore_dangling_symlinks=False): |
|---|
| 275 | n/a | """Recursively copy a directory tree. |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | The destination directory must not already exist. |
|---|
| 278 | n/a | If exception(s) occur, an Error is raised with a list of reasons. |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | If the optional symlinks flag is true, symbolic links in the |
|---|
| 281 | n/a | source tree result in symbolic links in the destination tree; if |
|---|
| 282 | n/a | it is false, the contents of the files pointed to by symbolic |
|---|
| 283 | n/a | links are copied. If the file pointed by the symlink doesn't |
|---|
| 284 | n/a | exist, an exception will be added in the list of errors raised in |
|---|
| 285 | n/a | an Error exception at the end of the copy process. |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | You can set the optional ignore_dangling_symlinks flag to true if you |
|---|
| 288 | n/a | want to silence this exception. Notice that this has no effect on |
|---|
| 289 | n/a | platforms that don't support os.symlink. |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | The optional ignore argument is a callable. If given, it |
|---|
| 292 | n/a | is called with the `src` parameter, which is the directory |
|---|
| 293 | n/a | being visited by copytree(), and `names` which is the list of |
|---|
| 294 | n/a | `src` contents, as returned by os.listdir(): |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | callable(src, names) -> ignored_names |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | Since copytree() is called recursively, the callable will be |
|---|
| 299 | n/a | called once for each directory that is copied. It returns a |
|---|
| 300 | n/a | list of names relative to the `src` directory that should |
|---|
| 301 | n/a | not be copied. |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | The optional copy_function argument is a callable that will be used |
|---|
| 304 | n/a | to copy each file. It will be called with the source path and the |
|---|
| 305 | n/a | destination path as arguments. By default, copy2() is used, but any |
|---|
| 306 | n/a | function that supports the same signature (like copy()) can be used. |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | """ |
|---|
| 309 | n/a | names = os.listdir(src) |
|---|
| 310 | n/a | if ignore is not None: |
|---|
| 311 | n/a | ignored_names = ignore(src, names) |
|---|
| 312 | n/a | else: |
|---|
| 313 | n/a | ignored_names = set() |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | os.makedirs(dst) |
|---|
| 316 | n/a | errors = [] |
|---|
| 317 | n/a | for name in names: |
|---|
| 318 | n/a | if name in ignored_names: |
|---|
| 319 | n/a | continue |
|---|
| 320 | n/a | srcname = os.path.join(src, name) |
|---|
| 321 | n/a | dstname = os.path.join(dst, name) |
|---|
| 322 | n/a | try: |
|---|
| 323 | n/a | if os.path.islink(srcname): |
|---|
| 324 | n/a | linkto = os.readlink(srcname) |
|---|
| 325 | n/a | if symlinks: |
|---|
| 326 | n/a | # We can't just leave it to `copy_function` because legacy |
|---|
| 327 | n/a | # code with a custom `copy_function` may rely on copytree |
|---|
| 328 | n/a | # doing the right thing. |
|---|
| 329 | n/a | os.symlink(linkto, dstname) |
|---|
| 330 | n/a | copystat(srcname, dstname, follow_symlinks=not symlinks) |
|---|
| 331 | n/a | else: |
|---|
| 332 | n/a | # ignore dangling symlink if the flag is on |
|---|
| 333 | n/a | if not os.path.exists(linkto) and ignore_dangling_symlinks: |
|---|
| 334 | n/a | continue |
|---|
| 335 | n/a | # otherwise let the copy occurs. copy2 will raise an error |
|---|
| 336 | n/a | if os.path.isdir(srcname): |
|---|
| 337 | n/a | copytree(srcname, dstname, symlinks, ignore, |
|---|
| 338 | n/a | copy_function) |
|---|
| 339 | n/a | else: |
|---|
| 340 | n/a | copy_function(srcname, dstname) |
|---|
| 341 | n/a | elif os.path.isdir(srcname): |
|---|
| 342 | n/a | copytree(srcname, dstname, symlinks, ignore, copy_function) |
|---|
| 343 | n/a | else: |
|---|
| 344 | n/a | # Will raise a SpecialFileError for unsupported file types |
|---|
| 345 | n/a | copy_function(srcname, dstname) |
|---|
| 346 | n/a | # catch the Error from the recursive copytree so that we can |
|---|
| 347 | n/a | # continue with other files |
|---|
| 348 | n/a | except Error as err: |
|---|
| 349 | n/a | errors.extend(err.args[0]) |
|---|
| 350 | n/a | except OSError as why: |
|---|
| 351 | n/a | errors.append((srcname, dstname, str(why))) |
|---|
| 352 | n/a | try: |
|---|
| 353 | n/a | copystat(src, dst) |
|---|
| 354 | n/a | except OSError as why: |
|---|
| 355 | n/a | # Copying file access times may fail on Windows |
|---|
| 356 | n/a | if getattr(why, 'winerror', None) is None: |
|---|
| 357 | n/a | errors.append((src, dst, str(why))) |
|---|
| 358 | n/a | if errors: |
|---|
| 359 | n/a | raise Error(errors) |
|---|
| 360 | n/a | return dst |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | # version vulnerable to race conditions |
|---|
| 363 | n/a | def _rmtree_unsafe(path, onerror): |
|---|
| 364 | n/a | try: |
|---|
| 365 | n/a | if os.path.islink(path): |
|---|
| 366 | n/a | # symlinks to directories are forbidden, see bug #1669 |
|---|
| 367 | n/a | raise OSError("Cannot call rmtree on a symbolic link") |
|---|
| 368 | n/a | except OSError: |
|---|
| 369 | n/a | onerror(os.path.islink, path, sys.exc_info()) |
|---|
| 370 | n/a | # can't continue even if onerror hook returns |
|---|
| 371 | n/a | return |
|---|
| 372 | n/a | names = [] |
|---|
| 373 | n/a | try: |
|---|
| 374 | n/a | names = os.listdir(path) |
|---|
| 375 | n/a | except OSError: |
|---|
| 376 | n/a | onerror(os.listdir, path, sys.exc_info()) |
|---|
| 377 | n/a | for name in names: |
|---|
| 378 | n/a | fullname = os.path.join(path, name) |
|---|
| 379 | n/a | try: |
|---|
| 380 | n/a | mode = os.lstat(fullname).st_mode |
|---|
| 381 | n/a | except OSError: |
|---|
| 382 | n/a | mode = 0 |
|---|
| 383 | n/a | if stat.S_ISDIR(mode): |
|---|
| 384 | n/a | _rmtree_unsafe(fullname, onerror) |
|---|
| 385 | n/a | else: |
|---|
| 386 | n/a | try: |
|---|
| 387 | n/a | os.unlink(fullname) |
|---|
| 388 | n/a | except OSError: |
|---|
| 389 | n/a | onerror(os.unlink, fullname, sys.exc_info()) |
|---|
| 390 | n/a | try: |
|---|
| 391 | n/a | os.rmdir(path) |
|---|
| 392 | n/a | except OSError: |
|---|
| 393 | n/a | onerror(os.rmdir, path, sys.exc_info()) |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | # Version using fd-based APIs to protect against races |
|---|
| 396 | n/a | def _rmtree_safe_fd(topfd, path, onerror): |
|---|
| 397 | n/a | names = [] |
|---|
| 398 | n/a | try: |
|---|
| 399 | n/a | names = os.listdir(topfd) |
|---|
| 400 | n/a | except OSError as err: |
|---|
| 401 | n/a | err.filename = path |
|---|
| 402 | n/a | onerror(os.listdir, path, sys.exc_info()) |
|---|
| 403 | n/a | for name in names: |
|---|
| 404 | n/a | fullname = os.path.join(path, name) |
|---|
| 405 | n/a | try: |
|---|
| 406 | n/a | orig_st = os.stat(name, dir_fd=topfd, follow_symlinks=False) |
|---|
| 407 | n/a | mode = orig_st.st_mode |
|---|
| 408 | n/a | except OSError: |
|---|
| 409 | n/a | mode = 0 |
|---|
| 410 | n/a | if stat.S_ISDIR(mode): |
|---|
| 411 | n/a | try: |
|---|
| 412 | n/a | dirfd = os.open(name, os.O_RDONLY, dir_fd=topfd) |
|---|
| 413 | n/a | except OSError: |
|---|
| 414 | n/a | onerror(os.open, fullname, sys.exc_info()) |
|---|
| 415 | n/a | else: |
|---|
| 416 | n/a | try: |
|---|
| 417 | n/a | if os.path.samestat(orig_st, os.fstat(dirfd)): |
|---|
| 418 | n/a | _rmtree_safe_fd(dirfd, fullname, onerror) |
|---|
| 419 | n/a | try: |
|---|
| 420 | n/a | os.rmdir(name, dir_fd=topfd) |
|---|
| 421 | n/a | except OSError: |
|---|
| 422 | n/a | onerror(os.rmdir, fullname, sys.exc_info()) |
|---|
| 423 | n/a | else: |
|---|
| 424 | n/a | try: |
|---|
| 425 | n/a | # This can only happen if someone replaces |
|---|
| 426 | n/a | # a directory with a symlink after the call to |
|---|
| 427 | n/a | # stat.S_ISDIR above. |
|---|
| 428 | n/a | raise OSError("Cannot call rmtree on a symbolic " |
|---|
| 429 | n/a | "link") |
|---|
| 430 | n/a | except OSError: |
|---|
| 431 | n/a | onerror(os.path.islink, fullname, sys.exc_info()) |
|---|
| 432 | n/a | finally: |
|---|
| 433 | n/a | os.close(dirfd) |
|---|
| 434 | n/a | else: |
|---|
| 435 | n/a | try: |
|---|
| 436 | n/a | os.unlink(name, dir_fd=topfd) |
|---|
| 437 | n/a | except OSError: |
|---|
| 438 | n/a | onerror(os.unlink, fullname, sys.exc_info()) |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <= |
|---|
| 441 | n/a | os.supports_dir_fd and |
|---|
| 442 | n/a | os.listdir in os.supports_fd and |
|---|
| 443 | n/a | os.stat in os.supports_follow_symlinks) |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | def rmtree(path, ignore_errors=False, onerror=None): |
|---|
| 446 | n/a | """Recursively delete a directory tree. |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | If ignore_errors is set, errors are ignored; otherwise, if onerror |
|---|
| 449 | n/a | is set, it is called to handle the error with arguments (func, |
|---|
| 450 | n/a | path, exc_info) where func is platform and implementation dependent; |
|---|
| 451 | n/a | path is the argument to that function that caused it to fail; and |
|---|
| 452 | n/a | exc_info is a tuple returned by sys.exc_info(). If ignore_errors |
|---|
| 453 | n/a | is false and onerror is None, an exception is raised. |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | """ |
|---|
| 456 | n/a | if ignore_errors: |
|---|
| 457 | n/a | def onerror(*args): |
|---|
| 458 | n/a | pass |
|---|
| 459 | n/a | elif onerror is None: |
|---|
| 460 | n/a | def onerror(*args): |
|---|
| 461 | n/a | raise |
|---|
| 462 | n/a | if _use_fd_functions: |
|---|
| 463 | n/a | # While the unsafe rmtree works fine on bytes, the fd based does not. |
|---|
| 464 | n/a | if isinstance(path, bytes): |
|---|
| 465 | n/a | path = os.fsdecode(path) |
|---|
| 466 | n/a | # Note: To guard against symlink races, we use the standard |
|---|
| 467 | n/a | # lstat()/open()/fstat() trick. |
|---|
| 468 | n/a | try: |
|---|
| 469 | n/a | orig_st = os.lstat(path) |
|---|
| 470 | n/a | except Exception: |
|---|
| 471 | n/a | onerror(os.lstat, path, sys.exc_info()) |
|---|
| 472 | n/a | return |
|---|
| 473 | n/a | try: |
|---|
| 474 | n/a | fd = os.open(path, os.O_RDONLY) |
|---|
| 475 | n/a | except Exception: |
|---|
| 476 | n/a | onerror(os.lstat, path, sys.exc_info()) |
|---|
| 477 | n/a | return |
|---|
| 478 | n/a | try: |
|---|
| 479 | n/a | if os.path.samestat(orig_st, os.fstat(fd)): |
|---|
| 480 | n/a | _rmtree_safe_fd(fd, path, onerror) |
|---|
| 481 | n/a | try: |
|---|
| 482 | n/a | os.rmdir(path) |
|---|
| 483 | n/a | except OSError: |
|---|
| 484 | n/a | onerror(os.rmdir, path, sys.exc_info()) |
|---|
| 485 | n/a | else: |
|---|
| 486 | n/a | try: |
|---|
| 487 | n/a | # symlinks to directories are forbidden, see bug #1669 |
|---|
| 488 | n/a | raise OSError("Cannot call rmtree on a symbolic link") |
|---|
| 489 | n/a | except OSError: |
|---|
| 490 | n/a | onerror(os.path.islink, path, sys.exc_info()) |
|---|
| 491 | n/a | finally: |
|---|
| 492 | n/a | os.close(fd) |
|---|
| 493 | n/a | else: |
|---|
| 494 | n/a | return _rmtree_unsafe(path, onerror) |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | # Allow introspection of whether or not the hardening against symlink |
|---|
| 497 | n/a | # attacks is supported on the current platform |
|---|
| 498 | n/a | rmtree.avoids_symlink_attacks = _use_fd_functions |
|---|
| 499 | n/a | |
|---|
| 500 | n/a | def _basename(path): |
|---|
| 501 | n/a | # A basename() variant which first strips the trailing slash, if present. |
|---|
| 502 | n/a | # Thus we always get the last component of the path, even for directories. |
|---|
| 503 | n/a | sep = os.path.sep + (os.path.altsep or '') |
|---|
| 504 | n/a | return os.path.basename(path.rstrip(sep)) |
|---|
| 505 | n/a | |
|---|
| 506 | n/a | def move(src, dst, copy_function=copy2): |
|---|
| 507 | n/a | """Recursively move a file or directory to another location. This is |
|---|
| 508 | n/a | similar to the Unix "mv" command. Return the file or directory's |
|---|
| 509 | n/a | destination. |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | If the destination is a directory or a symlink to a directory, the source |
|---|
| 512 | n/a | is moved inside the directory. The destination path must not already |
|---|
| 513 | n/a | exist. |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | If the destination already exists but is not a directory, it may be |
|---|
| 516 | n/a | overwritten depending on os.rename() semantics. |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | If the destination is on our current filesystem, then rename() is used. |
|---|
| 519 | n/a | Otherwise, src is copied to the destination and then removed. Symlinks are |
|---|
| 520 | n/a | recreated under the new name if os.rename() fails because of cross |
|---|
| 521 | n/a | filesystem renames. |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | The optional `copy_function` argument is a callable that will be used |
|---|
| 524 | n/a | to copy the source or it will be delegated to `copytree`. |
|---|
| 525 | n/a | By default, copy2() is used, but any function that supports the same |
|---|
| 526 | n/a | signature (like copy()) can be used. |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | A lot more could be done here... A look at a mv.c shows a lot of |
|---|
| 529 | n/a | the issues this implementation glosses over. |
|---|
| 530 | n/a | |
|---|
| 531 | n/a | """ |
|---|
| 532 | n/a | real_dst = dst |
|---|
| 533 | n/a | if os.path.isdir(dst): |
|---|
| 534 | n/a | if _samefile(src, dst): |
|---|
| 535 | n/a | # We might be on a case insensitive filesystem, |
|---|
| 536 | n/a | # perform the rename anyway. |
|---|
| 537 | n/a | os.rename(src, dst) |
|---|
| 538 | n/a | return |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | real_dst = os.path.join(dst, _basename(src)) |
|---|
| 541 | n/a | if os.path.exists(real_dst): |
|---|
| 542 | n/a | raise Error("Destination path '%s' already exists" % real_dst) |
|---|
| 543 | n/a | try: |
|---|
| 544 | n/a | os.rename(src, real_dst) |
|---|
| 545 | n/a | except OSError: |
|---|
| 546 | n/a | if os.path.islink(src): |
|---|
| 547 | n/a | linkto = os.readlink(src) |
|---|
| 548 | n/a | os.symlink(linkto, real_dst) |
|---|
| 549 | n/a | os.unlink(src) |
|---|
| 550 | n/a | elif os.path.isdir(src): |
|---|
| 551 | n/a | if _destinsrc(src, dst): |
|---|
| 552 | n/a | raise Error("Cannot move a directory '%s' into itself" |
|---|
| 553 | n/a | " '%s'." % (src, dst)) |
|---|
| 554 | n/a | copytree(src, real_dst, copy_function=copy_function, |
|---|
| 555 | n/a | symlinks=True) |
|---|
| 556 | n/a | rmtree(src) |
|---|
| 557 | n/a | else: |
|---|
| 558 | n/a | copy_function(src, real_dst) |
|---|
| 559 | n/a | os.unlink(src) |
|---|
| 560 | n/a | return real_dst |
|---|
| 561 | n/a | |
|---|
| 562 | n/a | def _destinsrc(src, dst): |
|---|
| 563 | n/a | src = os.path.abspath(src) |
|---|
| 564 | n/a | dst = os.path.abspath(dst) |
|---|
| 565 | n/a | if not src.endswith(os.path.sep): |
|---|
| 566 | n/a | src += os.path.sep |
|---|
| 567 | n/a | if not dst.endswith(os.path.sep): |
|---|
| 568 | n/a | dst += os.path.sep |
|---|
| 569 | n/a | return dst.startswith(src) |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | def _get_gid(name): |
|---|
| 572 | n/a | """Returns a gid, given a group name.""" |
|---|
| 573 | n/a | if getgrnam is None or name is None: |
|---|
| 574 | n/a | return None |
|---|
| 575 | n/a | try: |
|---|
| 576 | n/a | result = getgrnam(name) |
|---|
| 577 | n/a | except KeyError: |
|---|
| 578 | n/a | result = None |
|---|
| 579 | n/a | if result is not None: |
|---|
| 580 | n/a | return result[2] |
|---|
| 581 | n/a | return None |
|---|
| 582 | n/a | |
|---|
| 583 | n/a | def _get_uid(name): |
|---|
| 584 | n/a | """Returns an uid, given a user name.""" |
|---|
| 585 | n/a | if getpwnam is None or name is None: |
|---|
| 586 | n/a | return None |
|---|
| 587 | n/a | try: |
|---|
| 588 | n/a | result = getpwnam(name) |
|---|
| 589 | n/a | except KeyError: |
|---|
| 590 | n/a | result = None |
|---|
| 591 | n/a | if result is not None: |
|---|
| 592 | n/a | return result[2] |
|---|
| 593 | n/a | return None |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, |
|---|
| 596 | n/a | owner=None, group=None, logger=None): |
|---|
| 597 | n/a | """Create a (possibly compressed) tar file from all the files under |
|---|
| 598 | n/a | 'base_dir'. |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | 'compress' must be "gzip" (the default), "bzip2", "xz", or None. |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | 'owner' and 'group' can be used to define an owner and a group for the |
|---|
| 603 | n/a | archive that is being built. If not provided, the current owner and group |
|---|
| 604 | n/a | will be used. |
|---|
| 605 | n/a | |
|---|
| 606 | n/a | The output tar file will be named 'base_name' + ".tar", possibly plus |
|---|
| 607 | n/a | the appropriate compression extension (".gz", ".bz2", or ".xz"). |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | Returns the output filename. |
|---|
| 610 | n/a | """ |
|---|
| 611 | n/a | if compress is None: |
|---|
| 612 | n/a | tar_compression = '' |
|---|
| 613 | n/a | elif _ZLIB_SUPPORTED and compress == 'gzip': |
|---|
| 614 | n/a | tar_compression = 'gz' |
|---|
| 615 | n/a | elif _BZ2_SUPPORTED and compress == 'bzip2': |
|---|
| 616 | n/a | tar_compression = 'bz2' |
|---|
| 617 | n/a | elif _LZMA_SUPPORTED and compress == 'xz': |
|---|
| 618 | n/a | tar_compression = 'xz' |
|---|
| 619 | n/a | else: |
|---|
| 620 | n/a | raise ValueError("bad value for 'compress', or compression format not " |
|---|
| 621 | n/a | "supported : {0}".format(compress)) |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | import tarfile # late import for breaking circular dependency |
|---|
| 624 | n/a | |
|---|
| 625 | n/a | compress_ext = '.' + tar_compression if compress else '' |
|---|
| 626 | n/a | archive_name = base_name + '.tar' + compress_ext |
|---|
| 627 | n/a | archive_dir = os.path.dirname(archive_name) |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | if archive_dir and not os.path.exists(archive_dir): |
|---|
| 630 | n/a | if logger is not None: |
|---|
| 631 | n/a | logger.info("creating %s", archive_dir) |
|---|
| 632 | n/a | if not dry_run: |
|---|
| 633 | n/a | os.makedirs(archive_dir) |
|---|
| 634 | n/a | |
|---|
| 635 | n/a | # creating the tarball |
|---|
| 636 | n/a | if logger is not None: |
|---|
| 637 | n/a | logger.info('Creating tar archive') |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | uid = _get_uid(owner) |
|---|
| 640 | n/a | gid = _get_gid(group) |
|---|
| 641 | n/a | |
|---|
| 642 | n/a | def _set_uid_gid(tarinfo): |
|---|
| 643 | n/a | if gid is not None: |
|---|
| 644 | n/a | tarinfo.gid = gid |
|---|
| 645 | n/a | tarinfo.gname = group |
|---|
| 646 | n/a | if uid is not None: |
|---|
| 647 | n/a | tarinfo.uid = uid |
|---|
| 648 | n/a | tarinfo.uname = owner |
|---|
| 649 | n/a | return tarinfo |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | if not dry_run: |
|---|
| 652 | n/a | tar = tarfile.open(archive_name, 'w|%s' % tar_compression) |
|---|
| 653 | n/a | try: |
|---|
| 654 | n/a | tar.add(base_dir, filter=_set_uid_gid) |
|---|
| 655 | n/a | finally: |
|---|
| 656 | n/a | tar.close() |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | return archive_name |
|---|
| 659 | n/a | |
|---|
| 660 | n/a | def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None): |
|---|
| 661 | n/a | """Create a zip file from all the files under 'base_dir'. |
|---|
| 662 | n/a | |
|---|
| 663 | n/a | The output zip file will be named 'base_name' + ".zip". Returns the |
|---|
| 664 | n/a | name of the output zip file. |
|---|
| 665 | n/a | """ |
|---|
| 666 | n/a | import zipfile # late import for breaking circular dependency |
|---|
| 667 | n/a | |
|---|
| 668 | n/a | zip_filename = base_name + ".zip" |
|---|
| 669 | n/a | archive_dir = os.path.dirname(base_name) |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | if archive_dir and not os.path.exists(archive_dir): |
|---|
| 672 | n/a | if logger is not None: |
|---|
| 673 | n/a | logger.info("creating %s", archive_dir) |
|---|
| 674 | n/a | if not dry_run: |
|---|
| 675 | n/a | os.makedirs(archive_dir) |
|---|
| 676 | n/a | |
|---|
| 677 | n/a | if logger is not None: |
|---|
| 678 | n/a | logger.info("creating '%s' and adding '%s' to it", |
|---|
| 679 | n/a | zip_filename, base_dir) |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | if not dry_run: |
|---|
| 682 | n/a | with zipfile.ZipFile(zip_filename, "w", |
|---|
| 683 | n/a | compression=zipfile.ZIP_DEFLATED) as zf: |
|---|
| 684 | n/a | path = os.path.normpath(base_dir) |
|---|
| 685 | n/a | if path != os.curdir: |
|---|
| 686 | n/a | zf.write(path, path) |
|---|
| 687 | n/a | if logger is not None: |
|---|
| 688 | n/a | logger.info("adding '%s'", path) |
|---|
| 689 | n/a | for dirpath, dirnames, filenames in os.walk(base_dir): |
|---|
| 690 | n/a | for name in sorted(dirnames): |
|---|
| 691 | n/a | path = os.path.normpath(os.path.join(dirpath, name)) |
|---|
| 692 | n/a | zf.write(path, path) |
|---|
| 693 | n/a | if logger is not None: |
|---|
| 694 | n/a | logger.info("adding '%s'", path) |
|---|
| 695 | n/a | for name in filenames: |
|---|
| 696 | n/a | path = os.path.normpath(os.path.join(dirpath, name)) |
|---|
| 697 | n/a | if os.path.isfile(path): |
|---|
| 698 | n/a | zf.write(path, path) |
|---|
| 699 | n/a | if logger is not None: |
|---|
| 700 | n/a | logger.info("adding '%s'", path) |
|---|
| 701 | n/a | |
|---|
| 702 | n/a | return zip_filename |
|---|
| 703 | n/a | |
|---|
| 704 | n/a | _ARCHIVE_FORMATS = { |
|---|
| 705 | n/a | 'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"), |
|---|
| 706 | n/a | } |
|---|
| 707 | n/a | |
|---|
| 708 | n/a | if _ZLIB_SUPPORTED: |
|---|
| 709 | n/a | _ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')], |
|---|
| 710 | n/a | "gzip'ed tar-file") |
|---|
| 711 | n/a | _ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file") |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | if _BZ2_SUPPORTED: |
|---|
| 714 | n/a | _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')], |
|---|
| 715 | n/a | "bzip2'ed tar-file") |
|---|
| 716 | n/a | |
|---|
| 717 | n/a | if _LZMA_SUPPORTED: |
|---|
| 718 | n/a | _ARCHIVE_FORMATS['xztar'] = (_make_tarball, [('compress', 'xz')], |
|---|
| 719 | n/a | "xz'ed tar-file") |
|---|
| 720 | n/a | |
|---|
| 721 | n/a | def get_archive_formats(): |
|---|
| 722 | n/a | """Returns a list of supported formats for archiving and unarchiving. |
|---|
| 723 | n/a | |
|---|
| 724 | n/a | Each element of the returned sequence is a tuple (name, description) |
|---|
| 725 | n/a | """ |
|---|
| 726 | n/a | formats = [(name, registry[2]) for name, registry in |
|---|
| 727 | n/a | _ARCHIVE_FORMATS.items()] |
|---|
| 728 | n/a | formats.sort() |
|---|
| 729 | n/a | return formats |
|---|
| 730 | n/a | |
|---|
| 731 | n/a | def register_archive_format(name, function, extra_args=None, description=''): |
|---|
| 732 | n/a | """Registers an archive format. |
|---|
| 733 | n/a | |
|---|
| 734 | n/a | name is the name of the format. function is the callable that will be |
|---|
| 735 | n/a | used to create archives. If provided, extra_args is a sequence of |
|---|
| 736 | n/a | (name, value) tuples that will be passed as arguments to the callable. |
|---|
| 737 | n/a | description can be provided to describe the format, and will be returned |
|---|
| 738 | n/a | by the get_archive_formats() function. |
|---|
| 739 | n/a | """ |
|---|
| 740 | n/a | if extra_args is None: |
|---|
| 741 | n/a | extra_args = [] |
|---|
| 742 | n/a | if not callable(function): |
|---|
| 743 | n/a | raise TypeError('The %s object is not callable' % function) |
|---|
| 744 | n/a | if not isinstance(extra_args, (tuple, list)): |
|---|
| 745 | n/a | raise TypeError('extra_args needs to be a sequence') |
|---|
| 746 | n/a | for element in extra_args: |
|---|
| 747 | n/a | if not isinstance(element, (tuple, list)) or len(element) !=2: |
|---|
| 748 | n/a | raise TypeError('extra_args elements are : (arg_name, value)') |
|---|
| 749 | n/a | |
|---|
| 750 | n/a | _ARCHIVE_FORMATS[name] = (function, extra_args, description) |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | def unregister_archive_format(name): |
|---|
| 753 | n/a | del _ARCHIVE_FORMATS[name] |
|---|
| 754 | n/a | |
|---|
| 755 | n/a | def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, |
|---|
| 756 | n/a | dry_run=0, owner=None, group=None, logger=None): |
|---|
| 757 | n/a | """Create an archive file (eg. zip or tar). |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | 'base_name' is the name of the file to create, minus any format-specific |
|---|
| 760 | n/a | extension; 'format' is the archive format: one of "zip", "tar", "gztar", |
|---|
| 761 | n/a | "bztar", or "xztar". Or any other registered format. |
|---|
| 762 | n/a | |
|---|
| 763 | n/a | 'root_dir' is a directory that will be the root directory of the |
|---|
| 764 | n/a | archive; ie. we typically chdir into 'root_dir' before creating the |
|---|
| 765 | n/a | archive. 'base_dir' is the directory where we start archiving from; |
|---|
| 766 | n/a | ie. 'base_dir' will be the common prefix of all files and |
|---|
| 767 | n/a | directories in the archive. 'root_dir' and 'base_dir' both default |
|---|
| 768 | n/a | to the current directory. Returns the name of the archive file. |
|---|
| 769 | n/a | |
|---|
| 770 | n/a | 'owner' and 'group' are used when creating a tar archive. By default, |
|---|
| 771 | n/a | uses the current owner and group. |
|---|
| 772 | n/a | """ |
|---|
| 773 | n/a | save_cwd = os.getcwd() |
|---|
| 774 | n/a | if root_dir is not None: |
|---|
| 775 | n/a | if logger is not None: |
|---|
| 776 | n/a | logger.debug("changing into '%s'", root_dir) |
|---|
| 777 | n/a | base_name = os.path.abspath(base_name) |
|---|
| 778 | n/a | if not dry_run: |
|---|
| 779 | n/a | os.chdir(root_dir) |
|---|
| 780 | n/a | |
|---|
| 781 | n/a | if base_dir is None: |
|---|
| 782 | n/a | base_dir = os.curdir |
|---|
| 783 | n/a | |
|---|
| 784 | n/a | kwargs = {'dry_run': dry_run, 'logger': logger} |
|---|
| 785 | n/a | |
|---|
| 786 | n/a | try: |
|---|
| 787 | n/a | format_info = _ARCHIVE_FORMATS[format] |
|---|
| 788 | n/a | except KeyError: |
|---|
| 789 | n/a | raise ValueError("unknown archive format '%s'" % format) |
|---|
| 790 | n/a | |
|---|
| 791 | n/a | func = format_info[0] |
|---|
| 792 | n/a | for arg, val in format_info[1]: |
|---|
| 793 | n/a | kwargs[arg] = val |
|---|
| 794 | n/a | |
|---|
| 795 | n/a | if format != 'zip': |
|---|
| 796 | n/a | kwargs['owner'] = owner |
|---|
| 797 | n/a | kwargs['group'] = group |
|---|
| 798 | n/a | |
|---|
| 799 | n/a | try: |
|---|
| 800 | n/a | filename = func(base_name, base_dir, **kwargs) |
|---|
| 801 | n/a | finally: |
|---|
| 802 | n/a | if root_dir is not None: |
|---|
| 803 | n/a | if logger is not None: |
|---|
| 804 | n/a | logger.debug("changing back to '%s'", save_cwd) |
|---|
| 805 | n/a | os.chdir(save_cwd) |
|---|
| 806 | n/a | |
|---|
| 807 | n/a | return filename |
|---|
| 808 | n/a | |
|---|
| 809 | n/a | |
|---|
| 810 | n/a | def get_unpack_formats(): |
|---|
| 811 | n/a | """Returns a list of supported formats for unpacking. |
|---|
| 812 | n/a | |
|---|
| 813 | n/a | Each element of the returned sequence is a tuple |
|---|
| 814 | n/a | (name, extensions, description) |
|---|
| 815 | n/a | """ |
|---|
| 816 | n/a | formats = [(name, info[0], info[3]) for name, info in |
|---|
| 817 | n/a | _UNPACK_FORMATS.items()] |
|---|
| 818 | n/a | formats.sort() |
|---|
| 819 | n/a | return formats |
|---|
| 820 | n/a | |
|---|
| 821 | n/a | def _check_unpack_options(extensions, function, extra_args): |
|---|
| 822 | n/a | """Checks what gets registered as an unpacker.""" |
|---|
| 823 | n/a | # first make sure no other unpacker is registered for this extension |
|---|
| 824 | n/a | existing_extensions = {} |
|---|
| 825 | n/a | for name, info in _UNPACK_FORMATS.items(): |
|---|
| 826 | n/a | for ext in info[0]: |
|---|
| 827 | n/a | existing_extensions[ext] = name |
|---|
| 828 | n/a | |
|---|
| 829 | n/a | for extension in extensions: |
|---|
| 830 | n/a | if extension in existing_extensions: |
|---|
| 831 | n/a | msg = '%s is already registered for "%s"' |
|---|
| 832 | n/a | raise RegistryError(msg % (extension, |
|---|
| 833 | n/a | existing_extensions[extension])) |
|---|
| 834 | n/a | |
|---|
| 835 | n/a | if not callable(function): |
|---|
| 836 | n/a | raise TypeError('The registered function must be a callable') |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | def register_unpack_format(name, extensions, function, extra_args=None, |
|---|
| 840 | n/a | description=''): |
|---|
| 841 | n/a | """Registers an unpack format. |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | `name` is the name of the format. `extensions` is a list of extensions |
|---|
| 844 | n/a | corresponding to the format. |
|---|
| 845 | n/a | |
|---|
| 846 | n/a | `function` is the callable that will be |
|---|
| 847 | n/a | used to unpack archives. The callable will receive archives to unpack. |
|---|
| 848 | n/a | If it's unable to handle an archive, it needs to raise a ReadError |
|---|
| 849 | n/a | exception. |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | If provided, `extra_args` is a sequence of |
|---|
| 852 | n/a | (name, value) tuples that will be passed as arguments to the callable. |
|---|
| 853 | n/a | description can be provided to describe the format, and will be returned |
|---|
| 854 | n/a | by the get_unpack_formats() function. |
|---|
| 855 | n/a | """ |
|---|
| 856 | n/a | if extra_args is None: |
|---|
| 857 | n/a | extra_args = [] |
|---|
| 858 | n/a | _check_unpack_options(extensions, function, extra_args) |
|---|
| 859 | n/a | _UNPACK_FORMATS[name] = extensions, function, extra_args, description |
|---|
| 860 | n/a | |
|---|
| 861 | n/a | def unregister_unpack_format(name): |
|---|
| 862 | n/a | """Removes the pack format from the registry.""" |
|---|
| 863 | n/a | del _UNPACK_FORMATS[name] |
|---|
| 864 | n/a | |
|---|
| 865 | n/a | def _ensure_directory(path): |
|---|
| 866 | n/a | """Ensure that the parent directory of `path` exists""" |
|---|
| 867 | n/a | dirname = os.path.dirname(path) |
|---|
| 868 | n/a | if not os.path.isdir(dirname): |
|---|
| 869 | n/a | os.makedirs(dirname) |
|---|
| 870 | n/a | |
|---|
| 871 | n/a | def _unpack_zipfile(filename, extract_dir): |
|---|
| 872 | n/a | """Unpack zip `filename` to `extract_dir` |
|---|
| 873 | n/a | """ |
|---|
| 874 | n/a | import zipfile # late import for breaking circular dependency |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | if not zipfile.is_zipfile(filename): |
|---|
| 877 | n/a | raise ReadError("%s is not a zip file" % filename) |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | zip = zipfile.ZipFile(filename) |
|---|
| 880 | n/a | try: |
|---|
| 881 | n/a | for info in zip.infolist(): |
|---|
| 882 | n/a | name = info.filename |
|---|
| 883 | n/a | |
|---|
| 884 | n/a | # don't extract absolute paths or ones with .. in them |
|---|
| 885 | n/a | if name.startswith('/') or '..' in name: |
|---|
| 886 | n/a | continue |
|---|
| 887 | n/a | |
|---|
| 888 | n/a | target = os.path.join(extract_dir, *name.split('/')) |
|---|
| 889 | n/a | if not target: |
|---|
| 890 | n/a | continue |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | _ensure_directory(target) |
|---|
| 893 | n/a | if not name.endswith('/'): |
|---|
| 894 | n/a | # file |
|---|
| 895 | n/a | data = zip.read(info.filename) |
|---|
| 896 | n/a | f = open(target, 'wb') |
|---|
| 897 | n/a | try: |
|---|
| 898 | n/a | f.write(data) |
|---|
| 899 | n/a | finally: |
|---|
| 900 | n/a | f.close() |
|---|
| 901 | n/a | del data |
|---|
| 902 | n/a | finally: |
|---|
| 903 | n/a | zip.close() |
|---|
| 904 | n/a | |
|---|
| 905 | n/a | def _unpack_tarfile(filename, extract_dir): |
|---|
| 906 | n/a | """Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir` |
|---|
| 907 | n/a | """ |
|---|
| 908 | n/a | import tarfile # late import for breaking circular dependency |
|---|
| 909 | n/a | try: |
|---|
| 910 | n/a | tarobj = tarfile.open(filename) |
|---|
| 911 | n/a | except tarfile.TarError: |
|---|
| 912 | n/a | raise ReadError( |
|---|
| 913 | n/a | "%s is not a compressed or uncompressed tar file" % filename) |
|---|
| 914 | n/a | try: |
|---|
| 915 | n/a | tarobj.extractall(extract_dir) |
|---|
| 916 | n/a | finally: |
|---|
| 917 | n/a | tarobj.close() |
|---|
| 918 | n/a | |
|---|
| 919 | n/a | _UNPACK_FORMATS = { |
|---|
| 920 | n/a | 'tar': (['.tar'], _unpack_tarfile, [], "uncompressed tar file"), |
|---|
| 921 | n/a | 'zip': (['.zip'], _unpack_zipfile, [], "ZIP file"), |
|---|
| 922 | n/a | } |
|---|
| 923 | n/a | |
|---|
| 924 | n/a | if _ZLIB_SUPPORTED: |
|---|
| 925 | n/a | _UNPACK_FORMATS['gztar'] = (['.tar.gz', '.tgz'], _unpack_tarfile, [], |
|---|
| 926 | n/a | "gzip'ed tar-file") |
|---|
| 927 | n/a | |
|---|
| 928 | n/a | if _BZ2_SUPPORTED: |
|---|
| 929 | n/a | _UNPACK_FORMATS['bztar'] = (['.tar.bz2', '.tbz2'], _unpack_tarfile, [], |
|---|
| 930 | n/a | "bzip2'ed tar-file") |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | if _LZMA_SUPPORTED: |
|---|
| 933 | n/a | _UNPACK_FORMATS['xztar'] = (['.tar.xz', '.txz'], _unpack_tarfile, [], |
|---|
| 934 | n/a | "xz'ed tar-file") |
|---|
| 935 | n/a | |
|---|
| 936 | n/a | def _find_unpack_format(filename): |
|---|
| 937 | n/a | for name, info in _UNPACK_FORMATS.items(): |
|---|
| 938 | n/a | for extension in info[0]: |
|---|
| 939 | n/a | if filename.endswith(extension): |
|---|
| 940 | n/a | return name |
|---|
| 941 | n/a | return None |
|---|
| 942 | n/a | |
|---|
| 943 | n/a | def unpack_archive(filename, extract_dir=None, format=None): |
|---|
| 944 | n/a | """Unpack an archive. |
|---|
| 945 | n/a | |
|---|
| 946 | n/a | `filename` is the name of the archive. |
|---|
| 947 | n/a | |
|---|
| 948 | n/a | `extract_dir` is the name of the target directory, where the archive |
|---|
| 949 | n/a | is unpacked. If not provided, the current working directory is used. |
|---|
| 950 | n/a | |
|---|
| 951 | n/a | `format` is the archive format: one of "zip", "tar", "gztar", "bztar", |
|---|
| 952 | n/a | or "xztar". Or any other registered format. If not provided, |
|---|
| 953 | n/a | unpack_archive will use the filename extension and see if an unpacker |
|---|
| 954 | n/a | was registered for that extension. |
|---|
| 955 | n/a | |
|---|
| 956 | n/a | In case none is found, a ValueError is raised. |
|---|
| 957 | n/a | """ |
|---|
| 958 | n/a | if extract_dir is None: |
|---|
| 959 | n/a | extract_dir = os.getcwd() |
|---|
| 960 | n/a | |
|---|
| 961 | n/a | if format is not None: |
|---|
| 962 | n/a | try: |
|---|
| 963 | n/a | format_info = _UNPACK_FORMATS[format] |
|---|
| 964 | n/a | except KeyError: |
|---|
| 965 | n/a | raise ValueError("Unknown unpack format '{0}'".format(format)) |
|---|
| 966 | n/a | |
|---|
| 967 | n/a | func = format_info[1] |
|---|
| 968 | n/a | func(filename, extract_dir, **dict(format_info[2])) |
|---|
| 969 | n/a | else: |
|---|
| 970 | n/a | # we need to look at the registered unpackers supported extensions |
|---|
| 971 | n/a | format = _find_unpack_format(filename) |
|---|
| 972 | n/a | if format is None: |
|---|
| 973 | n/a | raise ReadError("Unknown archive format '{0}'".format(filename)) |
|---|
| 974 | n/a | |
|---|
| 975 | n/a | func = _UNPACK_FORMATS[format][1] |
|---|
| 976 | n/a | kwargs = dict(_UNPACK_FORMATS[format][2]) |
|---|
| 977 | n/a | func(filename, extract_dir, **kwargs) |
|---|
| 978 | n/a | |
|---|
| 979 | n/a | |
|---|
| 980 | n/a | if hasattr(os, 'statvfs'): |
|---|
| 981 | n/a | |
|---|
| 982 | n/a | __all__.append('disk_usage') |
|---|
| 983 | n/a | _ntuple_diskusage = collections.namedtuple('usage', 'total used free') |
|---|
| 984 | n/a | _ntuple_diskusage.total.__doc__ = 'Total space in bytes' |
|---|
| 985 | n/a | _ntuple_diskusage.used.__doc__ = 'Used space in bytes' |
|---|
| 986 | n/a | _ntuple_diskusage.free.__doc__ = 'Free space in bytes' |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | def disk_usage(path): |
|---|
| 989 | n/a | """Return disk usage statistics about the given path. |
|---|
| 990 | n/a | |
|---|
| 991 | n/a | Returned value is a named tuple with attributes 'total', 'used' and |
|---|
| 992 | n/a | 'free', which are the amount of total, used and free space, in bytes. |
|---|
| 993 | n/a | """ |
|---|
| 994 | n/a | st = os.statvfs(path) |
|---|
| 995 | n/a | free = st.f_bavail * st.f_frsize |
|---|
| 996 | n/a | total = st.f_blocks * st.f_frsize |
|---|
| 997 | n/a | used = (st.f_blocks - st.f_bfree) * st.f_frsize |
|---|
| 998 | n/a | return _ntuple_diskusage(total, used, free) |
|---|
| 999 | n/a | |
|---|
| 1000 | n/a | elif os.name == 'nt': |
|---|
| 1001 | n/a | |
|---|
| 1002 | n/a | import nt |
|---|
| 1003 | n/a | __all__.append('disk_usage') |
|---|
| 1004 | n/a | _ntuple_diskusage = collections.namedtuple('usage', 'total used free') |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | def disk_usage(path): |
|---|
| 1007 | n/a | """Return disk usage statistics about the given path. |
|---|
| 1008 | n/a | |
|---|
| 1009 | n/a | Returned values is a named tuple with attributes 'total', 'used' and |
|---|
| 1010 | n/a | 'free', which are the amount of total, used and free space, in bytes. |
|---|
| 1011 | n/a | """ |
|---|
| 1012 | n/a | total, free = nt._getdiskusage(path) |
|---|
| 1013 | n/a | used = total - free |
|---|
| 1014 | n/a | return _ntuple_diskusage(total, used, free) |
|---|
| 1015 | n/a | |
|---|
| 1016 | n/a | |
|---|
| 1017 | n/a | def chown(path, user=None, group=None): |
|---|
| 1018 | n/a | """Change owner user and group of the given path. |
|---|
| 1019 | n/a | |
|---|
| 1020 | n/a | user and group can be the uid/gid or the user/group names, and in that case, |
|---|
| 1021 | n/a | they are converted to their respective uid/gid. |
|---|
| 1022 | n/a | """ |
|---|
| 1023 | n/a | |
|---|
| 1024 | n/a | if user is None and group is None: |
|---|
| 1025 | n/a | raise ValueError("user and/or group must be set") |
|---|
| 1026 | n/a | |
|---|
| 1027 | n/a | _user = user |
|---|
| 1028 | n/a | _group = group |
|---|
| 1029 | n/a | |
|---|
| 1030 | n/a | # -1 means don't change it |
|---|
| 1031 | n/a | if user is None: |
|---|
| 1032 | n/a | _user = -1 |
|---|
| 1033 | n/a | # user can either be an int (the uid) or a string (the system username) |
|---|
| 1034 | n/a | elif isinstance(user, str): |
|---|
| 1035 | n/a | _user = _get_uid(user) |
|---|
| 1036 | n/a | if _user is None: |
|---|
| 1037 | n/a | raise LookupError("no such user: {!r}".format(user)) |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | if group is None: |
|---|
| 1040 | n/a | _group = -1 |
|---|
| 1041 | n/a | elif not isinstance(group, int): |
|---|
| 1042 | n/a | _group = _get_gid(group) |
|---|
| 1043 | n/a | if _group is None: |
|---|
| 1044 | n/a | raise LookupError("no such group: {!r}".format(group)) |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | os.chown(path, _user, _group) |
|---|
| 1047 | n/a | |
|---|
| 1048 | n/a | def get_terminal_size(fallback=(80, 24)): |
|---|
| 1049 | n/a | """Get the size of the terminal window. |
|---|
| 1050 | n/a | |
|---|
| 1051 | n/a | For each of the two dimensions, the environment variable, COLUMNS |
|---|
| 1052 | n/a | and LINES respectively, is checked. If the variable is defined and |
|---|
| 1053 | n/a | the value is a positive integer, it is used. |
|---|
| 1054 | n/a | |
|---|
| 1055 | n/a | When COLUMNS or LINES is not defined, which is the common case, |
|---|
| 1056 | n/a | the terminal connected to sys.__stdout__ is queried |
|---|
| 1057 | n/a | by invoking os.get_terminal_size. |
|---|
| 1058 | n/a | |
|---|
| 1059 | n/a | If the terminal size cannot be successfully queried, either because |
|---|
| 1060 | n/a | the system doesn't support querying, or because we are not |
|---|
| 1061 | n/a | connected to a terminal, the value given in fallback parameter |
|---|
| 1062 | n/a | is used. Fallback defaults to (80, 24) which is the default |
|---|
| 1063 | n/a | size used by many terminal emulators. |
|---|
| 1064 | n/a | |
|---|
| 1065 | n/a | The value returned is a named tuple of type os.terminal_size. |
|---|
| 1066 | n/a | """ |
|---|
| 1067 | n/a | # columns, lines are the working values |
|---|
| 1068 | n/a | try: |
|---|
| 1069 | n/a | columns = int(os.environ['COLUMNS']) |
|---|
| 1070 | n/a | except (KeyError, ValueError): |
|---|
| 1071 | n/a | columns = 0 |
|---|
| 1072 | n/a | |
|---|
| 1073 | n/a | try: |
|---|
| 1074 | n/a | lines = int(os.environ['LINES']) |
|---|
| 1075 | n/a | except (KeyError, ValueError): |
|---|
| 1076 | n/a | lines = 0 |
|---|
| 1077 | n/a | |
|---|
| 1078 | n/a | # only query if necessary |
|---|
| 1079 | n/a | if columns <= 0 or lines <= 0: |
|---|
| 1080 | n/a | try: |
|---|
| 1081 | n/a | size = os.get_terminal_size(sys.__stdout__.fileno()) |
|---|
| 1082 | n/a | except (AttributeError, ValueError, OSError): |
|---|
| 1083 | n/a | # stdout is None, closed, detached, or not a terminal, or |
|---|
| 1084 | n/a | # os.get_terminal_size() is unsupported |
|---|
| 1085 | n/a | size = os.terminal_size(fallback) |
|---|
| 1086 | n/a | if columns <= 0: |
|---|
| 1087 | n/a | columns = size.columns |
|---|
| 1088 | n/a | if lines <= 0: |
|---|
| 1089 | n/a | lines = size.lines |
|---|
| 1090 | n/a | |
|---|
| 1091 | n/a | return os.terminal_size((columns, lines)) |
|---|
| 1092 | n/a | |
|---|
| 1093 | n/a | def which(cmd, mode=os.F_OK | os.X_OK, path=None): |
|---|
| 1094 | n/a | """Given a command, mode, and a PATH string, return the path which |
|---|
| 1095 | n/a | conforms to the given mode on the PATH, or None if there is no such |
|---|
| 1096 | n/a | file. |
|---|
| 1097 | n/a | |
|---|
| 1098 | n/a | `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
|---|
| 1099 | n/a | of os.environ.get("PATH"), or can be overridden with a custom search |
|---|
| 1100 | n/a | path. |
|---|
| 1101 | n/a | |
|---|
| 1102 | n/a | """ |
|---|
| 1103 | n/a | # Check that a given file can be accessed with the correct mode. |
|---|
| 1104 | n/a | # Additionally check that `file` is not a directory, as on Windows |
|---|
| 1105 | n/a | # directories pass the os.access check. |
|---|
| 1106 | n/a | def _access_check(fn, mode): |
|---|
| 1107 | n/a | return (os.path.exists(fn) and os.access(fn, mode) |
|---|
| 1108 | n/a | and not os.path.isdir(fn)) |
|---|
| 1109 | n/a | |
|---|
| 1110 | n/a | # If we're given a path with a directory part, look it up directly rather |
|---|
| 1111 | n/a | # than referring to PATH directories. This includes checking relative to the |
|---|
| 1112 | n/a | # current directory, e.g. ./script |
|---|
| 1113 | n/a | if os.path.dirname(cmd): |
|---|
| 1114 | n/a | if _access_check(cmd, mode): |
|---|
| 1115 | n/a | return cmd |
|---|
| 1116 | n/a | return None |
|---|
| 1117 | n/a | |
|---|
| 1118 | n/a | if path is None: |
|---|
| 1119 | n/a | path = os.environ.get("PATH", os.defpath) |
|---|
| 1120 | n/a | if not path: |
|---|
| 1121 | n/a | return None |
|---|
| 1122 | n/a | path = path.split(os.pathsep) |
|---|
| 1123 | n/a | |
|---|
| 1124 | n/a | if sys.platform == "win32": |
|---|
| 1125 | n/a | # The current directory takes precedence on Windows. |
|---|
| 1126 | n/a | if not os.curdir in path: |
|---|
| 1127 | n/a | path.insert(0, os.curdir) |
|---|
| 1128 | n/a | |
|---|
| 1129 | n/a | # PATHEXT is necessary to check on Windows. |
|---|
| 1130 | n/a | pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
|---|
| 1131 | n/a | # See if the given file matches any of the expected path extensions. |
|---|
| 1132 | n/a | # This will allow us to short circuit when given "python.exe". |
|---|
| 1133 | n/a | # If it does match, only test that one, otherwise we have to try |
|---|
| 1134 | n/a | # others. |
|---|
| 1135 | n/a | if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
|---|
| 1136 | n/a | files = [cmd] |
|---|
| 1137 | n/a | else: |
|---|
| 1138 | n/a | files = [cmd + ext for ext in pathext] |
|---|
| 1139 | n/a | else: |
|---|
| 1140 | n/a | # On other platforms you don't have things like PATHEXT to tell you |
|---|
| 1141 | n/a | # what file suffixes are executable, so just pass on cmd as-is. |
|---|
| 1142 | n/a | files = [cmd] |
|---|
| 1143 | n/a | |
|---|
| 1144 | n/a | seen = set() |
|---|
| 1145 | n/a | for dir in path: |
|---|
| 1146 | n/a | normdir = os.path.normcase(dir) |
|---|
| 1147 | n/a | if not normdir in seen: |
|---|
| 1148 | n/a | seen.add(normdir) |
|---|
| 1149 | n/a | for thefile in files: |
|---|
| 1150 | n/a | name = os.path.join(dir, thefile) |
|---|
| 1151 | n/a | if _access_check(name, mode): |
|---|
| 1152 | n/a | return name |
|---|
| 1153 | n/a | return None |
|---|