blob: 8d2b18a68c7216b22ac5df9b0786f16fe7c6c725 [file] [log] [blame]
Guido van Rossumb5e05e91991-01-01 18:10:40 +00001# module 'macpath' -- pathname (or -related) operations for the Macintosh
Guido van Rossum217a5fa1990-12-26 15:40:07 +00002
Guido van Rossum599f2ed1992-01-14 18:28:18 +00003import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +00004import mac
Guido van Rossum217a5fa1990-12-26 15:40:07 +00005from stat import *
6
Guido van Rossumb5e05e91991-01-01 18:10:40 +00007
Guido van Rossum599f2ed1992-01-14 18:28:18 +00008# Normalize the case of a pathname. Dummy in Posix, but string.lower here.
9
10normcase = string.lower
11
12
Guido van Rossumb5e05e91991-01-01 18:10:40 +000013# Return true if a path is absolute.
14# On the Mac, relative paths begin with a colon,
15# but as a special case, paths with no colons at all are also relative.
16# Anything else is absolute (the string up to the first colon is the
17# volume name).
18
Guido van Rossum217a5fa1990-12-26 15:40:07 +000019def isabs(s):
20 return ':' in s and s[0] <> ':'
21
Guido van Rossumb5e05e91991-01-01 18:10:40 +000022
Barry Warsaw384d2491997-02-18 21:53:25 +000023def join(s, *p):
24 path = s
25 for t in p:
26 if (not s) or isabs(t):
27 path = t
28 continue
29 if t[:1] == ':':
30 t = t[1:]
31 if ':' not in path:
32 path = ':' + path
33 if path[-1:] <> ':':
34 path = path + ':'
35 path = path + t
36 return path
Guido van Rossum217a5fa1990-12-26 15:40:07 +000037
Guido van Rossumb5e05e91991-01-01 18:10:40 +000038
39# Split a pathname in two parts: the directory leading up to the final bit,
40# and the basename (the filename, without colons, in that directory).
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000041# The result (s, t) is such that join(s, t) yields the original argument.
Guido van Rossumb5e05e91991-01-01 18:10:40 +000042
43def split(s):
44 if ':' not in s: return '', s
45 colon = 0
46 for i in range(len(s)):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000047 if s[i] == ':': colon = i+1
Guido van Rossum7aeb4b91994-08-23 13:32:20 +000048 return s[:colon-1], s[colon:]
Guido van Rossumb5e05e91991-01-01 18:10:40 +000049
50
Guido van Rossuma48bf791996-07-23 02:28:32 +000051# Split a path in root and extension.
52# The extension is everything starting at the last dot in the last
53# pathname component; the root is everything before that.
54# It is always true that root + ext == p.
55
56def splitext(p):
57 root, ext = '', ''
58 for c in p:
59 if c == ':':
60 root, ext = root + ext + c, ''
61 elif c == '.':
62 if ext:
63 root, ext = root + ext, c
64 else:
65 ext = c
66 elif ext:
67 ext = ext + c
68 else:
69 root = root + c
70 return root, ext
71
72
Guido van Rossum0ec31261995-08-10 18:09:16 +000073# Split a pathname into a drive specification and the rest of the
74# path. Useful on DOS/Windows/NT; on the Mac, the drive is always
75# empty (don't use the volume name -- it doesn't have the same
76# syntactic and semantic oddities as DOS drive letters, such as there
77# being a separate current directory per drive).
78
79def splitdrive(p):
80 return '', p
81
82
Guido van Rossumc629d341992-11-05 10:43:02 +000083# Short interfaces to split()
84
85def dirname(s): return split(s)[0]
86def basename(s): return split(s)[1]
87
88
Guido van Rossumb5e05e91991-01-01 18:10:40 +000089# Return true if the pathname refers to an existing directory.
90
Guido van Rossum217a5fa1990-12-26 15:40:07 +000091def isdir(s):
92 try:
93 st = mac.stat(s)
94 except mac.error:
95 return 0
96 return S_ISDIR(st[ST_MODE])
97
Guido van Rossumb5e05e91991-01-01 18:10:40 +000098
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000099# Return true if the pathname refers to a symbolic link.
100# (Always false on the Mac, until we understand Aliases.)
101
102def islink(s):
103 return 0
104
105
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000106# Return true if the pathname refers to an existing regular file.
107
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000108def isfile(s):
109 try:
110 st = mac.stat(s)
111 except mac.error:
112 return 0
113 return S_ISREG(st[ST_MODE])
114
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000115
116# Return true if the pathname refers to an existing file or directory.
117
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000118def exists(s):
119 try:
120 st = mac.stat(s)
121 except mac.error:
122 return 0
123 return 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000124
Jack Jansenf4e7d2a1995-12-15 13:23:37 +0000125#
126# dummy expandvars to retain interface-compatability with other
127# operating systems.
128def expandvars(path):
129 return path
130
131#
132# dummy expanduser to retain interface-compatability with other
133# operating systems.
134def expanduser(path):
135 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000136
Guido van Rossum0ec31261995-08-10 18:09:16 +0000137# Normalize a pathname: get rid of '::' sequences by backing up,
138# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
139# Raise the exception norm_error below if backing up is impossible,
140# e.g., for '::foo'.
141# XXX The Unix version doesn't raise an exception but simply
142# returns an unnormalized path. Should do so here too.
143
144norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossumc629d341992-11-05 10:43:02 +0000145
146def normpath(s):
Guido van Rossum0ec31261995-08-10 18:09:16 +0000147 import string
148 if ':' not in s:
149 return ':' + s
150 f = string.splitfields(s, ':')
151 pre = []
152 post = []
153 if not f[0]:
154 pre = f[:1]
155 f = f[1:]
156 if not f[len(f)-1]:
157 post = f[-1:]
158 f = f[:-1]
159 res = []
160 for seg in f:
161 if seg:
162 res.append(seg)
163 else:
164 if not res: raise norm_error, 'path starts with ::'
165 del res[len(res)-1]
166 if not (pre or res):
167 raise norm_error, 'path starts with volume::'
168 if pre: res = pre + res
169 if post: res = res + post
170 s = res[0]
171 for seg in res[1:]:
172 s = s + ':' + seg
Guido van Rossumc629d341992-11-05 10:43:02 +0000173 return s
Jack Jansena68bfe21995-08-07 14:09:27 +0000174
Guido van Rossum0ec31261995-08-10 18:09:16 +0000175
Jack Jansena68bfe21995-08-07 14:09:27 +0000176# Directory tree walk.
177# For each directory under top (including top itself),
178# func(arg, dirname, filenames) is called, where
179# dirname is the name of the directory and filenames is the list
180# of files (and subdirectories etc.) in the directory.
181# The func may modify the filenames list, to implement a filter,
182# or to impose a different order of visiting.
183
184def walk(top, func, arg):
185 try:
186 names = mac.listdir(top)
187 except mac.error:
188 return
189 func(arg, top, names)
190 for name in names:
191 name = join(top, name)
192 if isdir(name):
193 walk(name, func, arg)