Mercurial > hgbook
annotate en/examples/run-example @ 172:5f305adeb584
Try to tighten up the run environment to make things more reproducible.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Tue, 27 Mar 2007 15:04:47 -0700 |
parents | f8b5b782e150 |
children | 754312dc23d5 |
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 |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
8 import errno |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
9 import getopt |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 import os |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 import pty |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 import re |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
13 import select |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
14 import shutil |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
15 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
|
16 import stat |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 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
|
18 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
|
19 import time |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 |
77
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
21 tex_subs = { |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
22 '\\': '\\textbackslash{}', |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
23 '{': '\\{', |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
24 '}': '\\}', |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
25 } |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
26 |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
27 def gensubs(s): |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
28 start = 0 |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
29 for i, c in enumerate(s): |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
30 sub = tex_subs.get(c) |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
31 if sub: |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
32 yield s[start:i] |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
33 start = i + 1 |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
34 yield sub |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
35 yield s[start:] |
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
36 |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
37 def tex_escape(s): |
77
773f4a9e7975
Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents:
75
diff
changeset
|
38 return ''.join(gensubs(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
|
39 |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
40 def maybe_unlink(name): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
41 try: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
42 os.unlink(name) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
43 return True |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
44 except OSError, err: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
45 if err.errno != errno.ENOENT: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
46 raise |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
47 return False |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
48 |
172
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
49 def find_path_to(program): |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
50 for p in os.environ.get('PATH', os.defpath).split(os.pathsep): |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
51 name = os.path.join(p, program) |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
52 if os.access(name, os.X_OK): |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
53 return p |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
54 return None |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
55 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 class example: |
70
365b41b6a15e
bash is not necessarily in /bin.
Bryan O'Sullivan <bos@serpentine.com>
parents:
68
diff
changeset
|
57 shell = '/usr/bin/env bash' |
103 | 58 ps1 = '__run_example_ps1__ ' |
59 ps2 = '__run_example_ps2__ ' | |
168
f8b5b782e150
Backed out changeset 08a4467f489162c14cf17b94aa771a23dd6f02dc
Bryan O'Sullivan <bos@serpentine.com>
parents:
166
diff
changeset
|
60 pi_re = re.compile(r'#\$\s*(name|ignore):\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
|
61 |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
62 timeout = 5 |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
63 |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
64 def __init__(self, name, verbose): |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 self.name = name |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
66 self.verbose = verbose |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
67 self.poll = select.poll() |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 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
|
70 '''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
|
71 fp = open(self.name) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
72 cfp = cStringIO.StringIO() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 for line in fp: |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
74 cfp.write(line) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
75 if not line.rstrip().endswith('\\'): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
76 yield cfp.getvalue() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 cfp.seek(0) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 cfp.truncate() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 def status(self, s): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
81 sys.stdout.write(s) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 if not s.endswith('\n'): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 sys.stdout.flush() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
85 def send(self, s): |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
86 if self.verbose: |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
87 print >> sys.stderr, '>', self.debugrepr(s) |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
88 while s: |
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
89 count = os.write(self.cfd, s) |
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
90 s = s[count:] |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
91 |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
92 def debugrepr(self, s): |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
93 rs = repr(s) |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
94 limit = 60 |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
95 if len(rs) > limit: |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
96 return ('%s%s ... [%d bytes]' % (rs[:limit], rs[0], len(s))) |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
97 else: |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
98 return rs |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
99 |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
100 timeout = 5 |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
101 |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
102 def read(self): |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
103 events = self.poll.poll(self.timeout * 1000) |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
104 if not events: |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
105 print >> sys.stderr, '[timed out after %d seconds]' % self.timeout |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
106 os.kill(self.pid, signal.SIGHUP) |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
107 return '' |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
108 return os.read(self.cfd, 1024) |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
109 |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
110 def receive(self): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
111 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
|
112 while True: |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
113 try: |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
114 if self.verbose: |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
115 sys.stderr.write('< ') |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
116 s = self.read() |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
117 except OSError, err: |
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
118 if err.errno == errno.EIO: |
103 | 119 return '', '' |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
120 raise |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
121 if self.verbose: |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
122 print >> sys.stderr, self.debugrepr(s) |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
123 out.write(s) |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
124 s = out.getvalue() |
103 | 125 if s.endswith(self.ps1): |
126 return self.ps1, s.replace('\r\n', '\n')[:-len(self.ps1)] | |
127 if s.endswith(self.ps2): | |
128 return self.ps2, s.replace('\r\n', '\n')[:-len(self.ps2)] | |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
129 |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
130 def sendreceive(self, s): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
131 self.send(s) |
103 | 132 ps, r = self.receive() |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
133 if r.startswith(s): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
134 r = r[len(s):] |
103 | 135 return ps, r |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
136 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
137 def run(self): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 tmpdir = tempfile.mkdtemp(prefix=basename) |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
142 |
136
7b5894fffc37
Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents:
124
diff
changeset
|
143 # remove the marker file that we tell make to use to see if |
7b5894fffc37
Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents:
124
diff
changeset
|
144 # this run succeeded |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
145 maybe_unlink(self.name + '.run') |
136
7b5894fffc37
Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents:
124
diff
changeset
|
146 |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
147 rcfile = os.path.join(tmpdir, '.hgrc') |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
148 rcfp = open(rcfile, 'w') |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
149 print >> rcfp, '[ui]' |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
150 print >> rcfp, "username = Bryan O'Sullivan <bos@serpentine.com>" |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
151 |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
152 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
|
153 rcfp = open(rcfile, 'w') |
103 | 154 print >> rcfp, 'PS1="%s"' % self.ps1 |
155 print >> rcfp, 'PS2="%s"' % self.ps2 | |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
156 print >> rcfp, 'unset HISTFILE' |
172
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
157 path = ['/usr/bin', '/bin'] |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
158 hg = find_path_to('hg') |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
159 if hg and hg not in path: |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
160 path.append(hg) |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
161 def re_export(envar): |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
162 v = os.getenv(envar) |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
163 if v is not None: |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
164 print >> rcfp, 'export ' + envar + '=' + v |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
165 print >> rcfp, 'export PATH=' + ':'.join(path) |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
166 re_export('PYTHONPATH') |
19
187702df428b
Piles of new content for MQ chapter - cookbook stuff.
Bryan O'Sullivan <bos@serpentine.com>
parents:
6
diff
changeset
|
167 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd() |
124
c9aad709bd3a
Document the backout command.
Bryan O'Sullivan <bos@serpentine.com>
parents:
103
diff
changeset
|
168 print >> rcfp, 'export HGMERGE=merge' |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 sys.stderr.flush() |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
178 self.pid, self.cfd = pty.fork() |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
179 if self.pid == 0: |
172
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
180 cmdline = ['/usr/bin/env', '-i', 'bash', '--noediting', |
5f305adeb584
Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents:
168
diff
changeset
|
181 '--noprofile', '--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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 os._exit(0) |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
188 self.poll.register(self.cfd, select.POLLIN | select.POLLERR | |
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
189 select.POLLHUP) |
103 | 190 |
191 prompts = { | |
192 '': '', | |
193 self.ps1: '$', | |
194 self.ps2: '>', | |
195 } | |
196 | |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
197 ignore = [ |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
198 r'\d+:[0-9a-f]{12}', # changeset number:hash |
141
627effec9d4e
More attempts to tidy up regexps for ignoring.
Bryan O'Sullivan <bos@serpentine.com>
parents:
138
diff
changeset
|
199 r'[0-9a-f]{40}', # long changeset hash |
627effec9d4e
More attempts to tidy up regexps for ignoring.
Bryan O'Sullivan <bos@serpentine.com>
parents:
138
diff
changeset
|
200 r'[0-9a-f]{12}', # short changeset hash |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
201 r'^(?:---|\+\+\+) .*', # diff header with dates |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
202 r'^date:.*', # date |
141
627effec9d4e
More attempts to tidy up regexps for ignoring.
Bryan O'Sullivan <bos@serpentine.com>
parents:
138
diff
changeset
|
203 #r'^diff -r.*', # "diff -r" is followed by hash |
138
d374685eb7fa
Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents:
137
diff
changeset
|
204 r'^# Date \d+ \d+', # hg patch header |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
205 ] |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
206 |
138
d374685eb7fa
Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents:
137
diff
changeset
|
207 err = False |
d374685eb7fa
Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents:
137
diff
changeset
|
208 |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
209 try: |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
210 try: |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
211 # eat first prompt string from shell |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
212 self.read() |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
213 # setup env and prompt |
103 | 214 ps, output = self.sendreceive('source %s\n' % rcfile) |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
215 for hunk in self.parse(): |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
216 # is this line a processing instruction? |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
217 m = self.pi_re.match(hunk) |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
218 if m: |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
219 pi, rest = m.groups() |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
220 if pi == 'name': |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
221 self.status('.') |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
222 out = rest |
155
914babdc99c8
run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents:
154
diff
changeset
|
223 if out in ('err', 'lxo', 'out', 'run', 'tmp'): |
914babdc99c8
run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents:
154
diff
changeset
|
224 print >> sys.stderr, ('%s: illegal section ' |
914babdc99c8
run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents:
154
diff
changeset
|
225 'name %r' % |
914babdc99c8
run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents:
154
diff
changeset
|
226 (self.name, out)) |
914babdc99c8
run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents:
154
diff
changeset
|
227 return 1 |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
228 assert os.sep not in out |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
229 if ofp is not None: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
230 ofp.close() |
160 | 231 err |= self.rename_output(ofp_basename, ignore) |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
232 if out: |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
233 ofp_basename = '%s.%s' % (self.name, out) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
234 ofp = open(ofp_basename + '.tmp', 'w') |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
235 else: |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
236 ofp = None |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
237 elif pi == 'ignore': |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
238 ignore.append(rest) |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
239 elif hunk.strip(): |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
240 # it's something we should execute |
103 | 241 newps, output = self.sendreceive(hunk) |
168
f8b5b782e150
Backed out changeset 08a4467f489162c14cf17b94aa771a23dd6f02dc
Bryan O'Sullivan <bos@serpentine.com>
parents:
166
diff
changeset
|
242 if not ofp: |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
243 continue |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
244 # first, print the command we ran |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
245 if not hunk.startswith('#'): |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
246 nl = hunk.endswith('\n') |
103 | 247 hunk = ('%s \\textbf{%s}' % |
248 (prompts[ps], | |
249 tex_escape(hunk.rstrip('\n')))) | |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
250 if nl: hunk += '\n' |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
251 ofp.write(hunk) |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
252 # then its output |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
253 ofp.write(tex_escape(output)) |
103 | 254 ps = newps |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
255 self.status('\n') |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
256 except: |
72
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
257 print >> sys.stderr, '(killed)' |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
258 os.kill(self.pid, signal.SIGKILL) |
72
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
259 pid, rc = os.wait() |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
260 raise |
72
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
261 else: |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
262 try: |
103 | 263 ps, output = self.sendreceive('exit\n') |
138
d374685eb7fa
Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents:
137
diff
changeset
|
264 if ofp is not None: |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
265 ofp.write(output) |
138
d374685eb7fa
Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents:
137
diff
changeset
|
266 ofp.close() |
160 | 267 err |= self.rename_output(ofp_basename, ignore) |
73
9604dd885616
Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents:
72
diff
changeset
|
268 os.close(self.cfd) |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
269 except IOError: |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
270 pass |
79
53427f786a0f
Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents:
78
diff
changeset
|
271 os.kill(self.pid, signal.SIGTERM) |
72
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
272 pid, rc = os.wait() |
160 | 273 err = err or rc |
274 if err: | |
72
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
275 if os.WIFEXITED(rc): |
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
276 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc) |
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
277 elif os.WIFSIGNALED(rc): |
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
278 print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc) |
136
7b5894fffc37
Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents:
124
diff
changeset
|
279 else: |
7b5894fffc37
Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents:
124
diff
changeset
|
280 open(self.name + '.run', 'w') |
160 | 281 return err |
72
12df31afb4e1
Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
71
diff
changeset
|
282 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
|
283 shutil.rmtree(tmpdir) |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
284 |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
285 def rename_output(self, base, ignore): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
286 mangle_re = re.compile('(?:' + '|'.join(ignore) + ')') |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
287 def mangle(s): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
288 return mangle_re.sub('', s) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
289 def matchfp(fp1, fp2): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
290 while True: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
291 s1 = mangle(fp1.readline()) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
292 s2 = mangle(fp2.readline()) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
293 if cmp(s1, s2): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
294 break |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
295 if not s1: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
296 return True |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
297 return False |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
298 |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
299 oldname = base + '.out' |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
300 tmpname = base + '.tmp' |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
301 errname = base + '.err' |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
302 errfp = open(errname, 'w+') |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
303 for line in open(tmpname): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
304 errfp.write(mangle_re.sub('', line)) |
146
65f6f9d18fa1
Oops! I forgot that I need the undoctored output files in the book!
Bryan O'Sullivan <bos@serpentine.com>
parents:
142
diff
changeset
|
305 os.rename(tmpname, base + '.lxo') |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
306 errfp.seek(0) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
307 try: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
308 oldfp = open(oldname) |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
309 except IOError, err: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
310 if err.errno != errno.ENOENT: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
311 raise |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
312 os.rename(errname, oldname) |
160 | 313 return False |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
314 if matchfp(oldfp, errfp): |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
315 os.unlink(errname) |
160 | 316 return False |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
317 else: |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
318 print >> sys.stderr, '\nOutput of %s has changed!' % base |
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
319 os.system('diff -u %s %s 1>&2' % (oldname, errname)) |
138
d374685eb7fa
Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents:
137
diff
changeset
|
320 return True |
137
9d7dffe74b2c
Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents:
136
diff
changeset
|
321 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
322 def main(path='.'): |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
323 opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose']) |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
324 verbose = False |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
325 for o, a in opts: |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
326 if o in ('-v', '--verbose'): |
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
327 verbose = True |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
328 errs = 0 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
329 if args: |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
330 for a in args: |
75
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
331 try: |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
332 st = os.lstat(a) |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
333 except OSError, err: |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
334 print >> sys.stderr, '%s: %s' % (a, err.strerror) |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
335 errs += 1 |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
336 continue |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
337 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
338 if example(a, verbose).run(): |
75
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
339 errs += 1 |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
340 else: |
2bfa2499e971
Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents:
73
diff
changeset
|
341 print >> sys.stderr, '%s: not a file, or not executable' % a |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
342 errs += 1 |
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
343 return errs |
164
8f4c9ae918af
Run examples in predictable order if we're doing the lot.
Bryan O'Sullivan <bos@serpentine.com>
parents:
160
diff
changeset
|
344 names = os.listdir(path) |
8f4c9ae918af
Run examples in predictable order if we're doing the lot.
Bryan O'Sullivan <bos@serpentine.com>
parents:
160
diff
changeset
|
345 names.sort() |
8f4c9ae918af
Run examples in predictable order if we're doing the lot.
Bryan O'Sullivan <bos@serpentine.com>
parents:
160
diff
changeset
|
346 for name in names: |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
347 if name == 'run-example' or name.startswith('.'): continue |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
348 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
|
349 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
|
350 pathname = os.path.join(path, name) |
142
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
351 try: |
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
352 st = os.lstat(pathname) |
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
353 except OSError, err: |
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
354 # could be an output file that was removed while we ran |
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
355 if err.errno != errno.ENOENT: |
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
356 raise |
d9f332f85673
Handle YA corner case.
Bryan O'Sullivan <bos@serpentine.com>
parents:
141
diff
changeset
|
357 continue |
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
|
358 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: |
78
a893de25bc24
Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents:
77
diff
changeset
|
359 if example(pathname, verbose).run(): |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
360 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
|
361 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
|
362 return errs |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
363 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
364 if __name__ == '__main__': |
71
ddf533d41c09
Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents:
70
diff
changeset
|
365 sys.exit(main()) |