2020-01-13 15:14:18 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-09-11 19:16:35 +00:00
|
|
|
#
|
|
|
|
# This script reads a syntaxTest file and writes all
|
|
|
|
# sources into their own files. If one source-name specifies subdirectories
|
|
|
|
# those will be created too.
|
|
|
|
|
|
|
|
# Usage: scripts/splitSources.py pathToTestfile
|
|
|
|
# as a result prints
|
|
|
|
# - string of created files separated by whitespaces
|
|
|
|
# - 'false' if the file only had one source
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
hasMultipleSources = False
|
|
|
|
createdSources = []
|
|
|
|
|
|
|
|
def extractSourceName(line):
|
|
|
|
if line.find("/") > -1:
|
|
|
|
filePath = line[13: line.rindex("/")]
|
|
|
|
# fileName = line[line.rindex("/")+1: line.find(" ====")]
|
|
|
|
srcName = line[line.find(":")+2: line.find(" ====")]
|
|
|
|
return filePath, srcName
|
|
|
|
return False, line[line.find(":")+2 : line.find(" ====")]
|
|
|
|
|
|
|
|
# expects the first line of lines to be "==== Source: sourceName ===="
|
|
|
|
# writes the following source into a file named sourceName
|
|
|
|
def writeSourceToFile(lines):
|
|
|
|
filePath, srcName = extractSourceName(lines[0])
|
2020-01-31 13:26:55 +00:00
|
|
|
# print("sourceName is ", srcName)
|
|
|
|
# print("filePath is", filePath)
|
2019-09-11 19:16:35 +00:00
|
|
|
if filePath != False:
|
|
|
|
os.system("mkdir -p " + filePath)
|
2020-01-23 14:06:47 +00:00
|
|
|
f = open(srcName, mode='a+', encoding='utf8')
|
2019-09-11 19:16:35 +00:00
|
|
|
createdSources.append(srcName)
|
|
|
|
i = 0
|
|
|
|
for idx, line in enumerate(lines[1:]):
|
|
|
|
|
|
|
|
# write to file
|
|
|
|
if line[:12] != "==== Source:":
|
|
|
|
f.write(line)
|
|
|
|
|
|
|
|
# recursive call if there is another source
|
|
|
|
else:
|
|
|
|
writeSourceToFile(lines[1+idx:])
|
|
|
|
break
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
filePath = sys.argv[1]
|
|
|
|
# decide if file has multiple sources
|
2020-01-27 10:39:54 +00:00
|
|
|
lines = open(filePath, mode='r', encoding='utf8').read().splitlines()
|
2019-09-11 19:16:35 +00:00
|
|
|
if lines[0][:12] == "==== Source:":
|
|
|
|
hasMultipleSources = True
|
|
|
|
writeSourceToFile(lines)
|
|
|
|
|
|
|
|
if hasMultipleSources:
|
|
|
|
srcString = ""
|
|
|
|
for src in createdSources:
|
|
|
|
srcString += src + ' '
|
2020-01-13 15:14:18 +00:00
|
|
|
print(srcString)
|
2019-09-11 19:16:35 +00:00
|
|
|
else:
|
|
|
|
sys.exit(1)
|