Skip to content

Commit 4902ca6

Browse files
[analyzer] SATestBuild.py: Refactor and add type annotations
Summary: SATest scripts should be more python-style than they are now. This includes better architecture, type annotations, naming convesions, and up-to-date language features. This commit starts with two scripts SATestBuild and SATestAdd. Differential Revision: https://reviews.llvm.org/D80423
1 parent 053b063 commit 4902ca6

File tree

3 files changed

+699
-595
lines changed

3 files changed

+699
-595
lines changed

clang/utils/analyzer/SATestAdd.py

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,75 +42,80 @@
4242
diff -ur CachedSource PatchedSource \
4343
> changes_for_analyzer.patch
4444
"""
45-
from __future__ import absolute_import, division, print_function
4645
import SATestBuild
4746

48-
import os
4947
import csv
48+
import os
5049
import sys
5150

51+
from typing import IO
5252

53-
def isExistingProject(PMapFile, projectID):
54-
PMapReader = csv.reader(PMapFile)
55-
for ProjectInfo in PMapReader:
56-
if projectID == ProjectInfo[0]:
57-
return True
58-
return False
5953

60-
61-
def addNewProject(ID, BuildMode):
54+
def add_new_project(name: str, build_mode: int):
6255
"""
6356
Add a new project for testing: build it and add to the Project Map file.
64-
:param ID: is a short string used to identify a project.
57+
:param name: is a short string used to identify a project.
6558
"""
6659

67-
CurDir = os.path.abspath(os.curdir)
68-
Dir = SATestBuild.getProjectDir(ID)
69-
if not os.path.exists(Dir):
70-
print("Error: Project directory is missing: %s" % Dir)
60+
project_info = SATestBuild.ProjectInfo(name, build_mode,
61+
is_reference_build=True)
62+
tester = SATestBuild.ProjectTester(project_info)
63+
64+
project_dir = tester.get_project_dir()
65+
if not os.path.exists(project_dir):
66+
print(f"Error: Project directory is missing: {project_dir}")
7167
sys.exit(-1)
7268

7369
# Build the project.
74-
# TODO: Repair this call. We give it a wrong amount wrong arguments and it
75-
# is not trivial to construct argparse arguments in here.
76-
# Requires refactoring of the 'testProject' function.
77-
SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True)
70+
tester.test()
7871

79-
# Add the project ID to the project map.
80-
ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
72+
# Add the project name to the project map.
73+
project_map_path = SATestBuild.get_project_map_path(should_exist=False)
8174

82-
if os.path.exists(ProjectMapPath):
83-
FileMode = "r+"
75+
if os.path.exists(project_map_path):
76+
file_mode = "r+"
8477
else:
85-
print("Warning: Creating the Project Map file!!")
86-
FileMode = "w+"
78+
print("Warning: Creating the project map file!")
79+
file_mode = "w+"
8780

88-
with open(ProjectMapPath, FileMode) as PMapFile:
89-
if (isExistingProject(PMapFile, ID)):
90-
print('Warning: Project with ID \'', ID,
91-
'\' already exists.', file=sys.stdout)
81+
with open(project_map_path, file_mode) as map_file:
82+
if is_existing_project(map_file, name):
83+
print(f"Warning: Project with name '{name}' already exists.",
84+
file=sys.stdout)
9285
print("Reference output has been regenerated.", file=sys.stdout)
9386
else:
94-
PMapWriter = csv.writer(PMapFile)
95-
PMapWriter.writerow((ID, int(BuildMode)))
96-
print("The project map is updated: ", ProjectMapPath)
87+
map_writer = csv.writer(map_file)
88+
map_writer.writerow((name, build_mode))
89+
print(f"The project map is updated: {project_map_path}")
90+
9791

92+
def is_existing_project(map_file: IO, project_name: str) -> bool:
93+
map_reader = csv.reader(map_file)
9894

95+
for raw_info in map_reader:
96+
if project_name == raw_info[0]:
97+
return True
98+
99+
return False
100+
101+
102+
# TODO: Use argparse
99103
# TODO: Add an option not to build.
100104
# TODO: Set the path to the Repository directory.
101-
if __name__ == '__main__':
102-
if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
103-
print('Add a new project for testing to the analyzer'
104-
'\nUsage: ', sys.argv[0],
105-
'project_ID <mode>\n'
106-
'mode: 0 for single file project, '
107-
'1 for scan_build, '
108-
'2 for single file c++11 project', file=sys.stderr)
105+
if __name__ == "__main__":
106+
if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"):
107+
print("Add a new project for testing to the analyzer"
108+
"\nUsage: ", sys.argv[0],
109+
"project_ID <mode>\n"
110+
"mode: 0 for single file project, "
111+
"1 for scan_build, "
112+
"2 for single file c++11 project", file=sys.stderr)
109113
sys.exit(-1)
110114

111-
BuildMode = 1
112-
if (len(sys.argv) >= 3):
113-
BuildMode = int(sys.argv[2])
114-
assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2))
115+
build_mode = 1
116+
if len(sys.argv) >= 3:
117+
build_mode = int(sys.argv[2])
118+
119+
assert((build_mode == 0) | (build_mode == 1) | (build_mode == 2))
115120

116-
addNewProject(sys.argv[1], BuildMode)
121+
add_new_project(sys.argv[1], build_mode)

0 commit comments

Comments
 (0)