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

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