Debian Codegraph @

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

raw
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 (absolute_import, division, print_function)
11 __metaclass__ = type
12
13 import os
14 import re
15 import shutil
16 import sys
17
18
19 def main():
20 """Main program entry point."""
21 source_directory = sys.argv[1]
22
23 if '/ansible_collections/' in os.getcwd():
24 output_path = "tests/output"
25 else:
26 output_path = "test/results"
27
28 destination_directory = os.path.join(output_path, 'coverage')
29
30 if not os.path.exists(destination_directory):
31 os.makedirs(destination_directory)
32
33 jobs = {}
34 count = 0
35
36 for name in os.listdir(source_directory):
37 match = re.search('^Coverage (?P<attempt>[0-9]+) (?P<label>.+)$', name)
38 label = match.group('label')
39 attempt = int(match.group('attempt'))
40 jobs[label] = max(attempt, jobs.get(label, 0))
41
42 for label, attempt in jobs.items():
43 name = 'Coverage {attempt} {label}'.format(label=label, attempt=attempt)
44 source = os.path.join(source_directory, name)
45 source_files = os.listdir(source)
46
47 for source_file in source_files:
48 source_path = os.path.join(source, source_file)
49 destination_path = os.path.join(destination_directory, source_file + '.' + label)
50 print('"%s" -> "%s"' % (source_path, destination_path))
51 shutil.copyfile(source_path, destination_path)
52 count += 1
53
54 print('Coverage file count: %d' % count)
55 print('##vso[task.setVariable variable=coverageFileCount]%d' % count)
56 print('##vso[task.setVariable variable=outputPath]%s' % output_path)
57
58
59 if __name__ == '__main__':
60 main()