load: Parse a badly put - sign correctly
[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 from optparse import OptionParser
11
12 def strip_file(filename):
13     with open(filename) as fobj:
14         data = fobj.read()
15     with open(filename, 'w') as fobj:
16         fobj.write(data.strip())
17
18 def open_files(outdir, i, name):
19     basename = '%02d_%s' % (i, name)
20     print basename
21     input_path = os.path.join(outdir, basename + '.in')
22     output_path = os.path.join(outdir, basename + '.out')
23     return open(input_path, 'w'), open(output_path, 'w')
24
25 def main():
26     parser = OptionParser('usage: %prog [options] inputfile outputdir')
27     parser.add_option('--strip', help='strip whitespace from input',
28                       action='store_true', default=False)
29     options, args = parser.parse_args()
30
31     if len(args) != 2:
32         parser.print_help()
33         return 2
34
35     infile = os.path.normpath(args[0])
36     outdir = os.path.normpath(args[1])
37
38     if not os.path.exists(outdir):
39         print >>sys.stderr, 'output directory %r does not exist!' % outdir
40         return 1
41
42     n = 0
43     current = None
44     input, output = None, None
45
46     for line in open(infile):
47         if line.startswith('==== '):
48             n += 1
49             if input is not None and output is not None:
50                 input.close()
51                 output.close()
52                 if options.strip:
53                     strip_file(input.name)
54             input, output = open_files(outdir, n, line[5:line.find(' ====\n')])
55             current = input
56         elif line == '====\n':
57             current = output
58         else:
59             current.write(line)
60
61     if input is not None and output is not None:
62         input.close()
63         output.close()
64
65     print >>sys.stderr, "%s: %d test cases" % (infile, n)
66
67 if __name__ == '__main__':
68     sys.exit(main() or 0)