fdbe6ba298d63639f4aae86cea0bbe6a2654141d
[jansson.git] / test / split-testfile.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5
6 def open_files(outdir, i, name):
7     basename = '%02d_%s' % (i, name)
8     print basename
9     input_path = os.path.join(outdir, basename + '.in')
10     output_path = os.path.join(outdir, basename + '.out')
11     return open(input_path, 'w'), open(output_path, 'w')
12
13 def main():
14     if len(sys.argv) != 3:
15         print 'usage: %s input-file output-directory' % sys.argv[0]
16         return 2
17
18     infile = os.path.normpath(sys.argv[1])
19     outdir = os.path.normpath(sys.argv[2])
20
21     if not os.path.exists(outdir):
22         print >>sys.stderr, 'output directory %r does not exist!' % outdir
23         return 1
24
25     n = 0
26     current = None
27     input, output = None, None
28
29     for line in open(infile):
30         if line.startswith('==== '):
31             n += 1
32             if input is not None and output is not None:
33                 input.close()
34                 output.close()
35             input, output = open_files(outdir, n, line[5:line.find(' ====\n')])
36             current = input
37         elif line == '====\n':
38             current = output
39         else:
40             current.write(line)
41
42     if input is not None and output is not None:
43         input.close()
44         output.close()
45
46     print >>sys.stderr, "%s: %d test cases" % (infile, n)
47
48 if __name__ == '__main__':
49     sys.exit(main() or 0)