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