sb: Back port the path module from master.

Update #3274
This commit is contained in:
Chris Johns
2018-01-18 14:35:31 +11:00
parent f7c729eeda
commit e1346e2647

View File

@@ -53,6 +53,11 @@ def host(path):
path = u'\\'.join([u'\\\\?', path]) path = u'\\'.join([u'\\\\?', path])
return path return path
def is_abspath(path):
if path is not None:
return '/' == path[0]
return False
def shell(path): def shell(path):
if path is not None: if path is not None:
if windows: if windows:
@@ -67,9 +72,11 @@ def shell(path):
return path return path
def basename(path): def basename(path):
return shell(os.path.basename(path)) path = shell(path)
return shell(os.path.basename(host(path)))
def dirname(path): def dirname(path):
path = shell(path)
return shell(os.path.dirname(path)) return shell(os.path.dirname(path))
def join(path, *args): def join(path, *args):
@@ -82,13 +89,24 @@ def join(path, *args):
return shell(path) return shell(path)
def abspath(path): def abspath(path):
path = shell(path)
return shell(os.path.abspath(host(path))) return shell(os.path.abspath(host(path)))
def relpath(path, start = None):
path = shell(path)
if start is None:
path = os.path.relpath(host(path))
else:
path = os.path.relpath(host(path), start)
return shell(path)
def splitext(path): def splitext(path):
path = shell(path)
root, ext = os.path.splitext(host(path)) root, ext = os.path.splitext(host(path))
return shell(root), ext return shell(root), ext
def listdir(path): def listdir(path):
path = shell(path)
hp = host(path) hp = host(path)
if not os.path.exists(hp): if not os.path.exists(hp):
return [] return []
@@ -96,37 +114,43 @@ def listdir(path):
def exists(paths): def exists(paths):
def _exists(p): def _exists(p):
return os.path.basename(p) in listdir(dirname(p)) if not is_abspath(p):
p = shell(join(os.getcwd(), host(p)))
return basename(p) in ['.'] + listdir(dirname(p))
if type(paths) == list: if type(paths) == list:
results = [] results = []
for p in paths: for p in paths:
results += [_exists(p)] results += [_exists(shell(p))]
return results return results
return _exists(paths) return _exists(shell(paths))
def isdir(path): def isdir(path):
path = shell(path)
return os.path.isdir(host(path)) return os.path.isdir(host(path))
def isfile(path): def isfile(path):
path = shell(path)
return os.path.isfile(host(path)) return os.path.isfile(host(path))
def isabspath(path): def isabspath(path):
path = shell(path)
return path[0] == '/' return path[0] == '/'
def iswritable(path): def iswritable(path):
path = shell(path)
return os.access(host(path), os.W_OK) return os.access(host(path), os.W_OK)
def ispathwritable(path): def ispathwritable(path):
path = host(path) path = shell(path)
while len(path) != 0: while len(path) != 0:
if exists(path): if exists(path):
return iswritable(path) return iswritable(path)
path = os.path.dirname(path) path = dirname(path)
return False return False
def mkdir(path): def mkdir(path):
path = host(path) path = shell(path)
if exists(path): if exists(path):
if not isdir(path): if not isdir(path):
raise error.general('path exists and is not a directory: %s' % (path)) raise error.general('path exists and is not a directory: %s' % (path))
@@ -149,6 +173,7 @@ def mkdir(path):
raise error.general('cannot make directory: %s' % (path)) raise error.general('cannot make directory: %s' % (path))
def chdir(path): def chdir(path):
path = shell(path)
os.chdir(host(path)) os.chdir(host(path))
def removeall(path): def removeall(path):
@@ -185,6 +210,7 @@ def removeall(path):
_remove(dir) _remove(dir)
_remove_node(dir) _remove_node(dir)
path = shell(path)
hpath = host(path) hpath = host(path)
if os.path.exists(hpath): if os.path.exists(hpath):
@@ -192,12 +218,15 @@ def removeall(path):
_remove_node(path) _remove_node(path)
def expand(name, paths): def expand(name, paths):
path = shell(path)
l = [] l = []
for p in paths: for p in paths:
l += [join(p, name)] l += [join(shell(p), name)]
return l return l
def copy(src, dst): def copy(src, dst):
src = shell(src)
dst = shell(dst)
hsrc = host(src) hsrc = host(src)
hdst = host(dst) hdst = host(dst)
try: try:
@@ -207,7 +236,7 @@ def copy(src, dst):
if WindowsError is not None and isinstance(why, WindowsError): if WindowsError is not None and isinstance(why, WindowsError):
pass pass
else: else:
raise error.general('copying tree: %s -> %s: %s' % (hsrc, hdst, str(why))) raise error.general('copying tree (1): %s -> %s: %s' % (hsrc, hdst, str(why)))
def copy_tree(src, dst): def copy_tree(src, dst):
trace = False trace = False
@@ -215,8 +244,8 @@ def copy_tree(src, dst):
hsrc = host(src) hsrc = host(src)
hdst = host(dst) hdst = host(dst)
if exists(hsrc): if exists(src):
names = listdir(hsrc) names = listdir(src)
else: else:
names = [] names = []
@@ -243,7 +272,7 @@ def copy_tree(src, dst):
try: try:
if os.path.islink(srcname): if os.path.islink(srcname):
linkto = os.readlink(srcname) linkto = os.readlink(srcname)
if exists(dstname): if exists(shell(dstname)):
if os.path.islink(dstname): if os.path.islink(dstname):
dstlinkto = os.readlink(dstname) dstlinkto = os.readlink(dstname)
if linkto != dstlinkto: if linkto != dstlinkto:
@@ -262,10 +291,10 @@ def copy_tree(src, dst):
shutil.copyfile(host(srcname), host(dstname)) shutil.copyfile(host(srcname), host(dstname))
shutil.copystat(host(srcname), host(dstname)) shutil.copystat(host(srcname), host(dstname))
except shutil.Error as err: except shutil.Error as err:
raise error.general('copying tree: %s -> %s: %s' % \ raise error.general('copying tree (2): %s -> %s: %s' % \
(hsrc, hdst, str(err))) (hsrc, hdst, str(err)))
except EnvironmentError as why: except EnvironmentError as why:
raise error.general('copying tree: %s -> %s: %s' % \ raise error.general('copying tree (3): %s -> %s: %s' % \
(srcname, dstname, str(why))) (srcname, dstname, str(why)))
try: try:
shutil.copystat(hsrc, hdst) shutil.copystat(hsrc, hdst)
@@ -274,7 +303,7 @@ def copy_tree(src, dst):
if WindowsError is not None and isinstance(why, WindowsError): if WindowsError is not None and isinstance(why, WindowsError):
pass pass
else: else:
raise error.general('copying tree: %s -> %s: %s' % (hsrc, hdst, str(why))) raise error.general('copying tree (4): %s -> %s: %s' % (hsrc, hdst, str(why)))
if __name__ == '__main__': if __name__ == '__main__':
print(host('/a/b/c/d-e-f')) print(host('/a/b/c/d-e-f'))