From d60d7b4d3d73e11b4d6281a08f12e12d40527e0a Mon Sep 17 00:00:00 2001
From: Sebastien Luttringer <sebastien.luttringer@smartjog.com>
Date: Thu, 13 Oct 2011 13:54:16 +0200
Subject: [PATCH] Repository add now user PipeFile

Also fix some bug in PipeFile
---
 installsystems/repository.py | 11 ++++++++---
 installsystems/tools.py      | 36 +++++++-----------------------------
 2 files changed, 15 insertions(+), 32 deletions(-)

diff --git a/installsystems/repository.py b/installsystems/repository.py
index a4fe3a9..bcf7d29 100644
--- a/installsystems/repository.py
+++ b/installsystems/repository.py
@@ -168,11 +168,16 @@ class Repository(object):
                 arrow("Skipping %s: already exists" % basesrc, 1)
             else:
                 arrow("Adding %s (%s)" % (basesrc, obj.md5), 1)
-                istools.copy(obj.path, dest,
-                             self.config.uid, self.config.gid, self.config.fmod)
+                dfo = open(dest, "wb")
+                sfo = PipeFile(obj.path, "r", progressbar=True)
+                sfo.consume(dfo)
+                sfo.close()
+                dfo.close()
+                istools.chrights(dest, self.config.uid,
+                                 self.config.gid, self.config.fmod)
         # copy is done. create a image inside repo
         r_image = PackageImage(os.path.join(self.config.path, image.md5),
-                                 md5name=True)
+                               md5name=True)
         # checking must be done with original md5
         r_image.md5 = image.md5
         # checking image and payload after copy
diff --git a/installsystems/tools.py b/installsystems/tools.py
index 3cfafaa..794ab0a 100644
--- a/installsystems/tools.py
+++ b/installsystems/tools.py
@@ -44,7 +44,7 @@ class PipeFile(object):
         self.mode = mode
         self.timeout = timeout
         self._md5 = hashlib.md5()
-        self.size = None
+        self.size = 0
         self.mtime = None
         self.consumed_size = 0
         # we already have and fo, nothing to open
@@ -68,7 +68,8 @@ class PipeFile(object):
             else:
                 raise IOError("URL type not supported")
         # init progress bar
-        if self.size is None:
+        # we use 0 because a null file is cannot show a progression during write
+        if self.size == 0:
             widget = [ BouncingBar(), " ", FileTransferSpeed() ]
             maxval = UnknownLength
         else:
@@ -98,7 +99,7 @@ class PipeFile(object):
         if "Content-Length" in self.fo.headers:
             self.size = int(self.fo.headers["Content-Length"])
         else:
-            self.size = None
+            self.size = 0
         # get mtime
         try:
             self.mtime = int(time.mktime(time.strptime(self.fo.headers["Last-Modified"],
@@ -119,7 +120,7 @@ class PipeFile(object):
         try:
             self.size = int(self.fo.headers["content-length"])
         except:
-            self.size = None
+            self.size = 0
 
     def _open_ssh(self, path):
         '''
@@ -174,6 +175,7 @@ class PipeFile(object):
     def write(self, buf):
         if self.mode == "r":
             raise IOError("Unable to write in r mode")
+        self.fo.write(buf)
         length = len(buf)
         self._md5.update(buf)
         self.consumed_size += length
@@ -208,7 +210,7 @@ class PipeFile(object):
         '''
         Set this property to true enable progress bar
         '''
-        if val == True:
+        if val == True and not hasattr(self, "_progressbar_started"):
             self._progressbar_started = True
             self._progressbar.start()
 
@@ -245,30 +247,6 @@ def smd5sum(buf):
     m.update(buf)
     return m.hexdigest()
 
-def copy(source, destination, uid=None, gid=None, mode=None, timeout=None):
-    '''
-    Copy a source to destination. Take care of path type
-    '''
-    stype = pathtype(source)
-    dtype = pathtype(destination)
-    # ensure destination is not a directory
-    if dtype == "file" and os.path.isdir(destination):
-        destination = os.path.join(destination, os.path.basename(source))
-    # trivial case
-    if stype == dtype == "file":
-        shutil.copy(source, destination)
-    elif (stype == "http" or stype == "ftp") and dtype == "file":
-        f_dest = open(destination, "w")
-        f_source = urllib2.urlopen(source, timeout=timeout)
-        copyfileobj(f_source, f_dest)
-    elif stype == "file" and dtype == "":
-        raise NotImplementedError
-    else:
-        raise NotImplementedError
-    # setting destination file rights
-    if dtype == "file":
-        chrights(destination, uid, gid, mode)
-
 def mkdir(path, uid=None, gid=None, mode=None):
     '''
     Create a directory and set rights
-- 
GitLab