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