forked from fhh2626/BFEE2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonTools.py
75 lines (58 loc) · 2.2 KB
/
commonTools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
def checkExistence(files):
"""check the existence of a series of files,
print unexisted files
Args:
files (str): path of files
"""
exist = True
for name in files:
if not os.path.exists(name):
print(f'Error! {name} does not exist!')
exist = False
print('File existence check finished!')
return exist
def compareTwoFiles(refFile, targetFile):
"""compare two files, print different lines
Args:
refFile (str): path of reference file
targetFile (str): path of target file
Returns:
bool: whether the two files are the same
"""
ref = open(refFile, 'r')
target = open(targetFile, 'r')
same = True
lineCount = 0
while True:
refLine = ref.readline()
targetLine = target.readline()
if not refLine:
break
if refLine.strip() != targetLine.strip():
#print(f'Warning! Lines {lineCount} of {refFile} and {targetFile} are not the same!')
#print(f'The reference is {refLine}')
#print(f'The target is {targetLine}')
same = False
lineCount += 1
#print(f'Comparison between {refFile} and {targetFile} finished!')
ref.close()
target.close()
return same
def checkAllFiles(refPath, targetPath, files):
"""check the existence of all the generated files, compare them with the references one by one,
print all the differences
Args:
refPath (str): path of reference folder'
targetPath (str): path of target folder'
files (str): files to compare, relative to '{targetPath}/BFEE' and '{refPath}'
"""
allFilesExist = checkExistence([f'{targetPath}/BFEE/{name}' for name in files])
if allFilesExist:
for name in files:
#print(f'Comparing {targetPath}/BFEE/{name} with {refPath}/{name}')
if not compareTwoFiles(f'{refPath}/{name}', f'{targetPath}/BFEE/{name}'):
print(f'Warning, {targetPath}/BFEE/{name} and {refPath}/{name} are not the same!')
else:
print(f'Error! some files do not exist!')
print(f'Checking finished!')