69 args = user and ("%s@%s" % (user, host)) or host |
69 args = user and ("%s@%s" % (user, host)) or host |
70 return port and ("%s -p %s" % (args, port)) or args |
70 return port and ("%s -p %s" % (args, port)) or args |
71 |
71 |
72 def isexec(f): |
72 def isexec(f): |
73 """check whether a file is executable""" |
73 """check whether a file is executable""" |
74 return (os.lstat(f).st_mode & 0100 != 0) |
74 return (os.lstat(f).st_mode & 0o100 != 0) |
75 |
75 |
76 def setflags(f, l, x): |
76 def setflags(f, l, x): |
77 s = os.lstat(f).st_mode |
77 s = os.lstat(f).st_mode |
78 if l: |
78 if l: |
79 if not stat.S_ISLNK(s): |
79 if not stat.S_ISLNK(s): |
96 data = os.readlink(f) |
96 data = os.readlink(f) |
97 os.unlink(f) |
97 os.unlink(f) |
98 fp = open(f, "w") |
98 fp = open(f, "w") |
99 fp.write(data) |
99 fp.write(data) |
100 fp.close() |
100 fp.close() |
101 s = 0666 & ~umask # avoid restatting for chmod |
101 s = 0o666 & ~umask # avoid restatting for chmod |
102 |
102 |
103 sx = s & 0100 |
103 sx = s & 0o100 |
104 if x and not sx: |
104 if x and not sx: |
105 # Turn on +x for every +r bit when making a file executable |
105 # Turn on +x for every +r bit when making a file executable |
106 # and obey umask. |
106 # and obey umask. |
107 os.chmod(f, s | (s & 0444) >> 2 & ~umask) |
107 os.chmod(f, s | (s & 0o444) >> 2 & ~umask) |
108 elif not x and sx: |
108 elif not x and sx: |
109 # Turn off all +x bits |
109 # Turn off all +x bits |
110 os.chmod(f, s & 0666) |
110 os.chmod(f, s & 0o666) |
111 |
111 |
112 def copymode(src, dst, mode=None): |
112 def copymode(src, dst, mode=None): |
113 '''Copy the file mode from the file at path src to dst. |
113 '''Copy the file mode from the file at path src to dst. |
114 If src doesn't exist, we're using mode instead. If mode is None, we're |
114 If src doesn't exist, we're using mode instead. If mode is None, we're |
115 using umask.''' |
115 using umask.''' |
116 try: |
116 try: |
117 st_mode = os.lstat(src).st_mode & 0777 |
117 st_mode = os.lstat(src).st_mode & 0o777 |
118 except OSError, inst: |
118 except OSError, inst: |
119 if inst.errno != errno.ENOENT: |
119 if inst.errno != errno.ENOENT: |
120 raise |
120 raise |
121 st_mode = mode |
121 st_mode = mode |
122 if st_mode is None: |
122 if st_mode is None: |
123 st_mode = ~umask |
123 st_mode = ~umask |
124 st_mode &= 0666 |
124 st_mode &= 0o666 |
125 os.chmod(dst, st_mode) |
125 os.chmod(dst, st_mode) |
126 |
126 |
127 def checkexec(path): |
127 def checkexec(path): |
128 """ |
128 """ |
129 Check whether the given path is on a filesystem with UNIX-like exec flags |
129 Check whether the given path is on a filesystem with UNIX-like exec flags |
138 try: |
138 try: |
139 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
139 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
140 fh, fn = tempfile.mkstemp(dir=path, prefix='hg-checkexec-') |
140 fh, fn = tempfile.mkstemp(dir=path, prefix='hg-checkexec-') |
141 try: |
141 try: |
142 os.close(fh) |
142 os.close(fh) |
143 m = os.stat(fn).st_mode & 0777 |
143 m = os.stat(fn).st_mode & 0o777 |
144 new_file_has_exec = m & EXECFLAGS |
144 new_file_has_exec = m & EXECFLAGS |
145 os.chmod(fn, m ^ EXECFLAGS) |
145 os.chmod(fn, m ^ EXECFLAGS) |
146 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m) |
146 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0o777) == m) |
147 finally: |
147 finally: |
148 os.unlink(fn) |
148 os.unlink(fn) |
149 except (IOError, OSError): |
149 except (IOError, OSError): |
150 # we don't care, the user probably won't be able to commit anyway |
150 # we don't care, the user probably won't be able to commit anyway |
151 return False |
151 return False |
591 '''check whether a stat result is a symlink''' |
591 '''check whether a stat result is a symlink''' |
592 return st and stat.S_ISLNK(st.st_mode) |
592 return st and stat.S_ISLNK(st.st_mode) |
593 |
593 |
594 def statisexec(st): |
594 def statisexec(st): |
595 '''check whether a stat result is an executable file''' |
595 '''check whether a stat result is an executable file''' |
596 return st and (st.st_mode & 0100 != 0) |
596 return st and (st.st_mode & 0o100 != 0) |
597 |
597 |
598 def poll(fds): |
598 def poll(fds): |
599 """block until something happens on any file descriptor |
599 """block until something happens on any file descriptor |
600 |
600 |
601 This is a generic helper that will check for any activity |
601 This is a generic helper that will check for any activity |