hgbook

annotate en/examples/run-example @ 77:773f4a9e7975

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