xref: /netbsd-src/external/apache2/llvm/dist/clang/utils/analyzer/SATestUpdateDiffs.py (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg#!/usr/bin/env python
27330f729Sjoerg
37330f729Sjoerg"""
47330f729SjoergUpdate reference results for static analyzer.
57330f729Sjoerg"""
67330f729Sjoergimport SATestBuild
7*e038c9c4Sjoergfrom ProjectMap import ProjectInfo, ProjectMap
8*e038c9c4Sjoerg
9*e038c9c4Sjoergimport os
10*e038c9c4Sjoergimport shutil
11*e038c9c4Sjoergimport sys
127330f729Sjoerg
137330f729Sjoergfrom subprocess import check_call
147330f729Sjoerg
157330f729SjoergVerbose = 0
167330f729Sjoerg
177330f729Sjoerg
18*e038c9c4Sjoergdef update_reference_results(project: ProjectInfo, git: bool = False):
19*e038c9c4Sjoerg    test_info = SATestBuild.TestInfo(project)
20*e038c9c4Sjoerg    tester = SATestBuild.ProjectTester(test_info)
21*e038c9c4Sjoerg    project_dir = tester.get_project_dir()
22*e038c9c4Sjoerg
23*e038c9c4Sjoerg    tester.is_reference_build = True
24*e038c9c4Sjoerg    ref_results_path = tester.get_output_dir()
25*e038c9c4Sjoerg
26*e038c9c4Sjoerg    tester.is_reference_build = False
27*e038c9c4Sjoerg    created_results_path = tester.get_output_dir()
28*e038c9c4Sjoerg
29*e038c9c4Sjoerg    if not os.path.exists(created_results_path):
30*e038c9c4Sjoerg        print(f"Skipping project '{project.name}', "
31*e038c9c4Sjoerg              f"it doesn't have newer results.",
32*e038c9c4Sjoerg              file=sys.stderr)
33*e038c9c4Sjoerg        return
34*e038c9c4Sjoerg
35*e038c9c4Sjoerg    build_log_path = SATestBuild.get_build_log_path(ref_results_path)
36*e038c9c4Sjoerg    build_log_dir = os.path.dirname(os.path.abspath(build_log_path))
37*e038c9c4Sjoerg
38*e038c9c4Sjoerg    os.makedirs(build_log_dir)
39*e038c9c4Sjoerg
40*e038c9c4Sjoerg    with open(build_log_path, "w+") as build_log_file:
41*e038c9c4Sjoerg        def run_cmd(command: str):
427330f729Sjoerg            if Verbose:
43*e038c9c4Sjoerg                print(f"Executing {command}")
44*e038c9c4Sjoerg            check_call(command, shell=True, stdout=build_log_file)
457330f729Sjoerg
467330f729Sjoerg        # Remove reference results: in git, and then again for a good measure
477330f729Sjoerg        # with rm, as git might not remove things fully if there are empty
487330f729Sjoerg        # directories involved.
49*e038c9c4Sjoerg        if git:
50*e038c9c4Sjoerg            run_cmd(f"git rm -r -q '{ref_results_path}'")
51*e038c9c4Sjoerg        shutil.rmtree(ref_results_path)
527330f729Sjoerg
537330f729Sjoerg        # Replace reference results with a freshly computed once.
54*e038c9c4Sjoerg        shutil.copytree(created_results_path, ref_results_path, symlinks=True)
557330f729Sjoerg
567330f729Sjoerg        # Run cleanup script.
57*e038c9c4Sjoerg        SATestBuild.run_cleanup_script(project_dir, build_log_file)
587330f729Sjoerg
59*e038c9c4Sjoerg        SATestBuild.normalize_reference_results(
60*e038c9c4Sjoerg            project_dir, ref_results_path, project.mode)
617330f729Sjoerg
627330f729Sjoerg        # Clean up the generated difference results.
63*e038c9c4Sjoerg        SATestBuild.cleanup_reference_results(ref_results_path)
647330f729Sjoerg
65*e038c9c4Sjoerg        if git:
66*e038c9c4Sjoerg            run_cmd(f"git add '{ref_results_path}'")
677330f729Sjoerg
687330f729Sjoerg
69*e038c9c4Sjoergif __name__ == "__main__":
70*e038c9c4Sjoerg    print("SATestUpdateDiffs.py should not be used on its own.")
71*e038c9c4Sjoerg    print("Please use 'SATest.py update' instead")
727330f729Sjoerg    sys.exit(1)
73