blob: 58cbb88e5003286b4c761dd06c5c91674942838d [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
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000018# Join two pathnames.
Guido van Rossumb5e05e91991-01-01 18:10:40 +000019# The result is equivalent to what the second pathname would refer to
20# if the first pathname were the current directory.
21
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000022def join(s, t):
Guido van Rossum217a5fa1990-12-26 15:40:07 +000023 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
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000032cat = join # For compatibility
33
34
Guido van Rossumb5e05e91991-01-01 18:10:40 +000035# Split a pathname in two parts: the directory leading up to the final bit,
36# and the basename (the filename, without colons, in that directory).
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000037# The result (s, t) is such that join(s, t) yields the original argument.
Guido van Rossumb5e05e91991-01-01 18:10:40 +000038
39def split(s):
40 if ':' not in s: return '', s
41 colon = 0
42 for i in range(len(s)):
43 if s[i] = ':': colon = i+1
44 return s[:colon], s[colon:]
45
46
47# Normalize a pathname: get rid of '::' sequences by backing up,
48# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
49# Raise the exception norm_error below if backing up is impossible,
50# e.g., for '::foo'.
51
52norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000053
54def norm(s):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000055 import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +000056 if ':' not in s:
57 return ':' + s
58 f = string.splitfields(s, ':')
59 pre = []
60 post = []
61 if not f[0]:
62 pre = f[:1]
63 f = f[1:]
64 if not f[len(f)-1]:
65 post = f[-1:]
66 f = f[:-1]
67 res = []
68 for seg in f:
69 if seg:
70 res.append(seg)
71 else:
Guido van Rossumb5e05e91991-01-01 18:10:40 +000072 if not res: raise norm_error, 'path starts with ::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000073 del res[len(res)-1]
74 if not (pre or res):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000075 raise norm_error, 'path starts with volume::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000076 if pre: res = pre + res
77 if post: res = res + post
78 s = res[0]
79 for seg in res[1:]:
80 s = s + ':' + seg
81 return s
82
Guido van Rossumb5e05e91991-01-01 18:10:40 +000083
84# Return true if the pathname refers to an existing directory.
85
Guido van Rossum217a5fa1990-12-26 15:40:07 +000086def isdir(s):
87 try:
88 st = mac.stat(s)
89 except mac.error:
90 return 0
91 return S_ISDIR(st[ST_MODE])
92
Guido van Rossumb5e05e91991-01-01 18:10:40 +000093
94# Return true if the pathname refers to an existing regular file.
95
Guido van Rossum217a5fa1990-12-26 15:40:07 +000096def isfile(s):
97 try:
98 st = mac.stat(s)
99 except mac.error:
100 return 0
101 return S_ISREG(st[ST_MODE])
102
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000103
104# Return true if the pathname refers to an existing file or directory.
105
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000106def exists(s):
107 try:
108 st = mac.stat(s)
109 except mac.error:
110 return 0
111 return 1