File: test_closure.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (213 lines) | stat: -rw-r--r-- 8,256 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import fnmatch
import functools
import importlib.metadata
import json
import os
import re
import site
from collections.abc import Iterator
from pathlib import Path
from typing import Dict, List, Tuple

import pytest
from packaging.requirements import Requirement

_NESTED_STR_DICT = Dict[str, "_NESTED_STR_DICT"]
_ROOT_PATH = Path(__file__).parents[2]
_LOCKFILE_PATTERN = re.compile(r'^(?P<package>.+)==(?P<version>[^\s\\]+)')


@pytest.fixture()
def awscli_package():
    return Package(name="awscli")


def parse_lockfile(lockfile: Path):
    requirements = {}
    with open(lockfile) as f:
        for line in f.readlines():
            match = _LOCKFILE_PATTERN.match(line)
            if not match:
                continue
            package = match.group("package")
            version = match.group("version")
            requirements[package] = version
    return requirements


class Package:
    def __init__(self, name: str) -> None:
        self.name = name

    @functools.cached_property
    def runtime_dependencies(self) -> "DependencyClosure":
        return self._get_runtime_closure()

    def _get_runtime_closure(self) -> "DependencyClosure":
        closure = DependencyClosure()
        for requirement in self._get_runtime_requirements():
            if self._requirement_applies_to_environment(requirement):
                closure[requirement] = Package(name=requirement.name)
        return closure

    def _get_runtime_requirements(self) -> List[Requirement]:
        req_strings = self._get_distribution(self.name).requires
        if req_strings is None:
            return []
        return [Requirement(req_string) for req_string in req_strings]

    def _requirement_applies_to_environment(
        self, requirement: Requirement
    ) -> bool:
        # Do not include any requirements defined as extras as currently
        # our dependency closure does not use any extras
        if requirement.extras:
            return False
        # Only include requirements where the markers apply to the current
        # environment.
        if requirement.marker and not requirement.marker.evaluate():
            return False
        return True

    def _get_distribution(self, name: str) -> importlib.metadata.Distribution:
        # For v2, we inject our own MetaPathFinder to handle
        # botocore/s3transfer import aliases. However for the typical
        # importlib.metadata.distribution(), it extends the built-in
        # MetaPathFinder to include its own find_distributions() method
        # to search for distribution directories. Read more here:
        # https://docs.python.org/3/library/importlib.metadata.html#extending-the-search-algorithm
        #
        # Our MetaPathFinder class does not implement this method, which
        # causes importlib.metadata.distribution() to not find the "awscli"
        # package. So instead, this helper method is implemented to locate the
        # dist-info directories based off our current site-packages
        # and explicitly provide the directory to avoid needing to use
        # MetaPathFinders and thus avoid this issue.

        # Package names may have a "-". These get converted to "_" for
        # their respective directory names in the site packages directory.
        # Directory names may have a "_" in place of a "." if they were
        # built from source. If the initial normalization fails to match,
        # we try replacing "." with "_" in the package name.
        for c in ['-', '.']:
            snake_case_name = name.replace(c, "_")
            for sitepackages in site.getsitepackages():
                for filename in os.listdir(sitepackages):
                    if fnmatch.fnmatch(
                        filename, f"{snake_case_name}-*.dist-info"
                    ):
                        return importlib.metadata.Distribution.at(
                            os.path.join(sitepackages, filename)
                        )
        raise ValueError(
            f'Could not find .dist-info directory for {snake_case_name}'
        )


class DependencyClosure:
    def __init__(self) -> None:
        self._req_to_package: Dict[Requirement, Package] = {}

    def __setitem__(self, key: Requirement, value: Package) -> None:
        self._req_to_package[key] = value

    def __getitem__(self, key: Requirement) -> Package:
        return self._req_to_package[key]

    def __delitem__(self, key: Requirement) -> None:
        del self._req_to_package[key]

    def __iter__(self) -> Iterator[Requirement]:
        return iter(self._req_to_package)

    def __len__(self) -> int:
        return len(self._req_to_package)

    def walk(self) -> Iterator[Tuple[Requirement, Package]]:
        for req, package in self._req_to_package.items():
            yield req, package
            yield from package.runtime_dependencies.walk()

    def to_dict(self) -> _NESTED_STR_DICT:
        reqs = {}
        for req, package in self._req_to_package.items():
            reqs[str(req)] = package.runtime_dependencies.to_dict()
        return reqs


class TestDependencyClosure:
    def _is_bounded_version_requirement(
        self, requirement: Requirement
    ) -> bool:
        for specifier in requirement.specifier:
            if specifier.operator in ["==", "<=", "<"]:
                return True
        return False

    def _pformat_closure(self, closure: DependencyClosure) -> str:
        return json.dumps(closure.to_dict(), sort_keys=True, indent=2)

    def test_expected_runtime_dependencies(self, awscli_package):
        expected_dependencies = {
            "awscrt",
            "colorama",
            "distro",
            "docutils",
            "jmespath",
            "prompt-toolkit",
            "python-dateutil",
            "ruamel.yaml",
            "ruamel.yaml.clib",
            "six",
            "urllib3",
            "wcwidth",
        }
        actual_dependencies = set()
        for _, package in awscli_package.runtime_dependencies.walk():
            actual_dependencies.add(package.name)
        assert actual_dependencies == expected_dependencies, (
            f"Unexpected dependency found in runtime closure: "
            f"{self._pformat_closure(awscli_package.runtime_dependencies)}"
        )

    def test_expected_unbounded_runtime_dependencies(self, awscli_package):
        expected_unbounded_dependencies = {
            "six",  # Transitive dependency from python-dateutil
            "wcwidth",  # Transitive dependency from prompt-toolkit
        }
        all_dependencies = set()
        bounded_dependencies = set()
        for req, package in awscli_package.runtime_dependencies.walk():
            all_dependencies.add(package.name)
            if self._is_bounded_version_requirement(req):
                bounded_dependencies.add(package.name)
        actual_unbounded_dependencies = all_dependencies - bounded_dependencies
        assert (
            actual_unbounded_dependencies == expected_unbounded_dependencies
        ), (
            f"Unexpected unbounded dependency found in runtime closure: "
            f"{self._pformat_closure(awscli_package.runtime_dependencies)}"
        )

    # Test lockfiles generated with pyproject.toml as a source.
    @pytest.mark.parametrize("filename", ["requirements-docs-lock.txt"])
    def test_lockfile_pins_within_runtime_dependencies_bounds(
        self, filename, awscli_package
    ):
        lockfile = parse_lockfile(Path(_ROOT_PATH, filename))
        for req, _ in awscli_package.runtime_dependencies.walk():
            if req.name not in lockfile:
                continue
            assert req.specifier.contains(lockfile[req.name])