Clang Project

clang_source_code/utils/analyzer/SATestUpdateDiffs.py
1#!/usr/bin/env python
2
3"""
4Update reference results for static analyzer.
5"""
6from __future__ import absolute_import, division, print_function
7
8import SATestBuild
9
10from subprocess import check_call
11import os
12import sys
13
14Verbose = 0
15
16
17def runCmd(Command, **kwargs):
18    if Verbose:
19        print("Executing %s" % Command)
20    check_call(Command, shell=True, **kwargs)
21
22
23def updateReferenceResults(ProjName, ProjBuildMode):
24    ProjDir = SATestBuild.getProjectDir(ProjName)
25
26    RefResultsPath = os.path.join(
27        ProjDir,
28        SATestBuild.getSBOutputDirName(IsReferenceBuild=True))
29    CreatedResultsPath = os.path.join(
30        ProjDir,
31        SATestBuild.getSBOutputDirName(IsReferenceBuild=False))
32
33    if not os.path.exists(CreatedResultsPath):
34        print("New results not found, was SATestBuild.py "\
35                             "previously run?", file=sys.stderr)
36        sys.exit(1)
37
38    BuildLogPath = SATestBuild.getBuildLogPath(RefResultsPath)
39    Dirname = os.path.dirname(os.path.abspath(BuildLogPath))
40    runCmd("mkdir -p '%s'" % Dirname)
41    with open(BuildLogPath, "wb+") as PBuildLogFile:
42        # Remove reference results: in git, and then again for a good measure
43        # with rm, as git might not remove things fully if there are empty
44        # directories involved.
45        runCmd('git rm -r -q "%s"' % (RefResultsPath,), stdout=PBuildLogFile)
46        runCmd('rm -rf "%s"' % (RefResultsPath,), stdout=PBuildLogFile)
47
48        # Replace reference results with a freshly computed once.
49        runCmd('cp -r "%s" "%s"' % (CreatedResultsPath, RefResultsPath,),
50               stdout=PBuildLogFile)
51
52        # Run cleanup script.
53        SATestBuild.runCleanupScript(ProjDir, PBuildLogFile)
54
55        SATestBuild.normalizeReferenceResults(
56            ProjDir, RefResultsPath, ProjBuildMode)
57
58        # Clean up the generated difference results.
59        SATestBuild.cleanupReferenceResults(RefResultsPath)
60
61        runCmd('git add "%s"' % (RefResultsPath,), stdout=PBuildLogFile)
62
63
64def main(argv):
65    if len(argv) == 2 and argv[1] in ('-h', '--help'):
66        print("Update static analyzer reference results based "\
67                             "\non the previous run of SATestBuild.py.\n"\
68                             "\nN.B.: Assumes that SATestBuild.py was just run", file=sys.stderr)
69        sys.exit(1)
70
71    with SATestBuild.projectFileHandler() as f:
72        for (ProjName, ProjBuildMode) in SATestBuild.iterateOverProjects(f):
73            updateReferenceResults(ProjName, int(ProjBuildMode))
74
75
76if __name__ == '__main__':
77    main(sys.argv)
78