Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Pathname and path-related operations for the Macintosh.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 5c1d229 | 1998-03-03 21:49:01 +0000 | [diff] [blame] | 3 | import os |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 4 | from stat import * |
| 5 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 6 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 7 | "basename","dirname","commonprefix","getsize","getmtime", |
| 8 | "getatime","islink","exists","isdir","isfile", |
| 9 | "walk","expanduser","expandvars","normpath","abspath"] |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 10 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 11 | # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here. |
Guido van Rossum | 599f2ed | 1992-01-14 18:28:18 +0000 | [diff] [blame] | 12 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 13 | def normcase(path): |
| 14 | return path.lower() |
Guido van Rossum | 599f2ed | 1992-01-14 18:28:18 +0000 | [diff] [blame] | 15 | |
| 16 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 17 | def isabs(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 18 | """Return true if a path is absolute. |
| 19 | On the Mac, relative paths begin with a colon, |
| 20 | but as a special case, paths with no colons at all are also relative. |
| 21 | Anything else is absolute (the string up to the first colon is the |
| 22 | volume name).""" |
| 23 | |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 24 | return ':' in s and s[0] != ':' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 26 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 27 | def join(s, *p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 28 | path = s |
| 29 | for t in p: |
| 30 | if (not s) or isabs(t): |
| 31 | path = t |
| 32 | continue |
| 33 | if t[:1] == ':': |
| 34 | t = t[1:] |
| 35 | if ':' not in path: |
| 36 | path = ':' + path |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 37 | if path[-1:] != ':': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 38 | path = path + ':' |
| 39 | path = path + t |
| 40 | return path |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 43 | def split(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 44 | """Split a pathname into two parts: the directory leading up to the final |
| 45 | bit, and the basename (the filename, without colons, in that directory). |
| 46 | 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] | 47 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 48 | if ':' not in s: return '', s |
| 49 | colon = 0 |
| 50 | for i in range(len(s)): |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 51 | if s[i] == ':': colon = i + 1 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 52 | path, file = s[:colon-1], s[colon:] |
| 53 | if path and not ':' in path: |
| 54 | path = path + ':' |
| 55 | return path, file |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 57 | |
| 58 | def splitext(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 59 | """Split a path into root and extension. |
| 60 | The extension is everything starting at the last dot in the last |
| 61 | pathname component; the root is everything before that. |
| 62 | It is always true that root + ext == p.""" |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 64 | root, ext = '', '' |
| 65 | for c in p: |
| 66 | if c == ':': |
| 67 | root, ext = root + ext + c, '' |
| 68 | elif c == '.': |
| 69 | if ext: |
| 70 | root, ext = root + ext, c |
| 71 | else: |
| 72 | ext = c |
| 73 | elif ext: |
| 74 | ext = ext + c |
| 75 | else: |
| 76 | root = root + c |
| 77 | return root, ext |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 79 | |
| 80 | def splitdrive(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 81 | """Split a pathname into a drive specification and the rest of the |
| 82 | path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
| 83 | empty (don't use the volume name -- it doesn't have the same |
| 84 | syntactic and semantic oddities as DOS drive letters, such as there |
| 85 | being a separate current directory per drive).""" |
| 86 | |
| 87 | return '', p |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 88 | |
| 89 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 90 | # Short interfaces to split() |
| 91 | |
| 92 | def dirname(s): return split(s)[0] |
| 93 | def basename(s): return split(s)[1] |
| 94 | |
| 95 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 96 | def isdir(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 97 | """Return true if the pathname refers to an existing directory.""" |
| 98 | |
| 99 | try: |
| 100 | st = os.stat(s) |
| 101 | except os.error: |
| 102 | return 0 |
| 103 | return S_ISDIR(st[ST_MODE]) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 106 | # Get size, mtime, atime of files. |
| 107 | |
| 108 | def getsize(filename): |
| 109 | """Return the size of a file, reported by os.stat().""" |
| 110 | st = os.stat(filename) |
Fred Drake | 69f87c5 | 1999-07-23 15:04:05 +0000 | [diff] [blame] | 111 | return st[ST_SIZE] |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 112 | |
| 113 | def getmtime(filename): |
| 114 | """Return the last modification time of a file, reported by os.stat().""" |
| 115 | st = os.stat(filename) |
Fred Drake | 69f87c5 | 1999-07-23 15:04:05 +0000 | [diff] [blame] | 116 | return st[ST_MTIME] |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 117 | |
| 118 | def getatime(filename): |
| 119 | """Return the last access time of a file, reported by os.stat().""" |
| 120 | st = os.stat(filename) |
Guido van Rossum | 46d5651 | 2000-07-01 10:52:49 +0000 | [diff] [blame] | 121 | return st[ST_ATIME] |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 122 | |
| 123 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 124 | def islink(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 125 | """Return true if the pathname refers to a symbolic link. |
| 126 | Always false on the Mac, until we understand Aliases.)""" |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 128 | return 0 |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 131 | def isfile(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 132 | """Return true if the pathname refers to an existing regular file.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 134 | try: |
| 135 | st = os.stat(s) |
| 136 | except os.error: |
| 137 | return 0 |
| 138 | return S_ISREG(st[ST_MODE]) |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 141 | def exists(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 142 | """Return true if the pathname refers to an existing file or directory.""" |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 144 | try: |
| 145 | st = os.stat(s) |
| 146 | except os.error: |
| 147 | return 0 |
| 148 | return 1 |
| 149 | |
Jack Jansen | 03c06ee | 2000-08-23 09:13:40 +0000 | [diff] [blame] | 150 | # Return the longest prefix of all list elements. |
| 151 | |
| 152 | def commonprefix(m): |
| 153 | "Given a list of pathnames, returns the longest common leading component" |
| 154 | if not m: return '' |
| 155 | prefix = m[0] |
| 156 | for item in m: |
| 157 | for i in range(len(prefix)): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 158 | if prefix[:i+1] != item[:i+1]: |
Jack Jansen | 03c06ee | 2000-08-23 09:13:40 +0000 | [diff] [blame] | 159 | prefix = prefix[:i] |
| 160 | if i == 0: return '' |
| 161 | break |
| 162 | return prefix |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 163 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 164 | def expandvars(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 165 | """Dummy to retain interface-compatibility with other operating systems.""" |
| 166 | return path |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 168 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 169 | def expanduser(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 170 | """Dummy to retain interface-compatibility with other operating systems.""" |
| 171 | return path |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 172 | |
| 173 | norm_error = 'macpath.norm_error: path cannot be normalized' |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 174 | |
| 175 | def normpath(s): |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 176 | """Normalize a pathname. Will return the same result for |
| 177 | equivalent paths.""" |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 178 | |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 179 | if ":" not in s: |
| 180 | return ":"+s |
| 181 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 182 | comps = s.split(":") |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 183 | i = 1 |
| 184 | while i < len(comps)-1: |
| 185 | if comps[i] == "" and comps[i-1] != "": |
| 186 | if i > 1: |
| 187 | del comps[i-1:i+1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 188 | i = i - 1 |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 189 | else: |
| 190 | # best way to handle this is to raise an exception |
| 191 | raise norm_error, 'Cannot use :: immedeately after volume name' |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 192 | else: |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 193 | i = i + 1 |
| 194 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 195 | s = ":".join(comps) |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 196 | |
| 197 | # remove trailing ":" except for ":" and "Volume:" |
| 198 | if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s): |
| 199 | s = s[:-1] |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 200 | return s |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 201 | |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 202 | |
| 203 | def walk(top, func, arg): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 204 | """Directory tree walk. |
| 205 | For each directory under top (including top itself), |
| 206 | func(arg, dirname, filenames) is called, where |
| 207 | dirname is the name of the directory and filenames is the list |
| 208 | of files (and subdirectories etc.) in the directory. |
| 209 | The func may modify the filenames list, to implement a filter, |
| 210 | or to impose a different order of visiting.""" |
| 211 | |
| 212 | try: |
| 213 | names = os.listdir(top) |
| 214 | except os.error: |
| 215 | return |
| 216 | func(arg, top, names) |
| 217 | for name in names: |
| 218 | name = join(top, name) |
| 219 | if isdir(name): |
| 220 | walk(name, func, arg) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 221 | |
| 222 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 223 | def abspath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 224 | """Return an absolute path.""" |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 225 | if not isabs(path): |
| 226 | path = join(os.getcwd(), path) |
| 227 | return normpath(path) |