Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Common operations on Posix pathnames. |
| 2 | |
| 3 | Instead of importing this module directly, import os and refer to |
| 4 | this module as os.path. The "os.path" name is an alias for this |
| 5 | module on Posix systems; on other systems (e.g. Mac, Windows), |
| 6 | os.path provides the same operations in a manner specific to that |
| 7 | platform, and is an alias to another module (e.g. macpath, ntpath). |
| 8 | |
| 9 | Some of this can actually be useful on non-Posix systems too, e.g. |
| 10 | for manipulation of the pathname component of URLs. |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 11 | """ |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 13 | import os |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 14 | import sys |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 15 | import stat |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | import genericpath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 17 | from genericpath import * |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 18 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 19 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 20 | "basename","dirname","commonprefix","getsize","getmtime", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 21 | "getatime","getctime","islink","exists","lexists","isdir","isfile", |
Benjamin Peterson | d71ca41 | 2008-05-08 23:44:58 +0000 | [diff] [blame] | 22 | "ismount", "expanduser","expandvars","normpath","abspath", |
Neal Norwitz | 61cdac6 | 2003-01-03 18:01:57 +0000 | [diff] [blame] | 23 | "samefile","sameopenfile","samestat", |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 24 | "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 25 | "devnull","realpath","supports_unicode_filenames","relpath"] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 27 | # Strings representing various path-related bits and pieces. |
| 28 | # These are primarily for export; internally, they are hardcoded. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 29 | curdir = '.' |
| 30 | pardir = '..' |
| 31 | extsep = '.' |
| 32 | sep = '/' |
| 33 | pathsep = ':' |
| 34 | defpath = ':/bin:/usr/bin' |
| 35 | altsep = None |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 36 | devnull = '/dev/null' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 38 | def _get_sep(path): |
| 39 | if isinstance(path, bytes): |
| 40 | return b'/' |
| 41 | else: |
| 42 | return '/' |
| 43 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 44 | # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 45 | # On MS-DOS this may also turn slashes into backslashes; however, other |
| 46 | # normalizations (such as optimizing '../' away) are not allowed |
| 47 | # (another function should be defined to do that). |
| 48 | |
| 49 | def normcase(s): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 50 | """Normalize case of pathname. Has no effect under Posix""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 51 | # TODO: on Mac OS X, this should really return s.lower(). |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 52 | return s |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 53 | |
| 54 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 55 | # Return whether a path is absolute. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 56 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 57 | |
| 58 | def isabs(s): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 59 | """Test whether a path is absolute""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 60 | sep = _get_sep(s) |
| 61 | return s.startswith(sep) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 62 | |
| 63 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 64 | # Join pathnames. |
| 65 | # Ignore the previous parts if a part is absolute. |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 66 | # Insert a '/' unless the first part is empty or already ends in '/'. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 67 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 68 | def join(a, *p): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 69 | """Join two or more pathname components, inserting '/' as needed. |
| 70 | If any component is an absolute path, all previous path components |
| 71 | will be discarded.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 72 | sep = _get_sep(a) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 73 | path = a |
| 74 | for b in p: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 75 | if b.startswith(sep): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 76 | path = b |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 77 | elif not path or path.endswith(sep): |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 78 | path += b |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 79 | else: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 80 | path += sep + b |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 81 | return path |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 82 | |
| 83 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 84 | # Split a path in head (everything up to the last '/') and tail (the |
Guido van Rossum | a89b1ba | 1995-09-01 20:32:21 +0000 | [diff] [blame] | 85 | # rest). If the path ends in '/', tail will be empty. If there is no |
| 86 | # '/' in the path, head will be empty. |
| 87 | # Trailing '/'es are stripped from head unless it is the root. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 89 | def split(p): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 90 | """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 91 | everything after the final slash. Either part may be empty.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 92 | sep = _get_sep(p) |
| 93 | i = p.rfind(sep) + 1 |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 94 | head, tail = p[:i], p[i:] |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 95 | if head and head != sep*len(head): |
| 96 | head = head.rstrip(sep) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 97 | return head, tail |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 98 | |
| 99 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 100 | # Split a path in root and extension. |
Guido van Rossum | 422869a | 1996-08-20 20:24:17 +0000 | [diff] [blame] | 101 | # The extension is everything starting at the last dot in the last |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 102 | # pathname component; the root is everything before that. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 103 | # It is always true that root + ext == p. |
| 104 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 105 | def splitext(p): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 106 | if isinstance(p, bytes): |
| 107 | sep = b'/' |
| 108 | extsep = b'.' |
| 109 | else: |
| 110 | sep = '/' |
| 111 | extsep = '.' |
| 112 | return genericpath._splitext(p, sep, None, extsep) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 113 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 221df24 | 1995-08-07 20:17:55 +0000 | [diff] [blame] | 115 | # Split a pathname into a drive specification and the rest of the |
| 116 | # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. |
| 117 | |
| 118 | def splitdrive(p): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 119 | """Split a pathname into drive and path. On Posix, drive is always |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 120 | empty.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 121 | return p[:0], p |
Guido van Rossum | 221df24 | 1995-08-07 20:17:55 +0000 | [diff] [blame] | 122 | |
| 123 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | # Return the tail (basename) part of a path, same as split(path)[1]. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 126 | def basename(p): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 127 | """Returns the final component of a pathname""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 128 | sep = _get_sep(p) |
| 129 | i = p.rfind(sep) + 1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | return p[i:] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 131 | |
| 132 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | # Return the head (dirname) part of a path, same as split(path)[0]. |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 134 | |
| 135 | def dirname(p): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 136 | """Returns the directory component of a pathname""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 137 | sep = _get_sep(p) |
| 138 | i = p.rfind(sep) + 1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 139 | head = p[:i] |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 140 | if head and head != sep*len(head): |
| 141 | head = head.rstrip(sep) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | return head |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 143 | |
| 144 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 145 | # Is a path a symbolic link? |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 146 | # This will always return false on systems where os.lstat doesn't exist. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 147 | |
| 148 | def islink(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 149 | """Test whether a path is a symbolic link""" |
| 150 | try: |
| 151 | st = os.lstat(path) |
| 152 | except (os.error, AttributeError): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 153 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 154 | return stat.S_ISLNK(st.st_mode) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 155 | |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 156 | # Being true for dangling symbolic links is also useful. |
| 157 | |
| 158 | def lexists(path): |
| 159 | """Test whether a path exists. Returns True for broken symbolic links""" |
| 160 | try: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 161 | os.lstat(path) |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 162 | except os.error: |
| 163 | return False |
| 164 | return True |
| 165 | |
| 166 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 167 | # Are two filenames really pointing to the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 169 | def samefile(f1, f2): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 170 | """Test whether two pathnames reference the same actual file""" |
| 171 | s1 = os.stat(f1) |
| 172 | s2 = os.stat(f2) |
| 173 | return samestat(s1, s2) |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | # Are two open files really referencing the same file? |
| 177 | # (Not necessarily the same file descriptor!) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 179 | def sameopenfile(fp1, fp2): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 180 | """Test whether two open file objects reference the same file""" |
| 181 | s1 = os.fstat(fp1) |
| 182 | s2 = os.fstat(fp2) |
| 183 | return samestat(s1, s2) |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 187 | # describing the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 189 | def samestat(s1, s2): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 190 | """Test whether two stat buffers reference the same file""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 191 | return s1.st_ino == s2.st_ino and \ |
| 192 | s1.st_dev == s2.st_dev |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | # Is a path a mount point? |
Guido van Rossum | d3876d3 | 1996-07-23 03:47:28 +0000 | [diff] [blame] | 196 | # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 198 | def ismount(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 199 | """Test whether a path is a mount point""" |
| 200 | try: |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 201 | s1 = os.lstat(path) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 202 | if isinstance(path, bytes): |
| 203 | parent = join(path, b'..') |
| 204 | else: |
| 205 | parent = join(path, '..') |
| 206 | s2 = os.lstat(parent) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 207 | except os.error: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 208 | return False # It doesn't exist -- so not a mount point :-) |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 209 | dev1 = s1.st_dev |
| 210 | dev2 = s2.st_dev |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 211 | if dev1 != dev2: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 212 | return True # path/.. on a different device as path |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 213 | ino1 = s1.st_ino |
| 214 | ino2 = s2.st_ino |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 215 | if ino1 == ino2: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 216 | return True # path/.. is the same i-node as path |
| 217 | return False |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 218 | |
| 219 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 220 | # Expand paths beginning with '~' or '~user'. |
| 221 | # '~' means $HOME; '~user' means that user's home directory. |
| 222 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 223 | # the path is returned unchanged (leaving error reporting to whatever |
| 224 | # function is called with the expanded path as argument). |
| 225 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 226 | # (A function should also be defined to do full *sh-style environment |
| 227 | # variable expansion.) |
| 228 | |
| 229 | def expanduser(path): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 230 | """Expand ~ and ~user constructions. If user or $HOME is unknown, |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 231 | do nothing.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 232 | if isinstance(path, bytes): |
| 233 | tilde = b'~' |
| 234 | else: |
| 235 | tilde = '~' |
| 236 | if not path.startswith(tilde): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 237 | return path |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 238 | sep = _get_sep(path) |
| 239 | i = path.find(sep, 1) |
Walter Dörwald | a9da5ae | 2003-06-19 10:21:14 +0000 | [diff] [blame] | 240 | if i < 0: |
| 241 | i = len(path) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 242 | if i == 1: |
Walter Dörwald | a9da5ae | 2003-06-19 10:21:14 +0000 | [diff] [blame] | 243 | if 'HOME' not in os.environ: |
Neal Norwitz | 609ba81 | 2002-09-05 21:08:25 +0000 | [diff] [blame] | 244 | import pwd |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 245 | userhome = pwd.getpwuid(os.getuid()).pw_dir |
Neal Norwitz | 609ba81 | 2002-09-05 21:08:25 +0000 | [diff] [blame] | 246 | else: |
| 247 | userhome = os.environ['HOME'] |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 248 | else: |
| 249 | import pwd |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 250 | name = path[1:i] |
| 251 | if isinstance(name, bytes): |
| 252 | name = str(name, 'ASCII') |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 253 | try: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 254 | pwent = pwd.getpwnam(name) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 255 | except KeyError: |
| 256 | return path |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 257 | userhome = pwent.pw_dir |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 258 | if isinstance(path, bytes): |
| 259 | userhome = userhome.encode(sys.getfilesystemencoding()) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 260 | root = b'/' |
| 261 | else: |
| 262 | root = '/' |
| 263 | userhome = userhome.rstrip(root) or userhome |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 264 | return userhome + path[i:] |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 265 | |
| 266 | |
| 267 | # Expand paths containing shell variable substitutions. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | # This expands the forms $variable and ${variable} only. |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 269 | # Non-existent variables are left unchanged. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
| 271 | _varprog = None |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 272 | _varprogb = None |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 273 | |
| 274 | def expandvars(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 275 | """Expand shell variables of form $var and ${var}. Unknown variables |
Fred Drake | c0ab93e | 2000-09-28 16:22:52 +0000 | [diff] [blame] | 276 | are left unchanged.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 277 | global _varprog, _varprogb |
| 278 | if isinstance(path, bytes): |
| 279 | if b'$' not in path: |
| 280 | return path |
| 281 | if not _varprogb: |
| 282 | import re |
| 283 | _varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII) |
| 284 | search = _varprogb.search |
| 285 | start = b'{' |
| 286 | end = b'}' |
| 287 | else: |
| 288 | if '$' not in path: |
| 289 | return path |
| 290 | if not _varprog: |
| 291 | import re |
| 292 | _varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII) |
| 293 | search = _varprog.search |
| 294 | start = '{' |
| 295 | end = '}' |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 296 | i = 0 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 297 | while True: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 298 | m = search(path, i) |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 299 | if not m: |
| 300 | break |
| 301 | i, j = m.span(0) |
| 302 | name = m.group(1) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 303 | if name.startswith(start) and name.endswith(end): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 304 | name = name[1:-1] |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 305 | if isinstance(name, bytes): |
| 306 | name = str(name, 'ASCII') |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 307 | if name in os.environ: |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 308 | tail = path[j:] |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 309 | value = os.environ[name] |
| 310 | if isinstance(path, bytes): |
| 311 | value = value.encode('ASCII') |
| 312 | path = path[:i] + value |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 313 | i = len(path) |
Walter Dörwald | 77cdeaf | 2003-06-17 13:13:40 +0000 | [diff] [blame] | 314 | path += tail |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 315 | else: |
| 316 | i = j |
| 317 | return path |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 318 | |
| 319 | |
| 320 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
| 321 | # It should be understood that this may change the meaning of the path |
| 322 | # if it contains symbolic links! |
| 323 | |
| 324 | def normpath(path): |
Guido van Rossum | 346f7af | 1997-12-05 19:04:51 +0000 | [diff] [blame] | 325 | """Normalize path, eliminating double slashes, etc.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 326 | if isinstance(path, bytes): |
| 327 | sep = b'/' |
| 328 | empty = b'' |
| 329 | dot = b'.' |
| 330 | dotdot = b'..' |
| 331 | else: |
| 332 | sep = '/' |
| 333 | empty = '' |
| 334 | dot = '.' |
| 335 | dotdot = '..' |
| 336 | if path == empty: |
| 337 | return dot |
| 338 | initial_slashes = path.startswith(sep) |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 339 | # POSIX allows one or two initial slashes, but treats three or more |
| 340 | # as single slash. |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 341 | if (initial_slashes and |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 342 | path.startswith(sep*2) and not path.startswith(sep*3)): |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 343 | initial_slashes = 2 |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 344 | comps = path.split(sep) |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 345 | new_comps = [] |
| 346 | for comp in comps: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 347 | if comp in (empty, dot): |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 348 | continue |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 349 | if (comp != dotdot or (not initial_slashes and not new_comps) or |
| 350 | (new_comps and new_comps[-1] == dotdot)): |
Skip Montanaro | 018dfae | 2000-07-19 17:09:51 +0000 | [diff] [blame] | 351 | new_comps.append(comp) |
| 352 | elif new_comps: |
| 353 | new_comps.pop() |
| 354 | comps = new_comps |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 355 | path = sep.join(comps) |
Marc-André Lemburg | bf222c9 | 2001-01-29 11:29:44 +0000 | [diff] [blame] | 356 | if initial_slashes: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 357 | path = sep*initial_slashes + path |
| 358 | return path or dot |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 359 | |
| 360 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 361 | def abspath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 362 | """Return an absolute path.""" |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 363 | if not isabs(path): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 364 | if isinstance(path, bytes): |
| 365 | cwd = os.getcwdb() |
| 366 | else: |
| 367 | cwd = os.getcwd() |
| 368 | path = join(cwd, path) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 369 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 370 | |
| 371 | |
| 372 | # Return a canonical path (i.e. the absolute location of a file on the |
| 373 | # filesystem). |
| 374 | |
| 375 | def realpath(filename): |
| 376 | """Return the canonical path of the specified filename, eliminating any |
| 377 | symbolic links encountered in the path.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 378 | if isinstance(filename, bytes): |
| 379 | sep = b'/' |
| 380 | empty = b'' |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 381 | else: |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 382 | sep = '/' |
| 383 | empty = '' |
| 384 | if isabs(filename): |
| 385 | bits = [sep] + filename.split(sep)[1:] |
| 386 | else: |
| 387 | bits = [empty] + filename.split(sep) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 389 | for i in range(2, len(bits)+1): |
| 390 | component = join(*bits[0:i]) |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 391 | # Resolve symbolic links. |
Brett Cannon | dfa5d95 | 2004-07-11 19:16:21 +0000 | [diff] [blame] | 392 | if islink(component): |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 393 | resolved = _resolve_link(component) |
| 394 | if resolved is None: |
| 395 | # Infinite loop -- return original component + rest of the path |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 396 | return abspath(join(*([component] + bits[i:]))) |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 397 | else: |
| 398 | newpath = join(*([resolved] + bits[i:])) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 399 | return realpath(newpath) |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 400 | |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 401 | return abspath(filename) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 402 | |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 403 | |
| 404 | def _resolve_link(path): |
| 405 | """Internal helper function. Takes a path and follows symlinks |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 406 | until we either arrive at something that isn't a symlink, or |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 407 | encounter a path we've seen before (meaning that there's a loop). |
| 408 | """ |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame] | 409 | paths_seen = set() |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 410 | while islink(path): |
Brett Cannon | dfa5d95 | 2004-07-11 19:16:21 +0000 | [diff] [blame] | 411 | if path in paths_seen: |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 412 | # Already seen this path, so we must have a symlink loop |
| 413 | return None |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame] | 414 | paths_seen.add(path) |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 415 | # Resolve where the link points to |
Brett Cannon | dfa5d95 | 2004-07-11 19:16:21 +0000 | [diff] [blame] | 416 | resolved = os.readlink(path) |
Andrew M. Kuchling | c75f112 | 2004-08-02 14:54:16 +0000 | [diff] [blame] | 417 | if not isabs(resolved): |
Brett Cannon | f50299c | 2004-07-10 22:55:15 +0000 | [diff] [blame] | 418 | dir = dirname(path) |
| 419 | path = normpath(join(dir, resolved)) |
| 420 | else: |
| 421 | path = normpath(resolved) |
| 422 | return path |
| 423 | |
Just van Rossum | 2d4e988 | 2003-07-17 15:11:49 +0000 | [diff] [blame] | 424 | supports_unicode_filenames = False |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 426 | def relpath(path, start=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 427 | """Return a relative version of a path""" |
| 428 | |
| 429 | if not path: |
| 430 | raise ValueError("no path specified") |
| 431 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 432 | if isinstance(path, bytes): |
| 433 | curdir = b'.' |
| 434 | sep = b'/' |
| 435 | pardir = b'..' |
| 436 | else: |
| 437 | curdir = '.' |
| 438 | sep = '/' |
| 439 | pardir = '..' |
| 440 | |
| 441 | if start is None: |
| 442 | start = curdir |
| 443 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 444 | start_list = abspath(start).split(sep) |
| 445 | path_list = abspath(path).split(sep) |
| 446 | |
| 447 | # Work out how much of the filepath is shared by start and path. |
| 448 | i = len(commonprefix([start_list, path_list])) |
| 449 | |
| 450 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 451 | if not rel_list: |
| 452 | return curdir |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 453 | return join(*rel_list) |