hgbook

annotate en/examples/run-example @ 72:12df31afb4e1

Propagate exceptions more correctly.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue Aug 29 22:34:03 2006 -0700 (2006-08-29)
parents ddf533d41c09
children 9604dd885616
rev   line source
bos@67 1 #!/usr/bin/env python
bos@4 2 #
bos@4 3 # This program takes something that resembles a shell script and runs
bos@4 4 # it, spitting input (commands from the script) and output into text
bos@4 5 # files, for use in examples.
bos@3 6
bos@3 7 import cStringIO
bos@3 8 import os
bos@3 9 import pty
bos@3 10 import re
bos@4 11 import shutil
bos@6 12 import signal
bos@36 13 import stat
bos@3 14 import sys
bos@4 15 import tempfile
bos@4 16 import time
bos@3 17
bos@4 18 def tex_escape(s):
bos@4 19 if '\\' in s:
bos@4 20 s = s.replace('\\', '\\\\')
bos@4 21 if '{' in s:
bos@4 22 s = s.replace('{', '\\{')
bos@4 23 if '}' in s:
bos@4 24 s = s.replace('}', '\\}')
bos@4 25 return s
bos@4 26
bos@3 27 class example:
bos@70 28 shell = '/usr/bin/env bash'
bos@6 29 prompt = '__run_example_prompt__\n'
bos@71 30 pi_re = re.compile(r'#\$\s*(name):\s*(.*)$')
bos@4 31
bos@3 32 def __init__(self, name):
bos@3 33 self.name = name
bos@3 34
bos@3 35 def parse(self):
bos@4 36 '''yield each hunk of input from the file.'''
bos@3 37 fp = open(self.name)
bos@3 38 cfp = cStringIO.StringIO()
bos@3 39 for line in fp:
bos@3 40 cfp.write(line)
bos@3 41 if not line.rstrip().endswith('\\'):
bos@3 42 yield cfp.getvalue()
bos@3 43 cfp.seek(0)
bos@3 44 cfp.truncate()
bos@3 45
bos@3 46 def status(self, s):
bos@3 47 sys.stdout.write(s)
bos@3 48 if not s.endswith('\n'):
bos@3 49 sys.stdout.flush()
bos@3 50
bos@6 51 def send(self, s):
bos@6 52 self.cfp.write(s)
bos@6 53 self.cfp.flush()
bos@6 54
bos@6 55 def receive(self):
bos@6 56 out = cStringIO.StringIO()
bos@4 57 while True:
bos@6 58 s = self.cfp.readline().replace('\r\n', '\n')
bos@6 59 if not s or s == self.prompt:
bos@6 60 break
bos@6 61 out.write(s)
bos@6 62 return out.getvalue()
bos@4 63
bos@6 64 def sendreceive(self, s):
bos@6 65 self.send(s)
bos@6 66 r = self.receive()
bos@6 67 if r.startswith(s):
bos@6 68 r = r[len(s):]
bos@6 69 return r
bos@6 70
bos@3 71 def run(self):
bos@3 72 ofp = None
bos@4 73 basename = os.path.basename(self.name)
bos@4 74 self.status('running %s ' % basename)
bos@4 75 tmpdir = tempfile.mkdtemp(prefix=basename)
bos@6 76 rcfile = os.path.join(tmpdir, '.bashrc')
bos@6 77 rcfp = open(rcfile, 'w')
bos@6 78 print >> rcfp, 'PS1="%s"' % self.prompt
bos@6 79 print >> rcfp, 'unset HISTFILE'
bos@19 80 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
bos@6 81 print >> rcfp, 'export LANG=C'
bos@6 82 print >> rcfp, 'export LC_ALL=C'
bos@6 83 print >> rcfp, 'export TZ=GMT'
bos@6 84 print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir
bos@6 85 print >> rcfp, 'export HGRCPATH=$HGRC'
bos@6 86 print >> rcfp, 'cd %s' % tmpdir
bos@6 87 rcfp.close()
bos@68 88 sys.stdout.flush()
bos@68 89 sys.stderr.flush()
bos@6 90 pid, fd = pty.fork()
bos@6 91 if pid == 0:
bos@70 92 cmdline = ['/usr/bin/env', 'bash', '--noediting', '--noprofile',
bos@70 93 '--norc']
bos@68 94 try:
bos@68 95 os.execv(cmdline[0], cmdline)
bos@68 96 except OSError, err:
bos@68 97 print >> sys.stderr, '%s: %s' % (cmdline[0], err.strerror)
bos@68 98 sys.stderr.flush()
bos@68 99 os._exit(0)
bos@6 100 self.cfp = os.fdopen(fd, 'w+')
bos@4 101 try:
bos@71 102 try:
bos@71 103 # setup env and prompt
bos@71 104 self.sendreceive('source %s\n\n' % rcfile)
bos@71 105 for hunk in self.parse():
bos@71 106 # is this line a processing instruction?
bos@71 107 m = self.pi_re.match(hunk)
bos@71 108 if m:
bos@71 109 pi, rest = m.groups()
bos@71 110 if pi == 'name':
bos@71 111 self.status('.')
bos@71 112 out = rest
bos@71 113 assert os.sep not in out
bos@71 114 if out:
bos@71 115 ofp = open('%s.%s.out' % (self.name, out), 'w')
bos@71 116 else:
bos@71 117 ofp = None
bos@71 118 elif hunk.strip():
bos@71 119 # it's something we should execute
bos@71 120 output = self.sendreceive(hunk)
bos@71 121 if not ofp:
bos@71 122 continue
bos@71 123 # first, print the command we ran
bos@71 124 if not hunk.startswith('#'):
bos@71 125 nl = hunk.endswith('\n')
bos@71 126 hunk = ('$ \\textbf{%s}' %
bos@71 127 tex_escape(hunk.rstrip('\n')))
bos@71 128 if nl: hunk += '\n'
bos@71 129 ofp.write(hunk)
bos@71 130 # then its output
bos@71 131 ofp.write(tex_escape(output))
bos@71 132 self.status('\n')
bos@71 133 open(self.name + '.run', 'w')
bos@71 134 except:
bos@72 135 print >> sys.stderr, '(killed)'
bos@72 136 os.kill(pid, signal.SIGKILL)
bos@72 137 pid, rc = os.wait()
bos@71 138 raise
bos@72 139 else:
bos@71 140 try:
bos@71 141 output = self.sendreceive('exit\n')
bos@71 142 if ofp:
bos@71 143 ofp.write(output)
bos@71 144 self.cfp.close()
bos@71 145 except IOError:
bos@71 146 pass
bos@72 147 os.kill(pid, signal.SIGTERM)
bos@72 148 pid, rc = os.wait()
bos@72 149 if rc:
bos@72 150 if os.WIFEXITED(rc):
bos@72 151 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
bos@72 152 elif os.WIFSIGNALED(rc):
bos@72 153 print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc)
bos@72 154 return rc
bos@72 155 finally:
bos@4 156 shutil.rmtree(tmpdir)
bos@3 157
bos@3 158 def main(path='.'):
bos@3 159 args = sys.argv[1:]
bos@71 160 errs = 0
bos@3 161 if args:
bos@3 162 for a in args:
bos@71 163 if example(a).run():
bos@71 164 errs += 1
bos@71 165 return errs
bos@3 166 for name in os.listdir(path):
bos@3 167 if name == 'run-example' or name.startswith('.'): continue
bos@3 168 if name.endswith('.out') or name.endswith('~'): continue
bos@45 169 if name.endswith('.run'): continue
bos@19 170 pathname = os.path.join(path, name)
bos@36 171 st = os.lstat(pathname)
bos@36 172 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
bos@71 173 if example(pathname).run():
bos@71 174 errs += 1
bos@4 175 print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
bos@71 176 return errs
bos@3 177
bos@3 178 if __name__ == '__main__':
bos@71 179 sys.exit(main())