blob: 0b32d636ca83f9db894adc2095241d51c9da778d [file] [log] [blame]
Andrew MacIntyre5cef5712002-02-24 05:32:32 +00001# Module 'os2emxpath' -- common operations on OS/2 pathnames
Tim Peters863ac442002-04-16 01:38:40 +00002"""Common pathname manipulations, OS/2 EMX version.
Andrew MacIntyre5cef5712002-02-24 05:32:32 +00003
4Instead of importing this module directly, import os and refer to this
5module as os.path.
6"""
7
8import os
9import stat
Jack Diederich7b604642006-08-26 18:42:06 +000010from genericpath import *
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +020011from genericpath import _unicode
Jack Diederich7b604642006-08-26 18:42:06 +000012from ntpath import (expanduser, expandvars, isabs, islink, splitdrive,
13 splitext, split, walk)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000014
15__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
16 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000017 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
18 "ismount","walk","expanduser","expandvars","normpath","abspath",
19 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
20 "extsep","devnull","realpath","supports_unicode_filenames"]
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000021
Skip Montanaro117910d2003-02-14 19:35:31 +000022# strings representing various path-related bits and pieces
23curdir = '.'
24pardir = '..'
25extsep = '.'
26sep = '/'
27altsep = '\\'
28pathsep = ';'
29defpath = '.;C:\\bin'
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000030devnull = 'nul'
Skip Montanaro117910d2003-02-14 19:35:31 +000031
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000032# Normalize the case of a pathname and map slashes to backslashes.
33# Other normalizations (such as optimizing '../' away) are not done
34# (this is done by normpath).
35
36def normcase(s):
37 """Normalize case of pathname.
38
39 Makes all characters lowercase and all altseps into seps."""
40 return s.replace('\\', '/').lower()
41
42
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000043# Join two (or more) paths.
44
45def join(a, *p):
46 """Join two or more pathname components, inserting sep as needed"""
47 path = a
48 for b in p:
49 if isabs(b):
50 path = b
51 elif path == '' or path[-1:] in '/\\:':
52 path = path + b
53 else:
54 path = path + '/' + b
55 return path
56
57
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000058# Parse UNC paths
59def splitunc(p):
60 """Split a pathname into UNC mount point and relative path specifiers.
61
62 Return a 2-tuple (unc, rest); either part may be empty.
63 If unc is not empty, it has the form '//host/mount' (or similar
64 using backslashes). unc+rest is always the input path.
65 Paths containing drive letters never have an UNC part.
66 """
67 if p[1:2] == ':':
68 return '', p # Drive letter present
69 firstTwo = p[0:2]
70 if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
71 # is a UNC path:
72 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
73 # \\machine\mountpoint\directories...
74 # directory ^^^^^^^^^^^^^^^
75 normp = normcase(p)
76 index = normp.find('/', 2)
77 if index == -1:
78 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
79 return ("", p)
80 index = normp.find('/', index + 1)
81 if index == -1:
82 index = len(p)
83 return p[:index], p[index:]
84 return '', p
85
86
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000087# Return the tail (basename) part of a path.
88
89def basename(p):
90 """Returns the final component of a pathname"""
91 return split(p)[1]
92
93
94# Return the head (dirname) part of a path.
95
96def dirname(p):
97 """Returns the directory component of a pathname"""
98 return split(p)[0]
99
100
Jack Diederich7b604642006-08-26 18:42:06 +0000101# alias exists to lexists
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000102lexists = exists
103
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000104
105# Is a path a directory?
106
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000107# Is a path a mount point? Either a root (with or without drive letter)
108# or an UNC path with at most a / or \ after the mount point.
109
110def ismount(path):
111 """Test whether a path is a mount point (defined as root of drive)"""
112 unc, rest = splitunc(path)
113 if unc:
114 return rest in ("", "/", "\\")
115 p = splitdrive(path)[1]
116 return len(p) == 1 and p[0] in '/\\'
117
118
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000119# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
120
121def normpath(path):
122 """Normalize path, eliminating double slashes, etc."""
123 path = path.replace('\\', '/')
124 prefix, path = splitdrive(path)
125 while path[:1] == '/':
126 prefix = prefix + '/'
127 path = path[1:]
128 comps = path.split('/')
129 i = 0
130 while i < len(comps):
131 if comps[i] == '.':
132 del comps[i]
133 elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
134 del comps[i-1:i+1]
135 i = i - 1
136 elif comps[i] == '' and i > 0 and comps[i-1] != '':
137 del comps[i]
138 else:
139 i = i + 1
140 # If the path is now empty, substitute '.'
141 if not prefix and not comps:
142 comps.append('.')
143 return prefix + '/'.join(comps)
144
145
146# Return an absolute path.
147def abspath(path):
148 """Return the absolute version of a path"""
149 if not isabs(path):
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200150 if isinstance(path, _unicode):
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000151 cwd = os.getcwdu()
152 else:
153 cwd = os.getcwd()
154 path = join(cwd, path)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000155 return normpath(path)
Mark Hammond8696ebc2002-10-08 02:44:31 +0000156
Neal Norwitz61cdac62003-01-03 18:01:57 +0000157# realpath is a no-op on systems without islink support
158realpath = abspath
159
Mark Hammond8696ebc2002-10-08 02:44:31 +0000160supports_unicode_filenames = False