1 | # Module 'os2emxpath' -- common operations on OS/2 pathnames |
---|
2 | """Common pathname manipulations, OS/2 EMX version. |
---|
3 | |
---|
4 | Instead of importing this module directly, import os and refer to this |
---|
5 | module as os.path. |
---|
6 | """ |
---|
7 | |
---|
8 | import os |
---|
9 | import stat |
---|
10 | from genericpath import * |
---|
11 | from genericpath import _unicode |
---|
12 | from ntpath import (expanduser, expandvars, isabs, islink, splitdrive, |
---|
13 | splitext, split, walk) |
---|
14 | |
---|
15 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
---|
16 | "basename","dirname","commonprefix","getsize","getmtime", |
---|
17 | "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"] |
---|
21 | |
---|
22 | # strings representing various path-related bits and pieces |
---|
23 | curdir = '.' |
---|
24 | pardir = '..' |
---|
25 | extsep = '.' |
---|
26 | sep = '/' |
---|
27 | altsep = '\\' |
---|
28 | pathsep = ';' |
---|
29 | defpath = '.;C:\\bin' |
---|
30 | devnull = 'nul' |
---|
31 | |
---|
32 | # 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 | |
---|
36 | def 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 | |
---|
43 | # Join two (or more) paths. |
---|
44 | |
---|
45 | def 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 | |
---|
58 | # Parse UNC paths |
---|
59 | def 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 | |
---|
87 | # Return the tail (basename) part of a path. |
---|
88 | |
---|
89 | def 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 | |
---|
96 | def dirname(p): |
---|
97 | """Returns the directory component of a pathname""" |
---|
98 | return split(p)[0] |
---|
99 | |
---|
100 | |
---|
101 | # alias exists to lexists |
---|
102 | lexists = exists |
---|
103 | |
---|
104 | |
---|
105 | # Is a path a directory? |
---|
106 | |
---|
107 | # 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 | |
---|
110 | def 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 | |
---|
119 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
---|
120 | |
---|
121 | def 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. |
---|
147 | def abspath(path): |
---|
148 | """Return the absolute version of a path""" |
---|
149 | if not isabs(path): |
---|
150 | if isinstance(path, _unicode): |
---|
151 | cwd = os.getcwdu() |
---|
152 | else: |
---|
153 | cwd = os.getcwd() |
---|
154 | path = join(cwd, path) |
---|
155 | return normpath(path) |
---|
156 | |
---|
157 | # realpath is a no-op on systems without islink support |
---|
158 | realpath = abspath |
---|
159 | |
---|
160 | supports_unicode_filenames = False |
---|