Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # Module 'path' -- common operations on POSIX pathnames |
| 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 | |
| 34 | # Split a path in head (empty or ending in '/') and tail (no '/'). |
| 35 | # The tail will be empty if the path ends in '/'. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 36 | # It is always true that head + tail == p; also join(head, tail) == p. |
| 37 | # Note that because head ends in '/', if you want to find all components |
| 38 | # of a path by repeatedly getting the head, you will have to strip off |
| 39 | # the trailing '/' yourself (another function should be defined to |
| 40 | # split an entire path into components.) |
| 41 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 42 | def split(p): |
| 43 | head, tail = '', '' |
| 44 | for c in p: |
| 45 | tail = tail + c |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 46 | if c == '/': |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 47 | head, tail = head + tail, '' |
| 48 | return head, tail |
| 49 | |
| 50 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 51 | # Split a path in root and extension. |
| 52 | # The extension is everything starting at the first dot in the last |
| 53 | # pathname component; the root is everything before that. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 54 | # It is always true that root + ext == p. |
| 55 | |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 56 | def splitext(p): |
| 57 | root, ext = '', '' |
| 58 | for c in p: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 59 | if c == '/': |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 60 | root, ext = root + ext + c, '' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 61 | elif c == '.' or ext: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 62 | ext = ext + c |
| 63 | else: |
| 64 | root = root + c |
| 65 | return root, ext |
| 66 | |
| 67 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 68 | # Return the tail (basename) part of a path. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 70 | def basename(p): |
| 71 | return split(p)[1] |
| 72 | |
| 73 | |
| 74 | # Return the longest prefix of all list elements. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 76 | def commonprefix(m): |
| 77 | if not m: return '' |
| 78 | prefix = m[0] |
| 79 | for item in m: |
| 80 | for i in range(len(prefix)): |
| 81 | if prefix[:i+1] <> item[:i+1]: |
| 82 | prefix = prefix[:i] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 83 | if i == 0: return '' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 84 | break |
| 85 | return prefix |
| 86 | |
| 87 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 88 | # Is a path a symbolic link? |
| 89 | # This will always return false on systems where posix.lstat doesn't exist. |
| 90 | |
| 91 | def islink(path): |
| 92 | try: |
| 93 | st = posix.lstat(path) |
| 94 | except (posix.error, AttributeError): |
| 95 | return 0 |
| 96 | return stat.S_ISLNK(st[stat.ST_MODE]) |
| 97 | |
| 98 | |
| 99 | # Does a path exist? |
| 100 | # This is false for dangling symbolic links. |
| 101 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 102 | def exists(path): |
| 103 | try: |
| 104 | st = posix.stat(path) |
| 105 | except posix.error: |
| 106 | return 0 |
| 107 | return 1 |
| 108 | |
| 109 | |
| 110 | # Is a path a posix directory? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 111 | # This follows symbolic links, so both islink() and isdir() can be true |
| 112 | # for the same path. |
| 113 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 114 | def isdir(path): |
| 115 | try: |
| 116 | st = posix.stat(path) |
| 117 | except posix.error: |
| 118 | return 0 |
Guido van Rossum | 40d9304 | 1990-10-21 16:17:34 +0000 | [diff] [blame] | 119 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 120 | |
| 121 | |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 122 | # Is a path a regulat file? |
| 123 | # This follows symbolic links, so both islink() and isdir() can be true |
| 124 | # for the same path. |
| 125 | |
| 126 | def isfile(path): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 127 | try: |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 128 | st = posix.stat(path) |
| 129 | except posix.error: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 130 | return 0 |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 131 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 132 | |
| 133 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 134 | # Are two filenames really pointing to the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 136 | def samefile(f1, f2): |
| 137 | s1 = posix.stat(f1) |
| 138 | s2 = posix.stat(f2) |
| 139 | return samestat(s1, s2) |
| 140 | |
| 141 | |
| 142 | # Are two open files really referencing the same file? |
| 143 | # (Not necessarily the same file descriptor!) |
| 144 | # XXX Oops, posix.fstat() doesn't exist yet! |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 146 | def sameopenfile(fp1, fp2): |
| 147 | s1 = posix.fstat(fp1) |
| 148 | s2 = posix.fstat(fp2) |
| 149 | return samestat(s1, s2) |
| 150 | |
| 151 | |
| 152 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 153 | # describing the same file? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 155 | def samestat(s1, s2): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 156 | return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
| 157 | s1[stat.ST_DEV] == s2[stat.STD_DEV] |
Guido van Rossum | d3778f9 | 1991-11-12 15:37:40 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | # Subroutine and global data used by ismount(). |
| 161 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 162 | _mounts = [] |
| 163 | |
| 164 | def _getmounts(): |
| 165 | import commands, string |
| 166 | mounts = [] |
| 167 | data = commands.getoutput('/etc/mount') |
| 168 | lines = string.splitfields(data, '\n') |
| 169 | for line in lines: |
| 170 | words = string.split(line) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 171 | if len(words) >= 3 and words[1] == 'on': |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 172 | mounts.append(words[2]) |
| 173 | return mounts |
| 174 | |
| 175 | |
| 176 | # Is a path a mount point? |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 177 | # This only works for normalized paths, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 178 | # and only if the mount table as printed by /etc/mount is correct. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 179 | # It tries to make relative paths absolute by prefixing them with the |
| 180 | # current directory, but it won't normalize arguments containing '../' |
| 181 | # or symbolic links. |
| 182 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 183 | def ismount(path): |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 184 | if not isabs(path): |
| 185 | path = join(posix.getcwd(), path) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 186 | if not _mounts: |
| 187 | _mounts[:] = _getmounts() |
| 188 | return path in _mounts |
| 189 | |
| 190 | |
| 191 | # Directory tree walk. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 192 | # For each directory under top (including top itself, but excluding |
| 193 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 194 | # dirname is the name of the directory and filenames is the list |
| 195 | # files files (and subdirectories etc.) in the directory. |
| 196 | # The func may modify the filenames list, to implement a filter, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 197 | # or to impose a different order of visiting. |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 199 | def walk(top, func, arg): |
| 200 | try: |
| 201 | names = posix.listdir(top) |
| 202 | except posix.error: |
| 203 | return |
| 204 | func(arg, top, names) |
| 205 | exceptions = ('.', '..') |
| 206 | for name in names: |
| 207 | if name not in exceptions: |
Guido van Rossum | 4d0fdc3 | 1991-08-16 13:27:58 +0000 | [diff] [blame] | 208 | name = join(top, name) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 209 | if isdir(name): |
| 210 | walk(name, func, arg) |
Guido van Rossum | 7ac4878 | 1992-01-14 18:29:32 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | # Expand paths beginning with '~' or '~user'. |
| 214 | # '~' means $HOME; '~user' means that user's home directory. |
| 215 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 216 | # the path is returned unchanged (leaving error reporting to whatever |
| 217 | # function is called with the expanded path as argument). |
| 218 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 219 | # (A function should also be defined to do full *sh-style environment |
| 220 | # variable expansion.) |
| 221 | |
| 222 | def expanduser(path): |
| 223 | if path[:1] <> '~': |
| 224 | return path |
| 225 | i, n = 1, len(path) |
| 226 | while i < n and path[i] <> '/': |
| 227 | i = i+1 |
| 228 | if i == 1: |
| 229 | if not posix.environ.has_key('HOME'): |
| 230 | return path |
| 231 | userhome = posix.environ['HOME'] |
| 232 | else: |
| 233 | import pwd |
| 234 | try: |
| 235 | pwent = pwd.getpwnam(path[1:i]) |
| 236 | except KeyError: |
| 237 | return path |
| 238 | userhome = pwent[5] |
| 239 | return userhome + path[i:] |