import sys
import os
import traceback
hasMultipleSources = False
createdSources = []
def uncaught_exception_hook(exc_type, exc_value, exc_traceback):
print("Unhandled exception: %s", "".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
sys.exit(3)
def extractSourceName(line):
if line.find("/") > -1:
filePath = line[13: line.rindex("/")]
srcName = line[line.find(":")+2: line.find(" ====")]
return filePath, srcName
return False, line[line.find(":")+2 : line.find(" ====")]
def writeSourceToFile(lines):
filePath, srcName = extractSourceName(lines[0])
if filePath:
os.system("mkdir -p " + filePath)
with open(srcName, mode='a+', encoding='utf8', newline='') as f:
createdSources.append(srcName)
for idx, line in enumerate(lines[1:]):
if line[:12] != "==== Source:":
f.write(line)
else:
writeSourceToFile(lines[1+idx:])
break
if __name__ == '__main__':
filePath = sys.argv[1]
sys.excepthook = uncaught_exception_hook
try:
with open(filePath, mode='r', encoding='utf8', newline='') as f:
lines = f.read().splitlines()
if len(lines) >= 1 and lines[0][:12] == "==== Source:":
hasMultipleSources = True
writeSourceToFile(lines)
if hasMultipleSources:
srcString = ""
for src in createdSources:
srcString += src + ' '
print(srcString)
sys.exit(0)
else:
sys.exit(1)
except UnicodeDecodeError as ude:
print("UnicodeDecodeError in '" + filePath + "': " + str(ude))
print("This is expected for some tests containing invalid utf8 sequences. "
"Exception will be ignored.")
sys.exit(2)