##// END OF EJS Templates
Minor tweak: os.STOPSIG -> os.WSTOPSIG. Pychecker spotted this one.
mark.williamson@cl.cam.ac.uk -
r912:302f83b8 default
parent child Browse files
Show More
@@ -1,281 +1,281
1 1 # util.py - utility functions and platform specfic implementations
2 2 #
3 3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 import os, errno
9 9 from demandload import *
10 10 demandload(globals(), "re")
11 11
12 12 def unique(g):
13 13 seen = {}
14 14 for f in g:
15 15 if f not in seen:
16 16 seen[f] = 1
17 17 yield f
18 18
19 19 class Abort(Exception):
20 20 """Raised if a command needs to print an error and exit."""
21 21
22 22 def always(fn): return True
23 23 def never(fn): return False
24 24
25 25 def globre(pat, head = '^', tail = '$'):
26 26 "convert a glob pattern into a regexp"
27 27 i, n = 0, len(pat)
28 28 res = ''
29 29 group = False
30 30 def peek(): return i < n and pat[i]
31 31 while i < n:
32 32 c = pat[i]
33 33 i = i+1
34 34 if c == '*':
35 35 if peek() == '*':
36 36 i += 1
37 37 res += '.*'
38 38 else:
39 39 res += '[^/]*'
40 40 elif c == '?':
41 41 res += '.'
42 42 elif c == '[':
43 43 j = i
44 44 if j < n and pat[j] in '!]':
45 45 j += 1
46 46 while j < n and pat[j] != ']':
47 47 j += 1
48 48 if j >= n:
49 49 res += '\\['
50 50 else:
51 51 stuff = pat[i:j].replace('\\','\\\\')
52 52 i = j + 1
53 53 if stuff[0] == '!':
54 54 stuff = '^' + stuff[1:]
55 55 elif stuff[0] == '^':
56 56 stuff = '\\' + stuff
57 57 res = '%s[%s]' % (res, stuff)
58 58 elif c == '{':
59 59 group = True
60 60 res += '(?:'
61 61 elif c == '}' and group:
62 62 res += ')'
63 63 group = False
64 64 elif c == ',' and group:
65 65 res += '|'
66 66 else:
67 67 res += re.escape(c)
68 68 return head + res + tail
69 69
70 70 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
71 71
72 72 def pathto(n1, n2):
73 73 '''return the relative path from one place to another.
74 74 this returns a path in the form used by the local filesystem, not hg.'''
75 75 if not n1: return localpath(n2)
76 76 a, b = n1.split('/'), n2.split('/')
77 77 a.reverse(), b.reverse()
78 78 while a and b and a[-1] == b[-1]:
79 79 a.pop(), b.pop()
80 80 b.reverse()
81 81 return os.sep.join((['..'] * len(a)) + b)
82 82
83 83 def canonpath(repo, cwd, myname):
84 84 rootsep = repo.root + os.sep
85 85 name = myname
86 86 if not name.startswith(os.sep):
87 87 name = os.path.join(repo.root, cwd, name)
88 88 name = os.path.normpath(name)
89 89 if name.startswith(rootsep):
90 90 return pconvert(name[len(rootsep):])
91 91 elif name == repo.root:
92 92 return ''
93 93 else:
94 94 raise Abort('%s not under repository root' % myname)
95 95
96 96 def matcher(repo, cwd, names, inc, exc, head = ''):
97 97 def patkind(name):
98 98 for prefix in 're:', 'glob:', 'path:', 'relpath:':
99 99 if name.startswith(prefix): return name.split(':', 1)
100 100 for c in name:
101 101 if c in _globchars: return 'glob', name
102 102 return 'relpath', name
103 103
104 104 def regex(kind, name, tail):
105 105 '''convert a pattern into a regular expression'''
106 106 if kind == 're':
107 107 return name
108 108 elif kind == 'path':
109 109 return '^' + re.escape(name) + '(?:/|$)'
110 110 elif kind == 'relpath':
111 111 return head + re.escape(name) + tail
112 112 return head + globre(name, '', tail)
113 113
114 114 def matchfn(pats, tail):
115 115 """build a matching function from a set of patterns"""
116 116 if pats:
117 117 pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats])
118 118 return re.compile(pat).match
119 119
120 120 def globprefix(pat):
121 121 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
122 122 root = []
123 123 for p in pat.split(os.sep):
124 124 if patkind(p)[0] == 'glob': break
125 125 root.append(p)
126 126 return '/'.join(root)
127 127
128 128 pats = []
129 129 files = []
130 130 roots = []
131 131 for kind, name in map(patkind, names):
132 132 if kind in ('glob', 'relpath'):
133 133 name = canonpath(repo, cwd, name)
134 134 if name == '':
135 135 kind, name = 'glob', '**'
136 136 if kind in ('glob', 'path', 're'):
137 137 pats.append((kind, name))
138 138 if kind == 'glob':
139 139 root = globprefix(name)
140 140 if root: roots.append(root)
141 141 elif kind == 'relpath':
142 142 files.append((kind, name))
143 143 roots.append(name)
144 144
145 145 patmatch = matchfn(pats, '$') or always
146 146 filematch = matchfn(files, '(?:/|$)') or always
147 147 incmatch = always
148 148 if inc:
149 149 incmatch = matchfn(map(patkind, inc), '(?:/|$)')
150 150 excmatch = lambda fn: False
151 151 if exc:
152 152 excmatch = matchfn(map(patkind, exc), '(?:/|$)')
153 153
154 154 return roots, lambda fn: (incmatch(fn) and not excmatch(fn) and
155 155 (fn.endswith('/') or
156 156 (not pats and not files) or
157 157 (pats and patmatch(fn)) or
158 158 (files and filematch(fn))))
159 159
160 160 def system(cmd, errprefix=None):
161 161 """execute a shell command that must succeed"""
162 162 rc = os.system(cmd)
163 163 if rc:
164 164 errmsg = "%s %s" % (os.path.basename(cmd.split(None, 1)[0]),
165 165 explain_exit(rc)[0])
166 166 if errprefix:
167 167 errmsg = "%s: %s" % (errprefix, errmsg)
168 168 raise Abort(errmsg)
169 169
170 170 def rename(src, dst):
171 171 try:
172 172 os.rename(src, dst)
173 173 except:
174 174 os.unlink(dst)
175 175 os.rename(src, dst)
176 176
177 177 def copytree(src, dst, copyfile):
178 178 """Copy a directory tree, files are copied using 'copyfile'."""
179 179 names = os.listdir(src)
180 180 os.mkdir(dst)
181 181
182 182 for name in names:
183 183 srcname = os.path.join(src, name)
184 184 dstname = os.path.join(dst, name)
185 185 if os.path.isdir(srcname):
186 186 copytree(srcname, dstname, copyfile)
187 187 elif os.path.isfile(srcname):
188 188 copyfile(srcname, dstname)
189 189 else:
190 190 raise IOError("Not a regular file: %r" % srcname)
191 191
192 192 def _makelock_file(info, pathname):
193 193 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
194 194 os.write(ld, info)
195 195 os.close(ld)
196 196
197 197 def _readlock_file(pathname):
198 198 return file(pathname).read()
199 199
200 200 # Platfor specific varients
201 201 if os.name == 'nt':
202 202 nulldev = 'NUL:'
203 203
204 204 def is_exec(f, last):
205 205 return last
206 206
207 207 def set_exec(f, mode):
208 208 pass
209 209
210 210 def pconvert(path):
211 211 return path.replace("\\", "/")
212 212
213 213 def localpath(path):
214 214 return path.replace('/', '\\')
215 215
216 216 def normpath(path):
217 217 return pconvert(os.path.normpath(path))
218 218
219 219 makelock = _makelock_file
220 220 readlock = _readlock_file
221 221
222 222 def explain_exit(code):
223 223 return "exited with status %d" % code, code
224 224
225 225 else:
226 226 nulldev = '/dev/null'
227 227
228 228 def is_exec(f, last):
229 229 return (os.stat(f).st_mode & 0100 != 0)
230 230
231 231 def set_exec(f, mode):
232 232 s = os.stat(f).st_mode
233 233 if (s & 0100 != 0) == mode:
234 234 return
235 235 if mode:
236 236 # Turn on +x for every +r bit when making a file executable
237 237 # and obey umask.
238 238 umask = os.umask(0)
239 239 os.umask(umask)
240 240 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
241 241 else:
242 242 os.chmod(f, s & 0666)
243 243
244 244 def pconvert(path):
245 245 return path
246 246
247 247 def localpath(path):
248 248 return path
249 249
250 250 normpath = os.path.normpath
251 251
252 252 def makelock(info, pathname):
253 253 try:
254 254 os.symlink(info, pathname)
255 255 except OSError, why:
256 256 if why.errno == errno.EEXIST:
257 257 raise
258 258 else:
259 259 _makelock_file(info, pathname)
260 260
261 261 def readlock(pathname):
262 262 try:
263 263 return os.readlink(pathname)
264 264 except OSError, why:
265 265 if why.errno == errno.EINVAL:
266 266 return _readlock_file(pathname)
267 267 else:
268 268 raise
269 269
270 270 def explain_exit(code):
271 271 """return a 2-tuple (desc, code) describing a process's status"""
272 272 if os.WIFEXITED(code):
273 273 val = os.WEXITSTATUS(code)
274 274 return "exited with status %d" % val, val
275 275 elif os.WIFSIGNALED(code):
276 276 val = os.WTERMSIG(code)
277 277 return "killed by signal %d" % val, val
278 278 elif os.WIFSTOPPED(code):
279 val = os.STOPSIG(code)
279 val = os.WSTOPSIG(code)
280 280 return "stopped by signal %d" % val, val
281 281 raise ValueError("invalid exit code")
General Comments 0
You need to be logged in to leave comments. Login now