blob: c45ecbf50496a2448466d933c283a56d7173eb5b [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
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000023# Join two pathnames.
Guido van Rossumb5e05e91991-01-01 18:10:40 +000024# The result is equivalent to what the second pathname would refer to
25# if the first pathname were the current directory.
26
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000027def join(s, t):
Guido van Rossum217a5fa1990-12-26 15:40:07 +000028 if (not s) or isabs(t): return t
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000029 if t[:1] == ':': t = t[1:]
Guido van Rossum217a5fa1990-12-26 15:40:07 +000030 if ':' not in s:
31 s = ':' + s
32 if s[-1:] <> ':':
33 s = s + ':'
34 return s + t
35
Guido van Rossumb5e05e91991-01-01 18:10:40 +000036
37# Split a pathname in two parts: the directory leading up to the final bit,
38# and the basename (the filename, without colons, in that directory).
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000039# The result (s, t) is such that join(s, t) yields the original argument.
Guido van Rossumb5e05e91991-01-01 18:10:40 +000040
41def split(s):
42 if ':' not in s: return '', s
43 colon = 0
44 for i in range(len(s)):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000045 if s[i] == ':': colon = i+1
Guido van Rossum7aeb4b91994-08-23 13:32:20 +000046 return s[:colon-1], s[colon:]
Guido van Rossumb5e05e91991-01-01 18:10:40 +000047
48
Guido van Rossuma48bf791996-07-23 02:28:32 +000049# Split a path in root and extension.
50# The extension is everything starting at the last dot in the last
51# pathname component; the root is everything before that.
52# It is always true that root + ext == p.
53
54def splitext(p):
55 root, ext = '', ''
56 for c in p:
57 if c == ':':
58 root, ext = root + ext + c, ''
59 elif c == '.':
60 if ext:
61 root, ext = root + ext, c
62 else:
63 ext = c
64 elif ext:
65 ext = ext + c
66 else:
67 root = root + c
68 return root, ext
69
70
Guido van Rossum0ec31261995-08-10 18:09:16 +000071# Split a pathname into a drive specification and the rest of the
72# path. Useful on DOS/Windows/NT; on the Mac, the drive is always
73# empty (don't use the volume name -- it doesn't have the same
74# syntactic and semantic oddities as DOS drive letters, such as there
75# being a separate current directory per drive).
76
77def splitdrive(p):
78 return '', p
79
80
Guido van Rossumc629d341992-11-05 10:43:02 +000081# Short interfaces to split()
82
83def dirname(s): return split(s)[0]
84def basename(s): return split(s)[1]
85
86
Guido van Rossumb5e05e91991-01-01 18:10:40 +000087# Return true if the pathname refers to an existing directory.
88
Guido van Rossum217a5fa1990-12-26 15:40:07 +000089def isdir(s):
90 try:
91 st = mac.stat(s)
92 except mac.error:
93 return 0
94 return S_ISDIR(st[ST_MODE])
95
Guido van Rossumb5e05e91991-01-01 18:10:40 +000096
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000097# Return true if the pathname refers to a symbolic link.
98# (Always false on the Mac, until we understand Aliases.)
99
100def islink(s):
101 return 0
102
103
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000104# Return true if the pathname refers to an existing regular file.
105
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000106def isfile(s):
107 try:
108 st = mac.stat(s)
109 except mac.error:
110 return 0
111 return S_ISREG(st[ST_MODE])
112
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000113
114# Return true if the pathname refers to an existing file or directory.
115
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000116def exists(s):
117 try:
118 st = mac.stat(s)
119 except mac.error:
120 return 0
121 return 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000122
Jack Jansenf4e7d2a1995-12-15 13:23:37 +0000123#
124# dummy expandvars to retain interface-compatability with other
125# operating systems.
126def expandvars(path):
127 return path
128
129#
130# dummy expanduser to retain interface-compatability with other
131# operating systems.
132def expanduser(path):
133 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000134
Guido van Rossum0ec31261995-08-10 18:09:16 +0000135# Normalize a pathname: get rid of '::' sequences by backing up,
136# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
137# Raise the exception norm_error below if backing up is impossible,
138# e.g., for '::foo'.
139# XXX The Unix version doesn't raise an exception but simply
140# returns an unnormalized path. Should do so here too.
141
142norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossumc629d341992-11-05 10:43:02 +0000143
144def normpath(s):
Guido van Rossum0ec31261995-08-10 18:09:16 +0000145 import string
146 if ':' not in s:
147 return ':' + s
148 f = string.splitfields(s, ':')
149 pre = []
150 post = []
151 if not f[0]:
152 pre = f[:1]
153 f = f[1:]
154 if not f[len(f)-1]:
155 post = f[-1:]
156 f = f[:-1]
157 res = []
158 for seg in f:
159 if seg:
160 res.append(seg)
161 else:
162 if not res: raise norm_error, 'path starts with ::'
163 del res[len(res)-1]
164 if not (pre or res):
165 raise norm_error, 'path starts with volume::'
166 if pre: res = pre + res
167 if post: res = res + post
168 s = res[0]
169 for seg in res[1:]:
170 s = s + ':' + seg
Guido van Rossumc629d341992-11-05 10:43:02 +0000171 return s
Jack Jansena68bfe21995-08-07 14:09:27 +0000172
Guido van Rossum0ec31261995-08-10 18:09:16 +0000173
Jack Jansena68bfe21995-08-07 14:09:27 +0000174# Directory tree walk.
175# For each directory under top (including top itself),
176# func(arg, dirname, filenames) is called, where
177# dirname is the name of the directory and filenames is the list
178# of files (and subdirectories etc.) in the directory.
179# The func may modify the filenames list, to implement a filter,
180# or to impose a different order of visiting.
181
182def walk(top, func, arg):
183 try:
184 names = mac.listdir(top)
185 except mac.error:
186 return
187 func(arg, top, names)
188 for name in names:
189 name = join(top, name)
190 if isdir(name):
191 walk(name, func, arg)