| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import pathlib |
| 5 |
import re |
| 6 |
import sys |
| 7 |
|
| 8 |
|
| 9 |
def main(): |
| 10 |
constraints_path = 'test/lib/ansible_test/_data/requirements/constraints.txt' |
| 11 |
|
| 12 |
requirements = {} |
| 13 |
|
| 14 |
for path in sys.argv[1:] or sys.stdin.read().splitlines(): |
| 15 |
if path == 'test/lib/ansible_test/_data/requirements/ansible.txt': |
| 16 |
# This file is an exact copy of the ansible requirements.txt and should not conflict with other constraints. |
| 17 |
continue |
| 18 |
|
| 19 |
with open(path, 'r') as path_fd: |
| 20 |
requirements[path] = parse_requirements(path_fd.read().splitlines()) |
| 21 |
|
| 22 |
if path == 'test/lib/ansible_test/_data/requirements/ansible-test.txt': |
| 23 |
# Special handling is required for ansible-test's requirements file. |
| 24 |
check_ansible_test(path, requirements.pop(path)) |
| 25 |
continue |
| 26 |
|
| 27 |
frozen_sanity = {} |
| 28 |
non_sanity_requirements = set() |
| 29 |
|
| 30 |
for path, requirements in requirements.items(): |
| 31 |
filename = os.path.basename(path) |
| 32 |
|
| 33 |
is_sanity = filename.startswith('sanity.') or filename.endswith('.requirements.txt') |
| 34 |
is_constraints = path == constraints_path |
| 35 |
|
| 36 |
for lineno, line, requirement in requirements: |
| 37 |
if not requirement: |
| 38 |
print('%s:%d:%d: cannot parse requirement: %s' % (path, lineno, 1, line)) |
| 39 |
continue |
| 40 |
|
| 41 |
name = requirement.group('name').lower() |
| 42 |
raw_constraints = requirement.group('constraints') |
| 43 |
constraints = raw_constraints.strip() |
| 44 |
comment = requirement.group('comment') |
| 45 |
|
| 46 |
is_pinned = re.search('^ *== *[0-9.]+(rc[0-9]+)?(\\.post[0-9]+)?$', constraints) or re.search( |
| 47 |
r'^ *@ https://github.com/[a-z]+/[a-z]+/archive/[0-9a-f]{40}.zip$', constraints |
| 48 |
) |
| 49 |
|
| 50 |
if is_sanity: |
| 51 |
sanity = frozen_sanity.setdefault(name, []) |
| 52 |
sanity.append((path, lineno, line, requirement)) |
| 53 |
elif not is_constraints: |
| 54 |
non_sanity_requirements.add(name) |
| 55 |
|
| 56 |
if is_sanity: |
| 57 |
if not is_pinned: |
| 58 |
# sanity test requirements must be pinned |
| 59 |
print('%s:%d:%d: sanity test requirement (%s%s) must be frozen (use `==`)' % (path, lineno, 1, name, raw_constraints)) |
| 60 |
|
| 61 |
continue |
| 62 |
|
| 63 |
if constraints and not is_constraints: |
| 64 |
allow_constraints = 'sanity_ok' in comment |
| 65 |
|
| 66 |
if not allow_constraints: |
| 67 |
# keeping constraints for tests other than sanity tests in one file helps avoid conflicts |
| 68 |
print('%s:%d:%d: put the constraint (%s%s) in `%s`' % (path, lineno, 1, name, raw_constraints, constraints_path)) |
| 69 |
|
| 70 |
|
| 71 |
def check_ansible_test(path: str, requirements: list[tuple[int, str, re.Match]]) -> None: |
| 72 |
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent.parent.joinpath('lib'))) |
| 73 |
|
| 74 |
from ansible_test._internal.coverage_util import COVERAGE_VERSIONS |
| 75 |
from ansible_test._internal.util import version_to_str |
| 76 |
|
| 77 |
expected_lines = { |
| 78 |
f"coverage == {item.coverage_version} ; python_version >= '{version_to_str(item.min_python)}' and python_version <= '{version_to_str(item.max_python)}'" |
| 79 |
for item in COVERAGE_VERSIONS |
| 80 |
} |
| 81 |
|
| 82 |
for idx, requirement in enumerate(requirements): |
| 83 |
lineno, line, match = requirement |
| 84 |
|
| 85 |
if line in expected_lines: |
| 86 |
expected_lines.remove(line) |
| 87 |
continue |
| 88 |
|
| 89 |
print('%s:%d:%d: unexpected line: %s' % (path, lineno, 1, line)) |
| 90 |
|
| 91 |
for expected_line in sorted(expected_lines): |
| 92 |
print('%s:%d:%d: missing line: %s' % (path, requirements[-1][0] + 1, 1, expected_line)) |
| 93 |
|
| 94 |
|
| 95 |
def parse_requirements(lines): |
| 96 |
# see https://www.python.org/dev/peps/pep-0508/#names |
| 97 |
pattern = re.compile( |
| 98 |
pattern=r'^(?P<name>[A-Z0-9][A-Z0-9._-]*[A-Z0-9]|[A-Z0-9])(?P<extras> *\[[^]]*])?(?P<constraints>[^;#]*)(?P<markers>[^#]*)(?P<comment>.*)$', |
| 99 |
flags=re.IGNORECASE, |
| 100 |
) |
| 101 |
|
| 102 |
matches = [(lineno, line, pattern.search(line)) for lineno, line in enumerate(lines, start=1)] |
| 103 |
requirements = [] |
| 104 |
|
| 105 |
for lineno, line, match in matches: |
| 106 |
if not line.strip(): |
| 107 |
continue |
| 108 |
|
| 109 |
if line.strip().startswith('#'): |
| 110 |
continue |
| 111 |
|
| 112 |
if line.startswith('git+https://'): |
| 113 |
continue # hack to ignore git requirements |
| 114 |
|
| 115 |
requirements.append((lineno, line, match)) |
| 116 |
|
| 117 |
return requirements |
| 118 |
|
| 119 |
|
| 120 |
if __name__ == '__main__': |
| 121 |
main() |