Created
November 10, 2021 22:48
-
-
Save samdoran/b729827e2654be72d8205320d2745cfb to your computer and use it in GitHub Desktop.
Modify a collection to use the new coverage uploader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Modify a collection to use the new coverage uploader | |
import argparse | |
import pathlib | |
import shutil | |
import sys | |
import urllib.request | |
def parse_arguments(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('repo', type=pathlib.Path) | |
return parser.parse_args() | |
def sanity_check(repo): | |
print("Performing sanity check...") | |
if not repo.joinpath('.azure-pipelines', 'scripts').exists(): | |
sys.exit("This doesn't look like a repo that can be updated") | |
print("OK") | |
def download_new_script(repo): | |
url = 'https://raw.githubusercontent.com/ansible/ansible/devel/.azure-pipelines/scripts/publish-codecov.py' | |
dest = repo / '.azure-pipelines' / 'scripts' / 'publish-codecov.py' | |
if not dest.is_file(): | |
print(f"Dowloading new uploader to {dest}...") | |
with urllib.request.urlopen(url) as resp: | |
with dest.open('w+b') as f: | |
shutil.copyfileobj(resp, f, 64 * 1024) | |
dest.chmod(0o755) | |
def insert_args(repo): | |
new_args = '--group-by command' | |
replacements = [ | |
('aggregate-coverage.sh', 'coverage combine'), | |
('report-coverage.sh', 'coverage xml'), | |
] | |
for file, string in replacements: | |
script_path = repo / '.azure-pipelines' / 'scripts' / file | |
if script_path.is_file(): | |
with script_path.open() as f: | |
data = f.read() | |
if new_args not in data: | |
print(f"Updating {script_path}...") | |
with script_path.open('w') as f: | |
f.write(data.replace(string, f'{string} {new_args}')) | |
def update_template(repo): | |
template_path = repo / '.azure-pipelines' / 'templates' / 'coverage.yml' | |
if template_path.is_file(): | |
with template_path.open() as f: | |
data = f.read() | |
if 'publish-codecov.py' not in data: | |
print(f"Updaating {template_path}...") | |
with template_path.open('w') as f: | |
f.write(data.replace('publish-codecov.sh', 'publish-codecov.py')) | |
def add_ignores(repo): | |
sanity_path = repo / 'tests' / 'sanity' | |
if sanity_path.is_dir(): | |
for file in sanity_path.glob('ignore-*.txt'): | |
version = int(file.stem.split('.')[-1]) # Get the .Y from the version string | |
ignores = [ | |
".azure-pipelines/scripts/publish-codecov.py replace-urlopen", | |
] | |
if version < 12: | |
ignores.extend([ | |
".azure-pipelines/scripts/publish-codecov.py compile-2.6!skip # Uses Python 3.6+ syntax", | |
".azure-pipelines/scripts/publish-codecov.py compile-2.7!skip # Uses Python 3.6+ syntax", | |
".azure-pipelines/scripts/publish-codecov.py compile-3.5!skip # Uses Python 3.6+ syntax", | |
".azure-pipelines/scripts/publish-codecov.py future-import-boilerplate", | |
".azure-pipelines/scripts/publish-codecov.py metaclass-boilerplate", | |
]) | |
with file.open() as f: | |
file_lines = f.readlines() | |
changed = False | |
for line in reversed(ignores): | |
if f"{line}\n" not in file_lines: | |
changed = True | |
file_lines.insert(0, f"{line}\n") | |
if changed: | |
print(f"Updating {file}...") | |
with file.open('w') as f: | |
f.writelines(file_lines) | |
def main(): | |
args = parse_arguments() | |
repo = args.repo.resolve() | |
sanity_check(repo) | |
download_new_script(repo) | |
shell_script = repo.joinpath('.azure-pipelines', 'scripts', 'publish-codecov.sh') | |
try: | |
shell_script.unlink() | |
except FileNotFoundError: | |
pass | |
insert_args(repo) | |
update_template(repo) | |
add_ignores(repo) | |
print('Done! Commit the changes to the repo.') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment