blob: c288f3b5b39f7b427467c177b9a5bf97efa713eb [file] [log] [blame]
Guido van Rossum26847381992-03-31 18:54:35 +00001# Module 'posixpath' -- common operations on POSIX pathnames
Guido van Rossumc6360141990-10-13 19:23:40 +00002
3import posix
Guido van Rossum40d93041990-10-21 16:17:34 +00004import stat
Guido van Rossumc6360141990-10-13 19:23:40 +00005
6
Guido van Rossum7ac48781992-01-14 18:29:32 +00007# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
8# On MS-DOS this may also turn slashes into backslashes; however, other
9# normalizations (such as optimizing '../' away) are not allowed
10# (another function should be defined to do that).
11
12def normcase(s):
13 return s
14
15
16# Return wheter a path is absolute.
17# Trivial in Posix, harder on the Mac or MS-DOS.
18
19def isabs(s):
20 return s[:1] == '/'
21
22
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000023# Join two pathnames.
Guido van Rossum7ac48781992-01-14 18:29:32 +000024# Ignore the first part if the second part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000025# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000026
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000027def join(a, b):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000028 if b[:1] == '/': return b
29 if a == '' or a[-1:] == '/': return a + b
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000030 # Note: join('x', '') returns 'x/'; is this what we want?
Guido van Rossumc6360141990-10-13 19:23:40 +000031 return a + '/' + b
32
33
Guido van Rossum26847381992-03-31 18:54:35 +000034# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000035# rest). If the path ends in '/', tail will be empty. If there is no
36# '/' in the path, head will be empty.
37# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000038
Guido van Rossumc6360141990-10-13 19:23:40 +000039def split(p):
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000040 import string
41 i = string.rfind(p, '/') + 1
42 head, tail = p[:i], p[i:]
43 if head and head <> '/'*len(head):
Guido van Rossum26847381992-03-31 18:54:35 +000044 while head[-1] == '/':
45 head = head[:-1]
Guido van Rossumc6360141990-10-13 19:23:40 +000046 return head, tail
47
48
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000049# Split a path in root and extension.
50# The extension is everything starting at the first dot in the last
51# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000052# It is always true that root + ext == p.
53
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000054def splitext(p):
55 root, ext = '', ''
56 for c in p:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000057 if c == '/':
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000058 root, ext = root + ext + c, ''
Sjoerd Mullender43598601994-12-14 15:29:17 +000059 elif c == '.':
60 if ext:
61 root, ext = root + ext, c
62 else:
63 ext = c
64 elif ext:
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000065 ext = ext + c
66 else:
67 root = root + c
68 return root, ext
69
70
Guido van Rossum221df241995-08-07 20:17:55 +000071# Split a pathname into a drive specification and the rest of the
72# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
73
74def splitdrive(p):
75 return '', p
76
77
Guido van Rossumc6360141990-10-13 19:23:40 +000078# Return the tail (basename) part of a path.
Guido van Rossum7ac48781992-01-14 18:29:32 +000079
Guido van Rossumc6360141990-10-13 19:23:40 +000080def basename(p):
81 return split(p)[1]
82
83
Guido van Rossumc629d341992-11-05 10:43:02 +000084# Return the head (dirname) part of a path.
85
86def dirname(p):
87 return split(p)[0]
88
89
Guido van Rossumc6360141990-10-13 19:23:40 +000090# Return the longest prefix of all list elements.
Guido van Rossum7ac48781992-01-14 18:29:32 +000091
Guido van Rossumc6360141990-10-13 19:23:40 +000092def commonprefix(m):
93 if not m: return ''
94 prefix = m[0]
95 for item in m:
96 for i in range(len(prefix)):
97 if prefix[:i+1] <> item[:i+1]:
98 prefix = prefix[:i]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000099 if i == 0: return ''
Guido van Rossumc6360141990-10-13 19:23:40 +0000100 break
101 return prefix
102
103
Guido van Rossum7ac48781992-01-14 18:29:32 +0000104# Is a path a symbolic link?
105# This will always return false on systems where posix.lstat doesn't exist.
106
107def islink(path):
108 try:
109 st = posix.lstat(path)
110 except (posix.error, AttributeError):
111 return 0
112 return stat.S_ISLNK(st[stat.ST_MODE])
113
114
115# Does a path exist?
116# This is false for dangling symbolic links.
117
Guido van Rossumc6360141990-10-13 19:23:40 +0000118def exists(path):
119 try:
120 st = posix.stat(path)
121 except posix.error:
122 return 0
123 return 1
124
125
126# Is a path a posix directory?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000127# This follows symbolic links, so both islink() and isdir() can be true
128# for the same path.
129
Guido van Rossumc6360141990-10-13 19:23:40 +0000130def isdir(path):
131 try:
132 st = posix.stat(path)
133 except posix.error:
134 return 0
Guido van Rossum40d93041990-10-21 16:17:34 +0000135 return stat.S_ISDIR(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000136
137
Guido van Rossum26847381992-03-31 18:54:35 +0000138# Is a path a regular file?
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139# This follows symbolic links, so both islink() and isfile() can be true
Guido van Rossum7ac48781992-01-14 18:29:32 +0000140# for the same path.
141
142def isfile(path):
Guido van Rossumc6360141990-10-13 19:23:40 +0000143 try:
Guido van Rossum7ac48781992-01-14 18:29:32 +0000144 st = posix.stat(path)
145 except posix.error:
Guido van Rossumc6360141990-10-13 19:23:40 +0000146 return 0
Guido van Rossum7ac48781992-01-14 18:29:32 +0000147 return stat.S_ISREG(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000148
149
Guido van Rossumd3778f91991-11-12 15:37:40 +0000150# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000151
Guido van Rossumd3778f91991-11-12 15:37:40 +0000152def samefile(f1, f2):
153 s1 = posix.stat(f1)
154 s2 = posix.stat(f2)
155 return samestat(s1, s2)
156
157
158# Are two open files really referencing the same file?
159# (Not necessarily the same file descriptor!)
160# XXX Oops, posix.fstat() doesn't exist yet!
Guido van Rossum7ac48781992-01-14 18:29:32 +0000161
Guido van Rossumd3778f91991-11-12 15:37:40 +0000162def sameopenfile(fp1, fp2):
163 s1 = posix.fstat(fp1)
164 s2 = posix.fstat(fp2)
165 return samestat(s1, s2)
166
167
168# Are two stat buffers (obtained from stat, fstat or lstat)
169# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000170
Guido van Rossumd3778f91991-11-12 15:37:40 +0000171def samestat(s1, s2):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000172 return s1[stat.ST_INO] == s2[stat.ST_INO] and \
Guido van Rossum509d24a1992-05-06 11:36:49 +0000173 s1[stat.ST_DEV] == s2[stat.ST_DEV]
Guido van Rossumc6360141990-10-13 19:23:40 +0000174
175
176# Is a path a mount point?
Guido van Rossum509d24a1992-05-06 11:36:49 +0000177# (Does this work for all UNIXes? Is it even guaranteed to work by POSIX?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000178
Guido van Rossumc6360141990-10-13 19:23:40 +0000179def ismount(path):
Guido van Rossum509d24a1992-05-06 11:36:49 +0000180 try:
181 s1 = posix.stat(path)
182 s2 = posix.stat(join(path, '..'))
183 except posix.error:
184 return 0 # It doesn't exist -- so not a mount point :-)
185 dev1 = s1[stat.ST_DEV]
186 dev2 = s2[stat.ST_DEV]
187 if dev1 != dev2:
188 return 1 # path/.. on a different device as path
189 ino1 = s1[stat.ST_INO]
190 ino2 = s2[stat.ST_INO]
191 if ino1 == ino2:
192 return 1 # path/.. is the same i-node as path
193 return 0
Guido van Rossumc6360141990-10-13 19:23:40 +0000194
195
196# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000197# For each directory under top (including top itself, but excluding
198# '.' and '..'), func(arg, dirname, filenames) is called, where
199# dirname is the name of the directory and filenames is the list
200# files files (and subdirectories etc.) in the directory.
201# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000202# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000203
Guido van Rossumc6360141990-10-13 19:23:40 +0000204def walk(top, func, arg):
205 try:
206 names = posix.listdir(top)
207 except posix.error:
208 return
209 func(arg, top, names)
210 exceptions = ('.', '..')
211 for name in names:
212 if name not in exceptions:
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000213 name = join(top, name)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214 if isdir(name) and not islink(name):
Guido van Rossumc6360141990-10-13 19:23:40 +0000215 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000216
217
218# Expand paths beginning with '~' or '~user'.
219# '~' means $HOME; '~user' means that user's home directory.
220# If the path doesn't begin with '~', or if the user or $HOME is unknown,
221# the path is returned unchanged (leaving error reporting to whatever
222# function is called with the expanded path as argument).
223# See also module 'glob' for expansion of *, ? and [...] in pathnames.
224# (A function should also be defined to do full *sh-style environment
225# variable expansion.)
226
227def expanduser(path):
228 if path[:1] <> '~':
229 return path
230 i, n = 1, len(path)
231 while i < n and path[i] <> '/':
232 i = i+1
233 if i == 1:
234 if not posix.environ.has_key('HOME'):
235 return path
236 userhome = posix.environ['HOME']
237 else:
238 import pwd
239 try:
240 pwent = pwd.getpwnam(path[1:i])
241 except KeyError:
242 return path
243 userhome = pwent[5]
244 return userhome + path[i:]
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000245
246
247# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248# This expands the forms $variable and ${variable} only.
249# Non-existant variables are left unchanged.
250
251_varprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000252
253def expandvars(path):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254 global _varprog
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000255 if '$' not in path:
256 return path
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257 if not _varprog:
258 import regex
259 _varprog = regex.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)')
260 i = 0
261 while 1:
262 i = _varprog.search(path, i)
263 if i < 0:
264 break
265 name = _varprog.group(1)
266 j = i + len(_varprog.group(0))
267 if name[:1] == '{' and name[-1:] == '}':
268 name = name[1:-1]
269 if posix.environ.has_key(name):
270 tail = path[j:]
271 path = path[:i] + posix.environ[name]
272 i = len(path)
273 path = path + tail
274 else:
275 i = j
276 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000277
278
279# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
280# It should be understood that this may change the meaning of the path
281# if it contains symbolic links!
282
283def normpath(path):
284 import string
Guido van Rossumdf563861993-07-06 15:19:36 +0000285 # Treat initial slashes specially
286 slashes = ''
287 while path[:1] == '/':
288 slashes = slashes + '/'
289 path = path[1:]
Guido van Rossumc629d341992-11-05 10:43:02 +0000290 comps = string.splitfields(path, '/')
Guido van Rossumc629d341992-11-05 10:43:02 +0000291 i = 0
292 while i < len(comps):
293 if comps[i] == '.':
294 del comps[i]
295 elif comps[i] == '..' and i > 0 and \
296 comps[i-1] not in ('', '..'):
297 del comps[i-1:i+1]
298 i = i-1
299 elif comps[i] == '' and i > 0 and comps[i-1] <> '':
300 del comps[i]
301 else:
302 i = i+1
303 # If the path is now empty, substitute '.'
Guido van Rossumdf563861993-07-06 15:19:36 +0000304 if not comps and not slashes:
Guido van Rossumc629d341992-11-05 10:43:02 +0000305 comps.append('.')
Guido van Rossumdf563861993-07-06 15:19:36 +0000306 return slashes + string.joinfields(comps, '/')