blob: f244ca56a1552967e1b3dd2a9913cad1ee3f49de [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# Module 'path' -- common operations on POSIX pathnames
2
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
34# Split a path in head (empty or ending in '/') and tail (no '/').
35# The tail will be empty if the path ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000036# It is always true that head + tail == p; also join(head, tail) == p.
37# Note that because head ends in '/', if you want to find all components
38# of a path by repeatedly getting the head, you will have to strip off
39# the trailing '/' yourself (another function should be defined to
40# split an entire path into components.)
41
Guido van Rossumc6360141990-10-13 19:23:40 +000042def split(p):
43 head, tail = '', ''
44 for c in p:
45 tail = tail + c
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000046 if c == '/':
Guido van Rossumc6360141990-10-13 19:23:40 +000047 head, tail = head + tail, ''
48 return head, tail
49
50
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000051# Split a path in root and extension.
52# The extension is everything starting at the first dot in the last
53# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000054# It is always true that root + ext == p.
55
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000056def splitext(p):
57 root, ext = '', ''
58 for c in p:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000059 if c == '/':
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000060 root, ext = root + ext + c, ''
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000061 elif c == '.' or ext:
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000062 ext = ext + c
63 else:
64 root = root + c
65 return root, ext
66
67
Guido van Rossumc6360141990-10-13 19:23:40 +000068# Return the tail (basename) part of a path.
Guido van Rossum7ac48781992-01-14 18:29:32 +000069
Guido van Rossumc6360141990-10-13 19:23:40 +000070def basename(p):
71 return split(p)[1]
72
73
74# Return the longest prefix of all list elements.
Guido van Rossum7ac48781992-01-14 18:29:32 +000075
Guido van Rossumc6360141990-10-13 19:23:40 +000076def commonprefix(m):
77 if not m: return ''
78 prefix = m[0]
79 for item in m:
80 for i in range(len(prefix)):
81 if prefix[:i+1] <> item[:i+1]:
82 prefix = prefix[:i]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000083 if i == 0: return ''
Guido van Rossumc6360141990-10-13 19:23:40 +000084 break
85 return prefix
86
87
Guido van Rossum7ac48781992-01-14 18:29:32 +000088# Is a path a symbolic link?
89# This will always return false on systems where posix.lstat doesn't exist.
90
91def islink(path):
92 try:
93 st = posix.lstat(path)
94 except (posix.error, AttributeError):
95 return 0
96 return stat.S_ISLNK(st[stat.ST_MODE])
97
98
99# Does a path exist?
100# This is false for dangling symbolic links.
101
Guido van Rossumc6360141990-10-13 19:23:40 +0000102def exists(path):
103 try:
104 st = posix.stat(path)
105 except posix.error:
106 return 0
107 return 1
108
109
110# Is a path a posix directory?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000111# This follows symbolic links, so both islink() and isdir() can be true
112# for the same path.
113
Guido van Rossumc6360141990-10-13 19:23:40 +0000114def isdir(path):
115 try:
116 st = posix.stat(path)
117 except posix.error:
118 return 0
Guido van Rossum40d93041990-10-21 16:17:34 +0000119 return stat.S_ISDIR(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000120
121
Guido van Rossum7ac48781992-01-14 18:29:32 +0000122# Is a path a regulat file?
123# This follows symbolic links, so both islink() and isdir() can be true
124# for the same path.
125
126def isfile(path):
Guido van Rossumc6360141990-10-13 19:23:40 +0000127 try:
Guido van Rossum7ac48781992-01-14 18:29:32 +0000128 st = posix.stat(path)
129 except posix.error:
Guido van Rossumc6360141990-10-13 19:23:40 +0000130 return 0
Guido van Rossum7ac48781992-01-14 18:29:32 +0000131 return stat.S_ISREG(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000132
133
Guido van Rossumd3778f91991-11-12 15:37:40 +0000134# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000135
Guido van Rossumd3778f91991-11-12 15:37:40 +0000136def samefile(f1, f2):
137 s1 = posix.stat(f1)
138 s2 = posix.stat(f2)
139 return samestat(s1, s2)
140
141
142# Are two open files really referencing the same file?
143# (Not necessarily the same file descriptor!)
144# XXX Oops, posix.fstat() doesn't exist yet!
Guido van Rossum7ac48781992-01-14 18:29:32 +0000145
Guido van Rossumd3778f91991-11-12 15:37:40 +0000146def sameopenfile(fp1, fp2):
147 s1 = posix.fstat(fp1)
148 s2 = posix.fstat(fp2)
149 return samestat(s1, s2)
150
151
152# Are two stat buffers (obtained from stat, fstat or lstat)
153# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000154
Guido van Rossumd3778f91991-11-12 15:37:40 +0000155def samestat(s1, s2):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000156 return s1[stat.ST_INO] == s2[stat.ST_INO] and \
157 s1[stat.ST_DEV] == s2[stat.STD_DEV]
Guido van Rossumd3778f91991-11-12 15:37:40 +0000158
159
160# Subroutine and global data used by ismount().
161
Guido van Rossumc6360141990-10-13 19:23:40 +0000162_mounts = []
163
164def _getmounts():
165 import commands, string
166 mounts = []
167 data = commands.getoutput('/etc/mount')
168 lines = string.splitfields(data, '\n')
169 for line in lines:
170 words = string.split(line)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000171 if len(words) >= 3 and words[1] == 'on':
Guido van Rossumc6360141990-10-13 19:23:40 +0000172 mounts.append(words[2])
173 return mounts
174
175
176# Is a path a mount point?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000177# This only works for normalized paths,
Guido van Rossumc6360141990-10-13 19:23:40 +0000178# and only if the mount table as printed by /etc/mount is correct.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000179# It tries to make relative paths absolute by prefixing them with the
180# current directory, but it won't normalize arguments containing '../'
181# or symbolic links.
182
Guido van Rossumc6360141990-10-13 19:23:40 +0000183def ismount(path):
Guido van Rossum7ac48781992-01-14 18:29:32 +0000184 if not isabs(path):
185 path = join(posix.getcwd(), path)
Guido van Rossumc6360141990-10-13 19:23:40 +0000186 if not _mounts:
187 _mounts[:] = _getmounts()
188 return path in _mounts
189
190
191# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000192# For each directory under top (including top itself, but excluding
193# '.' and '..'), func(arg, dirname, filenames) is called, where
194# dirname is the name of the directory and filenames is the list
195# files files (and subdirectories etc.) in the directory.
196# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000197# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000198
Guido van Rossumc6360141990-10-13 19:23:40 +0000199def walk(top, func, arg):
200 try:
201 names = posix.listdir(top)
202 except posix.error:
203 return
204 func(arg, top, names)
205 exceptions = ('.', '..')
206 for name in names:
207 if name not in exceptions:
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000208 name = join(top, name)
Guido van Rossumc6360141990-10-13 19:23:40 +0000209 if isdir(name):
210 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000211
212
213# Expand paths beginning with '~' or '~user'.
214# '~' means $HOME; '~user' means that user's home directory.
215# If the path doesn't begin with '~', or if the user or $HOME is unknown,
216# the path is returned unchanged (leaving error reporting to whatever
217# function is called with the expanded path as argument).
218# See also module 'glob' for expansion of *, ? and [...] in pathnames.
219# (A function should also be defined to do full *sh-style environment
220# variable expansion.)
221
222def expanduser(path):
223 if path[:1] <> '~':
224 return path
225 i, n = 1, len(path)
226 while i < n and path[i] <> '/':
227 i = i+1
228 if i == 1:
229 if not posix.environ.has_key('HOME'):
230 return path
231 userhome = posix.environ['HOME']
232 else:
233 import pwd
234 try:
235 pwent = pwd.getpwnam(path[1:i])
236 except KeyError:
237 return path
238 userhome = pwent[5]
239 return userhome + path[i:]