Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 1 | # Module 'posixpath' -- common operations on POSIX pathnames |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 2 | |
| 3 | import posix |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 4 | import stat |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 5 | |
| 6 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 7 | # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 8 | # On MS-DOS this may also turn slashes into backslashes; however, other |
| 9 | # normalizations (such as optimizing '../' away) are not allowed |
| 10 | # (another function should be defined to do that). |
| 11 | |
| 12 | def normcase(s): |
| 13 | return s |
| 14 | |
| 15 | |
| 16 | # Return wheter a path is absolute. |
| 17 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 18 | |
| 19 | def isabs(s): |
| 20 | return s[:1] == '/' |
| 21 | |
| 22 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 23 | # Join two pathnames. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 24 | # Ignore the first part if the second part is absolute. |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 25 | # 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] | 26 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 27 | def join(a, b): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 28 | if b[:1] == '/': return b |
| 29 | if a == '' or a[-1:] == '/': return a + b |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 30 | # Note: join('x', '') returns 'x/'; is this what we want? |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 31 | return a + '/' + b |
| 32 | |
| 33 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 34 | # 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] | 35 | # rest). If the path ends in '/', tail will be empty. If there is no |
| 36 | # '/' in the path, head will be empty. |
| 37 | # Trailing '/'es are stripped from head unless it is the root. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 39 | def split(p): |
Guido van Rossum | a89b1ba | 1995-09-01 20:32:21 +0000 | [diff] [blame] | 40 | import string |
| 41 | i = string.rfind(p, '/') + 1 |
| 42 | head, tail = p[:i], p[i:] |
| 43 | if head and head <> '/'*len(head): |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 44 | while head[-1] == '/': |
| 45 | head = head[:-1] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 46 | return head, tail |
| 47 | |
| 48 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 49 | # Split a path in root and extension. |
| 50 | # The extension is everything starting at the first dot in the last |
| 51 | # pathname component; the root is everything before that. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 52 | # It is always true that root + ext == p. |
| 53 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 54 | def splitext(p): |
| 55 | root, ext = '', '' |
| 56 | for c in p: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 57 | if c == '/': |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 58 | root, ext = root + ext + c, '' |
Sjoerd Mullender | 4359860 | 1994-12-14 15:29:17 +0000 | [diff] [blame] | 59 | elif c == '.': |
| 60 | if ext: |
| 61 | root, ext = root + ext, c |
| 62 | else: |
| 63 | ext = c |
| 64 | elif ext: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 65 | ext = ext + c |
| 66 | else: |
| 67 | root = root + c |
| 68 | return root, ext |
| 69 | |
| 70 | |
Guido van Rossum | 221df24 | 1995-08-07 20:17:55 +0000 | [diff] [blame] | 71 | # Split a pathname into a drive specification and the rest of the |
| 72 | # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. |
| 73 | |
| 74 | def splitdrive(p): |
| 75 | return '', p |
| 76 | |
| 77 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 78 | # Return the tail (basename) part of a path. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 80 | def basename(p): |
| 81 | return split(p)[1] |
| 82 | |
| 83 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 84 | # Return the head (dirname) part of a path. |
| 85 | |
| 86 | def dirname(p): |
| 87 | return split(p)[0] |
| 88 | |
| 89 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 90 | # Return the longest prefix of all list elements. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 92 | def commonprefix(m): |
| 93 | if not m: return '' |
| 94 | prefix = m[0] |
| 95 | for item in m: |
| 96 | for i in range(len(prefix)): |
| 97 | if prefix[:i+1] <> item[:i+1]: |
| 98 | prefix = prefix[:i] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 99 | if i == 0: return '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 100 | break |
| 101 | return prefix |
| 102 | |
| 103 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 104 | # Is a path a symbolic link? |
| 105 | # This will always return false on systems where posix.lstat doesn't exist. |
| 106 | |
| 107 | def islink(path): |
| 108 | try: |
| 109 | st = posix.lstat(path) |
| 110 | except (posix.error, AttributeError): |
| 111 | return 0 |
| 112 | return stat.S_ISLNK(st[stat.ST_MODE]) |
| 113 | |
| 114 | |
| 115 | # Does a path exist? |
| 116 | # This is false for dangling symbolic links. |
| 117 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 118 | def exists(path): |
| 119 | try: |
| 120 | st = posix.stat(path) |
| 121 | except posix.error: |
| 122 | return 0 |
| 123 | return 1 |
| 124 | |
| 125 | |
| 126 | # Is a path a posix directory? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 127 | # This follows symbolic links, so both islink() and isdir() can be true |
| 128 | # for the same path. |
| 129 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 130 | def isdir(path): |
| 131 | try: |
| 132 | st = posix.stat(path) |
| 133 | except posix.error: |
| 134 | return 0 |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 135 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 136 | |
| 137 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 138 | # Is a path a regular file? |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 139 | # This follows symbolic links, so both islink() and isfile() can be true |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 140 | # for the same path. |
| 141 | |
| 142 | def isfile(path): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 143 | try: |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 144 | st = posix.stat(path) |
| 145 | except posix.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 146 | return 0 |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 147 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 148 | |
| 149 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 150 | # Are two filenames really pointing to the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 152 | def samefile(f1, f2): |
| 153 | s1 = posix.stat(f1) |
| 154 | s2 = posix.stat(f2) |
| 155 | return samestat(s1, s2) |
| 156 | |
| 157 | |
| 158 | # Are two open files really referencing the same file? |
| 159 | # (Not necessarily the same file descriptor!) |
| 160 | # XXX Oops, posix.fstat() doesn't exist yet! |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 162 | def sameopenfile(fp1, fp2): |
| 163 | s1 = posix.fstat(fp1) |
| 164 | s2 = posix.fstat(fp2) |
| 165 | return samestat(s1, s2) |
| 166 | |
| 167 | |
| 168 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 169 | # describing the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 171 | def samestat(s1, s2): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 172 | return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 173 | s1[stat.ST_DEV] == s2[stat.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 | 509d24a | 1992-05-06 11:36:49 +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 | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 180 | try: |
| 181 | s1 = posix.stat(path) |
| 182 | s2 = posix.stat(join(path, '..')) |
| 183 | except posix.error: |
| 184 | return 0 # It doesn't exist -- so not a mount point :-) |
| 185 | dev1 = s1[stat.ST_DEV] |
| 186 | dev2 = s2[stat.ST_DEV] |
| 187 | if dev1 != dev2: |
| 188 | return 1 # path/.. on a different device as path |
| 189 | ino1 = s1[stat.ST_INO] |
| 190 | ino2 = s2[stat.ST_INO] |
| 191 | if ino1 == ino2: |
| 192 | return 1 # path/.. is the same i-node as path |
| 193 | return 0 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 194 | |
| 195 | |
| 196 | # Directory tree walk. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 197 | # For each directory under top (including top itself, but excluding |
| 198 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 199 | # dirname is the name of the directory and filenames is the list |
| 200 | # files files (and subdirectories etc.) in the directory. |
| 201 | # The func may modify the filenames list, to implement a filter, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 202 | # or to impose a different order of visiting. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 204 | def walk(top, func, arg): |
| 205 | try: |
| 206 | names = posix.listdir(top) |
| 207 | except posix.error: |
| 208 | return |
| 209 | func(arg, top, names) |
| 210 | exceptions = ('.', '..') |
| 211 | for name in names: |
| 212 | if name not in exceptions: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 213 | name = join(top, name) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 214 | if isdir(name) and not islink(name): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 215 | walk(name, func, arg) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | # Expand paths beginning with '~' or '~user'. |
| 219 | # '~' means $HOME; '~user' means that user's home directory. |
| 220 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 221 | # the path is returned unchanged (leaving error reporting to whatever |
| 222 | # function is called with the expanded path as argument). |
| 223 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 224 | # (A function should also be defined to do full *sh-style environment |
| 225 | # variable expansion.) |
| 226 | |
| 227 | def expanduser(path): |
| 228 | if path[:1] <> '~': |
| 229 | return path |
| 230 | i, n = 1, len(path) |
| 231 | while i < n and path[i] <> '/': |
| 232 | i = i+1 |
| 233 | if i == 1: |
| 234 | if not posix.environ.has_key('HOME'): |
| 235 | return path |
| 236 | userhome = posix.environ['HOME'] |
| 237 | else: |
| 238 | import pwd |
| 239 | try: |
| 240 | pwent = pwd.getpwnam(path[1:i]) |
| 241 | except KeyError: |
| 242 | return path |
| 243 | userhome = pwent[5] |
Guido van Rossum | bbb4e10 | 1996-04-02 22:30:03 +0000 | [diff] [blame] | 244 | if userhome[-1:] == '/': i = i+1 |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 245 | return userhome + path[i:] |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 246 | |
| 247 | |
| 248 | # Expand paths containing shell variable substitutions. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | # This expands the forms $variable and ${variable} only. |
| 250 | # Non-existant variables are left unchanged. |
| 251 | |
| 252 | _varprog = None |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 253 | |
| 254 | def expandvars(path): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | global _varprog |
Guido van Rossum | 4732ccf | 1992-08-09 13:54:50 +0000 | [diff] [blame] | 256 | if '$' not in path: |
| 257 | return path |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | if not _varprog: |
| 259 | import regex |
| 260 | _varprog = regex.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)') |
| 261 | i = 0 |
| 262 | while 1: |
| 263 | i = _varprog.search(path, i) |
| 264 | if i < 0: |
| 265 | break |
| 266 | name = _varprog.group(1) |
| 267 | j = i + len(_varprog.group(0)) |
| 268 | if name[:1] == '{' and name[-1:] == '}': |
| 269 | name = name[1:-1] |
| 270 | if posix.environ.has_key(name): |
| 271 | tail = path[j:] |
| 272 | path = path[:i] + posix.environ[name] |
| 273 | i = len(path) |
| 274 | path = path + tail |
| 275 | else: |
| 276 | i = j |
| 277 | return path |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 278 | |
| 279 | |
| 280 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
| 281 | # It should be understood that this may change the meaning of the path |
| 282 | # if it contains symbolic links! |
| 283 | |
| 284 | def normpath(path): |
| 285 | import string |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 286 | # Treat initial slashes specially |
| 287 | slashes = '' |
| 288 | while path[:1] == '/': |
| 289 | slashes = slashes + '/' |
| 290 | path = path[1:] |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 291 | comps = string.splitfields(path, '/') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 292 | i = 0 |
| 293 | while i < len(comps): |
| 294 | if comps[i] == '.': |
| 295 | del comps[i] |
| 296 | elif comps[i] == '..' and i > 0 and \ |
| 297 | comps[i-1] not in ('', '..'): |
| 298 | del comps[i-1:i+1] |
| 299 | i = i-1 |
| 300 | elif comps[i] == '' and i > 0 and comps[i-1] <> '': |
| 301 | del comps[i] |
| 302 | else: |
| 303 | i = i+1 |
| 304 | # If the path is now empty, substitute '.' |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 305 | if not comps and not slashes: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 306 | comps.append('.') |
Guido van Rossum | df56386 | 1993-07-06 15:19:36 +0000 | [diff] [blame] | 307 | return slashes + string.joinfields(comps, '/') |