test/.gitignore: Add testprogs/test_simple
[jansson.git] / test / split-testfile.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
4 #
5 # Jansson is free software; you can redistribute it and/or modify
6 # it under the terms of the MIT license. See LICENSE for details.
7
8 import os
9 import sys
10
11 def open_files(outdir, i, name):
12     basename = '%02d_%s' % (i, name)
13     print basename
14     input_path = os.path.join(outdir, basename + '.in')
15     output_path = os.path.join(outdir, basename + '.out')
16     return open(input_path, 'w'), open(output_path, 'w')
17
18 def main():
19     if len(sys.argv) != 3:
20         print 'usage: %s input-file output-directory' % sys.argv[0]
21         return 2
22
23     infile = os.path.normpath(sys.argv[1])
24     outdir = os.path.normpath(sys.argv[2])
25
26     if not os.path.exists(outdir):
27         print >>sys.stderr, 'output directory %r does not exist!' % outdir
28         return 1
29
30     n = 0
31     current = None
32     input, output = None, None
33
34     for line in open(infile):
35         if line.startswith('==== '):
36             n += 1
37             if input is not None and output is not None:
38                 input.close()
39                 output.close()
40             input, output = open_files(outdir, n, line[5:line.find(' ====\n')])
41             current = input
42         elif line == '====\n':
43             current = output
44         else:
45             current.write(line)
46
47     if input is not None and output is not None:
48         input.close()
49         output.close()
50
51     print >>sys.stderr, "%s: %d test cases" % (infile, n)
52
53 if __name__ == '__main__':
54     sys.exit(main() or 0)