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, '' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 65 | elif c == '.' or ext: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 66 | ext = ext + c |
| 67 | else: |
| 68 | root = root + c |
| 69 | return root, ext |
| 70 | |
| 71 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 72 | # Return the tail (basename) part of a path. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 74 | def basename(p): |
| 75 | return split(p)[1] |
| 76 | |
| 77 | |
| 78 | # Return the longest prefix of all list elements. |
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 commonprefix(m): |
| 81 | if not m: return '' |
| 82 | prefix = m[0] |
| 83 | for item in m: |
| 84 | for i in range(len(prefix)): |
| 85 | if prefix[:i+1] <> item[:i+1]: |
| 86 | prefix = prefix[:i] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 87 | if i == 0: return '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 88 | break |
| 89 | return prefix |
| 90 | |
| 91 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 92 | # Is a path a symbolic link? |
| 93 | # This will always return false on systems where posix.lstat doesn't exist. |
| 94 | |
| 95 | def islink(path): |
| 96 | try: |
| 97 | st = posix.lstat(path) |
| 98 | except (posix.error, AttributeError): |
| 99 | return 0 |
| 100 | return stat.S_ISLNK(st[stat.ST_MODE]) |
| 101 | |
| 102 | |
| 103 | # Does a path exist? |
| 104 | # This is false for dangling symbolic links. |
| 105 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 106 | def exists(path): |
| 107 | try: |
| 108 | st = posix.stat(path) |
| 109 | except posix.error: |
| 110 | return 0 |
| 111 | return 1 |
| 112 | |
| 113 | |
| 114 | # Is a path a posix directory? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 115 | # This follows symbolic links, so both islink() and isdir() can be true |
| 116 | # for the same path. |
| 117 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 118 | def isdir(path): |
| 119 | try: |
| 120 | st = posix.stat(path) |
| 121 | except posix.error: |
| 122 | return 0 |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 123 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 124 | |
| 125 | |
Guido van Rossum | 2684738 | 1992-03-31 18:54:35 +0000 | [diff] [blame] | 126 | # Is a path a regular file? |
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 | |
| 130 | def isfile(path): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 131 | try: |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 132 | st = posix.stat(path) |
| 133 | except posix.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 134 | return 0 |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 135 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 136 | |
| 137 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 138 | # Are two filenames really pointing to the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 140 | def samefile(f1, f2): |
| 141 | s1 = posix.stat(f1) |
| 142 | s2 = posix.stat(f2) |
| 143 | return samestat(s1, s2) |
| 144 | |
| 145 | |
| 146 | # Are two open files really referencing the same file? |
| 147 | # (Not necessarily the same file descriptor!) |
| 148 | # XXX Oops, posix.fstat() doesn't exist yet! |
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 sameopenfile(fp1, fp2): |
| 151 | s1 = posix.fstat(fp1) |
| 152 | s2 = posix.fstat(fp2) |
| 153 | return samestat(s1, s2) |
| 154 | |
| 155 | |
| 156 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 157 | # describing the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 159 | def samestat(s1, s2): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 160 | return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 161 | s1[stat.ST_DEV] == s2[stat.ST_DEV] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | # Is a path a mount point? |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 165 | # (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] | 166 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 167 | def ismount(path): |
Guido van Rossum | 509d24a | 1992-05-06 11:36:49 +0000 | [diff] [blame] | 168 | try: |
| 169 | s1 = posix.stat(path) |
| 170 | s2 = posix.stat(join(path, '..')) |
| 171 | except posix.error: |
| 172 | return 0 # It doesn't exist -- so not a mount point :-) |
| 173 | dev1 = s1[stat.ST_DEV] |
| 174 | dev2 = s2[stat.ST_DEV] |
| 175 | if dev1 != dev2: |
| 176 | return 1 # path/.. on a different device as path |
| 177 | ino1 = s1[stat.ST_INO] |
| 178 | ino2 = s2[stat.ST_INO] |
| 179 | if ino1 == ino2: |
| 180 | return 1 # path/.. is the same i-node as path |
| 181 | return 0 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | # Directory tree walk. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 185 | # For each directory under top (including top itself, but excluding |
| 186 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 187 | # dirname is the name of the directory and filenames is the list |
| 188 | # files files (and subdirectories etc.) in the directory. |
| 189 | # The func may modify the filenames list, to implement a filter, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 190 | # or to impose a different order of visiting. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 192 | def walk(top, func, arg): |
| 193 | try: |
| 194 | names = posix.listdir(top) |
| 195 | except posix.error: |
| 196 | return |
| 197 | func(arg, top, names) |
| 198 | exceptions = ('.', '..') |
| 199 | for name in names: |
| 200 | if name not in exceptions: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 201 | name = join(top, name) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 202 | if isdir(name): |
| 203 | walk(name, func, arg) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | # Expand paths beginning with '~' or '~user'. |
| 207 | # '~' means $HOME; '~user' means that user's home directory. |
| 208 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 209 | # the path is returned unchanged (leaving error reporting to whatever |
| 210 | # function is called with the expanded path as argument). |
| 211 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 212 | # (A function should also be defined to do full *sh-style environment |
| 213 | # variable expansion.) |
| 214 | |
| 215 | def expanduser(path): |
| 216 | if path[:1] <> '~': |
| 217 | return path |
| 218 | i, n = 1, len(path) |
| 219 | while i < n and path[i] <> '/': |
| 220 | i = i+1 |
| 221 | if i == 1: |
| 222 | if not posix.environ.has_key('HOME'): |
| 223 | return path |
| 224 | userhome = posix.environ['HOME'] |
| 225 | else: |
| 226 | import pwd |
| 227 | try: |
| 228 | pwent = pwd.getpwnam(path[1:i]) |
| 229 | except KeyError: |
| 230 | return path |
| 231 | userhome = pwent[5] |
| 232 | return userhome + path[i:] |