1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Module 'os2emxpath' -- common operations on OS/2 pathnames 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""Common pathname manipulations, OS/2 EMX version. 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepInstead of importing this module directly, import os and refer to this 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmodule as os.path. 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport os 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport stat 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom genericpath import * 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom ntpath import (expanduser, expandvars, isabs, islink, splitdrive, 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep splitext, split, walk) 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__ = ["normcase","isabs","join","splitdrive","split","splitext", 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "basename","dirname","commonprefix","getsize","getmtime", 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "getatime","getctime", "islink","exists","lexists","isdir","isfile", 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "ismount","walk","expanduser","expandvars","normpath","abspath", 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "splitunc","curdir","pardir","sep","pathsep","defpath","altsep", 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "extsep","devnull","realpath","supports_unicode_filenames"] 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# strings representing various path-related bits and pieces 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcurdir = '.' 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppardir = '..' 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepextsep = '.' 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsep = '/' 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepaltsep = '\\' 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppathsep = ';' 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdefpath = '.;C:\\bin' 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdevnull = 'nul' 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Normalize the case of a pathname and map slashes to backslashes. 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Other normalizations (such as optimizing '../' away) are not done 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# (this is done by normpath). 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef normcase(s): 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Normalize case of pathname. 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Makes all characters lowercase and all altseps into seps.""" 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return s.replace('\\', '/').lower() 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Join two (or more) paths. 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef join(a, *p): 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Join two or more pathname components, inserting sep as needed""" 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = a 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for b in p: 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if isabs(b): 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = b 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif path == '' or path[-1:] in '/\\:': 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = path + b 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = path + '/' + b 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return path 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Parse UNC paths 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef splitunc(p): 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Split a pathname into UNC mount point and relative path specifiers. 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Return a 2-tuple (unc, rest); either part may be empty. 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep If unc is not empty, it has the form '//host/mount' (or similar 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep using backslashes). unc+rest is always the input path. 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Paths containing drive letters never have an UNC part. 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p[1:2] == ':': 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return '', p # Drive letter present 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep firstTwo = p[0:2] 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if firstTwo == '/' * 2 or firstTwo == '\\' * 2: 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # is a UNC path: 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # \\machine\mountpoint\directories... 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # directory ^^^^^^^^^^^^^^^ 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep normp = normcase(p) 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep index = normp.find('/', 2) 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if index == -1: 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ##raise RuntimeError, 'illegal UNC path: "' + p + '"' 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return ("", p) 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep index = normp.find('/', index + 1) 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if index == -1: 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep index = len(p) 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return p[:index], p[index:] 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return '', p 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Return the tail (basename) part of a path. 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef basename(p): 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Returns the final component of a pathname""" 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return split(p)[1] 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Return the head (dirname) part of a path. 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef dirname(p): 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Returns the directory component of a pathname""" 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return split(p)[0] 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# alias exists to lexists 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeplexists = exists 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Is a path a directory? 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Is a path a mount point? Either a root (with or without drive letter) 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# or an UNC path with at most a / or \ after the mount point. 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef ismount(path): 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Test whether a path is a mount point (defined as root of drive)""" 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep unc, rest = splitunc(path) 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if unc: 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return rest in ("", "/", "\\") 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p = splitdrive(path)[1] 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return len(p) == 1 and p[0] in '/\\' 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef normpath(path): 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Normalize path, eliminating double slashes, etc.""" 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = path.replace('\\', '/') 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep prefix, path = splitdrive(path) 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep while path[:1] == '/': 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep prefix = prefix + '/' 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = path[1:] 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep comps = path.split('/') 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = 0 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep while i < len(comps): 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if comps[i] == '.': 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del comps[i] 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'): 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del comps[i-1:i+1] 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = i - 1 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif comps[i] == '' and i > 0 and comps[i-1] != '': 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del comps[i] 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = i + 1 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # If the path is now empty, substitute '.' 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not prefix and not comps: 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep comps.append('.') 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return prefix + '/'.join(comps) 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Return an absolute path. 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef abspath(path): 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Return the absolute version of a path""" 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not isabs(path): 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if isinstance(path, unicode): 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd = os.getcwdu() 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd = os.getcwd() 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep path = join(cwd, path) 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return normpath(path) 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# realpath is a no-op on systems without islink support 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeprealpath = abspath 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsupports_unicode_filenames = False 160