hgbook

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