annotate en/examples/run-example @ 773:3b640272a966

Progres on resolve
author Bryan O'Sullivan <bos@serpentine.com>
date Sun, 12 Apr 2009 00:05:30 -0700
parents c82ff69f0935
children 019040fbf5f5
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
680
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
10 import glob
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
11 import os
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
12 import pty
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
13 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
14 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
15 import shutil
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
16 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
17 import stat
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
18 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
19 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
20 import time
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
21
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
22 xml_subs = {
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
23 '<': '&lt;',
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
24 '>': '&gt;',
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
25 '&': '&amp;',
77
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
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
28 def gensubs(s):
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
29 start = 0
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
30 for i, c in enumerate(s):
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
31 sub = xml_subs.get(c)
77
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
32 if sub:
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
33 yield s[start:i]
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
34 start = i + 1
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
35 yield sub
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
36 yield s[start:]
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
37
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
38 def xml_escape(s):
77
773f4a9e7975 Fix escaping of backslashes. Finally!
Bryan O'Sullivan <bos@serpentine.com>
parents: 75
diff changeset
39 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
40
137
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
41 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
42 try:
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
43 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
44 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
45 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
46 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
47 raise
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
48 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
49
172
5f305adeb584 Try to tighten up the run environment to make things more reproducible.
Bryan O'Sullivan <bos@serpentine.com>
parents: 168
diff changeset
50 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
51 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
52 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
53 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
54 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
55 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
56
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
57 def result_name(name):
680
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
58 return os.path.normpath(os.path.join('results', name.replace(os.sep, '-')))
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
59
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
60 def wopen(name):
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
61 path = os.path.dirname(name)
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
62 if path:
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
63 try:
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
64 os.makedirs(path)
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
65 except OSError, err:
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
66 if err.errno != errno.EEXIST:
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
67 raise
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
68 return open(name, 'w')
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
69
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
70 class example:
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
71 entities = dict.fromkeys(l.rstrip() for l in open('auto-snippets.xml'))
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
72
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
73 def __init__(self, name, verbose, keep_change):
680
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
74 self.name = os.path.normpath(name)
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
75 self.verbose = verbose
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
76 self.keep_change = keep_change
680
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
77
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
78 def status(self, s):
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
79 sys.stdout.write(s)
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
80 if not s.endswith('\n'):
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
81 sys.stdout.flush()
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
82
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
83 def rename_output(self, base, ignore=[]):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
84 mangle_re = re.compile('(?:' + '|'.join(ignore) + ')')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
85 def mangle(s):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
86 return mangle_re.sub('', s)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
87 def matchfp(fp1, fp2):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
88 while True:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
89 s1 = mangle(fp1.readline())
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
90 s2 = mangle(fp2.readline())
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
91 if cmp(s1, s2):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
92 break
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
93 if not s1:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
94 return True
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
95 return False
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
96
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
97 oldname = result_name(base + '.out')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
98 tmpname = result_name(base + '.tmp')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
99 errname = result_name(base + '.err')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
100 errfp = open(errname, 'w+')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
101 for line in open(tmpname):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
102 errfp.write(mangle_re.sub('', line))
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
103 os.rename(tmpname, result_name(base + '.lxo'))
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
104 errfp.seek(0)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
105 try:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
106 oldfp = open(oldname)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
107 except IOError, err:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
108 if err.errno != errno.ENOENT:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
109 raise
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
110 os.rename(errname, oldname)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
111 return False
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
112 if matchfp(oldfp, errfp):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
113 os.unlink(errname)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
114 return False
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
115 else:
680
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
116 print >> sys.stderr, '\nOutput of %s has changed!' % base
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
117 if self.keep_change:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
118 os.rename(errname, oldname)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
119 return False
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
120 else:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
121 os.system('diff -u %s %s 1>&2' % (oldname, errname))
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
122 return True
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
123
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
124 class static_example(example):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
125 def run(self):
680
8366882f67f2 Fix up more formatting goop
Bryan O'Sullivan <bos@serpentine.com>
parents: 679
diff changeset
126 self.status('running %s\n' % self.name)
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
127 s = open(self.name).read().rstrip()
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
128 s = s.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
129 ofp = wopen(result_name(self.name + '.tmp'))
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
130 ofp.write('<!-- BEGIN %s -->\n' % self.name)
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
131 ofp.write('<programlisting>')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
132 ofp.write(s)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
133 ofp.write('</programlisting>\n')
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
134 ofp.write('<!-- END %s -->\n' % self.name)
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
135 ofp.close()
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
136 self.rename_output(self.name)
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
137 norm = self.name.replace(os.sep, '-')
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
138 example.entities[
689
a2923aa93da9 Fix which files are included in the generated XML.
Bryan O'Sullivan <bos@serpentine.com>
parents: 680
diff changeset
139 '<!ENTITY %s SYSTEM "results/%s.lxo">' % (norm, norm)] = 1
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
140
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
141
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
142 class shell_example(example):
70
365b41b6a15e bash is not necessarily in /bin.
Bryan O'Sullivan <bos@serpentine.com>
parents: 68
diff changeset
143 shell = '/usr/bin/env bash'
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
144 ps1 = '__run_example_ps1__ '
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
145 ps2 = '__run_example_ps2__ '
168
f8b5b782e150 Backed out changeset 08a4467f489162c14cf17b94aa771a23dd6f02dc
Bryan O'Sullivan <bos@serpentine.com>
parents: 166
diff changeset
146 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
147
192
06ab90119fa6 Be more conservative in timeouts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 173
diff changeset
148 timeout = 10
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
149
644
d8913b7869b5 Add --keep option to run-example
Bryan O'Sullivan <bos@serpentine.com>
parents: 258
diff changeset
150 def __init__(self, name, verbose, keep_change):
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
151 example.__init__(self, name, verbose, keep_change)
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
152 self.poll = select.poll()
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
153
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
154 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
155 '''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
156 fp = open(self.name)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
157 cfp = cStringIO.StringIO()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
158 for line in fp:
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
159 cfp.write(line)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
160 if not line.rstrip().endswith('\\'):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
161 yield cfp.getvalue()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
162 cfp.seek(0)
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
163 cfp.truncate()
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
164
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
165 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
166 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
167 print >> sys.stderr, '>', self.debugrepr(s)
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
168 while s:
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
169 count = os.write(self.cfd, s)
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
170 s = s[count:]
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
171
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
172 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
173 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
174 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
175 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
176 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
177 else:
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
178 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
179
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
180 timeout = 5
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
181
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
182 def read(self, hint):
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
183 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
184 if not events:
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
185 print >> sys.stderr, ('[%stimed out after %d seconds]' %
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
186 (hint, self.timeout))
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
187 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
188 return ''
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
189 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
190
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
191 def receive(self, hint):
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
192 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
193 while True:
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
194 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
195 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
196 sys.stderr.write('< ')
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
197 s = self.read(hint)
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
198 except OSError, err:
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
199 if err.errno == errno.EIO:
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
200 return '', ''
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
201 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
202 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
203 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
204 out.write(s)
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
205 s = out.getvalue()
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
206 if s.endswith(self.ps1):
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
207 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
208 if s.endswith(self.ps2):
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
209 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
210
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
211 def sendreceive(self, s, hint):
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
212 self.send(s)
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
213 ps, r = self.receive(hint)
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
214 if r.startswith(s):
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
215 r = r[len(s):]
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
216 return ps, r
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
217
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
218 def run(self):
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
219 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
220 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
221 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
222 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
223
136
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
224 # 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
225 # 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
226 maybe_unlink(self.name + '.run')
136
7b5894fffc37 Don't falsely signal success to make.
Bryan O'Sullivan <bos@serpentine.com>
parents: 124
diff changeset
227
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
228 rcfile = os.path.join(tmpdir, '.hgrc')
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
229 rcfp = wopen(rcfile)
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
230 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
231 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
232
6
69d90ab9fd80 Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4
diff changeset
233 rcfile = os.path.join(tmpdir, '.bashrc')
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
234 rcfp = wopen(rcfile)
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
235 print >> rcfp, 'PS1="%s"' % self.ps1
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 re_export('PYTHONPATH')
19
187702df428b Piles of new content for MQ chapter - cookbook stuff.
Bryan O'Sullivan <bos@serpentine.com>
parents: 6
diff changeset
248 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd()
124
c9aad709bd3a Document the backout command.
Bryan O'Sullivan <bos@serpentine.com>
parents: 103
diff changeset
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 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
262 '--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
263 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
264 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
265 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
266 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
267 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
268 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
269 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
270 select.POLLHUP)
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
271
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
272 prompts = {
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
273 '': '',
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
274 self.ps1: '$',
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
275 self.ps2: '>',
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
276 }
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
277
137
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
278 ignore = [
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
279 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
280 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
281 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
282 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
283 r'^date:.*', # date
141
627effec9d4e More attempts to tidy up regexps for ignoring.
Bryan O'Sullivan <bos@serpentine.com>
parents: 138
diff changeset
284 #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
285 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
286 ]
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
287
138
d374685eb7fa Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents: 137
diff changeset
288 err = False
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
289 read_hint = ''
138
d374685eb7fa Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents: 137
diff changeset
290
4
33a2e7b9978d Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 3
diff changeset
291 try:
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
292 try:
73
9604dd885616 Fix run-example script on Debian.
Bryan O'Sullivan <bos@serpentine.com>
parents: 72
diff changeset
293 # eat first prompt string from shell
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
294 self.read(read_hint)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
295 # setup env and prompt
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
296 ps, output = self.sendreceive('source %s\n' % rcfile,
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
297 read_hint)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
298 for hunk in self.parse():
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
299 # is this line a processing instruction?
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
300 m = self.pi_re.match(hunk)
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
301 if m:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
302 pi, rest = m.groups()
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
303 if pi == 'name':
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
304 self.status('.')
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
305 out = rest
155
914babdc99c8 run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents: 154
diff changeset
306 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
307 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
308 'name %r' %
914babdc99c8 run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents: 154
diff changeset
309 (self.name, out))
914babdc99c8 run-example: better error if bogus section name found.
Bryan O'Sullivan <bos@serpentine.com>
parents: 154
diff changeset
310 return 1
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
311 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
312 if ofp is not None:
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
313 ofp.write('</screen>\n')
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
314 ofp.write('<!-- END %s -->\n' % ofp_basename)
137
9d7dffe74b2c Save "good" example output so we can see if something has broken.
Bryan O'Sullivan <bos@serpentine.com>
parents: 136
diff changeset
315 ofp.close()
160
745ff473c8c4 Catch more errors.
Bryan O'Sullivan <bos@serpentine.com>
parents: 155
diff changeset
316 err |= self.rename_output(ofp_basename, ignore)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
317 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
318 ofp_basename = '%s.%s' % (self.name, out)
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
319 norm = os.path.normpath(ofp_basename)
773
3b640272a966 Progres on resolve
Bryan O'Sullivan <bos@serpentine.com>
parents: 708
diff changeset
320 norm = norm.replace(os.sep, '-')
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
321 example.entities[
668
60ee738fdc0e Fix quoting of entity declarations.
Bryan O'Sullivan <bos@serpentine.com>
parents: 665
diff changeset
322 '<!ENTITY interaction.%s '
689
a2923aa93da9 Fix which files are included in the generated XML.
Bryan O'Sullivan <bos@serpentine.com>
parents: 680
diff changeset
323 'SYSTEM "results/%s.lxo">'
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
324 % (norm, norm)] = 1
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
325 read_hint = ofp_basename + ' '
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
326 ofp = wopen(result_name(ofp_basename + '.tmp'))
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
327 ofp.write('<!-- BEGIN %s -->\n' % ofp_basename)
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
328 ofp.write('<screen>')
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
329 else:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
330 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
331 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
332 ignore.append(rest)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
333 elif hunk.strip():
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
334 # it's something we should execute
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
335 newps, output = self.sendreceive(hunk, read_hint)
168
f8b5b782e150 Backed out changeset 08a4467f489162c14cf17b94aa771a23dd6f02dc
Bryan O'Sullivan <bos@serpentine.com>
parents: 166
diff changeset
336 if not ofp:
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
337 continue
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
338 # first, print the command we ran
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
339 if not hunk.startswith('#'):
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
340 nl = hunk.endswith('\n')
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
341 hunk = ('<prompt>%s</prompt> '
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
342 '<userinput>%s</userinput>' %
103
5b80c922ebdd More merge content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 83
diff changeset
343 (prompts[ps],
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
344 xml_escape(hunk.rstrip('\n'))))
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
345 if nl: hunk += '\n'
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
346 ofp.write(hunk)
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
347 # then its output
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
348 ofp.write(xml_escape(output))
773
3b640272a966 Progres on resolve
Bryan O'Sullivan <bos@serpentine.com>
parents: 708
diff changeset
349 ps = newps
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
350 self.status('\n')
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
351 except:
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
352 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
353 os.kill(self.pid, signal.SIGKILL)
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
354 pid, rc = os.wait()
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
355 raise
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
356 else:
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
357 try:
173
754312dc23d5 If something times out, try to tell what it was.
Bryan O'Sullivan <bos@serpentine.com>
parents: 172
diff changeset
358 ps, output = self.sendreceive('exit\n', read_hint)
138
d374685eb7fa Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents: 137
diff changeset
359 if ofp is not None:
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
360 ofp.write(output)
663
0d5935744f87 Switch from LaTeX to XML for examples.
Bryan O'Sullivan <bos@serpentine.com>
parents: 644
diff changeset
361 ofp.write('</screen>\n')
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
362 ofp.write('<!-- END %s -->\n' % ofp_basename)
138
d374685eb7fa Handle a few more common regexps. Clean up on exit nicely.
Bryan O'Sullivan <bos@serpentine.com>
parents: 137
diff changeset
363 ofp.close()
160
745ff473c8c4 Catch more errors.
Bryan O'Sullivan <bos@serpentine.com>
parents: 155
diff changeset
364 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
365 os.close(self.cfd)
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
366 except IOError:
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
367 pass
79
53427f786a0f Make run-example time out if shell seems to get stuck.
Bryan O'Sullivan <bos@serpentine.com>
parents: 78
diff changeset
368 os.kill(self.pid, signal.SIGTERM)
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
369 pid, rc = os.wait()
160
745ff473c8c4 Catch more errors.
Bryan O'Sullivan <bos@serpentine.com>
parents: 155
diff changeset
370 err = err or rc
745ff473c8c4 Catch more errors.
Bryan O'Sullivan <bos@serpentine.com>
parents: 155
diff changeset
371 if err:
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
372 if os.WIFEXITED(rc):
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
373 print >> sys.stderr, '(exit %s)' % os.WEXITSTATUS(rc)
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
374 elif os.WIFSIGNALED(rc):
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
375 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
376 else:
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
377 wopen(result_name(self.name + '.run'))
160
745ff473c8c4 Catch more errors.
Bryan O'Sullivan <bos@serpentine.com>
parents: 155
diff changeset
378 return err
72
12df31afb4e1 Propagate exceptions more correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 71
diff changeset
379 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
380 shutil.rmtree(tmpdir)
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
381
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
382 def print_help(exit, msg=None):
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
383 if msg:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
384 print >> sys.stderr, 'Error:', msg
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
385 print >> sys.stderr, 'Usage: run-example [options] [test...]'
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
386 print >> sys.stderr, 'Options:'
664
8a9c66da6fcb Fix thinko
Bryan O'Sullivan <bos@serpentine.com>
parents: 663
diff changeset
387 print >> sys.stderr, ' -a --all run all examples in this directory'
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
388 print >> sys.stderr, ' -h --help print this help message'
708
c82ff69f0935 Add markers to flag included snippets of code.
Bryan O'Sullivan <bos@serpentine.com>
parents: 689
diff changeset
389 print >> sys.stderr, ' --keep keep new output as desired output'
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
390 print >> sys.stderr, ' -v --verbose display extra debug output'
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
391 sys.exit(exit)
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
392
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
393 def main(path='.'):
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
394 if os.path.realpath(path).split(os.sep)[-1] != 'examples':
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
395 print >> sys.stderr, 'Not being run from the examples directory!'
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
396 sys.exit(1)
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
397
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
398 opts, args = getopt.getopt(sys.argv[1:], '?ahv',
644
d8913b7869b5 Add --keep option to run-example
Bryan O'Sullivan <bos@serpentine.com>
parents: 258
diff changeset
399 ['all', 'help', 'keep', 'verbose'])
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
400 verbose = False
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
401 run_all = False
644
d8913b7869b5 Add --keep option to run-example
Bryan O'Sullivan <bos@serpentine.com>
parents: 258
diff changeset
402 keep_change = False
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
403
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
404 for o, a in opts:
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
405 if o in ('-h', '-?', '--help'):
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
406 print_help(0)
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
407 if o in ('-a', '--all'):
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
408 run_all = True
644
d8913b7869b5 Add --keep option to run-example
Bryan O'Sullivan <bos@serpentine.com>
parents: 258
diff changeset
409 if o in ('--keep',):
d8913b7869b5 Add --keep option to run-example
Bryan O'Sullivan <bos@serpentine.com>
parents: 258
diff changeset
410 keep_change = True
78
a893de25bc24 Add -v option to run-example, to assist with debugging example scripts.
Bryan O'Sullivan <bos@serpentine.com>
parents: 77
diff changeset
411 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
412 verbose = True
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
413 errs = 0
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
414 if args:
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
415 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
416 try:
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
417 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
418 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
419 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
420 errs += 1
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
421 continue
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
422 if stat.S_ISREG(st.st_mode):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
423 if st.st_mode & 0111:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
424 if shell_example(a, verbose, keep_change).run():
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
425 errs += 1
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
426 elif a.endswith('.lst'):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
427 static_example(a, verbose, keep_change).run()
75
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
428 else:
2bfa2499e971 Make run-example print better error messages when things go wrong.
Bryan O'Sullivan <bos@serpentine.com>
parents: 73
diff changeset
429 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
430 errs += 1
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
431 elif run_all:
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
432 names = glob.glob("*") + glob.glob("app*/*") + glob.glob("ch*/*")
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
433 names.sort()
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
434 for name in names:
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
435 if name == 'run-example' or name.endswith('~'): continue
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
436 pathname = os.path.join(path, name)
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
437 try:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
438 st = os.lstat(pathname)
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
439 except OSError, err:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
440 # could be an output file that was removed while we ran
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
441 if err.errno != errno.ENOENT:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
442 raise
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
443 continue
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
444 if stat.S_ISREG(st.st_mode):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
445 if st.st_mode & 0111:
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
446 if shell_example(pathname, verbose, keep_change).run():
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
447 errs += 1
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
448 elif pathname.endswith('.lst'):
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
449 static_example(pathname, verbose, keep_change).run()
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
450 print >> wopen(os.path.join(path, '.run')), time.asctime()
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
451 else:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
452 print_help(1, msg='no test names given, and --all not provided')
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
453
679
80928ea6e7ae Add the ability to include text files and have them XML-mangled.
Bryan O'Sullivan <bos@serpentine.com>
parents: 668
diff changeset
454 fp = wopen('auto-snippets.xml')
665
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
455 for key in sorted(example.entities.iterkeys()):
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
456 print >> fp, key
27043f385f3f Get autogeneration of examples more or less working.
Bryan O'Sullivan <bos@serpentine.com>
parents: 664
diff changeset
457 fp.close()
71
ddf533d41c09 Propagate errors correctly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 70
diff changeset
458 return errs
3
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
459
906d9021f9e5 Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
460 if __name__ == '__main__':
258
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
461 try:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
462 sys.exit(main())
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
463 except KeyboardInterrupt:
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
464 print >> sys.stderr, 'interrupted!'
1a55ba6ceca1 Make run-example a bit more user friendly.
Bryan O'Sullivan <bos@serpentine.com>
parents: 192
diff changeset
465 sys.exit(1)