Debian Codegraph @

ansible_collections/hetzner/hcloud/.azure-pipelines/scripts/combine-coverage.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """
3 Combine coverage data from multiple jobs, keeping the data only from the most recent attempt from each job.
4 Coverage artifacts must be named using the format: "Coverage $(System.JobAttempt) {StableUniqueNameForEachJob}"
5 The recommended coverage artifact name format is: Coverage $(System.JobAttempt) $(System.StageDisplayName) $(System.JobDisplayName)
6 Keep in mind that Azure Pipelines does not enforce unique job display names (only names).
7 It is up to pipeline authors to avoid name collisions when deviating from the recommended format.
8 """
9
10 from __future__ import annotations
11
12 import os
13 import re
14 import shutil
15 import sys
16
17
18 def main():
19 """Main program entry point."""
20 source_directory = sys.argv[1]
21
22 if "/ansible_collections/" in os.getcwd():
23 output_path = "tests/output"
24 else:
25 output_path = "test/results"
26
27 destination_directory = os.path.join(output_path, "coverage")
28
29 if not os.path.exists(destination_directory):
30 os.makedirs(destination_directory)
31
32 jobs = {}
33 count = 0
34
35 for name in os.listdir(source_directory):
36 match = re.search("^Coverage (?P<attempt>[0-9]+) (?P<label>.+)$", name)
37 label = match.group("label")
38 attempt = int(match.group("attempt"))
39 jobs[label] = max(attempt, jobs.get(label, 0))
40
41 for label, attempt in jobs.items():
42 name = f"Coverage {attempt} {label}"
43 source = os.path.join(source_directory, name)
44 source_files = os.listdir(source)
45
46 for source_file in source_files:
47 source_path = os.path.join(source, source_file)
48 destination_path = os.path.join(destination_directory, source_file + "." + label)
49 print(f'"{source_path}" -> "{destination_path}"')
50 shutil.copyfile(source_path, destination_path)
51 count += 1
52
53 print("Coverage file count: %d" % count)
54 print("##vso[task.setVariable variable=coverageFileCount]%d" % count)
55 print("##vso[task.setVariable variable=outputPath]%s" % output_path)
56
57
58 if __name__ == "__main__":
59 main()