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