blob: 0ccbf8ae7b886a0462b1cfdc6891f7867216030a [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,
Benjamin Petersond71ca412008-05-08 23:44:58 +000012 splitext, split)
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",
Benjamin Petersond71ca412008-05-08 23:44:58 +000017 "ismount","expanduser","expandvars","normpath","abspath",
Georg Brandlf0de6a12005-08-22 18:02:59 +000018 "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."""
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +000039 if not isinstance(s, (bytes, str)):
40 raise TypeError("normcase() argument must be str or bytes, "
41 "not '{}'".format(s.__class__.__name__))
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000042 return s.replace('\\', '/').lower()
43
44
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000045# Join two (or more) paths.
46
47def join(a, *p):
48 """Join two or more pathname components, inserting sep as needed"""
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
58
59
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000060# 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
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000089# 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
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103# alias exists to lexists
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000104lexists = exists
105
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000106
107# Is a path a directory?
108
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000109# 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
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000121# 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 path = join(os.getcwd(), path)
153 return normpath(path)
Mark Hammond8696ebc2002-10-08 02:44:31 +0000154
Neal Norwitz61cdac62003-01-03 18:01:57 +0000155# realpath is a no-op on systems without islink support
156realpath = abspath
157
Mark Hammond8696ebc2002-10-08 02:44:31 +0000158supports_unicode_filenames = False