[llvm] [llvm][llvm-lit] Add option to create unique result file names if results already exist (PR #112729)

Paul T Robinson via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 17 19:25:53 PDT 2024


================
@@ -14,11 +16,43 @@ def by_suite_and_test_path(test):
     return (test.suite.name, id(test.suite), test.path_in_suite)
 
 
-class JsonReport(object):
+class Report(object):
     def __init__(self, output_file):
         self.output_file = output_file
+        # Set by the option parser later.
+        self.use_unique_output_file_name = False
 
     def write_results(self, tests, elapsed):
+        if self.use_unique_output_file_name:
+            file = None
+            filepath = self.output_file
+            attempt = 0
+            while file is None:
+                try:
+                    file = open(filepath, "x")
+                except FileExistsError:
+                    attempt += 1
+                    # If there is an extension insert before that because most
+                    # glob patterns for these will be '*.extension'. Otherwise
+                    # add to the end of the path.
+                    path, ext = os.path.splitext(self.output_file)
+                    filepath = path + f".{attempt}" + ext
+
+            with file:
+                self._write_results_to_file(tests, elapsed, file)
+        else:
+            # Overwrite if the results already exist.
+            with open(self.output_file, "w") as file:
+                self._write_results_to_file(tests, elapsed, file)
+
----------------
pogo59 wrote:

Could the `file` variable be moved up above the `if`, then the call to `_write_results_to` could be deduplicated. (That is, the `if` sets up `file` rather than also doing the write... call.)

https://github.com/llvm/llvm-project/pull/112729


More information about the llvm-commits mailing list