]> Savannah Git Hosting - gnulib.git/commitdiff
vfs: basic file operations
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 22 Oct 2017 17:04:06 +0000 (20:04 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sun, 22 Oct 2017 19:30:14 +0000 (22:30 +0300)
pygnulib/vfs.py

index 1809e412763249e5c9bb92e84f62e38002975124..ece2e0c5831aed2dda2e8e46e63512e564b16b17 100644 (file)
@@ -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: