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