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