-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·86 lines (60 loc) · 2.53 KB
/
update.py
File metadata and controls
executable file
·86 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
"""Update requirements from the ansible/ansible repository."""
from __future__ import annotations
import argparse
import json
import os
import urllib.request
def main() -> None:
"""Main program entry point."""
parser = argparse.ArgumentParser()
parser.add_argument('--branch')
parser.add_argument('--ref')
args = parser.parse_args()
branch = args.branch
ref = args.ref
if not branch:
with open('files/ansible-test-branch.txt') as file:
branch = file.read().strip()
if not ref:
with urllib.request.urlopen(f'https://api.github.com/repos/ansible/ansible/branches/{branch}') as response:
data = json.load(response)
ref = data['commit']['sha']
with open('files/ansible-test-branch.txt', 'w') as file:
file.write(f'{branch}\n')
with open('files/ansible-test-ref.txt', 'w') as file:
file.write(f'{ref}\n')
with urllib.request.urlopen(f'https://api.github.com/repos/ansible/ansible/contents/test/lib/ansible_test/_data/requirements?ref={ref}') as response:
files = json.loads(response.read().decode())
files.append(dict(
name='constants.py',
download_url=f'https://raw.githubusercontent.com/ansible/ansible/{ref}/test/lib/ansible_test/_util/target/common/constants.py',
))
requirements_dir = 'requirements'
untouched_paths = set(os.path.join(requirements_dir, file) for file in os.listdir(requirements_dir))
for file in files:
name = file['name']
download_url = file['download_url']
if name.startswith('sanity.') and (name.endswith('.txt') or name.endswith('.in')):
continue # sanity test requirements are installed by ansible-test's --prime-venvs option
path = os.path.join(requirements_dir, name)
if path in untouched_paths:
untouched_paths.remove(path)
with urllib.request.urlopen(download_url) as response:
latest_contents = response.read().decode()
if os.path.exists(path):
with open(path, 'r') as contents_fd:
current_contents = contents_fd.read()
else:
current_contents = ''
if latest_contents == current_contents:
print('%s: current' % path)
continue
with open(path, 'w') as contents_fd:
contents_fd.write(latest_contents)
print('%s: updated' % path)
for path in untouched_paths:
os.unlink(path)
print('%s: deleted' % path)
if __name__ == '__main__':
main()