blob: 7d6ae41ddfb2878b634ef3b906a3946e6a78f54b [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Pathname and path-related operations for the Macintosh."""
Guido van Rossum217a5fa1990-12-26 15:40:07 +00002
Guido van Rossum5c1d2291998-03-03 21:49:01 +00003import os
Guido van Rossum217a5fa1990-12-26 15:40:07 +00004from stat import *
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +00006from genericpath import *
Guido van Rossum217a5fa1990-12-26 15:40:07 +00007
Skip Montanaro17ab1232001-01-24 06:27:27 +00008__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
9 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000010 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
Benjamin Petersond71ca412008-05-08 23:44:58 +000011 "expanduser","expandvars","normpath","abspath",
Skip Montanaro117910d2003-02-14 19:35:31 +000012 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000013 "devnull","realpath","supports_unicode_filenames"]
Guido van Rossumb5e05e91991-01-01 18:10:40 +000014
Skip Montanaro117910d2003-02-14 19:35:31 +000015# strings representing various path-related bits and pieces
16curdir = ':'
17pardir = '::'
18extsep = '.'
19sep = ':'
20pathsep = '\n'
21defpath = ':'
22altsep = None
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000023devnull = 'Dev:Null'
Skip Montanaro117910d2003-02-14 19:35:31 +000024
Fred Drakeb4e460a2000-09-28 16:25:20 +000025# Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
Guido van Rossum599f2ed1992-01-14 18:28:18 +000026
Fred Drakeb4e460a2000-09-28 16:25:20 +000027def normcase(path):
28 return path.lower()
Guido van Rossum599f2ed1992-01-14 18:28:18 +000029
30
Guido van Rossum217a5fa1990-12-26 15:40:07 +000031def isabs(s):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000032 """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 Drake8152d322000-12-12 23:20:45 +000038 return ':' in s and s[0] != ':'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000039
Guido van Rossumb5e05e91991-01-01 18:10:40 +000040
Barry Warsaw384d2491997-02-18 21:53:25 +000041def join(s, *p):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000042 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 Drake8152d322000-12-12 23:20:45 +000051 if path[-1:] != ':':
Guido van Rossum54f22ed2000-02-04 15:10:34 +000052 path = path + ':'
53 path = path + t
54 return path
Guido van Rossum217a5fa1990-12-26 15:40:07 +000055
Guido van Rossumb5e05e91991-01-01 18:10:40 +000056
Guido van Rossumb5e05e91991-01-01 18:10:40 +000057def split(s):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000058 """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 Rossumb5e05e91991-01-01 18:10:40 +000061
Guido van Rossum54f22ed2000-02-04 15:10:34 +000062 if ':' not in s: return '', s
63 colon = 0
64 for i in range(len(s)):
Fred Drakeb4e460a2000-09-28 16:25:20 +000065 if s[i] == ':': colon = i + 1
Guido van Rossum54f22ed2000-02-04 15:10:34 +000066 path, file = s[:colon-1], s[colon:]
67 if path and not ':' in path:
68 path = path + ':'
69 return path, file
Guido van Rossumb5e05e91991-01-01 18:10:40 +000070
Guido van Rossuma48bf791996-07-23 02:28:32 +000071
72def splitext(p):
Guido van Rossumd8faa362007-04-27 19:54:29 +000073 return genericpath._splitext(p, sep, altsep, extsep)
74splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum0ec31261995-08-10 18:09:16 +000075
76def splitdrive(p):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000077 """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 Rossum0ec31261995-08-10 18:09:16 +000084
85
Guido van Rossumc629d341992-11-05 10:43:02 +000086# Short interfaces to split()
87
88def dirname(s): return split(s)[0]
89def basename(s): return split(s)[1]
90
Jack Jansen791f7d42003-01-15 22:45:48 +000091def ismount(s):
Tim Peters2c60f7a2003-01-29 03:49:43 +000092 if not isabs(s):
93 return False
94 components = split(s)
95 return len(components) == 2 and components[1] == ''
Guido van Rossumc629d341992-11-05 10:43:02 +000096
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000097def islink(s):
Jack Jansen992d58b2002-04-22 13:55:43 +000098 """Return true if the pathname refers to a symbolic link."""
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000099
Jack Jansen992d58b2002-04-22 13:55:43 +0000100 try:
Jack Jansen98fc6832003-02-27 23:18:46 +0000101 import Carbon.File
102 return Carbon.File.ResolveAliasFile(s, 0)[2]
Jack Jansen992d58b2002-04-22 13:55:43 +0000103 except:
104 return False
Guido van Rossum7e4b2de1995-01-27 02:41:45 +0000105
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000106# Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any
107# case.
108
109def 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 Jansenf4e7d2a1995-12-15 13:23:37 +0000118def expandvars(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000119 """Dummy to retain interface-compatibility with other operating systems."""
120 return path
Jack Jansenf4e7d2a1995-12-15 13:23:37 +0000121
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000122
Jack Jansenf4e7d2a1995-12-15 13:23:37 +0000123def expanduser(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000124 """Dummy to retain interface-compatibility with other operating systems."""
125 return path
Guido van Rossum0ec31261995-08-10 18:09:16 +0000126
Neal Norwitz93cf79f2002-03-31 14:06:41 +0000127class norm_error(Exception):
128 """Path cannot be normalized"""
Guido van Rossumc629d341992-11-05 10:43:02 +0000129
130def normpath(s):
Jack Jansen2fc01092000-08-06 21:18:35 +0000131 """Normalize a pathname. Will return the same result for
132 equivalent paths."""
Jack Jansena68bfe21995-08-07 14:09:27 +0000133
Jack Jansen2fc01092000-08-06 21:18:35 +0000134 if ":" not in s:
135 return ":"+s
136
Fred Drakeb4e460a2000-09-28 16:25:20 +0000137 comps = s.split(":")
Jack Jansen2fc01092000-08-06 21:18:35 +0000138 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 Drakeb4e460a2000-09-28 16:25:20 +0000143 i = i - 1
Jack Jansen2fc01092000-08-06 21:18:35 +0000144 else:
145 # best way to handle this is to raise an exception
Collin Winterce36ad82007-08-30 01:19:48 +0000146 raise norm_error('Cannot use :: immediately after volume name')
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000147 else:
Jack Jansen2fc01092000-08-06 21:18:35 +0000148 i = i + 1
149
Fred Drakeb4e460a2000-09-28 16:25:20 +0000150 s = ":".join(comps)
Jack Jansen2fc01092000-08-06 21:18:35 +0000151
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 Rossum54f22ed2000-02-04 15:10:34 +0000155 return s
Guido van Rossum0ec31261995-08-10 18:09:16 +0000156
Guido van Rossume294cf61999-01-29 18:05:18 +0000157def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000158 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000159 if not isabs(path):
160 path = join(os.getcwd(), path)
161 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000162
163# realpath is a no-op on systems without islink support
Jack Jansen992d58b2002-04-22 13:55:43 +0000164def realpath(path):
Tim Peters8ac14952002-05-23 15:15:30 +0000165 path = abspath(path)
166 try:
Jack Jansen98fc6832003-02-27 23:18:46 +0000167 import Carbon.File
Tim Peters8ac14952002-05-23 15:15:30 +0000168 except ImportError:
169 return path
170 if not path:
171 return path
172 components = path.split(':')
173 path = components[0] + ':'
174 for c in components[1:]:
175 path = join(path, c)
Jack Jansen98fc6832003-02-27 23:18:46 +0000176 path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname()
Tim Peters8ac14952002-05-23 15:15:30 +0000177 return path
Mark Hammond8696ebc2002-10-08 02:44:31 +0000178
179supports_unicode_filenames = False