source: python/trunk/Lib/os2emxpath.py@ 610

Last change on this file since 610 was 610, checked in by dmik, 10 years ago

python: Fix silly typo.

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1# Module 'os2emxpath' -- common operations on OS/2 pathnames
2"""Common pathname manipulations, OS/2 EMX version.
3
4Instead of importing this module directly, import os and refer to this
5module as os.path.
6"""
7
8import os
9import stat
10from genericpath import *
11from ntpath import (expanduser, expandvars, isabs, islink, splitdrive,
12 splitext, split, walk)
13
14__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
15 "basename","dirname","commonprefix","getsize","getmtime",
16 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
17 "ismount","walk","expanduser","expandvars","normpath","abspath",
18 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
19 "extsep","devnull","realpath","supports_unicode_filenames"]
20
21# strings representing various path-related bits and pieces
22curdir = '.'
23pardir = '..'
24extsep = '.'
25sep = '/'
26altsep = '\\'
27pathsep = ';'
28defpath = '.;C:\\bin'
29devnull = 'nul'
30
31# Normalize the case of a pathname and map slashes to backslashes.
32# Other normalizations (such as optimizing '../' away) are not done
33# (this is done by normpath).
34
35def normcase(s):
36 """Normalize case of pathname.
37
38 Makes all characters lowercase and all altseps into seps."""
39 return s.replace(altsep, sep).lower()
40
41
42# Join two (or more) paths.
43
44def join(a, *p):
45 """Join two or more pathname components, inserting sep as needed
46
47 Also replace all altsep chars with sep in the returned string
48 to make it consistent."""
49 path = a
50 for b in p:
51 if isabs(b):
52 path = b
53 elif path == '' or path[-1:] in '/\\:':
54 path = path + b
55 else:
56 path = path + '/' + b
57 return path.replace(altsep, sep)
58
59
60# Parse UNC paths
61def splitunc(p):
62 """Split a pathname into UNC mount point and relative path specifiers.
63
64 Return a 2-tuple (unc, rest); either part may be empty.
65 If unc is not empty, it has the form '//host/mount' (or similar
66 using backslashes). unc+rest is always the input path.
67 Paths containing drive letters never have an UNC part.
68 """
69 if p[1:2] == ':':
70 return '', p # Drive letter present
71 firstTwo = p[0:2]
72 if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
73 # is a UNC path:
74 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
75 # \\machine\mountpoint\directories...
76 # directory ^^^^^^^^^^^^^^^
77 normp = normcase(p)
78 index = normp.find('/', 2)
79 if index == -1:
80 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
81 return ("", p)
82 index = normp.find('/', index + 1)
83 if index == -1:
84 index = len(p)
85 return p[:index], p[index:]
86 return '', p
87
88
89# Return the tail (basename) part of a path.
90
91def basename(p):
92 """Returns the final component of a pathname"""
93 return split(p)[1]
94
95
96# Return the head (dirname) part of a path.
97
98def dirname(p):
99 """Returns the directory component of a pathname"""
100 return split(p)[0]
101
102
103# alias exists to lexists
104lexists = exists
105
106
107# Is a path a directory?
108
109# Is a path a mount point? Either a root (with or without drive letter)
110# or an UNC path with at most a / or \ after the mount point.
111
112def ismount(path):
113 """Test whether a path is a mount point (defined as root of drive)"""
114 unc, rest = splitunc(path)
115 if unc:
116 return rest in ("", "/", "\\")
117 p = splitdrive(path)[1]
118 return len(p) == 1 and p[0] in '/\\'
119
120
121# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
122
123def normpath(path):
124 """Normalize path, eliminating double slashes, etc."""
125 path = path.replace('\\', '/')
126 prefix, path = splitdrive(path)
127 while path[:1] == '/':
128 prefix = prefix + '/'
129 path = path[1:]
130 comps = path.split('/')
131 i = 0
132 while i < len(comps):
133 if comps[i] == '.':
134 del comps[i]
135 elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
136 del comps[i-1:i+1]
137 i = i - 1
138 elif comps[i] == '' and i > 0 and comps[i-1] != '':
139 del comps[i]
140 else:
141 i = i + 1
142 # If the path is now empty, substitute '.'
143 if not prefix and not comps:
144 comps.append('.')
145 return prefix + '/'.join(comps)
146
147
148# Return an absolute path.
149def abspath(path):
150 """Return the absolute version of a path"""
151 if not isabs(path):
152 if isinstance(path, unicode):
153 cwd = os.getcwdu()
154 else:
155 cwd = os.getcwd()
156 path = join(cwd, path)
157 return normpath(path)
158
159# realpath is a no-op on systems without islink support
160realpath = abspath
161
162supports_unicode_filenames = False
163
164
165def relpath(path, start=curdir):
166 """Return a relative version of a path"""
167
168 if not path:
169 raise ValueError("no path specified")
170 start_list = abspath(start).split(sep)
171 path_list = abspath(path).split(sep)
172 # Remove empty components after trailing slashes
173 if (start_list[-1] == ''):
174 start_list.pop()
175 if (path_list[-1] == ''):
176 path_list.pop()
177 if start_list[0].lower() != path_list[0].lower():
178 unc_path, rest = splitunc(path)
179 unc_start, rest = splitunc(start)
180 if bool(unc_path) ^ bool(unc_start):
181 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
182 % (path, start))
183 else:
184 raise ValueError("path is on drive %s, start on drive %s"
185 % (path_list[0], start_list[0]))
186 # Work out how much of the filepath is shared by start and path.
187 for i in range(min(len(start_list), len(path_list))):
188 if start_list[i].lower() != path_list[i].lower():
189 break
190 else:
191 i += 1
192
193 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
194 if not rel_list:
195 return curdir
196 return join(*rel_list)
Note: See TracBrowser for help on using the repository browser.