blob: 4e85c4d48dfc6b79e87dcddae08f61f2f9d0a075 [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
Thomas Wouters89f507f2006-12-13 04:49:30 +000010from genericpath import *
11from ntpath import (expanduser, expandvars, isabs, islink, splitdrive,
12 splitext, split, walk)
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000013
14__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
15 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000016 "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"]
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000020
Skip Montanaro117910d2003-02-14 19:35:31 +000021# strings representing various path-related bits and pieces
22curdir = '.'
23pardir = '..'
24extsep = '.'
25sep = '/'
26altsep = '\\'
27pathsep = ';'
28defpath = '.;C:\\bin'
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000029devnull = 'nul'
Skip Montanaro117910d2003-02-14 19:35:31 +000030
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000031# 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('\\', '/').lower()
40
41
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000042# Join two (or more) paths.
43
44def join(a, *p):
45 """Join two or more pathname components, inserting sep as needed"""
46 path = a
47 for b in p:
48 if isabs(b):
49 path = b
50 elif path == '' or path[-1:] in '/\\:':
51 path = path + b
52 else:
53 path = path + '/' + b
54 return path
55
56
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000057# Parse UNC paths
58def splitunc(p):
59 """Split a pathname into UNC mount point and relative path specifiers.
60
61 Return a 2-tuple (unc, rest); either part may be empty.
62 If unc is not empty, it has the form '//host/mount' (or similar
63 using backslashes). unc+rest is always the input path.
64 Paths containing drive letters never have an UNC part.
65 """
66 if p[1:2] == ':':
67 return '', p # Drive letter present
68 firstTwo = p[0:2]
69 if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
70 # is a UNC path:
71 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
72 # \\machine\mountpoint\directories...
73 # directory ^^^^^^^^^^^^^^^
74 normp = normcase(p)
75 index = normp.find('/', 2)
76 if index == -1:
77 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
78 return ("", p)
79 index = normp.find('/', index + 1)
80 if index == -1:
81 index = len(p)
82 return p[:index], p[index:]
83 return '', p
84
85
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000086# Return the tail (basename) part of a path.
87
88def basename(p):
89 """Returns the final component of a pathname"""
90 return split(p)[1]
91
92
93# Return the head (dirname) part of a path.
94
95def dirname(p):
96 """Returns the directory component of a pathname"""
97 return split(p)[0]
98
99
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100# alias exists to lexists
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000101lexists = exists
102
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000103
104# Is a path a directory?
105
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000106# Is a path a mount point? Either a root (with or without drive letter)
107# or an UNC path with at most a / or \ after the mount point.
108
109def ismount(path):
110 """Test whether a path is a mount point (defined as root of drive)"""
111 unc, rest = splitunc(path)
112 if unc:
113 return rest in ("", "/", "\\")
114 p = splitdrive(path)[1]
115 return len(p) == 1 and p[0] in '/\\'
116
117
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000118# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
119
120def normpath(path):
121 """Normalize path, eliminating double slashes, etc."""
122 path = path.replace('\\', '/')
123 prefix, path = splitdrive(path)
124 while path[:1] == '/':
125 prefix = prefix + '/'
126 path = path[1:]
127 comps = path.split('/')
128 i = 0
129 while i < len(comps):
130 if comps[i] == '.':
131 del comps[i]
132 elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
133 del comps[i-1:i+1]
134 i = i - 1
135 elif comps[i] == '' and i > 0 and comps[i-1] != '':
136 del comps[i]
137 else:
138 i = i + 1
139 # If the path is now empty, substitute '.'
140 if not prefix and not comps:
141 comps.append('.')
142 return prefix + '/'.join(comps)
143
144
145# Return an absolute path.
146def abspath(path):
147 """Return the absolute version of a path"""
148 if not isabs(path):
149 path = join(os.getcwd(), path)
150 return normpath(path)
Mark Hammond8696ebc2002-10-08 02:44:31 +0000151
Neal Norwitz61cdac62003-01-03 18:01:57 +0000152# realpath is a no-op on systems without islink support
153realpath = abspath
154
Mark Hammond8696ebc2002-10-08 02:44:31 +0000155supports_unicode_filenames = False