1 # Module 'posixpath' -- common operations on POSIX pathnames
7 # 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).
16 # Return wheter a path is absolute.
17 # Trivial in Posix, harder on the Mac or MS-DOS.
24 # Ignore the first part if the second part is absolute.
25 # Insert a '/' unless the first part is empty or already ends in '/'.
28 if b
[:1] == '/': return b
29 if a
== '' or a
[-1:] == '/': return a
+ b
30 # Note: join('x', '') returns 'x/'; is this what we want?
34 # Split a path in head (everything up to the last '/') and tail (the
35 # 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.
41 i
= string
.rfind(p
, '/') + 1
42 head
, tail
= p
[:i
], p
[i
:]
43 if head
and head
<> '/'*len(head
):
44 while head
[-1] == '/':
49 # 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.
52 # It is always true that root + ext == p.
58 root
, ext
= root
+ ext
+ c
, ''
61 root
, ext
= root
+ ext
, c
71 # 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.
78 # Return the tail (basename) part of a path.
84 # Return the head (dirname) part of a path.
90 # Return the longest prefix of all list elements.
96 for i
in range(len(prefix
)):
97 if prefix
[:i
+1] <> item
[:i
+1]:
104 # Is a path a symbolic link?
105 # This will always return false on systems where posix.lstat doesn't exist.
109 st
= posix
.lstat(path
)
110 except (posix
.error
, AttributeError):
112 return stat
.S_ISLNK(st
[stat
.ST_MODE
])
116 # This is false for dangling symbolic links.
120 st
= posix
.stat(path
)
126 # Is a path a posix directory?
127 # This follows symbolic links, so both islink() and isdir() can be true
132 st
= posix
.stat(path
)
135 return stat
.S_ISDIR(st
[stat
.ST_MODE
])
138 # Is a path a regular file?
139 # This follows symbolic links, so both islink() and isfile() can be true
144 st
= posix
.stat(path
)
147 return stat
.S_ISREG(st
[stat
.ST_MODE
])
150 # Are two filenames really pointing to the same file?
152 def samefile(f1
, f2
):
155 return samestat(s1
, s2
)
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!
162 def sameopenfile(fp1
, fp2
):
163 s1
= posix
.fstat(fp1
)
164 s2
= posix
.fstat(fp2
)
165 return samestat(s1
, s2
)
168 # Are two stat buffers (obtained from stat, fstat or lstat)
169 # describing the same file?
171 def samestat(s1
, s2
):
172 return s1
[stat
.ST_INO
] == s2
[stat
.ST_INO
] and \
173 s1
[stat
.ST_DEV
] == s2
[stat
.ST_DEV
]
176 # Is a path a mount point?
177 # (Does this work for all UNIXes? Is it even guaranteed to work by POSIX?)
181 s1
= posix
.stat(path
)
182 s2
= posix
.stat(join(path
, '..'))
184 return 0 # It doesn't exist -- so not a mount point :-)
185 dev1
= s1
[stat
.ST_DEV
]
186 dev2
= s2
[stat
.ST_DEV
]
188 return 1 # path/.. on a different device as path
189 ino1
= s1
[stat
.ST_INO
]
190 ino2
= s2
[stat
.ST_INO
]
192 return 1 # path/.. is the same i-node as path
196 # Directory tree walk.
197 # 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,
202 # or to impose a different order of visiting.
204 def walk(top
, func
, arg
):
206 names
= posix
.listdir(top
)
209 func(arg
, top
, names
)
210 exceptions
= ('.', '..')
212 if name
not in exceptions
:
213 name
= join(top
, name
)
214 if isdir(name
) and not islink(name
):
215 walk(name
, func
, arg
)
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.)
227 def expanduser(path
):
231 while i
< n
and path
[i
] <> '/':
234 if not posix
.environ
.has_key('HOME'):
236 userhome
= posix
.environ
['HOME']
240 pwent
= pwd
.getpwnam(path
[1:i
])
244 return userhome
+ path
[i
:]
247 # Expand paths containing shell variable substitutions.
248 # This expands the forms $variable and ${variable} only.
249 # Non-existant variables are left unchanged.
253 def expandvars(path
):
259 _varprog
= regex
.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)')
262 i
= _varprog
.search(path
, i
)
265 name
= _varprog
.group(1)
266 j
= i
+ len(_varprog
.group(0))
267 if name
[:1] == '{' and name
[-1:] == '}':
269 if posix
.environ
.has_key(name
):
271 path
= path
[:i
] + posix
.environ
[name
]
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!
285 # Treat initial slashes specially
287 while path
[:1] == '/':
288 slashes
= slashes
+ '/'
290 comps
= string
.splitfields(path
, '/')
292 while i
< len(comps
):
295 elif comps
[i
] == '..' and i
> 0 and \
296 comps
[i
-1] not in ('', '..'):
299 elif comps
[i
] == '' and i
> 0 and comps
[i
-1] <> '':
303 # If the path is now empty, substitute '.'
304 if not comps
and not slashes
:
306 return slashes
+ string
.joinfields(comps
, '/')