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