xref: /openbsd-src/gnu/llvm/clang/utils/analyzer/SATestUpdateDiffs.py (revision a9ac8606c53d55cee9c3a39778b249c51df111ef)
1e5dd7070Spatrick#!/usr/bin/env python
2e5dd7070Spatrick
3e5dd7070Spatrick"""
4e5dd7070SpatrickUpdate reference results for static analyzer.
5e5dd7070Spatrick"""
6e5dd7070Spatrickimport SATestBuild
7ec727ea7Spatrickfrom ProjectMap import ProjectInfo, ProjectMap
8ec727ea7Spatrick
9ec727ea7Spatrickimport os
10ec727ea7Spatrickimport shutil
11ec727ea7Spatrickimport sys
12e5dd7070Spatrick
13e5dd7070Spatrickfrom subprocess import check_call
14e5dd7070Spatrick
15e5dd7070SpatrickVerbose = 0
16e5dd7070Spatrick
17e5dd7070Spatrick
18*a9ac8606Spatrickdef update_reference_results(project: ProjectInfo, git: bool = False):
19ec727ea7Spatrick    test_info = SATestBuild.TestInfo(project)
20ec727ea7Spatrick    tester = SATestBuild.ProjectTester(test_info)
21ec727ea7Spatrick    project_dir = tester.get_project_dir()
22e5dd7070Spatrick
23ec727ea7Spatrick    tester.is_reference_build = True
24ec727ea7Spatrick    ref_results_path = tester.get_output_dir()
25e5dd7070Spatrick
26ec727ea7Spatrick    tester.is_reference_build = False
27ec727ea7Spatrick    created_results_path = tester.get_output_dir()
28e5dd7070Spatrick
29ec727ea7Spatrick    if not os.path.exists(created_results_path):
30*a9ac8606Spatrick        print(f"Skipping project '{project.name}', "
31*a9ac8606Spatrick              f"it doesn't have newer results.",
32ec727ea7Spatrick              file=sys.stderr)
33*a9ac8606Spatrick        return
34e5dd7070Spatrick
35ec727ea7Spatrick    build_log_path = SATestBuild.get_build_log_path(ref_results_path)
36ec727ea7Spatrick    build_log_dir = os.path.dirname(os.path.abspath(build_log_path))
37ec727ea7Spatrick
38ec727ea7Spatrick    os.makedirs(build_log_dir)
39ec727ea7Spatrick
40ec727ea7Spatrick    with open(build_log_path, "w+") as build_log_file:
41ec727ea7Spatrick        def run_cmd(command: str):
42ec727ea7Spatrick            if Verbose:
43ec727ea7Spatrick                print(f"Executing {command}")
44ec727ea7Spatrick            check_call(command, shell=True, stdout=build_log_file)
45ec727ea7Spatrick
46e5dd7070Spatrick        # Remove reference results: in git, and then again for a good measure
47e5dd7070Spatrick        # with rm, as git might not remove things fully if there are empty
48e5dd7070Spatrick        # directories involved.
49*a9ac8606Spatrick        if git:
50ec727ea7Spatrick            run_cmd(f"git rm -r -q '{ref_results_path}'")
51ec727ea7Spatrick        shutil.rmtree(ref_results_path)
52e5dd7070Spatrick
53e5dd7070Spatrick        # Replace reference results with a freshly computed once.
54ec727ea7Spatrick        shutil.copytree(created_results_path, ref_results_path, symlinks=True)
55e5dd7070Spatrick
56e5dd7070Spatrick        # Run cleanup script.
57ec727ea7Spatrick        SATestBuild.run_cleanup_script(project_dir, build_log_file)
58e5dd7070Spatrick
59ec727ea7Spatrick        SATestBuild.normalize_reference_results(
60ec727ea7Spatrick            project_dir, ref_results_path, project.mode)
61e5dd7070Spatrick
62e5dd7070Spatrick        # Clean up the generated difference results.
63ec727ea7Spatrick        SATestBuild.cleanup_reference_results(ref_results_path)
64e5dd7070Spatrick
65*a9ac8606Spatrick        if git:
66ec727ea7Spatrick            run_cmd(f"git add '{ref_results_path}'")
67e5dd7070Spatrick
68e5dd7070Spatrick
69*a9ac8606Spatrickif __name__ == "__main__":
70*a9ac8606Spatrick    print("SATestUpdateDiffs.py should not be used on its own.")
71*a9ac8606Spatrick    print("Please use 'SATest.py update' instead")
72e5dd7070Spatrick    sys.exit(1)
73