Debian Codegraph @

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