Debian Codegraph @

ansible_collections/community/general/.azure-pipelines/scripts/combine-coverage.py

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