[PATCH 1 of 2 V2] util: factor new function copymode out of mktempcopy
Adrian Buehlmann
adrian at cadifra.com
Tue Aug 2 07:19:31 CDT 2011
# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1312280988 -7200
# Node ID c3114acd8ea20a7f2f786084a7a833b54be43e75
# Parent caa5283390f84849fafb4acdbe6054ff43ef3075
util: factor new function copymode out of mktempcopy
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -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:
More information about the Mercurial-devel
mailing list