blob: 99254abce0305e14dfe88f9ac9940de190461ebb [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
3import mac
4
Guido van Rossum217a5fa1990-12-26 15:40:07 +00005from stat import *
6
Guido van Rossumb5e05e91991-01-01 18:10:40 +00007
8# Return true if a path is absolute.
9# On the Mac, relative paths begin with a colon,
10# but as a special case, paths with no colons at all are also relative.
11# Anything else is absolute (the string up to the first colon is the
12# volume name).
13
Guido van Rossum217a5fa1990-12-26 15:40:07 +000014def isabs(s):
15 return ':' in s and s[0] <> ':'
16
Guido van Rossumb5e05e91991-01-01 18:10:40 +000017
18# Concatenate two pathnames.
19# The result is equivalent to what the second pathname would refer to
20# if the first pathname were the current directory.
21
Guido van Rossum217a5fa1990-12-26 15:40:07 +000022def cat(s, t):
23 if (not s) or isabs(t): return t
24 if t[:1] = ':': t = t[1:]
25 if ':' not in s:
26 s = ':' + s
27 if s[-1:] <> ':':
28 s = s + ':'
29 return s + t
30
Guido van Rossumb5e05e91991-01-01 18:10:40 +000031
32# Split a pathname in two parts: the directory leading up to the final bit,
33# and the basename (the filename, without colons, in that directory).
34# The result (s, t) is such that cat(s, t) yields the original argument.
35
36def split(s):
37 if ':' not in s: return '', s
38 colon = 0
39 for i in range(len(s)):
40 if s[i] = ':': colon = i+1
41 return s[:colon], s[colon:]
42
43
44# Normalize a pathname: get rid of '::' sequences by backing up,
45# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
46# Raise the exception norm_error below if backing up is impossible,
47# e.g., for '::foo'.
48
49norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000050
51def norm(s):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000052 import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +000053 if ':' not in s:
54 return ':' + s
55 f = string.splitfields(s, ':')
56 pre = []
57 post = []
58 if not f[0]:
59 pre = f[:1]
60 f = f[1:]
61 if not f[len(f)-1]:
62 post = f[-1:]
63 f = f[:-1]
64 res = []
65 for seg in f:
66 if seg:
67 res.append(seg)
68 else:
Guido van Rossumb5e05e91991-01-01 18:10:40 +000069 if not res: raise norm_error, 'path starts with ::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000070 del res[len(res)-1]
71 if not (pre or res):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000072 raise norm_error, 'path starts with volume::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000073 if pre: res = pre + res
74 if post: res = res + post
75 s = res[0]
76 for seg in res[1:]:
77 s = s + ':' + seg
78 return s
79
Guido van Rossumb5e05e91991-01-01 18:10:40 +000080
81# Return true if the pathname refers to an existing directory.
82
Guido van Rossum217a5fa1990-12-26 15:40:07 +000083def isdir(s):
84 try:
85 st = mac.stat(s)
86 except mac.error:
87 return 0
88 return S_ISDIR(st[ST_MODE])
89
Guido van Rossumb5e05e91991-01-01 18:10:40 +000090
91# Return true if the pathname refers to an existing regular file.
92
Guido van Rossum217a5fa1990-12-26 15:40:07 +000093def isfile(s):
94 try:
95 st = mac.stat(s)
96 except mac.error:
97 return 0
98 return S_ISREG(st[ST_MODE])
99
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000100
101# Return true if the pathname refers to an existing file or directory.
102
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000103def exists(s):
104 try:
105 st = mac.stat(s)
106 except mac.error:
107 return 0
108 return 1