util: factor new function copymode out of mktempcopy
authorAdrian Buehlmann <adrian@cadifra.com>
Tue, 02 Aug 2011 12:29:48 +0200
changeset 15010 c3114acd8ea2
parent 15009 caa5283390f8
child 15011 5e44e4b3a0a3
util: factor new function copymode out of mktempcopy
mercurial/util.py
--- a/mercurial/util.py	Tue Aug 02 11:32:39 2011 +0200
+++ b/mercurial/util.py	Tue Aug 02 12:29:48 2011 +0200
@@ -701,6 +701,21 @@
     else:
         return os.name == "nt" or os.environ.get("DISPLAY")
 
+def copymode(src, dst, mode=None):
+    '''Copy the file mode from the file at path src to dst.
+    If src doesn't exist, we're using mode instead. If mode is None, we're
+    using umask.'''
+    try:
+        st_mode = os.lstat(src).st_mode & 0777
+    except OSError, inst:
+        if inst.errno != errno.ENOENT:
+            raise
+        st_mode = mode
+        if st_mode is None:
+            st_mode = ~umask
+        st_mode &= 0666
+    os.chmod(dst, st_mode)
+
 def mktempcopy(name, emptyok=False, createmode=None):
     """Create a temporary file with the same contents from name
 
@@ -717,16 +732,7 @@
     # Temporary files are created with mode 0600, which is usually not
     # what we want.  If the original file already exists, just copy
     # its mode.  Otherwise, manually obey umask.
-    try:
-        st_mode = os.lstat(name).st_mode & 0777
-    except OSError, inst:
-        if inst.errno != errno.ENOENT:
-            raise
-        st_mode = createmode
-        if st_mode is None:
-            st_mode = ~umask
-        st_mode &= 0666
-    os.chmod(temp, st_mode)
+    copymode(name, temp, createmode)
     if emptyok:
         return temp
     try: