[clang] 5a9aff1 - [analyzer] SATestUpdateDiffs.py: Refactor and add type annotations

Valeriy Savchenko via cfe-commits cfe-commits at lists.llvm.org
Fri May 22 03:52:18 PDT 2020


Author: Valeriy Savchenko
Date: 2020-05-22T13:51:58+03:00
New Revision: 5a9aff12ff3bc68109f41930ec296b7a19cbe76c

URL: https://github.com/llvm/llvm-project/commit/5a9aff12ff3bc68109f41930ec296b7a19cbe76c
DIFF: https://github.com/llvm/llvm-project/commit/5a9aff12ff3bc68109f41930ec296b7a19cbe76c.diff

LOG: [analyzer] SATestUpdateDiffs.py: Refactor and add type annotations

Differential Revision: https://reviews.llvm.org/D80426

Added: 
    

Modified: 
    clang/utils/analyzer/SATestUpdateDiffs.py

Removed: 
    


################################################################################
diff  --git a/clang/utils/analyzer/SATestUpdateDiffs.py b/clang/utils/analyzer/SATestUpdateDiffs.py
index ac2832d40e6e..e89b06dd75eb 100755
--- a/clang/utils/analyzer/SATestUpdateDiffs.py
+++ b/clang/utils/analyzer/SATestUpdateDiffs.py
@@ -3,67 +3,67 @@
 """
 Update reference results for static analyzer.
 """
-from __future__ import absolute_import, division, print_function
-
 import SATestBuild
 
-from subprocess import check_call
 import os
+import shutil
 import sys
 
+from subprocess import check_call
+
 Verbose = 0
 
 
-def runCmd(Command, **kwargs):
-    if Verbose:
-        print("Executing %s" % Command)
-    check_call(Command, shell=True, **kwargs)
+def update_reference_results(project_name: str, build_mode: int):
+    project_info = SATestBuild.ProjectInfo(project_name, build_mode)
+    tester = SATestBuild.ProjectTester(project_info)
+    project_dir = tester.get_project_dir()
 
+    tester.is_reference_build = True
+    ref_results_path = os.path.join(project_dir, tester.get_output_dir())
 
-def updateReferenceResults(ProjName, ProjBuildMode):
-    ProjInfo = SATestBuild.ProjectInfo(ProjName, ProjBuildMode)
-    ProjTester = SATestBuild.ProjectTester(ProjInfo)
-    ProjDir = ProjTester.get_project_dir()
+    tester.is_reference_build = False
+    created_results_path = os.path.join(project_dir, tester.get_output_dir())
 
-    ProjTester.is_reference_build = True
-    RefResultsPath = os.path.join(ProjDir, ProjTester.get_output_dir())
+    if not os.path.exists(created_results_path):
+        print("New results not found, was SATestBuild.py previously run?",
+              file=sys.stderr)
+        sys.exit(1)
 
-    ProjTester.is_reference_build = False
-    CreatedResultsPath = os.path.join(ProjDir, ProjTester.get_output_dir())
+    build_log_path = SATestBuild.get_build_log_path(ref_results_path)
+    build_log_dir = os.path.dirname(os.path.abspath(build_log_path))
 
-    if not os.path.exists(CreatedResultsPath):
-        print("New results not found, was SATestBuild.py "
-              "previously run?", file=sys.stderr)
-        sys.exit(1)
+    os.makedirs(build_log_dir)
+
+    with open(build_log_path, "w+") as build_log_file:
+        def run_cmd(command: str):
+            if Verbose:
+                print(f"Executing {command}")
+            check_call(command, shell=True, stdout=build_log_file)
 
-    BuildLogPath = SATestBuild.get_build_log_path(RefResultsPath)
-    Dirname = os.path.dirname(os.path.abspath(BuildLogPath))
-    runCmd("mkdir -p '%s'" % Dirname)
-    with open(BuildLogPath, "w+") as PBuildLogFile:
         # Remove reference results: in git, and then again for a good measure
         # with rm, as git might not remove things fully if there are empty
         # directories involved.
-        runCmd('git rm -r -q "%s"' % (RefResultsPath,), stdout=PBuildLogFile)
-        runCmd('rm -rf "%s"' % (RefResultsPath,), stdout=PBuildLogFile)
+        run_cmd(f"git rm -r -q '{ref_results_path}'")
+        shutil.rmtree(ref_results_path)
 
         # Replace reference results with a freshly computed once.
-        runCmd('cp -r "%s" "%s"' % (CreatedResultsPath, RefResultsPath,),
-               stdout=PBuildLogFile)
+        shutil.copytree(created_results_path, ref_results_path, symlinks=True)
 
         # Run cleanup script.
-        SATestBuild.run_cleanup_script(ProjDir, PBuildLogFile)
+        SATestBuild.run_cleanup_script(project_dir, build_log_file)
 
         SATestBuild.normalize_reference_results(
-            ProjDir, RefResultsPath, ProjBuildMode)
+            project_dir, ref_results_path, build_mode)
 
         # Clean up the generated 
diff erence results.
-        SATestBuild.cleanup_reference_results(RefResultsPath)
+        SATestBuild.cleanup_reference_results(ref_results_path)
 
-        runCmd('git add "%s"' % (RefResultsPath,), stdout=PBuildLogFile)
+        run_cmd(f"git add '{ref_results_path}'")
 
 
 def main(argv):
-    if len(argv) == 2 and argv[1] in ('-h', '--help'):
+    if len(argv) == 2 and argv[1] in ("-h", "--help"):
         print("Update static analyzer reference results based "
               "\non the previous run of SATestBuild.py.\n"
               "\nN.B.: Assumes that SATestBuild.py was just run",
@@ -71,8 +71,8 @@ def main(argv):
         sys.exit(1)
 
     with open(SATestBuild.get_project_map_path(), "r") as f:
-        for ProjName, ProjBuildMode in SATestBuild.get_projects(f):
-            updateReferenceResults(ProjName, int(ProjBuildMode))
+        for project_name, build_mode in SATestBuild.get_projects(f):
+            update_reference_results(project_name, int(build_mode))
 
 
 if __name__ == '__main__':


        


More information about the cfe-commits mailing list