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
|
2020-09-01 00:04:38 +00:00
|
|
|
import traceback
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2020-09-01 00:04:38 +00:00
|
|
|
|
|
|
|
def uncaught_exception_hook(exc_type, exc_value, exc_traceback):
|
|
|
|
# The script `scripts/ASTImportTest.sh` will interpret return code 3
|
|
|
|
# as a critical error (because of the uncaught exception) and will
|
|
|
|
# terminate further execution.
|
|
|
|
print("Unhandled exception: %s", "".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
|
|
|
|
sys.exit(3)
|
|
|
|
|
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
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(" ====")]
|
|
|
|
|
2020-09-01 00:04:38 +00:00
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
# 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)
|
2021-12-21 14:25:14 +00:00
|
|
|
if filePath:
|
2019-09-11 19:16:35 +00:00
|
|
|
os.system("mkdir -p " + filePath)
|
2021-04-26 04:33:14 +00:00
|
|
|
with open(srcName, mode='a+', encoding='utf8', newline='') as f:
|
|
|
|
for idx, line in enumerate(lines[1:]):
|
|
|
|
# write to file
|
|
|
|
if line[:12] != "==== Source:":
|
2023-03-21 18:56:50 +00:00
|
|
|
f.write(line + '\n')
|
2021-04-26 04:33:14 +00:00
|
|
|
|
|
|
|
# recursive call if there is another source
|
|
|
|
else:
|
2023-09-17 15:34:22 +00:00
|
|
|
return [srcName] + writeSourceToFile(lines[1+idx:])
|
2019-09-11 19:16:35 +00:00
|
|
|
|
2023-09-17 15:34:22 +00:00
|
|
|
return [srcName]
|
2020-09-01 00:04:38 +00:00
|
|
|
|
2023-09-17 15:34:22 +00:00
|
|
|
def split_sources(filePath, suppress_output = False):
|
2020-09-01 00:04:38 +00:00
|
|
|
sys.excepthook = uncaught_exception_hook
|
|
|
|
|
|
|
|
try:
|
|
|
|
# decide if file has multiple sources
|
2021-06-30 08:21:41 +00:00
|
|
|
with open(filePath, mode='r', encoding='utf8', newline='') as f:
|
|
|
|
lines = f.read().splitlines()
|
2021-09-17 18:08:49 +00:00
|
|
|
if len(lines) >= 1 and lines[0][:12] == "==== Source:":
|
2020-09-01 00:04:38 +00:00
|
|
|
srcString = ""
|
2023-09-17 15:34:22 +00:00
|
|
|
for src in writeSourceToFile(lines):
|
2020-09-01 00:04:38 +00:00
|
|
|
srcString += src + ' '
|
2023-09-17 15:34:22 +00:00
|
|
|
if not suppress_output:
|
|
|
|
print(srcString)
|
|
|
|
return 0
|
|
|
|
return 1
|
2020-09-01 00:04:38 +00:00
|
|
|
|
|
|
|
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.")
|
2023-09-17 15:34:22 +00:00
|
|
|
return 2
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(split_sources(sys.argv[1]))
|