Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 54 lines (41 sloc) 1.75 KB
#!/usr/bin/python
#
import re, sys, itertools
# Don't match on the end of line marker because some debug traces will
# accidentally append to that line
seperator_regex = re.compile("^==================== ([^$]+) ====================", re.M)
SANITIZE_PAIRS = {
' ' : '',
':' : ''
}
def filename_sanitize(filename):
for from_str, to_str in SANITIZE_PAIRS.items():
filename = filename.replace(from_str, to_str)
return filename
def drop(iterator, n):
return itertools.islice(iterator, n, None)
for filename in sys.argv[1:]:
file = open(filename, 'r')
contents = file.read()
file.close()
overall_index = 1
seen_segments = {}
pending_segment = None
for segment in drop(seperator_regex.split(contents), 1):
segment_stripped = segment.strip()
if pending_segment:
output_filename = filename + '.' + format(overall_index, "0>2") + '.' + filename_sanitize(pending_segment)
output_file = open(output_filename, 'w')
output_file.write(segment_stripped)
output_file.close()
pending_segment = None
overall_index = overall_index + 1
else:
# Work out an index number for the segment, so that duplicate segment
# names do not get written to identical files
index = seen_segments.get(segment_stripped, 0)
seen_segments[segment_stripped] = index + 1
pending_segment = segment_stripped + " - " + str(index)
if len(pending_segment) > 100 or len(pending_segment) == 0:
raise Exception("Pending segment fails sanity check:\n" + pending_segment)
print 'Segment:', pending_segment