comparison mercurial/util.py @ 5702:1b914de8d0ba

util: add new set_flags method This is more efficient than calling set_link and set_exec
author Matt Mackall <mpm@selenic.com>
date Thu, 27 Dec 2007 22:27:45 -0600
parents 8e495dd6662e
children 76dd039c2bad
comparison
equal deleted inserted replaced
5701:32c2832682a9 5702:1b914de8d0ba
989 pass 989 pass
990 990
991 def set_link(f, mode): 991 def set_link(f, mode):
992 pass 992 pass
993 993
994 def set_flags(f, flags):
995 pass
996
994 def set_binary(fd): 997 def set_binary(fd):
995 msvcrt.setmode(fd.fileno(), os.O_BINARY) 998 msvcrt.setmode(fd.fileno(), os.O_BINARY)
996 999
997 def pconvert(path): 1000 def pconvert(path):
998 return path.replace("\\", "/") 1001 return path.replace("\\", "/")
1171 os.symlink(data, f) 1174 os.symlink(data, f)
1172 else: 1175 else:
1173 data = os.readlink(f) 1176 data = os.readlink(f)
1174 os.unlink(f) 1177 os.unlink(f)
1175 file(f, "w").write(data) 1178 file(f, "w").write(data)
1179
1180 def set_flags(f, flags):
1181 s = os.lstat(f).st_mode
1182 x = "x" in flags
1183 l = "l" in flags
1184 if l:
1185 if not stat.S_ISLNK(s):
1186 # switch file to link
1187 data = file(f).read()
1188 os.unlink(f)
1189 os.symlink(data, f)
1190 # no chmod needed at this point
1191 return
1192 if stat.S_ISLNK(s):
1193 # switch link to file
1194 data = os.readlink(f)
1195 os.unlink(f)
1196 file(f, "w").write(data)
1197 s = 0666 & ~_umask # avoid restatting for chmod
1198
1199 sx = s & 0100
1200 if x and not sx:
1201 # Turn on +x for every +r bit when making a file executable
1202 # and obey umask.
1203 os.chmod(f, s | (s & 0444) >> 2 & ~_umask)
1204 elif not x and sx:
1205 # Turn off all +x bits
1206 os.chmod(f, s & 0666)
1176 1207
1177 def set_binary(fd): 1208 def set_binary(fd):
1178 pass 1209 pass
1179 1210
1180 def pconvert(path): 1211 def pconvert(path):