blob: 2eddf5aeeec65fd6c1f092a1b00eeaeba424a3e4 [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 Rossumc629d341992-11-05 10:43:02 +000049# Short interfaces to split()
50
51def dirname(s): return split(s)[0]
52def basename(s): return split(s)[1]
53
54
Guido van Rossuma05026b1992-03-31 18:55:00 +000055# XXX This is undocumented and may go away!
Guido van Rossumb5e05e91991-01-01 18:10:40 +000056# Normalize a pathname: get rid of '::' sequences by backing up,
57# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
58# Raise the exception norm_error below if backing up is impossible,
59# e.g., for '::foo'.
60
61norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000062
63def norm(s):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000064 import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +000065 if ':' not in s:
66 return ':' + s
67 f = string.splitfields(s, ':')
68 pre = []
69 post = []
70 if not f[0]:
71 pre = f[:1]
72 f = f[1:]
73 if not f[len(f)-1]:
74 post = f[-1:]
75 f = f[:-1]
76 res = []
77 for seg in f:
78 if seg:
79 res.append(seg)
80 else:
Guido van Rossumb5e05e91991-01-01 18:10:40 +000081 if not res: raise norm_error, 'path starts with ::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000082 del res[len(res)-1]
83 if not (pre or res):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000084 raise norm_error, 'path starts with volume::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000085 if pre: res = pre + res
86 if post: res = res + post
87 s = res[0]
88 for seg in res[1:]:
89 s = s + ':' + seg
90 return s
91
Guido van Rossumb5e05e91991-01-01 18:10:40 +000092
93# Return true if the pathname refers to an existing directory.
94
Guido van Rossum217a5fa1990-12-26 15:40:07 +000095def isdir(s):
96 try:
97 st = mac.stat(s)
98 except mac.error:
99 return 0
100 return S_ISDIR(st[ST_MODE])
101
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000102
Guido van Rossum7e4b2de1995-01-27 02:41:45 +0000103# Return true if the pathname refers to a symbolic link.
104# (Always false on the Mac, until we understand Aliases.)
105
106def islink(s):
107 return 0
108
109
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000110# Return true if the pathname refers to an existing regular file.
111
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000112def isfile(s):
113 try:
114 st = mac.stat(s)
115 except mac.error:
116 return 0
117 return S_ISREG(st[ST_MODE])
118
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000119
120# Return true if the pathname refers to an existing file or directory.
121
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000122def exists(s):
123 try:
124 st = mac.stat(s)
125 except mac.error:
126 return 0
127 return 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000128
129
130# Normalize path, removing things like ...:A:..:... (yet to be written)
131
132def normpath(s):
133 return s