def relpath_win(self, path, start="."): """Return a relative version of a path""" sep="\\" if not path: raise ValueError("no path specified") start_list = ntpath.abspath(start).split(sep) path_list = ntpath.abspath(path).split(sep) if start_list[0].lower() != path_list[0].lower(): unc_path, rest = ntpath.splitunc(path) unc_start, rest = ntpath.splitunc(start) if bool(unc_path) ^ bool(unc_start): raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" % (path, start)) else: raise ValueError("path is on drive %s, start on drive %s" % (path_list[0], start_list[0])) # Work out how much of the filepath is shared by start and path. for i in range(min(len(start_list), len(path_list))): if start_list[i].lower() != path_list[i].lower(): break else: i += 1 rel_list = ['..'] * (len(start_list)-i) + path_list[i:] if not rel_list: return "." return ntpath.join(*rel_list)
def nt_relpath(path, start=curdir): """Implementa os.path.relpath para Windows ya que en python 2.5 no esta implementada""" from ntpath import abspath, splitunc, sep, pardir, join if not path: raise ValueError("no path specified") start_list = abspath(start).split(sep) path_list = abspath(path).split(sep) if start_list[0].lower() != path_list[0].lower(): unc_path, rest = splitunc(path) unc_start, rest = splitunc(start) if bool(unc_path) ^ bool(unc_start): raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" % (path, start)) else: raise ValueError("path is on drive %s, start on drive %s" % (path_list[0], start_list[0])) # Work out how much of the filepath is shared by start and path. for i in range(min(len(start_list), len(path_list))): if start_list[i].lower() != path_list[i].lower(): break else: i += 1 rel_list = [pardir] * (len(start_list) - i) + path_list[i:] if not rel_list: return curdir return join(*rel_list)
def _compatrelpath_win(path, start=os.path.curdir): """Return a relative version of a path""" if not path: raise ValueError("no path specified") start_list = ntpath.abspath(start).split(ntpath.sep) path_list = ntpath.abspath(path).split(ntpath.sep) if start_list[0].lower() != path_list[0].lower(): unc_path, rest = ntpath.splitunc(path) unc_start, rest = ntpath.splitunc(start) if bool(unc_path) ^ bool(unc_start): raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" % (path, start)) else: raise ValueError("path is on drive %s, start on drive %s" % (path_list[0], start_list[0])) # Work out how much of the filepath is shared by start and path. for i in range(min(len(start_list), len(path_list))): if start_list[i].lower() != path_list[i].lower(): break else: i += 1 rel_list = [ntpath.pardir] * (len(start_list) - i) + path_list[i:] if not rel_list: return ntpath.curdir return ntpath.join(*rel_list)
def nt_relpath(path, start=curdir): """Implementa os.path.relpath para Windows ya que en python 2.5 no esta implementada""" from ntpath import abspath, splitunc, sep, pardir, join if not path: raise ValueError("no path specified") start_list = abspath(start).split(sep) path_list = abspath(path).split(sep) if start_list[0].lower() != path_list[0].lower(): unc_path, rest = splitunc(path) unc_start, rest = splitunc(start) if bool(unc_path) ^ bool(unc_start): raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" % (path, start)) else: raise ValueError("path is on drive %s, start on drive %s" % (path_list[0], start_list[0])) # Work out how much of the filepath is shared by start and path. for i in range(min(len(start_list), len(path_list))): if start_list[i].lower() != path_list[i].lower(): break else: i += 1 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] if not rel_list: return curdir return join(*rel_list)
def test_splitunc(self): with self.assertWarns(DeprecationWarning): ntpath.splitunc("") with support.check_warnings(("", DeprecationWarning)): tester('ntpath.splitunc("c:\\foo\\bar")', ("", "c:\\foo\\bar")) tester('ntpath.splitunc("c:/foo/bar")', ("", "c:/foo/bar")) tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', ("\\\\conky\\mountpoint", "\\foo\\bar")) tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', ("//conky/mountpoint", "/foo/bar")) tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', ("", "\\\\\\conky\\mountpoint\\foo\\bar")) tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', ("", "///conky/mountpoint/foo/bar")) tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', ("", "\\\\conky\\\\mountpoint\\foo\\bar")) tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', ("", "//conky//mountpoint/foo/bar")) self.assertEqual(ntpath.splitunc("//conky/MOUNTPOİNT/foo/bar"), ("//conky/MOUNTPOİNT", "/foo/bar"))
def translate_path_linux(p): def linux_path_from_unc_path(unc_share, sub_path): mount = mount_drives.MountPoint(unc_share) unix_mountpoint = unicode(mount.mountpoint) if len(sub_path) == 0: ret = unix_mountpoint else: # Remove leading \ in the path part while len(sub_path) > 0 and sub_path[0] == u'\\': sub_path = sub_path[1:] ret = posixpath.join(unix_mountpoint, sub_path.replace(u'\\', u'/')) return ret # UNC path ? if p.startswith(u'\\\\'): unc, path = ntpath.splitunc(p) return linux_path_from_unc_path(unc, path) # Windows path with drive ? drive, path = ntpath.splitdrive(p) if drive != '': try: unc = unicode(settings.drives_mapping[drive.upper()]) except KeyError: pass else: return linux_path_from_unc_path(unc, path) return p
def test_splitunc(self): tester('ntpath.splitunc("c:\\foo\\bar")', ("", "c:\\foo\\bar")) tester('ntpath.splitunc("c:/foo/bar")', ("", "c:/foo/bar")) tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', ("\\\\conky\\mountpoint", "\\foo\\bar")) tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', ("//conky/mountpoint", "/foo/bar")) tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', ("", "\\\\\\conky\\mountpoint\\foo\\bar")) tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', ("", "///conky/mountpoint/foo/bar")) tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', ("", "\\\\conky\\\\mountpoint\\foo\\bar")) tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', ("", "//conky//mountpoint/foo/bar")) self.assertEqual(ntpath.splitunc(u"//conky/MOUNTPO\u0130NT/foo/bar"), (u"//conky/MOUNTPO\u0130NT", u"/foo/bar"))
def test_splitunc(self): with self.assertWarns(DeprecationWarning): ntpath.splitunc('') with support.check_warnings(('', DeprecationWarning)): tester('ntpath.splitunc("c:\\foo\\bar")', ('', 'c:\\foo\\bar')) tester('ntpath.splitunc("c:/foo/bar")', ('', 'c:/foo/bar')) tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar')) tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar')) tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', ('', '\\\\\\conky\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', ('', '///conky/mountpoint/foo/bar')) tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', ('', '\\\\conky\\\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', ('', '//conky//mountpoint/foo/bar')) self.assertEqual(ntpath.splitunc('//conky/MOUNTPOİNT/foo/bar'), ('//conky/MOUNTPOİNT', '/foo/bar'))
def normalize_unc_path(cls, unc_path): # Remove any remaining path part unc, path = ntpath.splitunc(unc_path) if unc != unc_path: logging.warning( 'Got unc_path %s which is non-canonical. Using %s instead' % (unc_path, unc)) # Lowercase the whole host/service unc = unc.lower() return unc
def _make_posix_filename(fn_or_uri): if ntpath.splitdrive(fn_or_uri)[0] or ntpath.splitunc(fn_or_uri)[0]: result = fn_or_uri else: parse_result = urlparse(fn_or_uri) if parse_result.scheme in ('', 'file'): result = parse_result.path else: raise ValueError("unsupported protocol '%s'" % fn_or_uri) if six.PY2 and isinstance(result, unicode): # SWIG needs UTF-8 result = result.encode("utf-8") return result
def normalize_unc_path(cls, unc_path): # Remove any remaining path part unc, path = ntpath.splitunc(unc_path) if unc != unc_path: logging.warning('Got unc_path %s which is non-canonical. Using %s instead' % ( unc_path, unc )) # Lowercase the whole host/service unc = unc.lower() return unc
def _translate_unc_path(self, filepath): ingestion_settings = self.bundle.ingestion_settings unc_mount_mapping = ingestion_settings.get('unc_mounts', {}) mount, rest = ntpath.splitunc(filepath) if mount not in unc_mount_mapping: logger.warning('UNC mount %s is not mapped!' % mount) self.bundle.errors['unmapped_unc_mounts'].add((mount, )) return None # posix_mountpoint is the location where the fileshare has been # mounted on the POSIX system (Linux / OS X) posix_mountpoint = unc_mount_mapping[mount] relative_path = rest.replace('\\', '/').lstrip('/') abs_filepath = os.path.join(posix_mountpoint, relative_path) return abs_filepath
def test_splitunc(self): tester('ntpath.splitunc("c:\\foo\\bar")', ('', 'c:\\foo\\bar')) tester('ntpath.splitunc("c:/foo/bar")', ('', 'c:/foo/bar')) tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar')) tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar')) tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', ('', '\\\\\\conky\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', ('', '///conky/mountpoint/foo/bar')) tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', ('', '\\\\conky\\\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', ('', '//conky//mountpoint/foo/bar')) self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO\u0130NT/foo/bar'), (u'//conky/MOUNTPO\u0130NT', u'/foo/bar'))
def test_splitunc(self): tester('ntpath.splitunc("c:\\foo\\bar")', ('', 'c:\\foo\\bar')) tester('ntpath.splitunc("c:/foo/bar")', ('', 'c:/foo/bar')) tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar')) tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar')) tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', ('', '\\\\\\conky\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', ('', '///conky/mountpoint/foo/bar')) tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', ('', '\\\\conky\\\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', ('', '//conky//mountpoint/foo/bar')) if test_support.have_unicode: self.assertEqual( ntpath.splitunc(u'//conky/MOUNTPO%cNT/foo/bar' % 0x0130), (u'//conky/MOUNTPO%cNT' % 0x0130, u'/foo/bar'))
def test_splitunc(self): tester('ntpath.splitunc("c:\\foo\\bar")', ('', 'c:\\foo\\bar')) tester('ntpath.splitunc("c:/foo/bar")', ('', 'c:/foo/bar')) tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar')) tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar')) tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")', ('', '\\\\\\conky\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("///conky/mountpoint/foo/bar")', ('', '///conky/mountpoint/foo/bar')) tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")', ('', '\\\\conky\\\\mountpoint\\foo\\bar')) tester('ntpath.splitunc("//conky//mountpoint/foo/bar")', ('', '//conky//mountpoint/foo/bar')) if test_support.have_unicode: self.assertEqual(ntpath.splitunc(u'//conky/MOUNTPO%cNT/foo/bar' % 0x0130), (u'//conky/MOUNTPO%cNT' % 0x0130, u'/foo/bar'))
def normalize_case_linux(name, listdir_cache): """Normalizes the case of a path. On a case-insensitive filesystem, this returns the case that exists on the filesystem for a given path. Note: This is linux-specific. Works only for existing files. Name should be a UNC path, in unicode. """ try: if name.startswith(u'\\\\'): unc, path = ntpath.splitunc(name) # return linux_path_from_unc_path(unc, path).encode('UTF-8') mount = mount_drives.MountPoint(unc) base_mount = unicode(mount.mountpoint) path = path.replace(u'/', u'\\') dirs = path.split(u'\\') curpath = u'' for d in dirs[1:]: curpath += u'\\' if d != u'': unix_dir = os.path.join(base_mount, curpath[1:].replace(u'\\', u'/')) potential_children = cached_listdir(unix_dir, cache=listdir_cache) curpath += potential_children[d.lower()] return curpath else: return None except Exception: logging.exception('Could not normalize case:') return None
def normalize_case_windows(name, listdir_cache): try: if name.startswith(u'\\\\'): unc, path = ntpath.splitunc(name) dirs = path.split(u'\\') curpath = u'' for d in dirs[1:]: curpath += u'\\' if d != u'': windows_dir = os.path.join(unc, curpath[1:]) potential_children = cached_listdir(windows_dir, cache=listdir_cache) curpath += potential_children[d.lower()] return curpath else: return None except Exception: logging.exception('Could not normalize case:') return None
def _ntpath_isabsulute(path): '''Checks if a path is fully absolute on windows (does not depend on the current directory or the current drive)''' return (_ntpath.splitdrive(path)[0] or _ntpath.splitunc(path)[0]) and _ntpath.isabs(path)