Debian Codegraph @

ansible_collections/community/hashi_vault/.github/actions/ansible-codecov/process.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Brian Scholer (@briantist)
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import (absolute_import, division, print_function)
9 __metaclass__ = type
10
11 import sys
12 import subprocess
13 import re
14 import getopt
15 import time
16 from pathlib import Path
17
18
19 def get_flags(pattern, input):
20 patpat = r'\{([^\}]+)\}'
21
22 pats = re.findall(patpat, pattern)
23 matcher = re.sub(patpat, r'(.*?)', pattern)
24 match = re.search(matcher, input)
25
26 if match:
27 return [pats[i].replace('%', result) for i, result in enumerate(match.groups())]
28
29 return None
30
31
32 def main(argv):
33 additional_flags = file_flag_pattern = directory_flag_pattern = directory = fail_on_error = None
34
35 opts, args = getopt.getopt(argv, '', [
36 'directory=',
37 'directory-flag-pattern=',
38 'file-flag-pattern=',
39 'additional-flags=',
40 'fail-on-error',
41 ])
42
43 for opt, arg in opts:
44 if opt == '--directory':
45 directory = arg
46 elif opt == '--directory-flag-pattern':
47 directory_flag_pattern = arg
48 elif opt == '--file-flag-pattern':
49 file_flag_pattern = arg
50 elif opt == '--additional-flags':
51 additional_flags = arg
52 elif opt == '--fail-on-error':
53 fail_on_error = True
54
55 extra_flags = additional_flags.split(',') if additional_flags else []
56
57 flags = {}
58
59 directory = Path(directory) if directory else Path.cwd()
60
61 for f in directory.rglob('*'):
62 if f.is_file():
63 iflags = set()
64 if directory_flag_pattern:
65 for part in f.parent.parts:
66 dflags = get_flags(directory_flag_pattern, part)
67 if dflags:
68 iflags.update(dflags)
69
70 fflags = get_flags(file_flag_pattern, str(f.name))
71 if fflags:
72 iflags.update(fflags)
73
74 for flag in iflags:
75 flags.setdefault(flag, []).append(str(f.resolve()))
76
77 logextra = ' (+%r)' % extra_flags if extra_flags else ''
78
79 for flag, files in flags.items():
80 cmd = ['codecov', 'upload-coverage', '-F', flag]
81 [cmd.extend(['-F', extra]) for extra in extra_flags]
82 [cmd.extend(['-f', file]) for file in files]
83 if fail_on_error:
84 cmd.append('-Z')
85
86 print('::group::Flag: %s%s' % (flag, logextra))
87
88 print('Executing: %r' % cmd)
89
90 max_attempt = 5
91 for attempt in range(1, max_attempt + 1):
92 try:
93 subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, check=True, text=True)
94 except subprocess.CalledProcessError as e:
95 match = re.search(r'Error: There was an error fetching the storage URL during POST: 429.*?time to availability: (?P<tta>\d+)s', e.stdout)
96 if not match:
97 print(f"stdout: {e.stdout}")
98 print(f"stderr: {e.stderr}")
99 raise
100
101 tta = int(match.group('tta')) + 10
102 print((
103 f"::warning title=Codecov upload issue::Codecov tokenless upload from fork failed. "
104 f"Waiting {tta} seconds to try again [attempt {attempt} of {max_attempt}]."
105 ))
106 time.sleep(tta)
107 else:
108 break
109
110 print('::endgroup::')
111
112
113 if __name__ == '__main__':
114 main(sys.argv[1:])