forked from ansible/ansible-core-test-container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freeze.py
executable file
·56 lines (38 loc) · 1.87 KB
/
freeze.py
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
#!/usr/bin/env python
"""Freeze container requirements for use with a final container build."""
from __future__ import annotations
import argparse
import os
import re
import subprocess
import typing as t
def main() -> None:
"""Main entry point."""
parser = argparse.ArgumentParser()
parser.add_argument('--container-runtime', default='docker', required=False)
parser.add_argument('container')
args = parser.parse_args()
container = args.container
container_runtime = args.container_runtime
print("Purging existing frozen requirements.")
for name in os.listdir('freeze'):
if name != '.freeze.txt':
os.remove(os.path.join('freeze', name))
print("Building a container to freeze the requirements.")
subprocess.run([container_runtime, 'build', '-t', container, '.'], check=True)
print('Finding supported Python versions.')
names = subprocess.run([container_runtime, 'run', container, 'ls', '/usr/bin'], check=True, capture_output=True, text=True).stdout.splitlines()
matches = [re.match(r'^python(?P<version>[0-9]+\.[0-9]+)$', name) for name in names]
versions = [match.group('version') for match in matches if match]
for version in sorted(versions, key=str_to_version):
print(f'Freezing requirements for Python {version}.')
command = [container_runtime, 'run', container, f'/usr/bin/python{version}', '-m', 'pip.__main__', 'freeze', '-qqq', '--disable-pip-version-check']
freeze = subprocess.run(command, check=True, capture_output=True, text=True).stdout
with open(f'freeze/{version}.txt', 'w') as freeze_file:
freeze_file.write(freeze)
print("Freezing completed.")
def str_to_version(version: str) -> t.Tuple[int, ...]:
"""Return a version tuple from a version string."""
return tuple(int(n) for n in version.split('.'))
if __name__ == '__main__':
main()