blob: 47f695a9b5d7d0adbc48bf991b683fe95aebde19 [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 Rossumb5e05e91991-01-01 18:10:40 +000046 return s[:colon], s[colon:]
47
48
Guido van Rossuma05026b1992-03-31 18:55:00 +000049# XXX This is undocumented and may go away!
Guido van Rossumb5e05e91991-01-01 18:10:40 +000050# Normalize a pathname: get rid of '::' sequences by backing up,
51# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
52# Raise the exception norm_error below if backing up is impossible,
53# e.g., for '::foo'.
54
55norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000056
57def norm(s):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000058 import string
Guido van Rossum217a5fa1990-12-26 15:40:07 +000059 if ':' not in s:
60 return ':' + s
61 f = string.splitfields(s, ':')
62 pre = []
63 post = []
64 if not f[0]:
65 pre = f[:1]
66 f = f[1:]
67 if not f[len(f)-1]:
68 post = f[-1:]
69 f = f[:-1]
70 res = []
71 for seg in f:
72 if seg:
73 res.append(seg)
74 else:
Guido van Rossumb5e05e91991-01-01 18:10:40 +000075 if not res: raise norm_error, 'path starts with ::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000076 del res[len(res)-1]
77 if not (pre or res):
Guido van Rossumb5e05e91991-01-01 18:10:40 +000078 raise norm_error, 'path starts with volume::'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000079 if pre: res = pre + res
80 if post: res = res + post
81 s = res[0]
82 for seg in res[1:]:
83 s = s + ':' + seg
84 return s
85
Guido van Rossumb5e05e91991-01-01 18:10:40 +000086
87# Return true if the pathname refers to an existing directory.
88
Guido van Rossum217a5fa1990-12-26 15:40:07 +000089def isdir(s):
90 try:
91 st = mac.stat(s)
92 except mac.error:
93 return 0
94 return S_ISDIR(st[ST_MODE])
95
Guido van Rossumb5e05e91991-01-01 18:10:40 +000096
97# Return true if the pathname refers to an existing regular file.
98
Guido van Rossum217a5fa1990-12-26 15:40:07 +000099def isfile(s):
100 try:
101 st = mac.stat(s)
102 except mac.error:
103 return 0
104 return S_ISREG(st[ST_MODE])
105
Guido van Rossumb5e05e91991-01-01 18:10:40 +0000106
107# Return true if the pathname refers to an existing file or directory.
108
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000109def exists(s):
110 try:
111 st = mac.stat(s)
112 except mac.error:
113 return 0
114 return 1