blob: b0a27c85bea66a7d935c5dcf1b953cf4240f5007 [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
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000037cat = join # For compatibility
38
39
Guido van Rossumb5e05e91991-01-01 18:10:40 +000040# Split a pathname in two parts: the directory leading up to the final bit,
41# and the basename (the filename, without colons, in that directory).
Guido van Rossumfbe0a8e1991-08-16 13:27:45 +000042# The result (s, t) is such that join(s, t) yields the original argument.
Guido van Rossumb5e05e91991-01-01 18:10:40 +000043
44def split(s):
45 if ':' not in s: return '', s
46 colon = 0
47 for i in range(len(s)):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000048 if s[i] == ':': colon = i+1
Guido van Rossumb5e05e91991-01-01 18:10:40 +000049 return s[:colon], s[colon:]
50
51
52# Normalize a pathname: get rid of '::' sequences by backing up,
53# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
54# Raise the exception norm_error below if backing up is impossible,
55# e.g., for '::foo'.
56
57norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000058
59def norm(s):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000060 import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +000061 if ':' not in s:
62 return ':' + s
63 f = string.splitfields(s, ':')
64 pre = []
65 post = []
66 if not f[0]:
67 pre = f[:1]
68 f = f[1:]
69 if not f[len(f)-1]:
70 post = f[-1:]
71 f = f[:-1]
72 res = []
73 for seg in f:
74 if seg:
75 res.append(seg)
76 else:
Guido van Rossumb5e05e91991-01-01 18:10:40 +000077 if not res: raise norm_error, 'path starts with ::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000078 del res[len(res)-1]
79 if not (pre or res):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000080 raise norm_error, 'path starts with volume::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000081 if pre: res = pre + res
82 if post: res = res + post
83 s = res[0]
84 for seg in res[1:]:
85 s = s + ':' + seg
86 return s
87
Guido van Rossumb5e05e91991-01-01 18:10:40 +000088
89# 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
99# Return true if the pathname refers to an existing regular file.
100
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000101def isfile(s):
102 try:
103 st = mac.stat(s)
104 except mac.error:
105 return 0
106 return S_ISREG(st[ST_MODE])
107
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000108
109# Return true if the pathname refers to an existing file or directory.
110
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000111def exists(s):
112 try:
113 st = mac.stat(s)
114 except mac.error:
115 return 0
116 return 1