2002-02-24 01:32:32 -04:00
|
|
|
# Module 'os2emxpath' -- common operations on OS/2 pathnames
|
2002-04-15 22:38:40 -03:00
|
|
|
"""Common pathname manipulations, OS/2 EMX version.
|
2002-02-24 01:32:32 -04:00
|
|
|
|
|
|
|
Instead of importing this module directly, import os and refer to this
|
|
|
|
module as os.path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import stat
|
2006-08-26 15:42:06 -03:00
|
|
|
from genericpath import *
|
|
|
|
from ntpath import (expanduser, expandvars, isabs, islink, splitdrive,
|
|
|
|
splitext, split, walk)
|
2002-02-24 01:32:32 -04:00
|
|
|
|
|
|
|
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
|
|
|
|
"basename","dirname","commonprefix","getsize","getmtime",
|
2005-08-22 15:02:59 -03:00
|
|
|
"getatime","getctime", "islink","exists","lexists","isdir","isfile",
|
|
|
|
"ismount","walk","expanduser","expandvars","normpath","abspath",
|
|
|
|
"splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
|
|
|
|
"extsep","devnull","realpath","supports_unicode_filenames"]
|
2002-02-24 01:32:32 -04:00
|
|
|
|
2003-02-14 15:35:31 -04:00
|
|
|
# strings representing various path-related bits and pieces
|
|
|
|
curdir = '.'
|
|
|
|
pardir = '..'
|
|
|
|
extsep = '.'
|
|
|
|
sep = '/'
|
|
|
|
altsep = '\\'
|
|
|
|
pathsep = ';'
|
|
|
|
defpath = '.;C:\\bin'
|
2004-06-08 05:29:33 -03:00
|
|
|
devnull = 'nul'
|
2003-02-14 15:35:31 -04:00
|
|
|
|
2002-02-24 01:32:32 -04:00
|
|
|
# Normalize the case of a pathname and map slashes to backslashes.
|
|
|
|
# Other normalizations (such as optimizing '../' away) are not done
|
|
|
|
# (this is done by normpath).
|
|
|
|
|
|
|
|
def normcase(s):
|
|
|
|
"""Normalize case of pathname.
|
|
|
|
|
|
|
|
Makes all characters lowercase and all altseps into seps."""
|
|
|
|
return s.replace('\\', '/').lower()
|
|
|
|
|
|
|
|
|
|
|
|
# Join two (or more) paths.
|
|
|
|
|
|
|
|
def join(a, *p):
|
|
|
|
"""Join two or more pathname components, inserting sep as needed"""
|
|
|
|
path = a
|
|
|
|
for b in p:
|
|
|
|
if isabs(b):
|
|
|
|
path = b
|
|
|
|
elif path == '' or path[-1:] in '/\\:':
|
|
|
|
path = path + b
|
|
|
|
else:
|
|
|
|
path = path + '/' + b
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
# Parse UNC paths
|
|
|
|
def splitunc(p):
|
|
|
|
"""Split a pathname into UNC mount point and relative path specifiers.
|
|
|
|
|
|
|
|
Return a 2-tuple (unc, rest); either part may be empty.
|
|
|
|
If unc is not empty, it has the form '//host/mount' (or similar
|
|
|
|
using backslashes). unc+rest is always the input path.
|
|
|
|
Paths containing drive letters never have an UNC part.
|
|
|
|
"""
|
|
|
|
if p[1:2] == ':':
|
|
|
|
return '', p # Drive letter present
|
|
|
|
firstTwo = p[0:2]
|
|
|
|
if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
|
|
|
|
# is a UNC path:
|
|
|
|
# vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
|
|
|
|
# \\machine\mountpoint\directories...
|
|
|
|
# directory ^^^^^^^^^^^^^^^
|
|
|
|
normp = normcase(p)
|
|
|
|
index = normp.find('/', 2)
|
|
|
|
if index == -1:
|
|
|
|
##raise RuntimeError, 'illegal UNC path: "' + p + '"'
|
|
|
|
return ("", p)
|
|
|
|
index = normp.find('/', index + 1)
|
|
|
|
if index == -1:
|
|
|
|
index = len(p)
|
|
|
|
return p[:index], p[index:]
|
|
|
|
return '', p
|
|
|
|
|
|
|
|
|
|
|
|
# Return the tail (basename) part of a path.
|
|
|
|
|
|
|
|
def basename(p):
|
|
|
|
"""Returns the final component of a pathname"""
|
|
|
|
return split(p)[1]
|
|
|
|
|
|
|
|
|
|
|
|
# Return the head (dirname) part of a path.
|
|
|
|
|
|
|
|
def dirname(p):
|
|
|
|
"""Returns the directory component of a pathname"""
|
|
|
|
return split(p)[0]
|
|
|
|
|
|
|
|
|
2006-08-26 15:42:06 -03:00
|
|
|
# alias exists to lexists
|
2004-08-30 07:19:56 -03:00
|
|
|
lexists = exists
|
|
|
|
|
2002-02-24 01:32:32 -04:00
|
|
|
|
|
|
|
# Is a path a directory?
|
|
|
|
|
|
|
|
# Is a path a mount point? Either a root (with or without drive letter)
|
|
|
|
# or an UNC path with at most a / or \ after the mount point.
|
|
|
|
|
|
|
|
def ismount(path):
|
|
|
|
"""Test whether a path is a mount point (defined as root of drive)"""
|
|
|
|
unc, rest = splitunc(path)
|
|
|
|
if unc:
|
|
|
|
return rest in ("", "/", "\\")
|
|
|
|
p = splitdrive(path)[1]
|
|
|
|
return len(p) == 1 and p[0] in '/\\'
|
|
|
|
|
|
|
|
|
|
|
|
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
|
|
|
|
|
|
|
|
def normpath(path):
|
|
|
|
"""Normalize path, eliminating double slashes, etc."""
|
|
|
|
path = path.replace('\\', '/')
|
|
|
|
prefix, path = splitdrive(path)
|
|
|
|
while path[:1] == '/':
|
|
|
|
prefix = prefix + '/'
|
|
|
|
path = path[1:]
|
|
|
|
comps = path.split('/')
|
|
|
|
i = 0
|
|
|
|
while i < len(comps):
|
|
|
|
if comps[i] == '.':
|
|
|
|
del comps[i]
|
|
|
|
elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
|
|
|
|
del comps[i-1:i+1]
|
|
|
|
i = i - 1
|
|
|
|
elif comps[i] == '' and i > 0 and comps[i-1] != '':
|
|
|
|
del comps[i]
|
|
|
|
else:
|
|
|
|
i = i + 1
|
|
|
|
# If the path is now empty, substitute '.'
|
|
|
|
if not prefix and not comps:
|
|
|
|
comps.append('.')
|
|
|
|
return prefix + '/'.join(comps)
|
|
|
|
|
|
|
|
|
|
|
|
# Return an absolute path.
|
|
|
|
def abspath(path):
|
|
|
|
"""Return the absolute version of a path"""
|
|
|
|
if not isabs(path):
|
2010-02-20 05:16:04 -04:00
|
|
|
if isinstance(path, unicode):
|
|
|
|
cwd = os.getcwdu()
|
|
|
|
else:
|
|
|
|
cwd = os.getcwd()
|
|
|
|
path = join(cwd, path)
|
2002-02-24 01:32:32 -04:00
|
|
|
return normpath(path)
|
2002-10-07 23:44:31 -03:00
|
|
|
|
2003-01-03 14:01:57 -04:00
|
|
|
# realpath is a no-op on systems without islink support
|
|
|
|
realpath = abspath
|
|
|
|
|
2002-10-07 23:44:31 -03:00
|
|
|
supports_unicode_filenames = False
|