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