blob: ab2aefface39e55962d18f1036c23537f8be4896 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Common operations on Posix pathnames.
2
3Instead of importing this module directly, import os and refer to
4this module as os.path. The "os.path" name is an alias for this
5module on Posix systems; on other systems (e.g. Mac, Windows),
6os.path provides the same operations in a manner specific to that
7platform, and is an alias to another module (e.g. macpath, ntpath).
8
9Some of this can actually be useful on non-Posix systems too, e.g.
10for manipulation of the pathname component of URLs.
Guido van Rossum346f7af1997-12-05 19:04:51 +000011"""
Guido van Rossumc6360141990-10-13 19:23:40 +000012
Guido van Rossumd3876d31996-07-23 03:47:28 +000013import os
Guido van Rossumf0af3e32008-10-02 18:55:37 +000014import sys
Guido van Rossum40d93041990-10-21 16:17:34 +000015import stat
Guido van Rossumd8faa362007-04-27 19:54:29 +000016import genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +000017from genericpath import *
Guido van Rossumc6360141990-10-13 19:23:40 +000018
Skip Montanaroc62c81e2001-02-12 02:00:42 +000019__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
20 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000021 "getatime","getctime","islink","exists","lexists","isdir","isfile",
Benjamin Petersond71ca412008-05-08 23:44:58 +000022 "ismount", "expanduser","expandvars","normpath","abspath",
Neal Norwitz61cdac62003-01-03 18:01:57 +000023 "samefile","sameopenfile","samestat",
Skip Montanaro117910d2003-02-14 19:35:31 +000024 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
Guido van Rossumd8faa362007-04-27 19:54:29 +000025 "devnull","realpath","supports_unicode_filenames","relpath"]
Guido van Rossumc6360141990-10-13 19:23:40 +000026
Guido van Rossumf0af3e32008-10-02 18:55:37 +000027# Strings representing various path-related bits and pieces.
28# These are primarily for export; internally, they are hardcoded.
Skip Montanaro117910d2003-02-14 19:35:31 +000029curdir = '.'
30pardir = '..'
31extsep = '.'
32sep = '/'
33pathsep = ':'
34defpath = ':/bin:/usr/bin'
35altsep = None
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000036devnull = '/dev/null'
Skip Montanaro117910d2003-02-14 19:35:31 +000037
Guido van Rossumf0af3e32008-10-02 18:55:37 +000038def _get_sep(path):
39 if isinstance(path, bytes):
40 return b'/'
41 else:
42 return '/'
43
Guido van Rossum7ac48781992-01-14 18:29:32 +000044# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
45# On MS-DOS this may also turn slashes into backslashes; however, other
46# normalizations (such as optimizing '../' away) are not allowed
47# (another function should be defined to do that).
48
49def normcase(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000050 """Normalize case of pathname. Has no effect under Posix"""
Guido van Rossumf0af3e32008-10-02 18:55:37 +000051 # TODO: on Mac OS X, this should really return s.lower().
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +000052 if not isinstance(s, (bytes, str)):
53 raise TypeError("normcase() argument must be str or bytes, "
54 "not '{}'".format(s.__class__.__name__))
Guido van Rossum346f7af1997-12-05 19:04:51 +000055 return s
Guido van Rossum7ac48781992-01-14 18:29:32 +000056
57
Jeremy Hyltona05e2932000-06-28 14:48:01 +000058# Return whether a path is absolute.
Guido van Rossum7ac48781992-01-14 18:29:32 +000059# Trivial in Posix, harder on the Mac or MS-DOS.
60
61def isabs(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000062 """Test whether a path is absolute"""
Guido van Rossumf0af3e32008-10-02 18:55:37 +000063 sep = _get_sep(s)
64 return s.startswith(sep)
Guido van Rossum7ac48781992-01-14 18:29:32 +000065
66
Barry Warsaw384d2491997-02-18 21:53:25 +000067# Join pathnames.
68# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000069# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000070
Barry Warsaw384d2491997-02-18 21:53:25 +000071def join(a, *p):
Guido van Rossum04110fb2007-08-24 16:32:05 +000072 """Join two or more pathname components, inserting '/' as needed.
73 If any component is an absolute path, all previous path components
74 will be discarded."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +000075 sep = _get_sep(a)
Guido van Rossum346f7af1997-12-05 19:04:51 +000076 path = a
Hynek Schlawack47749462012-07-15 16:21:30 +020077 try:
78 for b in p:
79 if b.startswith(sep):
80 path = b
81 elif not path or path.endswith(sep):
82 path += b
83 else:
84 path += sep + b
85 except TypeError:
86 strs = [isinstance(s, str) for s in (a, ) + p]
87 if any(strs) and not all(strs):
Hynek Schlawack9ac4d882012-07-15 16:46:23 +020088 raise TypeError("Can't mix strings and bytes in path "
89 "components.") from None
Guido van Rossum346f7af1997-12-05 19:04:51 +000090 else:
Hynek Schlawack47749462012-07-15 16:21:30 +020091 raise
Guido van Rossum346f7af1997-12-05 19:04:51 +000092 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000093
94
Guido van Rossum26847381992-03-31 18:54:35 +000095# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000096# rest). If the path ends in '/', tail will be empty. If there is no
97# '/' in the path, head will be empty.
98# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000099
Guido van Rossumc6360141990-10-13 19:23:40 +0000100def split(p):
Tim Peters2344fae2001-01-15 00:50:52 +0000101 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
Fred Drakec0ab93e2000-09-28 16:22:52 +0000102 everything after the final slash. Either part may be empty."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000103 sep = _get_sep(p)
104 i = p.rfind(sep) + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +0000105 head, tail = p[:i], p[i:]
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000106 if head and head != sep*len(head):
107 head = head.rstrip(sep)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000108 return head, tail
Guido van Rossumc6360141990-10-13 19:23:40 +0000109
110
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000111# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +0000112# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000113# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000114# It is always true that root + ext == p.
115
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000116def splitext(p):
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000117 if isinstance(p, bytes):
118 sep = b'/'
119 extsep = b'.'
120 else:
121 sep = '/'
122 extsep = '.'
123 return genericpath._splitext(p, sep, None, extsep)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000124splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000125
Guido van Rossum221df241995-08-07 20:17:55 +0000126# Split a pathname into a drive specification and the rest of the
127# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
128
129def splitdrive(p):
Tim Peters2344fae2001-01-15 00:50:52 +0000130 """Split a pathname into drive and path. On Posix, drive is always
Fred Drakec0ab93e2000-09-28 16:22:52 +0000131 empty."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000132 return p[:0], p
Guido van Rossum221df241995-08-07 20:17:55 +0000133
134
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135# Return the tail (basename) part of a path, same as split(path)[1].
Guido van Rossum7ac48781992-01-14 18:29:32 +0000136
Guido van Rossumc6360141990-10-13 19:23:40 +0000137def basename(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000138 """Returns the final component of a pathname"""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000139 sep = _get_sep(p)
140 i = p.rfind(sep) + 1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141 return p[i:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000142
143
Thomas Wouters89f507f2006-12-13 04:49:30 +0000144# Return the head (dirname) part of a path, same as split(path)[0].
Guido van Rossumc629d341992-11-05 10:43:02 +0000145
146def dirname(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000147 """Returns the directory component of a pathname"""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000148 sep = _get_sep(p)
149 i = p.rfind(sep) + 1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 head = p[:i]
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000151 if head and head != sep*len(head):
152 head = head.rstrip(sep)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 return head
Guido van Rossumc629d341992-11-05 10:43:02 +0000154
155
Guido van Rossum7ac48781992-01-14 18:29:32 +0000156# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000157# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000158
159def islink(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000160 """Test whether a path is a symbolic link"""
161 try:
162 st = os.lstat(path)
163 except (os.error, AttributeError):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000164 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000165 return stat.S_ISLNK(st.st_mode)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000166
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000167# Being true for dangling symbolic links is also useful.
168
169def lexists(path):
170 """Test whether a path exists. Returns True for broken symbolic links"""
171 try:
Georg Brandl89fad142010-03-14 10:23:39 +0000172 os.lstat(path)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000173 except os.error:
174 return False
175 return True
176
177
Guido van Rossumd3778f91991-11-12 15:37:40 +0000178# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000179
Guido van Rossumd3778f91991-11-12 15:37:40 +0000180def samefile(f1, f2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000181 """Test whether two pathnames reference the same actual file"""
182 s1 = os.stat(f1)
183 s2 = os.stat(f2)
184 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000185
186
187# Are two open files really referencing the same file?
188# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000189
Guido van Rossumd3778f91991-11-12 15:37:40 +0000190def sameopenfile(fp1, fp2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000191 """Test whether two open file objects reference the same file"""
192 s1 = os.fstat(fp1)
193 s2 = os.fstat(fp2)
194 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000195
196
197# Are two stat buffers (obtained from stat, fstat or lstat)
198# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000199
Guido van Rossumd3778f91991-11-12 15:37:40 +0000200def samestat(s1, s2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000201 """Test whether two stat buffers reference the same file"""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000202 return s1.st_ino == s2.st_ino and \
203 s1.st_dev == s2.st_dev
Guido van Rossumc6360141990-10-13 19:23:40 +0000204
205
206# Is a path a mount point?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000207# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000208
Guido van Rossumc6360141990-10-13 19:23:40 +0000209def ismount(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000210 """Test whether a path is a mount point"""
Georg Brandle6c59502010-08-01 15:30:56 +0000211 if islink(path):
212 # A symlink can never be a mount point
213 return False
Guido van Rossum346f7af1997-12-05 19:04:51 +0000214 try:
Christian Heimesfaf2f632008-01-06 16:59:19 +0000215 s1 = os.lstat(path)
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000216 if isinstance(path, bytes):
217 parent = join(path, b'..')
218 else:
219 parent = join(path, '..')
220 s2 = os.lstat(parent)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000221 except os.error:
Tim Petersbc0e9102002-04-04 22:55:58 +0000222 return False # It doesn't exist -- so not a mount point :-)
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000223 dev1 = s1.st_dev
224 dev2 = s2.st_dev
Guido van Rossum346f7af1997-12-05 19:04:51 +0000225 if dev1 != dev2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000226 return True # path/.. on a different device as path
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000227 ino1 = s1.st_ino
228 ino2 = s2.st_ino
Guido van Rossum346f7af1997-12-05 19:04:51 +0000229 if ino1 == ino2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000230 return True # path/.. is the same i-node as path
231 return False
Guido van Rossumc6360141990-10-13 19:23:40 +0000232
233
Guido van Rossum7ac48781992-01-14 18:29:32 +0000234# Expand paths beginning with '~' or '~user'.
235# '~' means $HOME; '~user' means that user's home directory.
236# If the path doesn't begin with '~', or if the user or $HOME is unknown,
237# the path is returned unchanged (leaving error reporting to whatever
238# function is called with the expanded path as argument).
239# See also module 'glob' for expansion of *, ? and [...] in pathnames.
240# (A function should also be defined to do full *sh-style environment
241# variable expansion.)
242
243def expanduser(path):
Tim Peters2344fae2001-01-15 00:50:52 +0000244 """Expand ~ and ~user constructions. If user or $HOME is unknown,
Fred Drakec0ab93e2000-09-28 16:22:52 +0000245 do nothing."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000246 if isinstance(path, bytes):
247 tilde = b'~'
248 else:
249 tilde = '~'
250 if not path.startswith(tilde):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000251 return path
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000252 sep = _get_sep(path)
253 i = path.find(sep, 1)
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000254 if i < 0:
255 i = len(path)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000256 if i == 1:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000257 if 'HOME' not in os.environ:
Neal Norwitz609ba812002-09-05 21:08:25 +0000258 import pwd
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000259 userhome = pwd.getpwuid(os.getuid()).pw_dir
Neal Norwitz609ba812002-09-05 21:08:25 +0000260 else:
261 userhome = os.environ['HOME']
Guido van Rossum346f7af1997-12-05 19:04:51 +0000262 else:
263 import pwd
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000264 name = path[1:i]
265 if isinstance(name, bytes):
266 name = str(name, 'ASCII')
Guido van Rossum346f7af1997-12-05 19:04:51 +0000267 try:
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000268 pwent = pwd.getpwnam(name)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000269 except KeyError:
270 return path
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000271 userhome = pwent.pw_dir
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000272 if isinstance(path, bytes):
Victor Stinner16004ac2010-09-29 16:59:18 +0000273 userhome = os.fsencode(userhome)
Benjamin Petersonef3e4c22009-04-11 19:48:14 +0000274 root = b'/'
275 else:
276 root = '/'
Jesus Cea7f0d8882012-05-10 05:10:50 +0200277 userhome = userhome.rstrip(root)
278 return (userhome + path[i:]) or root
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000279
280
281# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000283# Non-existent variables are left unchanged.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284
285_varprog = None
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000286_varprogb = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000287
288def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000289 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000290 are left unchanged."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000291 global _varprog, _varprogb
292 if isinstance(path, bytes):
293 if b'$' not in path:
294 return path
295 if not _varprogb:
296 import re
297 _varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII)
298 search = _varprogb.search
299 start = b'{'
300 end = b'}'
301 else:
302 if '$' not in path:
303 return path
304 if not _varprog:
305 import re
306 _varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII)
307 search = _varprog.search
308 start = '{'
309 end = '}'
Guido van Rossum346f7af1997-12-05 19:04:51 +0000310 i = 0
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000311 while True:
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000312 m = search(path, i)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000313 if not m:
314 break
315 i, j = m.span(0)
316 name = m.group(1)
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000317 if name.startswith(start) and name.endswith(end):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000318 name = name[1:-1]
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000319 if isinstance(name, bytes):
320 name = str(name, 'ASCII')
Raymond Hettinger54f02222002-06-01 14:18:47 +0000321 if name in os.environ:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000322 tail = path[j:]
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000323 value = os.environ[name]
324 if isinstance(path, bytes):
325 value = value.encode('ASCII')
326 path = path[:i] + value
Guido van Rossum346f7af1997-12-05 19:04:51 +0000327 i = len(path)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000328 path += tail
Guido van Rossum346f7af1997-12-05 19:04:51 +0000329 else:
330 i = j
331 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000332
333
334# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
335# It should be understood that this may change the meaning of the path
336# if it contains symbolic links!
337
338def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000339 """Normalize path, eliminating double slashes, etc."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000340 if isinstance(path, bytes):
341 sep = b'/'
342 empty = b''
343 dot = b'.'
344 dotdot = b'..'
345 else:
346 sep = '/'
347 empty = ''
348 dot = '.'
349 dotdot = '..'
350 if path == empty:
351 return dot
352 initial_slashes = path.startswith(sep)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000353 # POSIX allows one or two initial slashes, but treats three or more
354 # as single slash.
Tim Peters658cba62001-02-09 20:06:00 +0000355 if (initial_slashes and
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000356 path.startswith(sep*2) and not path.startswith(sep*3)):
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000357 initial_slashes = 2
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000358 comps = path.split(sep)
Skip Montanaro018dfae2000-07-19 17:09:51 +0000359 new_comps = []
360 for comp in comps:
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000361 if comp in (empty, dot):
Skip Montanaro018dfae2000-07-19 17:09:51 +0000362 continue
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000363 if (comp != dotdot or (not initial_slashes and not new_comps) or
364 (new_comps and new_comps[-1] == dotdot)):
Skip Montanaro018dfae2000-07-19 17:09:51 +0000365 new_comps.append(comp)
366 elif new_comps:
367 new_comps.pop()
368 comps = new_comps
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000369 path = sep.join(comps)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000370 if initial_slashes:
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000371 path = sep*initial_slashes + path
372 return path or dot
Guido van Rossume294cf61999-01-29 18:05:18 +0000373
374
Guido van Rossume294cf61999-01-29 18:05:18 +0000375def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000376 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000377 if not isabs(path):
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000378 if isinstance(path, bytes):
379 cwd = os.getcwdb()
380 else:
381 cwd = os.getcwd()
382 path = join(cwd, path)
Guido van Rossume294cf61999-01-29 18:05:18 +0000383 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000384
385
386# Return a canonical path (i.e. the absolute location of a file on the
387# filesystem).
388
389def realpath(filename):
390 """Return the canonical path of the specified filename, eliminating any
391symbolic links encountered in the path."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000392 if isinstance(filename, bytes):
393 sep = b'/'
394 empty = b''
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000395 else:
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000396 sep = '/'
397 empty = ''
398 if isabs(filename):
399 bits = [sep] + filename.split(sep)[1:]
400 else:
401 bits = [empty] + filename.split(sep)
Tim Petersa45cacf2004-08-20 03:47:14 +0000402
Guido van Rossum83eeef42001-09-17 15:16:09 +0000403 for i in range(2, len(bits)+1):
404 component = join(*bits[0:i])
Brett Cannonf50299c2004-07-10 22:55:15 +0000405 # Resolve symbolic links.
Brett Cannondfa5d952004-07-11 19:16:21 +0000406 if islink(component):
Brett Cannonf50299c2004-07-10 22:55:15 +0000407 resolved = _resolve_link(component)
408 if resolved is None:
409 # Infinite loop -- return original component + rest of the path
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000410 return abspath(join(*([component] + bits[i:])))
Brett Cannonf50299c2004-07-10 22:55:15 +0000411 else:
412 newpath = join(*([resolved] + bits[i:]))
Tim Petersa45cacf2004-08-20 03:47:14 +0000413 return realpath(newpath)
Tim Petersb64bec32001-09-18 02:26:39 +0000414
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000415 return abspath(filename)
Tim Petersa45cacf2004-08-20 03:47:14 +0000416
Brett Cannonf50299c2004-07-10 22:55:15 +0000417
418def _resolve_link(path):
419 """Internal helper function. Takes a path and follows symlinks
Tim Peters182b5ac2004-07-18 06:16:08 +0000420 until we either arrive at something that isn't a symlink, or
Brett Cannonf50299c2004-07-10 22:55:15 +0000421 encounter a path we've seen before (meaning that there's a loop).
422 """
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000423 paths_seen = set()
Brett Cannonf50299c2004-07-10 22:55:15 +0000424 while islink(path):
Brett Cannondfa5d952004-07-11 19:16:21 +0000425 if path in paths_seen:
Brett Cannonf50299c2004-07-10 22:55:15 +0000426 # Already seen this path, so we must have a symlink loop
427 return None
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000428 paths_seen.add(path)
Brett Cannonf50299c2004-07-10 22:55:15 +0000429 # Resolve where the link points to
Brett Cannondfa5d952004-07-11 19:16:21 +0000430 resolved = os.readlink(path)
Andrew M. Kuchlingc75f1122004-08-02 14:54:16 +0000431 if not isabs(resolved):
Brett Cannonf50299c2004-07-10 22:55:15 +0000432 dir = dirname(path)
433 path = normpath(join(dir, resolved))
434 else:
435 path = normpath(resolved)
436 return path
437
Victor Stinnere797c162010-09-17 23:34:26 +0000438supports_unicode_filenames = (sys.platform == 'darwin')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000439
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000440def relpath(path, start=None):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000441 """Return a relative version of a path"""
442
443 if not path:
444 raise ValueError("no path specified")
445
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000446 if isinstance(path, bytes):
447 curdir = b'.'
448 sep = b'/'
449 pardir = b'..'
450 else:
451 curdir = '.'
452 sep = '/'
453 pardir = '..'
454
455 if start is None:
456 start = curdir
457
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000458 start_list = [x for x in abspath(start).split(sep) if x]
459 path_list = [x for x in abspath(path).split(sep) if x]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000460
461 # Work out how much of the filepath is shared by start and path.
462 i = len(commonprefix([start_list, path_list]))
463
464 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Christian Heimesfaf2f632008-01-06 16:59:19 +0000465 if not rel_list:
466 return curdir
Guido van Rossumd8faa362007-04-27 19:54:29 +0000467 return join(*rel_list)