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 original path ends in '/' but is not the root, this
36 # '/' is stripped. After the trailing '/' is stripped, the invariant
37 # join(head, tail) == p holds.
38 # The resulting head won't end in '/' unless it is the root.
41 if p
[-1:] == '/' and p
<> '/'*len(p
):
48 head
, tail
= head
+ tail
, ''
49 if head
[-1:] == '/' and head
<> '/'*len(head
):
50 while head
[-1] == '/':
55 # Split a path in root and extension.
56 # The extension is everything starting at the first dot in the last
57 # pathname component; the root is everything before that.
58 # It is always true that root + ext == p.
64 root
, ext
= root
+ ext
+ c
, ''
72 # Return the tail (basename) part of a path.
78 # Return the head (dirname) part of a path.
84 # Return the longest prefix of all list elements.
90 for i
in range(len(prefix
)):
91 if prefix
[:i
+1] <> item
[:i
+1]:
98 # Is a path a symbolic link?
99 # This will always return false on systems where posix.lstat doesn't exist.
103 st
= posix
.lstat(path
)
104 except (posix
.error
, AttributeError):
106 return stat
.S_ISLNK(st
[stat
.ST_MODE
])
110 # This is false for dangling symbolic links.
114 st
= posix
.stat(path
)
120 # Is a path a posix directory?
121 # This follows symbolic links, so both islink() and isdir() can be true
126 st
= posix
.stat(path
)
129 return stat
.S_ISDIR(st
[stat
.ST_MODE
])
132 # Is a path a regular file?
133 # This follows symbolic links, so both islink() and isdir() can be true
138 st
= posix
.stat(path
)
141 return stat
.S_ISREG(st
[stat
.ST_MODE
])
144 # Are two filenames really pointing to the same file?
146 def samefile(f1
, f2
):
149 return samestat(s1
, s2
)
152 # Are two open files really referencing the same file?
153 # (Not necessarily the same file descriptor!)
154 # XXX Oops, posix.fstat() doesn't exist yet!
156 def sameopenfile(fp1
, fp2
):
157 s1
= posix
.fstat(fp1
)
158 s2
= posix
.fstat(fp2
)
159 return samestat(s1
, s2
)
162 # Are two stat buffers (obtained from stat, fstat or lstat)
163 # describing the same file?
165 def samestat(s1
, s2
):
166 return s1
[stat
.ST_INO
] == s2
[stat
.ST_INO
] and \
167 s1
[stat
.ST_DEV
] == s2
[stat
.ST_DEV
]
170 # Is a path a mount point?
171 # (Does this work for all UNIXes? Is it even guaranteed to work by POSIX?)
175 s1
= posix
.stat(path
)
176 s2
= posix
.stat(join(path
, '..'))
178 return 0 # It doesn't exist -- so not a mount point :-)
179 dev1
= s1
[stat
.ST_DEV
]
180 dev2
= s2
[stat
.ST_DEV
]
182 return 1 # path/.. on a different device as path
183 ino1
= s1
[stat
.ST_INO
]
184 ino2
= s2
[stat
.ST_INO
]
186 return 1 # path/.. is the same i-node as path
190 # Directory tree walk.
191 # For each directory under top (including top itself, but excluding
192 # '.' and '..'), func(arg, dirname, filenames) is called, where
193 # dirname is the name of the directory and filenames is the list
194 # files files (and subdirectories etc.) in the directory.
195 # The func may modify the filenames list, to implement a filter,
196 # or to impose a different order of visiting.
198 def walk(top
, func
, arg
):
200 names
= posix
.listdir(top
)
203 func(arg
, top
, names
)
204 exceptions
= ('.', '..')
206 if name
not in exceptions
:
207 name
= join(top
, name
)
209 walk(name
, func
, arg
)
212 # Expand paths beginning with '~' or '~user'.
213 # '~' means $HOME; '~user' means that user's home directory.
214 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
215 # the path is returned unchanged (leaving error reporting to whatever
216 # function is called with the expanded path as argument).
217 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
218 # (A function should also be defined to do full *sh-style environment
219 # variable expansion.)
221 def expanduser(path
):
225 while i
< n
and path
[i
] <> '/':
228 if not posix
.environ
.has_key('HOME'):
230 userhome
= posix
.environ
['HOME']
234 pwent
= pwd
.getpwnam(path
[1:i
])
238 return userhome
+ path
[i
:]
241 # Expand paths containing shell variable substitutions.
242 # This is done by piping it through the shell.
243 # Shell quoting characters (\ " ' `) are protected by a backslash.
244 # NB: a future version may avoid starting a subprocess and do the
245 # substitutions internally. This may slightly change the syntax
248 def expandvars(path
):
253 if c
in ('\\', '"', '\'', '`'):
259 p
= posix
.popen('cat <<' + d
+ '\n' + q
+ '\n' + d
+ '\n', 'r')
267 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
268 # It should be understood that this may change the meaning of the path
269 # if it contains symbolic links!
273 # Treat initial slashes specially
275 while path
[:1] == '/':
276 slashes
= slashes
+ '/'
278 comps
= string
.splitfields(path
, '/')
280 while i
< len(comps
):
283 elif comps
[i
] == '..' and i
> 0 and \
284 comps
[i
-1] not in ('', '..'):
287 elif comps
[i
] == '' and i
> 0 and comps
[i
-1] <> '':
291 # If the path is now empty, substitute '.'
292 if not comps
and not slashes
:
294 return slashes
+ string
.joinfields(comps
, '/')