hgbook
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/en/examples/run-example Sat Jun 24 17:42:40 2006 -0700 1.3 @@ -0,0 +1,58 @@ 1.4 +#!/usr/bin/python 1.5 + 1.6 +import cStringIO 1.7 +import os 1.8 +import pty 1.9 +import re 1.10 +import sys 1.11 + 1.12 +class example: 1.13 + def __init__(self, name): 1.14 + self.name = name 1.15 + 1.16 + def parse(self): 1.17 + fp = open(self.name) 1.18 + cfp = cStringIO.StringIO() 1.19 + for line in fp: 1.20 + cfp.write(line) 1.21 + if not line.rstrip().endswith('\\'): 1.22 + yield cfp.getvalue() 1.23 + cfp.seek(0) 1.24 + cfp.truncate() 1.25 + 1.26 + name_re = re.compile('#\s*name:\s*(.*)$') 1.27 + 1.28 + def status(self, s): 1.29 + sys.stdout.write(s) 1.30 + if not s.endswith('\n'): 1.31 + sys.stdout.flush() 1.32 + 1.33 + def run(self): 1.34 + ofp = None 1.35 + self.status('running %s ' % os.path.basename(self.name)) 1.36 + for hunk in self.parse(): 1.37 + m = self.name_re.match(hunk) 1.38 + if m: 1.39 + self.status('.') 1.40 + out = m.group(1) 1.41 + assert os.sep not in out 1.42 + if out: 1.43 + ofp = open('%s.%s.out' % (self.name, out), 'w') 1.44 + else: 1.45 + ofp = None 1.46 + elif ofp: ofp.write(hunk) 1.47 + self.status('\n') 1.48 + 1.49 +def main(path='.'): 1.50 + args = sys.argv[1:] 1.51 + if args: 1.52 + for a in args: 1.53 + example(a).run() 1.54 + return 1.55 + for name in os.listdir(path): 1.56 + if name == 'run-example' or name.startswith('.'): continue 1.57 + if name.endswith('.out') or name.endswith('~'): continue 1.58 + example(os.path.join(path, name)).run() 1.59 + 1.60 +if __name__ == '__main__': 1.61 + main()