From: Dmitry Selyutin Date: Sun, 22 Oct 2017 17:04:06 +0000 (+0300) Subject: vfs: basic file operations X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=84f24363eab8c578f474f88a30f5a03e348028ed;p=gnulib.git vfs: basic file operations --- diff --git a/pygnulib/vfs.py b/pygnulib/vfs.py index 1809e41276..ece2e0c583 100644 --- a/pygnulib/vfs.py +++ b/pygnulib/vfs.py @@ -113,6 +113,35 @@ class Project(Base): _shutil_.copy(self[name], self[backup]) + def copy(self, src, dst): + """Copy file data.""" + srcabs = _os_.path.isabs(self, src) + dstabs = _os_.path.isabs(self, dst) + if srcabs and dstabs: + raise ValueError("absolute src and dst") + limit = (16 * 1024) + src = src if srcabs else _os_.path.join(self.full_prefix, src) + dst = dst if dstabs else _os_.path.join(self.full_prefix, dst) + with _codecs_.open(src, "rb") as istream: + with _codecs_.open(dst, "wb") as ostream: + while 1: + data = istream.read(limit) + if not data: + break + ostream.write(data) + + + def hardlink(self, src, dst): + """Create a hard link to the file.""" + srcabs = _os_.path.isabs(self, src) + dstabs = _os_.path.isabs(self, dst) + if srcabs and dstabs: + raise ValueError("absolute src and dst") + src = src if srcabs else _os_.path.join(self.full_prefix, src) + dst = dst if dstabs else _os_.path.join(self.full_prefix, dst) + _os_.link(src, dst) + + def lookup(self, name, primary, secondary): """ Try to look up a regular file inside virtual file systems or combine it via patch utility. @@ -156,6 +185,28 @@ class Project(Base): _os_.makedirs(self[name], exist_ok=True) + def move(self, src, dst): + """Move file data.""" + srcabs = _os_.path.isabs(self, src) + dstabs = _os_.path.isabs(self, dst) + if srcabs and dstabs: + raise ValueError("absolute src and dst") + src = src if srcabs else _os_.path.join(self.full_prefix, src) + dst = dst if dstabs else _os_.path.join(self.full_prefix, dst) + _os_.rename(src, dst) + + + def symlink(self, src, dst): + """Create a symbolic link to the file.""" + srcabs = _os_.path.isabs(self, src) + dstabs = _os_.path.isabs(self, dst) + if srcabs and dstabs: + raise ValueError("absolute src and dst") + src = src if srcabs else _os_.path.join(self.full_prefix, src) + dst = dst if dstabs else _os_.path.join(self.full_prefix, dst) + _os_.symlink(src, dst) + + def unlink(self, name, backup=True): """Unlink a file, backing it up if necessary.""" if backup: