hgbook

annotate en/examples/run-example @ 136:7b5894fffc37

Don't falsely signal success to make.
author Bryan O'Sullivan <bos@serpentine.com>
date Mon Mar 05 20:26:23 2007 -0800 (2007-03-05)
parents c9aad709bd3a
children 9d7dffe74b2c
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@73 8 import errno
bos@78 9 import getopt
bos@3 10 import os
bos@3 11 import pty
bos@3 12 import re
bos@79 13 import select
bos@4 14 import shutil
bos@6 15 import signal
bos@36 16 import stat
bos@3 17 import sys
bos@4 18 import tempfile
bos@4 19 import time
bos@3 20
bos@77 21 tex_subs = {
bos@77 22 '\\': '\\textbackslash{}',
bos@77 23 '{': '\\{',
bos@77 24 '}': '\\}',
bos@77 25 }
bos@77 26
bos@77 27 def gensubs(s):
bos@77 28 start = 0
bos@77 29 for i, c in enumerate(s):
bos@77 30 sub = tex_subs.get(c)
bos@77 31 if sub:
bos@77 32 yield s[start:i]
bos@77 33 start = i + 1
bos@77 34 yield sub
bos@77 35 yield s[start:]
bos@77 36
bos@4 37 def tex_escape(s):
bos@77 38 return ''.join(gensubs(s))
bos@4 39
bos@3 40 class example:
bos@70 41 shell = '/usr/bin/env bash'
bos@103 42 ps1 = '__run_example_ps1__ '
bos@103 43 ps2 = '__run_example_ps2__ '
bos@71 44 pi_re = re.compile(r'#\$\s*(name):\s*(.*)$')
bos@4 45
bos@79 46 timeout = 5
bos@79 47
bos@78 48 def __init__(self, name, verbose):
bos@3 49 self.name = name
bos@78 50 self.verbose = verbose
bos@79 51 self.poll = select.poll()
bos@3 52
bos@3 53 def parse(self):
bos@4 54 '''yield each hunk of input from the file.'''
bos@3 55 fp = open(self.name)
bos@3 56 cfp = cStringIO.StringIO()
bos@3 57 for line in fp:
bos@3 58 cfp.write(line)
bos@3 59 if not line.rstrip().endswith('\\'):
bos@3 60 yield cfp.getvalue()
bos@3 61 cfp.seek(0)
bos@3 62 cfp.truncate()
bos@3 63
bos@3 64 def status(self, s):
bos@3 65 sys.stdout.write(s)
bos@3 66 if not s.endswith('\n'):
bos@3 67 sys.stdout.flush()
bos@3 68
bos@6 69 def send(self, s):
bos@78 70 if self.verbose:
bos@78 71 print >> sys.stderr, '>', self.debugrepr(s)
bos@73 72 while s:
bos@73 73 count = os.write(self.cfd, s)
bos@73 74 s = s[count:]
bos@6 75
bos@78 76 def debugrepr(self, s):
bos@78 77 rs = repr(s)
bos@78 78 limit = 60
bos@78 79 if len(rs) > limit:
bos@78 80 return ('%s%s ... [%d bytes]' % (rs[:limit], rs[0], len(s)))
bos@78 81 else:
bos@78 82 return rs
bos@78 83
bos@79 84 timeout = 5
bos@79 85
bos@79 86 def read(self):
bos@79 87 events = self.poll.poll(self.timeout * 1000)
bos@79 88 if not events:
bos@79 89 print >> sys.stderr, '[timed out after %d seconds]' % self.timeout
bos@79 90 os.kill(self.pid, signal.SIGHUP)
bos@79 91 return ''
bos@79 92 return os.read(self.cfd, 1024)
bos@79 93
bos@6 94 def receive(self):
bos@6 95 out = cStringIO.StringIO()
bos@4 96 while True:
bos@73 97 try:
bos@78 98 if self.verbose:
bos@78 99 sys.stderr.write('< ')
bos@79 100 s = self.read()
bos@73 101 except OSError, err:
bos@73 102 if err.errno == errno.EIO:
bos@103 103 return '', ''
bos@73 104 raise
bos@78 105 if self.verbose:
bos@78 106 print >> sys.stderr, self.debugrepr(s)
bos@6 107 out.write(s)
bos@73 108 s = out.getvalue()
bos@103 109 if s.endswith(self.ps1):
bos@103 110 return self.ps1, s.replace('\r\n', '\n')[:-len(self.ps1)]
bos@103 111 if s.endswith(self.ps2):
bos@103 112 return self.ps2, s.replace('\r\n', '\n')[:-len(self.ps2)]
bos@4 113
bos@6 114 def sendreceive(self, s):
bos@6 115 self.send(s)
bos@103 116 ps, r = self.receive()
bos@6 117 if r.startswith(s):
bos@6 118 r = r[len(s):]
bos@103 119 return ps, r
bos@6 120
bos@3 121 def run(self):
bos@3 122 ofp = None
bos@4 123 basename = os.path.basename(self.name)
bos@4 124 self.status('running %s ' % basename)
bos@4 125 tmpdir = tempfile.mkdtemp(prefix=basename)
bos@78 126
bos@136 127 # remove the marker file that we tell make to use to see if
bos@136 128 # this run succeeded
bos@136 129 try:
bos@136 130 os.unlink(self.name + '.run')
bos@136 131 except OSError, err:
bos@136 132 if err.errno != errno.ENOENT:
bos@136 133 raise
bos@136 134
bos@78 135 rcfile = os.path.join(tmpdir, '.hgrc')
bos@78 136 rcfp = open(rcfile, 'w')
bos@78 137 print >> rcfp, '[ui]'
bos@78 138 print >> rcfp, "username = Bryan O'Sullivan <bos@serpentine.com>"
bos@78 139
bos@6 140 rcfile = os.path.join(tmpdir, '.bashrc')
bos@6 141 rcfp = open(rcfile, 'w')
bos@103 142 print >> rcfp, 'PS1="%s"' % self.ps1
bos@103 143 print >> rcfp, 'PS2="%s"' % self.ps2
bos@6 144 print >> rcfp, 'unset HISTFILE'
bos@19 145 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
bos@124 146 print >> rcfp, 'export HGMERGE=merge'
bos@6 147 print >> rcfp, 'export LANG=C'
bos@6 148 print >> rcfp, 'export LC_ALL=C'
bos@6 149 print >> rcfp, 'export TZ=GMT'
bos@6 150 print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir
bos@6 151 print >> rcfp, 'export HGRCPATH=$HGRC'
bos@6 152 print >> rcfp, 'cd %s' % tmpdir
bos@6 153 rcfp.close()
bos@68 154 sys.stdout.flush()
bos@68 155 sys.stderr.flush()
bos@79 156 self.pid, self.cfd = pty.fork()
bos@79 157 if self.pid == 0:
bos@70 158 cmdline = ['/usr/bin/env', 'bash', '--noediting', '--noprofile',
bos@70 159 '--norc']
bos@68 160 try:
bos@68 161 os.execv(cmdline[0], cmdline)
bos@68 162 except OSError, err:
bos@68 163 print >> sys.stderr, '%s: %s' % (cmdline[0], err.strerror)
bos@68 164 sys.stderr.flush()
bos@68 165 os._exit(0)
bos@79 166 self.poll.register(self.cfd, select.POLLIN | select.POLLERR |
bos@79 167 select.POLLHUP)
bos@103 168
bos@103 169 prompts = {
bos@103 170 '': '',
bos@103 171 self.ps1: '$',
bos@103 172 self.ps2: '>',
bos@103 173 }
bos@103 174
bos@4 175 try:
bos@71 176 try:
bos@73 177 # eat first prompt string from shell
bos@79 178 self.read()
bos@71 179 # setup env and prompt
bos@103 180 ps, output = self.sendreceive('source %s\n' % rcfile)
bos@71 181 for hunk in self.parse():
bos@71 182 # is this line a processing instruction?
bos@71 183 m = self.pi_re.match(hunk)
bos@71 184 if m:
bos@71 185 pi, rest = m.groups()
bos@71 186 if pi == 'name':
bos@71 187 self.status('.')
bos@71 188 out = rest
bos@71 189 assert os.sep not in out
bos@71 190 if out:
bos@71 191 ofp = open('%s.%s.out' % (self.name, out), 'w')
bos@71 192 else:
bos@71 193 ofp = None
bos@71 194 elif hunk.strip():
bos@71 195 # it's something we should execute
bos@103 196 newps, output = self.sendreceive(hunk)
bos@71 197 if not ofp:
bos@71 198 continue
bos@71 199 # first, print the command we ran
bos@71 200 if not hunk.startswith('#'):
bos@71 201 nl = hunk.endswith('\n')
bos@103 202 hunk = ('%s \\textbf{%s}' %
bos@103 203 (prompts[ps],
bos@103 204 tex_escape(hunk.rstrip('\n'))))
bos@71 205 if nl: hunk += '\n'
bos@71 206 ofp.write(hunk)
bos@71 207 # then its output
bos@71 208 ofp.write(tex_escape(output))
bos@103 209 ps = newps
bos@71 210 self.status('\n')
bos@71 211 except:
bos@72 212 print >> sys.stderr, '(killed)'
bos@79 213 os.kill(self.pid, signal.SIGKILL)
bos@72 214 pid, rc = os.wait()
bos@71 215 raise
bos@72 216 else:
bos@71 217 try:
bos@103 218 ps, output = self.sendreceive('exit\n')
bos@71 219 if ofp:
bos@71 220 ofp.write(output)
bos@73 221 os.close(self.cfd)
bos@71 222 except IOError:
bos@71 223 pass
bos@79 224 os.kill(self.pid, signal.SIGTERM)
bos@72 225 pid, rc = os.wait()
bos@72 226 if rc:
bos@72 227 if os.WIFEXITED(rc):
bos@72 228 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
bos@72 229 elif os.WIFSIGNALED(rc):
bos@72 230 print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc)
bos@136 231 else:
bos@136 232 open(self.name + '.run', 'w')
bos@72 233 return rc
bos@72 234 finally:
bos@4 235 shutil.rmtree(tmpdir)
bos@3 236
bos@3 237 def main(path='.'):
bos@78 238 opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
bos@78 239 verbose = False
bos@78 240 for o, a in opts:
bos@78 241 if o in ('-v', '--verbose'):
bos@78 242 verbose = True
bos@71 243 errs = 0
bos@3 244 if args:
bos@3 245 for a in args:
bos@75 246 try:
bos@75 247 st = os.lstat(a)
bos@75 248 except OSError, err:
bos@75 249 print >> sys.stderr, '%s: %s' % (a, err.strerror)
bos@75 250 errs += 1
bos@75 251 continue
bos@75 252 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
bos@78 253 if example(a, verbose).run():
bos@75 254 errs += 1
bos@75 255 else:
bos@75 256 print >> sys.stderr, '%s: not a file, or not executable' % a
bos@71 257 errs += 1
bos@71 258 return errs
bos@3 259 for name in os.listdir(path):
bos@3 260 if name == 'run-example' or name.startswith('.'): continue
bos@3 261 if name.endswith('.out') or name.endswith('~'): continue
bos@45 262 if name.endswith('.run'): continue
bos@19 263 pathname = os.path.join(path, name)
bos@36 264 st = os.lstat(pathname)
bos@36 265 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
bos@78 266 if example(pathname, verbose).run():
bos@71 267 errs += 1
bos@4 268 print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
bos@71 269 return errs
bos@3 270
bos@3 271 if __name__ == '__main__':
bos@71 272 sys.exit(main())