2009-07-10 02:01:40 +08:00
|
|
|
#!/usr/bin/python
|
2009-07-28 16:01:52 +08:00
|
|
|
#
|
|
|
|
# Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
|
|
|
|
#
|
|
|
|
# Jansson is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the MIT license. See LICENSE for details.
|
2009-07-10 02:01:40 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2009-09-08 21:32:47 +08:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
def strip_file(filename):
|
|
|
|
with open(filename) as fobj:
|
|
|
|
data = fobj.read()
|
|
|
|
with open(filename, 'w') as fobj:
|
|
|
|
fobj.write(data.strip())
|
2009-07-10 02:01:40 +08:00
|
|
|
|
2009-07-27 03:44:11 +08:00
|
|
|
def open_files(outdir, i, name):
|
|
|
|
basename = '%02d_%s' % (i, name)
|
|
|
|
print basename
|
|
|
|
input_path = os.path.join(outdir, basename + '.in')
|
|
|
|
output_path = os.path.join(outdir, basename + '.out')
|
|
|
|
return open(input_path, 'w'), open(output_path, 'w')
|
2009-07-10 02:01:40 +08:00
|
|
|
|
|
|
|
def main():
|
2009-09-08 21:32:47 +08:00
|
|
|
parser = OptionParser('usage: %prog [options] inputfile outputdir')
|
|
|
|
parser.add_option('--strip', help='strip whitespace from input',
|
|
|
|
action='store_true', default=False)
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) != 2:
|
|
|
|
parser.print_help()
|
2009-07-10 02:01:40 +08:00
|
|
|
return 2
|
|
|
|
|
2009-09-08 21:32:47 +08:00
|
|
|
infile = os.path.normpath(args[0])
|
|
|
|
outdir = os.path.normpath(args[1])
|
2009-07-10 02:01:40 +08:00
|
|
|
|
|
|
|
if not os.path.exists(outdir):
|
|
|
|
print >>sys.stderr, 'output directory %r does not exist!' % outdir
|
|
|
|
return 1
|
|
|
|
|
2009-07-27 03:44:11 +08:00
|
|
|
n = 0
|
|
|
|
current = None
|
|
|
|
input, output = None, None
|
2009-07-10 02:01:40 +08:00
|
|
|
|
|
|
|
for line in open(infile):
|
2009-07-27 03:44:11 +08:00
|
|
|
if line.startswith('==== '):
|
|
|
|
n += 1
|
|
|
|
if input is not None and output is not None:
|
|
|
|
input.close()
|
|
|
|
output.close()
|
2009-09-08 21:32:47 +08:00
|
|
|
if options.strip:
|
|
|
|
strip_file(input.name)
|
2009-07-27 03:44:11 +08:00
|
|
|
input, output = open_files(outdir, n, line[5:line.find(' ====\n')])
|
2009-07-10 02:01:40 +08:00
|
|
|
current = input
|
2009-07-27 03:44:11 +08:00
|
|
|
elif line == '====\n':
|
|
|
|
current = output
|
2009-07-10 02:01:40 +08:00
|
|
|
else:
|
|
|
|
current.write(line)
|
|
|
|
|
2009-07-27 03:44:11 +08:00
|
|
|
if input is not None and output is not None:
|
|
|
|
input.close()
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
print >>sys.stderr, "%s: %d test cases" % (infile, n)
|
2009-07-10 02:01:40 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main() or 0)
|