Skip to content

Commit 0c02f53

Browse files
committed
add python2.7 commonpath backport
1 parent e490452 commit 0c02f53

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

fs/_pathcompat.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
try:
2+
from os.path import commonpath
3+
except ImportError:
4+
# Return the longest common sub-path of the sequence of paths given as input.
5+
# The paths are not normalized before comparing them (this is the
6+
# responsibility of the caller). Any trailing separator is stripped from the
7+
# returned path.
8+
9+
def commonpath(paths):
10+
"""Given a sequence of path names, returns the longest common sub-path."""
11+
12+
if not paths:
13+
raise ValueError("commonpath() arg is an empty sequence")
14+
15+
paths = tuple(paths)
16+
if isinstance(paths[0], bytes):
17+
sep = b"/"
18+
curdir = b"."
19+
else:
20+
sep = "/"
21+
curdir = "."
22+
23+
split_paths = [path.split(sep) for path in paths]
24+
25+
try:
26+
(isabs,) = set(p[:1] == sep for p in paths)
27+
except ValueError:
28+
raise ValueError("Can't mix absolute and relative paths")
29+
30+
split_paths = [[c for c in s if c and c != curdir] for s in split_paths]
31+
s1 = min(split_paths)
32+
s2 = max(split_paths)
33+
common = s1
34+
for i, c in enumerate(s1):
35+
if c != s2[i]:
36+
common = s1[:i]
37+
break
38+
39+
prefix = sep if isabs else sep[:0]
40+
return prefix + sep.join(common)

fs/move.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
"""Functions for moving files between filesystems.
22
"""
33

4-
from __future__ import print_function
5-
from __future__ import unicode_literals
4+
from __future__ import print_function, unicode_literals
65

7-
from os.path import commonpath
86
import typing
97

108
from . import open_fs
11-
from .copy import copy_dir
12-
from .copy import copy_file
9+
from ._pathcompat import commonpath
10+
from .copy import copy_dir, copy_file
1311
from .errors import FSError
1412
from .opener import manage_fs
1513
from .path import frombase
1614

1715
if typing.TYPE_CHECKING:
18-
from .base import FS
1916
from typing import Text, Union
2017

18+
from .base import FS
19+
2120

2221
def move_fs(
2322
src_fs, # type: Union[Text, FS]
@@ -80,7 +79,7 @@ def move_file(
8079
with _src_fs.lock(), _dst_fs.lock():
8180
with open_fs(common, writeable=True) as base:
8281
base.move(rel_src, rel_dst, preserve_time=preserve_time)
83-
return # optimization worked, exit early
82+
return # optimization worked, exit early
8483
except ValueError:
8584
# This is raised if we cannot find a common base folder.
8685
# In this case just fall through to the standard method.

0 commit comments

Comments
 (0)