Skip to content

Commit

Permalink
fs: use Path
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed May 2, 2022
1 parent d17385d commit 89c54c1
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions scmrepo/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,124 @@
from scmrepo.git.objects import GitTrie


class Path:
def __init__(self, sep, getcwd=None, realpath=None):
def _getcwd():
return ""

self.getcwd = getcwd or _getcwd
self.realpath = realpath or self.abspath

assert sep == posixpath.sep
self.flavour = posixpath

def chdir(self, path):
def _getcwd():
return path

self.getcwd = _getcwd

def join(self, *parts):
return self.flavour.join(*parts)

def split(self, path):
return self.flavour.split(path)

def normpath(self, path):
return self.flavour.normpath(path)

def isabs(self, path):
return self.flavour.isabs(path)

def abspath(self, path):
if not self.isabs(path):
path = self.join(self.getcwd(), path)
return self.normpath(path)

def commonprefix(self, path):
return self.flavour.commonprefix(path)

def parts(self, path):
drive, path = self.flavour.splitdrive(path.rstrip(self.flavour.sep))

ret = []
while True:
path, part = self.flavour.split(path)

if part:
ret.append(part)
continue

if path:
ret.append(path)

break

ret.reverse()

if drive:
ret = [drive] + ret

return tuple(ret)

def parent(self, path):
return self.flavour.dirname(path)

def dirname(self, path):
return self.parent(path)

def parents(self, path):
parts = self.parts(path)
return tuple(
self.join(*parts[:length])
for length in range(len(parts) - 1, 0, -1)
)

def name(self, path):
return self.parts(path)[-1]

def suffix(self, path):
name = self.name(path)
_, dot, suffix = name.partition(".")
return dot + suffix

def with_name(self, path, name):
parts = list(self.parts(path))
parts[-1] = name
return self.join(*parts)

def with_suffix(self, path, suffix):
parts = list(self.parts(path))
real_path, _, _ = parts[-1].partition(".")
parts[-1] = real_path + suffix
return self.join(*parts)

def isin(self, left, right):
left_parts = self.parts(left)
right_parts = self.parts(right)
left_len = len(left_parts)
right_len = len(right_parts)
return left_len > right_len and left_parts[:right_len] == right_parts

def isin_or_eq(self, left, right):
return left == right or self.isin(left, right)

def overlaps(self, left, right):
# pylint: disable=arguments-out-of-order
return self.isin_or_eq(left, right) or self.isin(right, left)

def relpath(self, path, start=None):
if start is None:
start = self.getcwd()
return self.flavour.relpath(path, start=start)

def relparts(self, path, base):
return self.parts(self.relpath(path, base))

def as_posix(self, path):
return path.replace(self.flavour.sep, posixpath.sep)


def bytesio_len(obj: "BytesIO") -> Optional[int]:
try:
offset = obj.tell()
Expand Down Expand Up @@ -58,7 +176,13 @@ def __init__(
self.trie = trie
self.rev = self.trie.rev

def _getcwd():
return self.root_marker

self.path = Path(self.sep, getcwd=_getcwd)

def _get_key(self, path: str) -> Tuple[str, ...]:
path = self.path.abspath(path)
if path == self.root_marker:
return ()
relparts = path.split(self.sep)
Expand Down

0 comments on commit 89c54c1

Please sign in to comment.