blob: 5317c36b3fc3987a608c507d15aae0d3aab26f48 [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 Rossum0ec31261995-08-10 18:09:16 +000049# Split a pathname into a drive specification and the rest of the
50# path. Useful on DOS/Windows/NT; on the Mac, the drive is always
51# empty (don't use the volume name -- it doesn't have the same
52# syntactic and semantic oddities as DOS drive letters, such as there
53# being a separate current directory per drive).
54
55def splitdrive(p):
56 return '', p
57
58
Guido van Rossumc629d341992-11-05 10:43:02 +000059# Short interfaces to split()
60
61def dirname(s): return split(s)[0]
62def basename(s): return split(s)[1]
63
64
Guido van Rossumb5e05e91991-01-01 18:10:40 +000065# Return true if the pathname refers to an existing directory.
66
Guido van Rossum217a5fa1990-12-26 15:40:07 +000067def isdir(s):
68 try:
69 st = mac.stat(s)
70 except mac.error:
71 return 0
72 return S_ISDIR(st[ST_MODE])
73
Guido van Rossumb5e05e91991-01-01 18:10:40 +000074
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000075# Return true if the pathname refers to a symbolic link.
76# (Always false on the Mac, until we understand Aliases.)
77
78def islink(s):
79 return 0
80
81
Guido van Rossumb5e05e91991-01-01 18:10:40 +000082# Return true if the pathname refers to an existing regular file.
83
Guido van Rossum217a5fa1990-12-26 15:40:07 +000084def isfile(s):
85 try:
86 st = mac.stat(s)
87 except mac.error:
88 return 0
89 return S_ISREG(st[ST_MODE])
90
Guido van Rossumb5e05e91991-01-01 18:10:40 +000091
92# Return true if the pathname refers to an existing file or directory.
93
Guido van Rossum217a5fa1990-12-26 15:40:07 +000094def exists(s):
95 try:
96 st = mac.stat(s)
97 except mac.error:
98 return 0
99 return 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000100
101
Guido van Rossum0ec31261995-08-10 18:09:16 +0000102# Normalize a pathname: get rid of '::' sequences by backing up,
103# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
104# Raise the exception norm_error below if backing up is impossible,
105# e.g., for '::foo'.
106# XXX The Unix version doesn't raise an exception but simply
107# returns an unnormalized path. Should do so here too.
108
109norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossumc629d341992-11-05 10:43:02 +0000110
111def normpath(s):
Guido van Rossum0ec31261995-08-10 18:09:16 +0000112 import string
113 if ':' not in s:
114 return ':' + s
115 f = string.splitfields(s, ':')
116 pre = []
117 post = []
118 if not f[0]:
119 pre = f[:1]
120 f = f[1:]
121 if not f[len(f)-1]:
122 post = f[-1:]
123 f = f[:-1]
124 res = []
125 for seg in f:
126 if seg:
127 res.append(seg)
128 else:
129 if not res: raise norm_error, 'path starts with ::'
130 del res[len(res)-1]
131 if not (pre or res):
132 raise norm_error, 'path starts with volume::'
133 if pre: res = pre + res
134 if post: res = res + post
135 s = res[0]
136 for seg in res[1:]:
137 s = s + ':' + seg
Guido van Rossumc629d341992-11-05 10:43:02 +0000138 return s
Jack Jansena68bfe21995-08-07 14:09:27 +0000139
Guido van Rossum0ec31261995-08-10 18:09:16 +0000140
Jack Jansena68bfe21995-08-07 14:09:27 +0000141# Directory tree walk.
142# For each directory under top (including top itself),
143# func(arg, dirname, filenames) is called, where
144# dirname is the name of the directory and filenames is the list
145# of files (and subdirectories etc.) in the directory.
146# The func may modify the filenames list, to implement a filter,
147# or to impose a different order of visiting.
148
149def walk(top, func, arg):
150 try:
151 names = mac.listdir(top)
152 except mac.error:
153 return
154 func(arg, top, names)
155 for name in names:
156 name = join(top, name)
157 if isdir(name):
158 walk(name, func, arg)