Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 1 | # module 'macpath' -- pathname (or -related) operations for the Macintosh |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 599f2ed | 1992-01-14 18:28:18 +0000 | [diff] [blame] | 3 | import string |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 4 | import mac |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 5 | from stat import * |
| 6 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 599f2ed | 1992-01-14 18:28:18 +0000 | [diff] [blame] | 8 | # Normalize the case of a pathname. Dummy in Posix, but string.lower here. |
| 9 | |
| 10 | normcase = string.lower |
| 11 | |
| 12 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 13 | # Return true if a path is absolute. |
| 14 | # On the Mac, relative paths begin with a colon, |
| 15 | # but as a special case, paths with no colons at all are also relative. |
| 16 | # Anything else is absolute (the string up to the first colon is the |
| 17 | # volume name). |
| 18 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 19 | def isabs(s): |
| 20 | return ':' in s and s[0] <> ':' |
| 21 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 22 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 23 | def join(s, *p): |
| 24 | path = s |
| 25 | for t in p: |
| 26 | if (not s) or isabs(t): |
| 27 | path = t |
| 28 | continue |
| 29 | if t[:1] == ':': |
| 30 | t = t[1:] |
| 31 | if ':' not in path: |
| 32 | path = ':' + path |
| 33 | if path[-1:] <> ':': |
| 34 | path = path + ':' |
| 35 | path = path + t |
| 36 | return path |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 38 | |
| 39 | # Split a pathname in two parts: the directory leading up to the final bit, |
| 40 | # and the basename (the filename, without colons, in that directory). |
Guido van Rossum | fbe0a8e | 1991-08-16 13:27:45 +0000 | [diff] [blame] | 41 | # The result (s, t) is such that join(s, t) yields the original argument. |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 42 | |
| 43 | def split(s): |
| 44 | if ':' not in s: return '', s |
| 45 | colon = 0 |
| 46 | for i in range(len(s)): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 47 | if s[i] == ':': colon = i+1 |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 48 | return s[:colon-1], s[colon:] |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 49 | |
| 50 | |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 51 | # Split a path in root and extension. |
| 52 | # The extension is everything starting at the last dot in the last |
| 53 | # pathname component; the root is everything before that. |
| 54 | # It is always true that root + ext == p. |
| 55 | |
| 56 | def splitext(p): |
| 57 | root, ext = '', '' |
| 58 | for c in p: |
| 59 | if c == ':': |
| 60 | root, ext = root + ext + c, '' |
| 61 | elif c == '.': |
| 62 | if ext: |
| 63 | root, ext = root + ext, c |
| 64 | else: |
| 65 | ext = c |
| 66 | elif ext: |
| 67 | ext = ext + c |
| 68 | else: |
| 69 | root = root + c |
| 70 | return root, ext |
| 71 | |
| 72 | |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 73 | # Split a pathname into a drive specification and the rest of the |
| 74 | # path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
| 75 | # empty (don't use the volume name -- it doesn't have the same |
| 76 | # syntactic and semantic oddities as DOS drive letters, such as there |
| 77 | # being a separate current directory per drive). |
| 78 | |
| 79 | def splitdrive(p): |
| 80 | return '', p |
| 81 | |
| 82 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 83 | # Short interfaces to split() |
| 84 | |
| 85 | def dirname(s): return split(s)[0] |
| 86 | def basename(s): return split(s)[1] |
| 87 | |
| 88 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 89 | # Return true if the pathname refers to an existing directory. |
| 90 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 91 | def isdir(s): |
| 92 | try: |
| 93 | st = mac.stat(s) |
| 94 | except mac.error: |
| 95 | return 0 |
| 96 | return S_ISDIR(st[ST_MODE]) |
| 97 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 99 | # Return true if the pathname refers to a symbolic link. |
| 100 | # (Always false on the Mac, until we understand Aliases.) |
| 101 | |
| 102 | def islink(s): |
| 103 | return 0 |
| 104 | |
| 105 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 106 | # Return true if the pathname refers to an existing regular file. |
| 107 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 108 | def isfile(s): |
| 109 | try: |
| 110 | st = mac.stat(s) |
| 111 | except mac.error: |
| 112 | return 0 |
| 113 | return S_ISREG(st[ST_MODE]) |
| 114 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 115 | |
| 116 | # Return true if the pathname refers to an existing file or directory. |
| 117 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 118 | def exists(s): |
| 119 | try: |
| 120 | st = mac.stat(s) |
| 121 | except mac.error: |
| 122 | return 0 |
| 123 | return 1 |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 124 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 125 | # |
| 126 | # dummy expandvars to retain interface-compatability with other |
| 127 | # operating systems. |
| 128 | def expandvars(path): |
| 129 | return path |
| 130 | |
| 131 | # |
| 132 | # dummy expanduser to retain interface-compatability with other |
| 133 | # operating systems. |
| 134 | def expanduser(path): |
| 135 | return path |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 137 | # Normalize a pathname: get rid of '::' sequences by backing up, |
| 138 | # e.g., 'foo:bar::bletch' becomes 'foo:bletch'. |
| 139 | # Raise the exception norm_error below if backing up is impossible, |
| 140 | # e.g., for '::foo'. |
| 141 | # XXX The Unix version doesn't raise an exception but simply |
| 142 | # returns an unnormalized path. Should do so here too. |
| 143 | |
| 144 | norm_error = 'macpath.norm_error: path cannot be normalized' |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 145 | |
| 146 | def normpath(s): |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 147 | import string |
| 148 | if ':' not in s: |
| 149 | return ':' + s |
| 150 | f = string.splitfields(s, ':') |
| 151 | pre = [] |
| 152 | post = [] |
| 153 | if not f[0]: |
| 154 | pre = f[:1] |
| 155 | f = f[1:] |
| 156 | if not f[len(f)-1]: |
| 157 | post = f[-1:] |
| 158 | f = f[:-1] |
| 159 | res = [] |
| 160 | for seg in f: |
| 161 | if seg: |
| 162 | res.append(seg) |
| 163 | else: |
| 164 | if not res: raise norm_error, 'path starts with ::' |
| 165 | del res[len(res)-1] |
| 166 | if not (pre or res): |
| 167 | raise norm_error, 'path starts with volume::' |
| 168 | if pre: res = pre + res |
| 169 | if post: res = res + post |
| 170 | s = res[0] |
| 171 | for seg in res[1:]: |
| 172 | s = s + ':' + seg |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 173 | return s |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 175 | |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 176 | # Directory tree walk. |
| 177 | # For each directory under top (including top itself), |
| 178 | # func(arg, dirname, filenames) is called, where |
| 179 | # dirname is the name of the directory and filenames is the list |
| 180 | # of files (and subdirectories etc.) in the directory. |
| 181 | # The func may modify the filenames list, to implement a filter, |
| 182 | # or to impose a different order of visiting. |
| 183 | |
| 184 | def walk(top, func, arg): |
| 185 | try: |
| 186 | names = mac.listdir(top) |
| 187 | except mac.error: |
| 188 | return |
| 189 | func(arg, top, names) |
| 190 | for name in names: |
| 191 | name = join(top, name) |
| 192 | if isdir(name): |
| 193 | walk(name, func, arg) |