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

Don't falsely signal success to make.
author Bryan O'Sullivan <bos@serpentine.com>
date Mon, 05 Mar 2007 20:26:23 -0800
parents c9aad709bd3a
children 9d7dffe74b2c
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
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
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
40 class example:
70
365b41b6a15e bash is not necessarily in /bin.
Bryan O'Sullivan <bos@serpentine.com>
parents: 68
diff changeset
41 shell = '/usr/bin/env bash'
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
42 ps1 = '__run_example_ps1__ '
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
43 ps2 = '__run_example_ps2__ '
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
44 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
45
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
46 timeout = 5
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
47
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
48 def __init__(self, name, verbose):
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
49 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
50 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
51 self.poll = select.poll()
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
52
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
53 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
54 '''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
55 fp = open(self.name)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
56 cfp = cStringIO.StringIO()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
57 for line in fp:
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
58 cfp.write(line)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
59 if not line.rstrip().endswith('\\'):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
60 yield cfp.getvalue()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
61 cfp.seek(0)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
62 cfp.truncate()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
63
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
64 def status(self, s):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
65 sys.stdout.write(s)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
66 if not s.endswith('\n'):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
67 sys.stdout.flush()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
68
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
69 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
70 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
71 print >> sys.stderr, '>', self.debugrepr(s)
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
72 while s:
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
73 count = os.write(self.cfd, s)
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
74 s = s[count:]
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
75
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
76 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
77 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
78 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
79 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
80 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
81 else:
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
82 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
83
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
84 timeout = 5
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
85
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
86 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
87 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
88 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
89 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
90 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
91 return ''
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
92 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
93
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
94 def receive(self):
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
95 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
96 while True:
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
97 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
98 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
99 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
100 s = self.read()
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
101 except OSError, err:
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
102 if err.errno == errno.EIO:
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
103 return '', ''
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
104 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
105 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
106 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
107 out.write(s)
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
108 s = out.getvalue()
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
109 if s.endswith(self.ps1):
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
110 return self.ps1, s.replace('\r\n', '\n')[:-len(self.ps1)]
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
111 if s.endswith(self.ps2):
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
112 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
113
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
114 def sendreceive(self, s):
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
115 self.send(s)
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
116 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
117 if r.startswith(s):
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
118 r = r[len(s):]
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
119 return ps, r
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
120
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
121 def run(self):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
122 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
123 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
124 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
125 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
126
136
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
127 # 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
128 # this run succeeded
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
129 try:
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
130 os.unlink(self.name + '.run')
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
131 except OSError, err:
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
132 if err.errno != errno.ENOENT:
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
133 raise
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
134
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
135 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
136 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
137 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
138 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
139
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
140 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
141 rcfp = open(rcfile, 'w')
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
142 print >> rcfp, 'PS1="%s"' % self.ps1
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
143 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
144 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
145 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
124
c9aad709bd3a Document the backout command.
Bryan O'Sullivan <bos@serpentine.com>
parents: 103
diff changeset
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153 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
154 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
155 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
156 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
157 if self.pid == 0:
70
365b41b6a15e bash is not necessarily in /bin.
Bryan O'Sullivan <bos@serpentine.com>
parents: 68
diff changeset
158 cmdline = ['/usr/bin/env', 'bash', '--noediting', '--noprofile',
365b41b6a15e bash is not necessarily in /bin.
Bryan O'Sullivan <bos@serpentine.com>
parents: 68
diff changeset
159 '--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
160 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
161 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
162 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
163 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
164 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
165 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
166 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
167 select.POLLHUP)
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
168
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
169 prompts = {
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
170 '': '',
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
171 self.ps1: '$',
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
172 self.ps2: '>',
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
173 }
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
174
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 try:
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
176 try:
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
177 # 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
178 self.read()
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
179 # setup env and prompt
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
180 ps, output = self.sendreceive('source %s\n' % rcfile)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
181 for hunk in self.parse():
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
182 # is this line a processing instruction?
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
183 m = self.pi_re.match(hunk)
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
184 if m:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
185 pi, rest = m.groups()
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
186 if pi == 'name':
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
187 self.status('.')
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
188 out = rest
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
189 assert os.sep not in out
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
190 if out:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
191 ofp = open('%s.%s.out' % (self.name, out), 'w')
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
192 else:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
193 ofp = None
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
194 elif hunk.strip():
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
195 # it's something we should execute
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
196 newps, output = self.sendreceive(hunk)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
197 if not ofp:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
198 continue
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
199 # first, print the command we ran
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
200 if not hunk.startswith('#'):
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
201 nl = hunk.endswith('\n')
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
202 hunk = ('%s \\textbf{%s}' %
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
203 (prompts[ps],
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
204 tex_escape(hunk.rstrip('\n'))))
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
205 if nl: hunk += '\n'
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
206 ofp.write(hunk)
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
207 # then its output
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
208 ofp.write(tex_escape(output))
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
209 ps = newps
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
210 self.status('\n')
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
211 except:
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
212 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
213 os.kill(self.pid, signal.SIGKILL)
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
214 pid, rc = os.wait()
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
215 raise
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
216 else:
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
217 try:
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
218 ps, output = self.sendreceive('exit\n')
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
219 if ofp:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
220 ofp.write(output)
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
221 os.close(self.cfd)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
222 except IOError:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
223 pass
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
224 os.kill(self.pid, signal.SIGTERM)
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
225 pid, rc = os.wait()
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
226 if rc:
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
227 if os.WIFEXITED(rc):
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
228 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
229 elif os.WIFSIGNALED(rc):
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
230 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
231 else:
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
232 open(self.name + '.run', 'w')
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
233 return rc
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
234 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
235 shutil.rmtree(tmpdir)
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
236
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
237 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
238 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
239 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
240 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
241 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
242 verbose = True
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
243 errs = 0
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
244 if args:
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
245 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
246 try:
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
247 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
248 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
249 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
250 errs += 1
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
251 continue
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
252 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
253 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
254 errs += 1
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
255 else:
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
256 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
257 errs += 1
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
258 return errs
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
259 for name in os.listdir(path):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
260 if name == 'run-example' or name.startswith('.'): continue
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
261 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
262 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
263 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
264 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
265 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
266 if example(pathname, verbose).run():
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
267 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
268 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
269 return errs
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
270
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
271 if __name__ == '__main__':
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
272 sys.exit(main())