util: add copyfile function
authorMatt Mackall <mpm@selenic.com>
Mon, 13 Nov 2006 13:26:57 -0600
changeset 3629 4cfb72bcb978
parent 3628 dc3504af7722
child 3630 508036290b00
util: add copyfile function
mercurial/commands.py
mercurial/patch.py
mercurial/util.py
--- a/mercurial/commands.py	Mon Nov 13 13:26:57 2006 -0600
+++ b/mercurial/commands.py	Mon Nov 13 13:26:57 2006 -0600
@@ -8,7 +8,7 @@
 from demandload import demandload
 from node import *
 from i18n import gettext as _
-demandload(globals(), "os re sys signal shutil imp urllib pdb shlex")
+demandload(globals(), "os re sys signal imp urllib pdb shlex")
 demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo")
 demandload(globals(), "difflib patch tempfile time")
 demandload(globals(), "traceback errno version atexit sets bz2")
@@ -1004,14 +1004,11 @@
                     repo.undelete([abstarget], wlock)
                 try:
                     if not opts.get('dry_run'):
-                        shutil.copyfile(relsrc, reltarget)
-                        shutil.copymode(relsrc, reltarget)
+                        util.copyfile(relsrc, reltarget)
                     restore = False
                 finally:
                     if restore:
                         repo.remove([abstarget], wlock)
-            except shutil.Error, inst:
-                raise util.Abort(str(inst))
             except IOError, inst:
                 if inst.errno == errno.ENOENT:
                     ui.warn(_('%s: deleted in working copy\n') % relsrc)
@@ -2419,8 +2416,7 @@
                 ui.note(_('saving current version of %s as %s\n') %
                         (rel, bakname))
                 if not opts.get('dry_run'):
-                    shutil.copyfile(rel, bakname)
-                    shutil.copymode(rel, bakname)
+                    util.copyfile(rel, bakname)
             if ui.verbose or not exact:
                 ui.status(xlist[1] % rel)
         for table, hitlist, misslist, backuphit, backupmiss in disptable:
--- a/mercurial/patch.py	Mon Nov 13 13:26:57 2006 -0600
+++ b/mercurial/patch.py	Mon Nov 13 13:26:57 2006 -0600
@@ -26,11 +26,8 @@
     targetdir = os.path.dirname(absdst)
     if not os.path.isdir(targetdir):
         os.makedirs(targetdir)
-    try:
-        shutil.copyfile(abssrc, absdst)
-        shutil.copymode(abssrc, absdst)
-    except shutil.Error, inst:
-        raise util.Abort(str(inst))
+
+    util.copyfile(abssrc, absdst)
 
 # public functions
 
--- a/mercurial/util.py	Mon Nov 13 13:26:57 2006 -0600
+++ b/mercurial/util.py	Mon Nov 13 13:26:57 2006 -0600
@@ -460,6 +460,14 @@
     except OSError:
         pass
 
+def copyfile(src, dest):
+    "copy a file, preserving mode"
+    try:
+        shutil.copyfile(src, dest)
+        shutil.copymode(src, dest)
+    except shutil.Error, inst:
+        raise util.Abort(str(inst))
+
 def copyfiles(src, dst, hardlink=None):
     """Copy a directory tree using hardlinks if possible"""