util: add progress callback support to copyfiles
authorAugie Fackler <augie@google.com>
Thu, 19 Mar 2015 10:24:22 -0400
changeset 24439 2ddfac2f163e
parent 24438 5b85a5bc5bbb
child 24440 27ad6b91f5c2
util: add progress callback support to copyfiles
mercurial/util.py
--- a/mercurial/util.py	Mon Mar 23 23:04:51 2015 -0700
+++ b/mercurial/util.py	Thu Mar 19 10:24:22 2015 -0400
@@ -739,20 +739,27 @@
         except shutil.Error, inst:
             raise Abort(str(inst))
 
-def copyfiles(src, dst, hardlink=None):
-    """Copy a directory tree using hardlinks if possible"""
+def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
+    """Copy a directory tree using hardlinks if possible."""
+    num = 0
 
     if hardlink is None:
         hardlink = (os.stat(src).st_dev ==
                     os.stat(os.path.dirname(dst)).st_dev)
+    if hardlink:
+        topic = _('linking')
+    else:
+        topic = _('copying')
 
-    num = 0
     if os.path.isdir(src):
         os.mkdir(dst)
         for name, kind in osutil.listdir(src):
             srcname = os.path.join(src, name)
             dstname = os.path.join(dst, name)
-            hardlink, n = copyfiles(srcname, dstname, hardlink)
+            def nprog(t, pos):
+                if pos is not None:
+                    return progress(t, pos + num)
+            hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
             num += n
     else:
         if hardlink:
@@ -764,6 +771,8 @@
         else:
             shutil.copy(src, dst)
         num += 1
+        progress(topic, num)
+    progress(topic, None)
 
     return hardlink, num