Mercurial > hgbook
diff en/examples/run-example @ 3:906d9021f9e5
Making progress on autogenerated example output.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Sat, 24 Jun 2006 17:42:40 -0700 |
parents | |
children | 33a2e7b9978d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/en/examples/run-example Sat Jun 24 17:42:40 2006 -0700 @@ -0,0 +1,58 @@ +#!/usr/bin/python + +import cStringIO +import os +import pty +import re +import sys + +class example: + def __init__(self, name): + self.name = name + + def parse(self): + fp = open(self.name) + cfp = cStringIO.StringIO() + for line in fp: + cfp.write(line) + if not line.rstrip().endswith('\\'): + yield cfp.getvalue() + cfp.seek(0) + cfp.truncate() + + name_re = re.compile('#\s*name:\s*(.*)$') + + def status(self, s): + sys.stdout.write(s) + if not s.endswith('\n'): + sys.stdout.flush() + + def run(self): + ofp = None + self.status('running %s ' % os.path.basename(self.name)) + for hunk in self.parse(): + m = self.name_re.match(hunk) + if m: + self.status('.') + out = m.group(1) + assert os.sep not in out + if out: + ofp = open('%s.%s.out' % (self.name, out), 'w') + else: + ofp = None + elif ofp: ofp.write(hunk) + self.status('\n') + +def main(path='.'): + args = sys.argv[1:] + if args: + for a in args: + example(a).run() + return + for name in os.listdir(path): + if name == 'run-example' or name.startswith('.'): continue + if name.endswith('.out') or name.endswith('~'): continue + example(os.path.join(path, name)).run() + +if __name__ == '__main__': + main()