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