comparison mercurial/util.py @ 15010:c3114acd8ea2

util: factor new function copymode out of mktempcopy
author Adrian Buehlmann <adrian@cadifra.com>
date Tue, 02 Aug 2011 12:29:48 +0200
parents f6a737357195
children 5e44e4b3a0a3
comparison
equal deleted inserted replaced
15009:caa5283390f8 15010:c3114acd8ea2
699 # pure build; use a safe default 699 # pure build; use a safe default
700 return True 700 return True
701 else: 701 else:
702 return os.name == "nt" or os.environ.get("DISPLAY") 702 return os.name == "nt" or os.environ.get("DISPLAY")
703 703
704 def copymode(src, dst, mode=None):
705 '''Copy the file mode from the file at path src to dst.
706 If src doesn't exist, we're using mode instead. If mode is None, we're
707 using umask.'''
708 try:
709 st_mode = os.lstat(src).st_mode & 0777
710 except OSError, inst:
711 if inst.errno != errno.ENOENT:
712 raise
713 st_mode = mode
714 if st_mode is None:
715 st_mode = ~umask
716 st_mode &= 0666
717 os.chmod(dst, st_mode)
718
704 def mktempcopy(name, emptyok=False, createmode=None): 719 def mktempcopy(name, emptyok=False, createmode=None):
705 """Create a temporary file with the same contents from name 720 """Create a temporary file with the same contents from name
706 721
707 The permission bits are copied from the original file. 722 The permission bits are copied from the original file.
708 723
715 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) 730 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
716 os.close(fd) 731 os.close(fd)
717 # Temporary files are created with mode 0600, which is usually not 732 # Temporary files are created with mode 0600, which is usually not
718 # what we want. If the original file already exists, just copy 733 # what we want. If the original file already exists, just copy
719 # its mode. Otherwise, manually obey umask. 734 # its mode. Otherwise, manually obey umask.
720 try: 735 copymode(name, temp, createmode)
721 st_mode = os.lstat(name).st_mode & 0777
722 except OSError, inst:
723 if inst.errno != errno.ENOENT:
724 raise
725 st_mode = createmode
726 if st_mode is None:
727 st_mode = ~umask
728 st_mode &= 0666
729 os.chmod(temp, st_mode)
730 if emptyok: 736 if emptyok:
731 return temp 737 return temp
732 try: 738 try:
733 try: 739 try:
734 ifp = posixfile(name, "rb") 740 ifp = posixfile(name, "rb")