hgbook
annotate ja/examples/run-example @ 1114:527b86d55d4a
inotify: update installation information
inotify is shipped in Mercurial since 1.0, which greatly simplifies the installation process
inotify is shipped in Mercurial since 1.0, which greatly simplifies the installation process
author | Nicolas Dumazet <nicdumz.commits@gmail.com> |
---|---|
date | Sun Dec 13 16:35:56 2009 +0900 (2009-12-13) |
parents | |
children |
rev | line source |
---|---|
foozy@708 | 1 #!/usr/bin/env python |
foozy@708 | 2 # |
foozy@708 | 3 # This program takes something that resembles a shell script and runs |
foozy@708 | 4 # it, spitting input (commands from the script) and output into text |
foozy@708 | 5 # files, for use in examples. |
foozy@708 | 6 |
foozy@708 | 7 import cStringIO |
foozy@708 | 8 import errno |
foozy@708 | 9 import getopt |
foozy@708 | 10 import os |
foozy@708 | 11 import pty |
foozy@708 | 12 import re |
foozy@708 | 13 import select |
foozy@708 | 14 import shutil |
foozy@708 | 15 import signal |
foozy@708 | 16 import stat |
foozy@708 | 17 import sys |
foozy@708 | 18 import tempfile |
foozy@708 | 19 import time |
foozy@708 | 20 |
foozy@708 | 21 tex_subs = { |
foozy@708 | 22 '\\': '\\textbackslash{}', |
foozy@708 | 23 '{': '\\{', |
foozy@708 | 24 '}': '\\}', |
foozy@708 | 25 } |
foozy@708 | 26 |
foozy@708 | 27 def gensubs(s): |
foozy@708 | 28 start = 0 |
foozy@708 | 29 for i, c in enumerate(s): |
foozy@708 | 30 sub = tex_subs.get(c) |
foozy@708 | 31 if sub: |
foozy@708 | 32 yield s[start:i] |
foozy@708 | 33 start = i + 1 |
foozy@708 | 34 yield sub |
foozy@708 | 35 yield s[start:] |
foozy@708 | 36 |
foozy@708 | 37 def tex_escape(s): |
foozy@708 | 38 return ''.join(gensubs(s)) |
foozy@708 | 39 |
foozy@708 | 40 def maybe_unlink(name): |
foozy@708 | 41 try: |
foozy@708 | 42 os.unlink(name) |
foozy@708 | 43 return True |
foozy@708 | 44 except OSError, err: |
foozy@708 | 45 if err.errno != errno.ENOENT: |
foozy@708 | 46 raise |
foozy@708 | 47 return False |
foozy@708 | 48 |
foozy@708 | 49 def find_path_to(program): |
foozy@708 | 50 for p in os.environ.get('PATH', os.defpath).split(os.pathsep): |
foozy@708 | 51 name = os.path.join(p, program) |
foozy@708 | 52 if os.access(name, os.X_OK): |
foozy@708 | 53 return p |
foozy@708 | 54 return None |
foozy@708 | 55 |
foozy@708 | 56 class example: |
foozy@708 | 57 shell = '/usr/bin/env bash' |
foozy@708 | 58 ps1 = '__run_example_ps1__ ' |
foozy@708 | 59 ps2 = '__run_example_ps2__ ' |
foozy@708 | 60 pi_re = re.compile(r'#\$\s*(name|ignore):\s*(.*)$') |
foozy@708 | 61 |
foozy@708 | 62 timeout = 10 |
foozy@708 | 63 |
foozy@708 | 64 def __init__(self, name, verbose): |
foozy@708 | 65 self.name = name |
foozy@708 | 66 self.verbose = verbose |
foozy@708 | 67 self.poll = select.poll() |
foozy@708 | 68 |
foozy@708 | 69 def parse(self): |
foozy@708 | 70 '''yield each hunk of input from the file.''' |
foozy@708 | 71 fp = open(self.name) |
foozy@708 | 72 cfp = cStringIO.StringIO() |
foozy@708 | 73 for line in fp: |
foozy@708 | 74 cfp.write(line) |
foozy@708 | 75 if not line.rstrip().endswith('\\'): |
foozy@708 | 76 yield cfp.getvalue() |
foozy@708 | 77 cfp.seek(0) |
foozy@708 | 78 cfp.truncate() |
foozy@708 | 79 |
foozy@708 | 80 def status(self, s): |
foozy@708 | 81 sys.stdout.write(s) |
foozy@708 | 82 if not s.endswith('\n'): |
foozy@708 | 83 sys.stdout.flush() |
foozy@708 | 84 |
foozy@708 | 85 def send(self, s): |
foozy@708 | 86 if self.verbose: |
foozy@708 | 87 print >> sys.stderr, '>', self.debugrepr(s) |
foozy@708 | 88 while s: |
foozy@708 | 89 count = os.write(self.cfd, s) |
foozy@708 | 90 s = s[count:] |
foozy@708 | 91 |
foozy@708 | 92 def debugrepr(self, s): |
foozy@708 | 93 rs = repr(s) |
foozy@708 | 94 limit = 60 |
foozy@708 | 95 if len(rs) > limit: |
foozy@708 | 96 return ('%s%s ... [%d bytes]' % (rs[:limit], rs[0], len(s))) |
foozy@708 | 97 else: |
foozy@708 | 98 return rs |
foozy@708 | 99 |
foozy@708 | 100 timeout = 5 |
foozy@708 | 101 |
foozy@708 | 102 def read(self, hint): |
foozy@708 | 103 events = self.poll.poll(self.timeout * 1000) |
foozy@708 | 104 if not events: |
foozy@708 | 105 print >> sys.stderr, ('[%stimed out after %d seconds]' % |
foozy@708 | 106 (hint, self.timeout)) |
foozy@708 | 107 os.kill(self.pid, signal.SIGHUP) |
foozy@708 | 108 return '' |
foozy@708 | 109 return os.read(self.cfd, 1024) |
foozy@708 | 110 |
foozy@708 | 111 def receive(self, hint): |
foozy@708 | 112 out = cStringIO.StringIO() |
foozy@708 | 113 while True: |
foozy@708 | 114 try: |
foozy@708 | 115 if self.verbose: |
foozy@708 | 116 sys.stderr.write('< ') |
foozy@708 | 117 s = self.read(hint) |
foozy@708 | 118 except OSError, err: |
foozy@708 | 119 if err.errno == errno.EIO: |
foozy@708 | 120 return '', '' |
foozy@708 | 121 raise |
foozy@708 | 122 if self.verbose: |
foozy@708 | 123 print >> sys.stderr, self.debugrepr(s) |
foozy@708 | 124 out.write(s) |
foozy@708 | 125 s = out.getvalue() |
foozy@708 | 126 if s.endswith(self.ps1): |
foozy@708 | 127 return self.ps1, s.replace('\r\n', '\n')[:-len(self.ps1)] |
foozy@708 | 128 if s.endswith(self.ps2): |
foozy@708 | 129 return self.ps2, s.replace('\r\n', '\n')[:-len(self.ps2)] |
foozy@708 | 130 |
foozy@708 | 131 def sendreceive(self, s, hint): |
foozy@708 | 132 self.send(s) |
foozy@708 | 133 ps, r = self.receive(hint) |
foozy@708 | 134 if r.startswith(s): |
foozy@708 | 135 r = r[len(s):] |
foozy@708 | 136 return ps, r |
foozy@708 | 137 |
foozy@708 | 138 def run(self): |
foozy@708 | 139 ofp = None |
foozy@708 | 140 basename = os.path.basename(self.name) |
foozy@708 | 141 self.status('running %s ' % basename) |
foozy@708 | 142 tmpdir = tempfile.mkdtemp(prefix=basename) |
foozy@708 | 143 |
foozy@708 | 144 # remove the marker file that we tell make to use to see if |
foozy@708 | 145 # this run succeeded |
foozy@708 | 146 maybe_unlink(self.name + '.run') |
foozy@708 | 147 |
foozy@708 | 148 rcfile = os.path.join(tmpdir, '.hgrc') |
foozy@708 | 149 rcfp = open(rcfile, 'w') |
foozy@708 | 150 print >> rcfp, '[ui]' |
foozy@708 | 151 print >> rcfp, "username = Bryan O'Sullivan <bos@serpentine.com>" |
foozy@708 | 152 |
foozy@708 | 153 rcfile = os.path.join(tmpdir, '.bashrc') |
foozy@708 | 154 rcfp = open(rcfile, 'w') |
foozy@708 | 155 print >> rcfp, 'PS1="%s"' % self.ps1 |
foozy@708 | 156 print >> rcfp, 'PS2="%s"' % self.ps2 |
foozy@708 | 157 print >> rcfp, 'unset HISTFILE' |
foozy@708 | 158 path = ['/usr/bin', '/bin'] |
foozy@708 | 159 hg = find_path_to('hg') |
foozy@708 | 160 if hg and hg not in path: |
foozy@708 | 161 path.append(hg) |
foozy@708 | 162 def re_export(envar): |
foozy@708 | 163 v = os.getenv(envar) |
foozy@708 | 164 if v is not None: |
foozy@708 | 165 print >> rcfp, 'export ' + envar + '=' + v |
foozy@708 | 166 print >> rcfp, 'export PATH=' + ':'.join(path) |
foozy@708 | 167 re_export('PYTHONPATH') |
foozy@708 | 168 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd() |
foozy@708 | 169 print >> rcfp, 'export HGMERGE=merge' |
foozy@708 | 170 print >> rcfp, 'export LANG=C' |
foozy@708 | 171 print >> rcfp, 'export LC_ALL=C' |
foozy@708 | 172 print >> rcfp, 'export TZ=GMT' |
foozy@708 | 173 print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir |
foozy@708 | 174 print >> rcfp, 'export HGRCPATH=$HGRC' |
foozy@708 | 175 print >> rcfp, 'cd %s' % tmpdir |
foozy@708 | 176 rcfp.close() |
foozy@708 | 177 sys.stdout.flush() |
foozy@708 | 178 sys.stderr.flush() |
foozy@708 | 179 self.pid, self.cfd = pty.fork() |
foozy@708 | 180 if self.pid == 0: |
foozy@708 | 181 cmdline = ['/usr/bin/env', '-i', 'bash', '--noediting', |
foozy@708 | 182 '--noprofile', '--norc'] |
foozy@708 | 183 try: |
foozy@708 | 184 os.execv(cmdline[0], cmdline) |
foozy@708 | 185 except OSError, err: |
foozy@708 | 186 print >> sys.stderr, '%s: %s' % (cmdline[0], err.strerror) |
foozy@708 | 187 sys.stderr.flush() |
foozy@708 | 188 os._exit(0) |
foozy@708 | 189 self.poll.register(self.cfd, select.POLLIN | select.POLLERR | |
foozy@708 | 190 select.POLLHUP) |
foozy@708 | 191 |
foozy@708 | 192 prompts = { |
foozy@708 | 193 '': '', |
foozy@708 | 194 self.ps1: '$', |
foozy@708 | 195 self.ps2: '>', |
foozy@708 | 196 } |
foozy@708 | 197 |
foozy@708 | 198 ignore = [ |
foozy@708 | 199 r'\d+:[0-9a-f]{12}', # changeset number:hash |
foozy@708 | 200 r'[0-9a-f]{40}', # long changeset hash |
foozy@708 | 201 r'[0-9a-f]{12}', # short changeset hash |
foozy@708 | 202 r'^(?:---|\+\+\+) .*', # diff header with dates |
foozy@708 | 203 r'^date:.*', # date |
foozy@708 | 204 #r'^diff -r.*', # "diff -r" is followed by hash |
foozy@708 | 205 r'^# Date \d+ \d+', # hg patch header |
foozy@708 | 206 ] |
foozy@708 | 207 |
foozy@708 | 208 err = False |
foozy@708 | 209 read_hint = '' |
foozy@708 | 210 |
foozy@708 | 211 try: |
foozy@708 | 212 try: |
foozy@708 | 213 # eat first prompt string from shell |
foozy@708 | 214 self.read(read_hint) |
foozy@708 | 215 # setup env and prompt |
foozy@708 | 216 ps, output = self.sendreceive('source %s\n' % rcfile, |
foozy@708 | 217 read_hint) |
foozy@708 | 218 for hunk in self.parse(): |
foozy@708 | 219 # is this line a processing instruction? |
foozy@708 | 220 m = self.pi_re.match(hunk) |
foozy@708 | 221 if m: |
foozy@708 | 222 pi, rest = m.groups() |
foozy@708 | 223 if pi == 'name': |
foozy@708 | 224 self.status('.') |
foozy@708 | 225 out = rest |
foozy@708 | 226 if out in ('err', 'lxo', 'out', 'run', 'tmp'): |
foozy@708 | 227 print >> sys.stderr, ('%s: illegal section ' |
foozy@708 | 228 'name %r' % |
foozy@708 | 229 (self.name, out)) |
foozy@708 | 230 return 1 |
foozy@708 | 231 assert os.sep not in out |
foozy@708 | 232 if ofp is not None: |
foozy@708 | 233 ofp.close() |
foozy@708 | 234 err |= self.rename_output(ofp_basename, ignore) |
foozy@708 | 235 if out: |
foozy@708 | 236 ofp_basename = '%s.%s' % (self.name, out) |
foozy@708 | 237 read_hint = ofp_basename + ' ' |
foozy@708 | 238 ofp = open(ofp_basename + '.tmp', 'w') |
foozy@708 | 239 else: |
foozy@708 | 240 ofp = None |
foozy@708 | 241 elif pi == 'ignore': |
foozy@708 | 242 ignore.append(rest) |
foozy@708 | 243 elif hunk.strip(): |
foozy@708 | 244 # it's something we should execute |
foozy@708 | 245 newps, output = self.sendreceive(hunk, read_hint) |
foozy@708 | 246 if not ofp: |
foozy@708 | 247 continue |
foozy@708 | 248 # first, print the command we ran |
foozy@708 | 249 if not hunk.startswith('#'): |
foozy@708 | 250 nl = hunk.endswith('\n') |
foozy@708 | 251 hunk = ('%s \\textbf{%s}' % |
foozy@708 | 252 (prompts[ps], |
foozy@708 | 253 tex_escape(hunk.rstrip('\n')))) |
foozy@708 | 254 if nl: hunk += '\n' |
foozy@708 | 255 ofp.write(hunk) |
foozy@708 | 256 # then its output |
foozy@708 | 257 ofp.write(tex_escape(output)) |
foozy@708 | 258 ps = newps |
foozy@708 | 259 self.status('\n') |
foozy@708 | 260 except: |
foozy@708 | 261 print >> sys.stderr, '(killed)' |
foozy@708 | 262 os.kill(self.pid, signal.SIGKILL) |
foozy@708 | 263 pid, rc = os.wait() |
foozy@708 | 264 raise |
foozy@708 | 265 else: |
foozy@708 | 266 try: |
foozy@708 | 267 ps, output = self.sendreceive('exit\n', read_hint) |
foozy@708 | 268 if ofp is not None: |
foozy@708 | 269 ofp.write(output) |
foozy@708 | 270 ofp.close() |
foozy@708 | 271 err |= self.rename_output(ofp_basename, ignore) |
foozy@708 | 272 os.close(self.cfd) |
foozy@708 | 273 except IOError: |
foozy@708 | 274 pass |
foozy@708 | 275 os.kill(self.pid, signal.SIGTERM) |
foozy@708 | 276 pid, rc = os.wait() |
foozy@708 | 277 err = err or rc |
foozy@708 | 278 if err: |
foozy@708 | 279 if os.WIFEXITED(rc): |
foozy@708 | 280 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc) |
foozy@708 | 281 elif os.WIFSIGNALED(rc): |
foozy@708 | 282 print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc) |
foozy@708 | 283 else: |
foozy@708 | 284 open(self.name + '.run', 'w') |
foozy@708 | 285 return err |
foozy@708 | 286 finally: |
foozy@708 | 287 shutil.rmtree(tmpdir) |
foozy@708 | 288 |
foozy@708 | 289 def rename_output(self, base, ignore): |
foozy@708 | 290 mangle_re = re.compile('(?:' + '|'.join(ignore) + ')') |
foozy@708 | 291 def mangle(s): |
foozy@708 | 292 return mangle_re.sub('', s) |
foozy@708 | 293 def matchfp(fp1, fp2): |
foozy@708 | 294 while True: |
foozy@708 | 295 s1 = mangle(fp1.readline()) |
foozy@708 | 296 s2 = mangle(fp2.readline()) |
foozy@708 | 297 if cmp(s1, s2): |
foozy@708 | 298 break |
foozy@708 | 299 if not s1: |
foozy@708 | 300 return True |
foozy@708 | 301 return False |
foozy@708 | 302 |
foozy@708 | 303 oldname = base + '.out' |
foozy@708 | 304 tmpname = base + '.tmp' |
foozy@708 | 305 errname = base + '.err' |
foozy@708 | 306 errfp = open(errname, 'w+') |
foozy@708 | 307 for line in open(tmpname): |
foozy@708 | 308 errfp.write(mangle_re.sub('', line)) |
foozy@708 | 309 os.rename(tmpname, base + '.lxo') |
foozy@708 | 310 errfp.seek(0) |
foozy@708 | 311 try: |
foozy@708 | 312 oldfp = open(oldname) |
foozy@708 | 313 except IOError, err: |
foozy@708 | 314 if err.errno != errno.ENOENT: |
foozy@708 | 315 raise |
foozy@708 | 316 os.rename(errname, oldname) |
foozy@708 | 317 return False |
foozy@708 | 318 if matchfp(oldfp, errfp): |
foozy@708 | 319 os.unlink(errname) |
foozy@708 | 320 return False |
foozy@708 | 321 else: |
foozy@708 | 322 print >> sys.stderr, '\nOutput of %s has changed!' % base |
foozy@708 | 323 os.system('diff -u %s %s 1>&2' % (oldname, errname)) |
foozy@708 | 324 return True |
foozy@708 | 325 |
foozy@708 | 326 def print_help(exit, msg=None): |
foozy@708 | 327 if msg: |
foozy@708 | 328 print >> sys.stderr, 'Error:', msg |
foozy@708 | 329 print >> sys.stderr, 'Usage: run-example [options] [test...]' |
foozy@708 | 330 print >> sys.stderr, 'Options:' |
foozy@708 | 331 print >> sys.stderr, ' -a --all run all tests in this directory' |
foozy@708 | 332 print >> sys.stderr, ' -h --help print this help message' |
foozy@708 | 333 print >> sys.stderr, ' -v --verbose display extra debug output' |
foozy@708 | 334 sys.exit(exit) |
foozy@708 | 335 |
foozy@708 | 336 def main(path='.'): |
foozy@708 | 337 opts, args = getopt.getopt(sys.argv[1:], '?ahv', |
foozy@708 | 338 ['all', 'help', 'verbose']) |
foozy@708 | 339 verbose = False |
foozy@708 | 340 run_all = False |
foozy@708 | 341 for o, a in opts: |
foozy@708 | 342 if o in ('-h', '-?', '--help'): |
foozy@708 | 343 print_help(0) |
foozy@708 | 344 if o in ('-a', '--all'): |
foozy@708 | 345 run_all = True |
foozy@708 | 346 if o in ('-v', '--verbose'): |
foozy@708 | 347 verbose = True |
foozy@708 | 348 errs = 0 |
foozy@708 | 349 if args: |
foozy@708 | 350 for a in args: |
foozy@708 | 351 try: |
foozy@708 | 352 st = os.lstat(a) |
foozy@708 | 353 except OSError, err: |
foozy@708 | 354 print >> sys.stderr, '%s: %s' % (a, err.strerror) |
foozy@708 | 355 errs += 1 |
foozy@708 | 356 continue |
foozy@708 | 357 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: |
foozy@708 | 358 if example(a, verbose).run(): |
foozy@708 | 359 errs += 1 |
foozy@708 | 360 else: |
foozy@708 | 361 print >> sys.stderr, '%s: not a file, or not executable' % a |
foozy@708 | 362 errs += 1 |
foozy@708 | 363 elif run_all: |
foozy@708 | 364 names = os.listdir(path) |
foozy@708 | 365 names.sort() |
foozy@708 | 366 for name in names: |
foozy@708 | 367 if name == 'run-example' or name.startswith('.'): continue |
foozy@708 | 368 if name.endswith('.out') or name.endswith('~'): continue |
foozy@708 | 369 if name.endswith('.run'): continue |
foozy@708 | 370 pathname = os.path.join(path, name) |
foozy@708 | 371 try: |
foozy@708 | 372 st = os.lstat(pathname) |
foozy@708 | 373 except OSError, err: |
foozy@708 | 374 # could be an output file that was removed while we ran |
foozy@708 | 375 if err.errno != errno.ENOENT: |
foozy@708 | 376 raise |
foozy@708 | 377 continue |
foozy@708 | 378 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: |
foozy@708 | 379 if example(pathname, verbose).run(): |
foozy@708 | 380 errs += 1 |
foozy@708 | 381 print >> open(os.path.join(path, '.run'), 'w'), time.asctime() |
foozy@708 | 382 else: |
foozy@708 | 383 print_help(1, msg='no test names given, and --all not provided') |
foozy@708 | 384 return errs |
foozy@708 | 385 |
foozy@708 | 386 if __name__ == '__main__': |
foozy@708 | 387 try: |
foozy@708 | 388 sys.exit(main()) |
foozy@708 | 389 except KeyboardInterrupt: |
foozy@708 | 390 print >> sys.stderr, 'interrupted!' |
foozy@708 | 391 sys.exit(1) |