hgbook

view en/examples/run-example @ 3:906d9021f9e5

Making progress on autogenerated example output.
author Bryan O'Sullivan <bos@serpentine.com>
date Sat Jun 24 17:42:40 2006 -0700 (2006-06-24)
parents
children 33a2e7b9978d
line source
1 #!/usr/bin/python
3 import cStringIO
4 import os
5 import pty
6 import re
7 import sys
9 class example:
10 def __init__(self, name):
11 self.name = name
13 def parse(self):
14 fp = open(self.name)
15 cfp = cStringIO.StringIO()
16 for line in fp:
17 cfp.write(line)
18 if not line.rstrip().endswith('\\'):
19 yield cfp.getvalue()
20 cfp.seek(0)
21 cfp.truncate()
23 name_re = re.compile('#\s*name:\s*(.*)$')
25 def status(self, s):
26 sys.stdout.write(s)
27 if not s.endswith('\n'):
28 sys.stdout.flush()
30 def run(self):
31 ofp = None
32 self.status('running %s ' % os.path.basename(self.name))
33 for hunk in self.parse():
34 m = self.name_re.match(hunk)
35 if m:
36 self.status('.')
37 out = m.group(1)
38 assert os.sep not in out
39 if out:
40 ofp = open('%s.%s.out' % (self.name, out), 'w')
41 else:
42 ofp = None
43 elif ofp: ofp.write(hunk)
44 self.status('\n')
46 def main(path='.'):
47 args = sys.argv[1:]
48 if args:
49 for a in args:
50 example(a).run()
51 return
52 for name in os.listdir(path):
53 if name == 'run-example' or name.startswith('.'): continue
54 if name.endswith('.out') or name.endswith('~'): continue
55 example(os.path.join(path, name)).run()
57 if __name__ == '__main__':
58 main()