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", |
Martin v. Löwis | 96a60e4 | 2002-12-31 13:11:54 +0000 | [diff] [blame] | 8 | "getatime","getctime", "islink","exists","isdir","isfile", |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 9 | "walk","expanduser","expandvars","normpath","abspath", |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 10 | "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 11 | "devnull","realpath","supports_unicode_filenames"] |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 12 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 13 | # strings representing various path-related bits and pieces |
| 14 | curdir = ':' |
| 15 | pardir = '::' |
| 16 | extsep = '.' |
| 17 | sep = ':' |
| 18 | pathsep = '\n' |
| 19 | defpath = ':' |
| 20 | altsep = None |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 21 | devnull = 'Dev:Null' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 22 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 23 | # 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] | 24 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 25 | def normcase(path): |
| 26 | return path.lower() |
Guido van Rossum | 599f2ed | 1992-01-14 18:28:18 +0000 | [diff] [blame] | 27 | |
| 28 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 29 | def isabs(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 30 | """Return true if a path is absolute. |
| 31 | On the Mac, relative paths begin with a colon, |
| 32 | but as a special case, paths with no colons at all are also relative. |
| 33 | Anything else is absolute (the string up to the first colon is the |
| 34 | volume name).""" |
| 35 | |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 36 | return ':' in s and s[0] != ':' |
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 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 39 | def join(s, *p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 40 | path = s |
| 41 | for t in p: |
| 42 | if (not s) or isabs(t): |
| 43 | path = t |
| 44 | continue |
| 45 | if t[:1] == ':': |
| 46 | t = t[1:] |
| 47 | if ':' not in path: |
| 48 | path = ':' + path |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 49 | if path[-1:] != ':': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 50 | path = path + ':' |
| 51 | path = path + t |
| 52 | return path |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 55 | def split(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 56 | """Split a pathname into two parts: the directory leading up to the final |
| 57 | bit, and the basename (the filename, without colons, in that directory). |
| 58 | 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] | 59 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | if ':' not in s: return '', s |
| 61 | colon = 0 |
| 62 | for i in range(len(s)): |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 63 | if s[i] == ':': colon = i + 1 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 64 | path, file = s[:colon-1], s[colon:] |
| 65 | if path and not ':' in path: |
| 66 | path = path + ':' |
| 67 | return path, file |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 69 | |
| 70 | def splitext(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 71 | """Split a path into root and extension. |
| 72 | The extension is everything starting at the last dot in the last |
| 73 | pathname component; the root is everything before that. |
| 74 | It is always true that root + ext == p.""" |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 75 | |
Martin v. Löwis | de33379 | 2002-12-12 20:30:20 +0000 | [diff] [blame] | 76 | i = p.rfind('.') |
| 77 | if i<=p.rfind(':'): |
| 78 | return p, '' |
| 79 | else: |
| 80 | return p[:i], p[i:] |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 82 | |
| 83 | def splitdrive(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 84 | """Split a pathname into a drive specification and the rest of the |
| 85 | path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
| 86 | empty (don't use the volume name -- it doesn't have the same |
| 87 | syntactic and semantic oddities as DOS drive letters, such as there |
| 88 | being a separate current directory per drive).""" |
| 89 | |
| 90 | return '', p |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 91 | |
| 92 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 93 | # Short interfaces to split() |
| 94 | |
| 95 | def dirname(s): return split(s)[0] |
| 96 | def basename(s): return split(s)[1] |
| 97 | |
Jack Jansen | 791f7d4 | 2003-01-15 22:45:48 +0000 | [diff] [blame] | 98 | def ismount(s): |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 99 | if not isabs(s): |
| 100 | return False |
| 101 | components = split(s) |
| 102 | return len(components) == 2 and components[1] == '' |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 104 | def isdir(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 105 | """Return true if the pathname refers to an existing directory.""" |
| 106 | |
| 107 | try: |
| 108 | st = os.stat(s) |
| 109 | except os.error: |
| 110 | return 0 |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 111 | return S_ISDIR(st.st_mode) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 114 | # Get size, mtime, atime of files. |
| 115 | |
| 116 | def getsize(filename): |
| 117 | """Return the size of a file, reported by os.stat().""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 118 | return os.stat(filename).st_size |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 119 | |
| 120 | def getmtime(filename): |
| 121 | """Return the last modification time of a file, reported by os.stat().""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 122 | return os.stat(filename).st_mtime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 123 | |
| 124 | def getatime(filename): |
| 125 | """Return the last access time of a file, reported by os.stat().""" |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 126 | return os.stat(filename).st_atime |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 127 | |
| 128 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 129 | def islink(s): |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 130 | """Return true if the pathname refers to a symbolic link.""" |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 131 | |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 132 | try: |
Jack Jansen | 98fc683 | 2003-02-27 23:18:46 +0000 | [diff] [blame] | 133 | import Carbon.File |
| 134 | return Carbon.File.ResolveAliasFile(s, 0)[2] |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 135 | except: |
| 136 | return False |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 139 | def isfile(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 140 | """Return true if the pathname refers to an existing regular file.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 142 | try: |
| 143 | st = os.stat(s) |
| 144 | except os.error: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 145 | return False |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 146 | return S_ISREG(st.st_mode) |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 147 | |
Martin v. Löwis | 96a60e4 | 2002-12-31 13:11:54 +0000 | [diff] [blame] | 148 | def getctime(filename): |
| 149 | """Return the creation time of a file, reported by os.stat().""" |
| 150 | return os.stat(filename).st_ctime |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 152 | def exists(s): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 153 | """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] | 154 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 155 | try: |
| 156 | st = os.stat(s) |
| 157 | except os.error: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 158 | return False |
| 159 | return True |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 160 | |
Jack Jansen | 03c06ee | 2000-08-23 09:13:40 +0000 | [diff] [blame] | 161 | # Return the longest prefix of all list elements. |
| 162 | |
| 163 | def commonprefix(m): |
| 164 | "Given a list of pathnames, returns the longest common leading component" |
| 165 | if not m: return '' |
| 166 | prefix = m[0] |
| 167 | for item in m: |
| 168 | for i in range(len(prefix)): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 169 | if prefix[:i+1] != item[:i+1]: |
Jack Jansen | 03c06ee | 2000-08-23 09:13:40 +0000 | [diff] [blame] | 170 | prefix = prefix[:i] |
| 171 | if i == 0: return '' |
| 172 | break |
| 173 | return prefix |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 174 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 175 | def expandvars(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 176 | """Dummy to retain interface-compatibility with other operating systems.""" |
| 177 | return path |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 179 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 180 | def expanduser(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 181 | """Dummy to retain interface-compatibility with other operating systems.""" |
| 182 | return path |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 183 | |
Neal Norwitz | 93cf79f | 2002-03-31 14:06:41 +0000 | [diff] [blame] | 184 | class norm_error(Exception): |
| 185 | """Path cannot be normalized""" |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 186 | |
| 187 | def normpath(s): |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 188 | """Normalize a pathname. Will return the same result for |
| 189 | equivalent paths.""" |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 190 | |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 191 | if ":" not in s: |
| 192 | return ":"+s |
| 193 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 194 | comps = s.split(":") |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 195 | i = 1 |
| 196 | while i < len(comps)-1: |
| 197 | if comps[i] == "" and comps[i-1] != "": |
| 198 | if i > 1: |
| 199 | del comps[i-1:i+1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 200 | i = i - 1 |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 201 | else: |
| 202 | # best way to handle this is to raise an exception |
Greg Ward | 034cbf1 | 2001-08-08 20:55:10 +0000 | [diff] [blame] | 203 | raise norm_error, 'Cannot use :: immediately after volume name' |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 204 | else: |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 205 | i = i + 1 |
| 206 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 207 | s = ":".join(comps) |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 208 | |
| 209 | # remove trailing ":" except for ":" and "Volume:" |
| 210 | if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s): |
| 211 | s = s[:-1] |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 212 | return s |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 213 | |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 214 | |
| 215 | def walk(top, func, arg): |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 216 | """Directory tree walk with callback function. |
| 217 | |
| 218 | For each directory in the directory tree rooted at top (including top |
| 219 | itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
| 220 | dirname is the name of the directory, and fnames a list of the names of |
| 221 | the files and subdirectories in dirname (excluding '.' and '..'). func |
| 222 | may modify the fnames list in-place (e.g. via del or slice assignment), |
| 223 | and walk will only recurse into the subdirectories whose names remain in |
| 224 | fnames; this can be used to implement a filter, or to impose a specific |
| 225 | order of visiting. No semantics are defined for, or required of, arg, |
| 226 | beyond that arg is always passed to func. It can be used, e.g., to pass |
| 227 | a filename pattern, or a mutable object designed to accumulate |
| 228 | statistics. Passing None for arg is common.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 229 | |
| 230 | try: |
| 231 | names = os.listdir(top) |
| 232 | except os.error: |
| 233 | return |
| 234 | func(arg, top, names) |
| 235 | for name in names: |
| 236 | name = join(top, name) |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 237 | if isdir(name) and not islink(name): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 238 | walk(name, func, arg) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 239 | |
| 240 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 241 | def abspath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 242 | """Return an absolute path.""" |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 243 | if not isabs(path): |
| 244 | path = join(os.getcwd(), path) |
| 245 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 246 | |
| 247 | # realpath is a no-op on systems without islink support |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 248 | def realpath(path): |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 249 | path = abspath(path) |
| 250 | try: |
Jack Jansen | 98fc683 | 2003-02-27 23:18:46 +0000 | [diff] [blame] | 251 | import Carbon.File |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 252 | except ImportError: |
| 253 | return path |
| 254 | if not path: |
| 255 | return path |
| 256 | components = path.split(':') |
| 257 | path = components[0] + ':' |
| 258 | for c in components[1:]: |
| 259 | path = join(path, c) |
Jack Jansen | 98fc683 | 2003-02-27 23:18:46 +0000 | [diff] [blame] | 260 | path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname() |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 261 | return path |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 262 | |
| 263 | supports_unicode_filenames = False |