bos@3: #!/usr/bin/python bos@3: bos@3: import cStringIO bos@3: import os bos@3: import pty bos@3: import re bos@3: import sys bos@3: bos@3: class example: bos@3: def __init__(self, name): bos@3: self.name = name bos@3: bos@3: def parse(self): bos@3: fp = open(self.name) bos@3: cfp = cStringIO.StringIO() bos@3: for line in fp: bos@3: cfp.write(line) bos@3: if not line.rstrip().endswith('\\'): bos@3: yield cfp.getvalue() bos@3: cfp.seek(0) bos@3: cfp.truncate() bos@3: bos@3: name_re = re.compile('#\s*name:\s*(.*)$') bos@3: bos@3: def status(self, s): bos@3: sys.stdout.write(s) bos@3: if not s.endswith('\n'): bos@3: sys.stdout.flush() bos@3: bos@3: def run(self): bos@3: ofp = None bos@3: self.status('running %s ' % os.path.basename(self.name)) bos@3: for hunk in self.parse(): bos@3: m = self.name_re.match(hunk) bos@3: if m: bos@3: self.status('.') bos@3: out = m.group(1) bos@3: assert os.sep not in out bos@3: if out: bos@3: ofp = open('%s.%s.out' % (self.name, out), 'w') bos@3: else: bos@3: ofp = None bos@3: elif ofp: ofp.write(hunk) bos@3: self.status('\n') bos@3: bos@3: def main(path='.'): bos@3: args = sys.argv[1:] bos@3: if args: bos@3: for a in args: bos@3: example(a).run() bos@3: return bos@3: for name in os.listdir(path): bos@3: if name == 'run-example' or name.startswith('.'): continue bos@3: if name.endswith('.out') or name.endswith('~'): continue bos@3: example(os.path.join(path, name)).run() bos@3: bos@3: if __name__ == '__main__': bos@3: main()