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", |
Benjamin Peterson | d71ca41 | 2008-05-08 23:44:58 +0000 | [diff] [blame] | 11 | "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 |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 16 | # These are primarily for export; internally, they are hardcoded. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 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 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 26 | def _get_colon(path): |
| 27 | if isinstance(path, bytes): |
| 28 | return b':' |
| 29 | else: |
| 30 | return ':' |
| 31 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 32 | # 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] | 33 | |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 34 | def normcase(path): |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 35 | if not isinstance(path, (bytes, str)): |
| 36 | raise TypeError("normcase() argument must be str or bytes, " |
| 37 | "not '{}'".format(path.__class__.__name__)) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 38 | return path.lower() |
Guido van Rossum | 599f2ed | 1992-01-14 18:28:18 +0000 | [diff] [blame] | 39 | |
| 40 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 41 | def isabs(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 42 | """Return true if a path is absolute. |
| 43 | On the Mac, relative paths begin with a colon, |
| 44 | but as a special case, paths with no colons at all are also relative. |
| 45 | Anything else is absolute (the string up to the first colon is the |
| 46 | volume name).""" |
| 47 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 48 | colon = _get_colon(s) |
| 49 | return colon in s and s[:1] != colon |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 51 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 52 | def join(s, *p): |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 53 | colon = _get_colon(s) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 54 | path = s |
| 55 | for t in p: |
| 56 | if (not s) or isabs(t): |
| 57 | path = t |
| 58 | continue |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 59 | if t[:1] == colon: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | t = t[1:] |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 61 | if colon not in path: |
| 62 | path = colon + path |
| 63 | if path[-1:] != colon: |
| 64 | path = path + colon |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 65 | path = path + t |
| 66 | return path |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 69 | def split(s): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 70 | """Split a pathname into two parts: the directory leading up to the final |
| 71 | bit, and the basename (the filename, without colons, in that directory). |
| 72 | 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] | 73 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 74 | colon = _get_colon(s) |
| 75 | if colon not in s: return s[:0], s |
| 76 | col = 0 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 77 | for i in range(len(s)): |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 78 | if s[i:i+1] == colon: col = i + 1 |
| 79 | path, file = s[:col-1], s[col:] |
| 80 | if path and not colon in path: |
| 81 | path = path + colon |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 82 | return path, file |
Guido van Rossum | b5e05e9 | 1991-01-01 18:10:40 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | a48bf79 | 1996-07-23 02:28:32 +0000 | [diff] [blame] | 84 | |
| 85 | def splitext(p): |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 86 | if isinstance(p, bytes): |
| 87 | return genericpath._splitext(p, b':', altsep, b'.') |
| 88 | else: |
| 89 | return genericpath._splitext(p, sep, altsep, extsep) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 90 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 91 | |
| 92 | def splitdrive(p): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 93 | """Split a pathname into a drive specification and the rest of the |
| 94 | path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
| 95 | empty (don't use the volume name -- it doesn't have the same |
| 96 | syntactic and semantic oddities as DOS drive letters, such as there |
| 97 | being a separate current directory per drive).""" |
| 98 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 99 | return p[:0], p |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 100 | |
| 101 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 102 | # Short interfaces to split() |
| 103 | |
| 104 | def dirname(s): return split(s)[0] |
| 105 | def basename(s): return split(s)[1] |
| 106 | |
Jack Jansen | 791f7d4 | 2003-01-15 22:45:48 +0000 | [diff] [blame] | 107 | def ismount(s): |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 108 | if not isabs(s): |
| 109 | return False |
| 110 | components = split(s) |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 111 | return len(components) == 2 and not components[1] |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 113 | def islink(s): |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 114 | """Return true if the pathname refers to a symbolic link.""" |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 115 | |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 116 | try: |
Jack Jansen | 98fc683 | 2003-02-27 23:18:46 +0000 | [diff] [blame] | 117 | import Carbon.File |
| 118 | return Carbon.File.ResolveAliasFile(s, 0)[2] |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 119 | except: |
| 120 | return False |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 121 | |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 122 | # Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any |
| 123 | # case. |
| 124 | |
| 125 | def lexists(path): |
| 126 | """Test whether a path exists. Returns True for broken symbolic links""" |
| 127 | |
| 128 | try: |
| 129 | st = os.lstat(path) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 130 | except OSError: |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 131 | return False |
| 132 | return True |
| 133 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 134 | def expandvars(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 135 | """Dummy to retain interface-compatibility with other operating systems.""" |
| 136 | return path |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 138 | |
Jack Jansen | f4e7d2a | 1995-12-15 13:23:37 +0000 | [diff] [blame] | 139 | def expanduser(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 140 | """Dummy to retain interface-compatibility with other operating systems.""" |
| 141 | return path |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 142 | |
Neal Norwitz | 93cf79f | 2002-03-31 14:06:41 +0000 | [diff] [blame] | 143 | class norm_error(Exception): |
| 144 | """Path cannot be normalized""" |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 145 | |
| 146 | def normpath(s): |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 147 | """Normalize a pathname. Will return the same result for |
| 148 | equivalent paths.""" |
Jack Jansen | a68bfe2 | 1995-08-07 14:09:27 +0000 | [diff] [blame] | 149 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 150 | colon = _get_colon(s) |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 151 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 152 | if colon not in s: |
| 153 | return colon + s |
| 154 | |
| 155 | comps = s.split(colon) |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 156 | i = 1 |
| 157 | while i < len(comps)-1: |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 158 | if not comps[i] and comps[i-1]: |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 159 | if i > 1: |
| 160 | del comps[i-1:i+1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 161 | i = i - 1 |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 162 | else: |
| 163 | # best way to handle this is to raise an exception |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 164 | raise norm_error('Cannot use :: immediately after volume name') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 165 | else: |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 166 | i = i + 1 |
| 167 | |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 168 | s = colon.join(comps) |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 169 | |
| 170 | # remove trailing ":" except for ":" and "Volume:" |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 171 | if s[-1:] == colon and len(comps) > 2 and s != colon*len(s): |
Jack Jansen | 2fc0109 | 2000-08-06 21:18:35 +0000 | [diff] [blame] | 172 | s = s[:-1] |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 173 | return s |
Guido van Rossum | 0ec3126 | 1995-08-10 18:09:16 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 175 | def abspath(path): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 176 | """Return an absolute path.""" |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 177 | if not isabs(path): |
Florent Xicluna | 19b02d4 | 2010-03-08 12:25:35 +0000 | [diff] [blame] | 178 | if isinstance(path, bytes): |
| 179 | cwd = os.getcwdb() |
| 180 | else: |
| 181 | cwd = os.getcwd() |
| 182 | path = join(cwd, path) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 183 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 184 | |
| 185 | # realpath is a no-op on systems without islink support |
Jack Jansen | 992d58b | 2002-04-22 13:55:43 +0000 | [diff] [blame] | 186 | def realpath(path): |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 187 | path = abspath(path) |
| 188 | try: |
Jack Jansen | 98fc683 | 2003-02-27 23:18:46 +0000 | [diff] [blame] | 189 | import Carbon.File |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 190 | except ImportError: |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 191 | return path |
| 192 | if not path: |
| 193 | return path |
Amaury Forgeot d'Arc | b186f34 | 2008-10-03 21:57:20 +0000 | [diff] [blame] | 194 | colon = _get_colon(path) |
| 195 | components = path.split(colon) |
| 196 | path = components[0] + colon |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 197 | for c in components[1:]: |
| 198 | path = join(path, c) |
Florent Xicluna | 54467e0 | 2010-03-21 12:29:50 +0000 | [diff] [blame] | 199 | try: |
| 200 | path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname() |
| 201 | except Carbon.File.Error: |
| 202 | pass |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 203 | return path |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 204 | |
Victor Stinner | ccb706c | 2010-09-13 19:41:36 +0000 | [diff] [blame] | 205 | supports_unicode_filenames = True |