|
21 | 21 |
|
22 | 22 | import json |
23 | 23 | import re |
24 | | -from typing import Dict, Iterable, List, Optional |
| 24 | +from dataclasses import dataclass |
| 25 | +from typing import Dict, Iterable, List, Optional, Sequence |
25 | 26 |
|
26 | 27 |
|
27 | 28 | def pip_requirements_from_dependency_metadata(dependencies: bytes) -> List[str]: |
@@ -89,3 +90,160 @@ def pip_requirements_satisfied(requirements: Iterable[str], installed_versions: |
89 | 90 | return False |
90 | 91 |
|
91 | 92 | return True |
| 93 | + |
| 94 | + |
| 95 | +@dataclass(frozen=True) |
| 96 | +class DependencyRequirement: |
| 97 | + plugin_name: str |
| 98 | + requirement: str |
| 99 | + |
| 100 | + |
| 101 | +@dataclass(frozen=True) |
| 102 | +class DependencyConflict: |
| 103 | + status: str |
| 104 | + package_name: str |
| 105 | + candidate_requirements: Sequence[DependencyRequirement] |
| 106 | + installed_requirements: Sequence[DependencyRequirement] |
| 107 | + |
| 108 | + |
| 109 | +def _requirement_parts(requirement_text: str): |
| 110 | + # Parse only the active dependency and version specifier forms we can |
| 111 | + # compare safely. See the following specifications for more information: |
| 112 | + # https://packaging.python.org/en/latest/specifications/dependency-specifiers/#dependency-specifiers |
| 113 | + # https://packaging.python.org/en/latest/specifications/version-specifiers/#version-specifiers |
| 114 | + try: |
| 115 | + from packaging.requirements import Requirement |
| 116 | + from packaging.specifiers import Specifier |
| 117 | + from packaging.utils import canonicalize_name |
| 118 | + from packaging.version import Version, InvalidVersion |
| 119 | + except Exception: |
| 120 | + from pip._vendor.packaging.requirements import Requirement |
| 121 | + from pip._vendor.packaging.specifiers import Specifier |
| 122 | + from pip._vendor.packaging.utils import canonicalize_name |
| 123 | + from pip._vendor.packaging.version import Version, InvalidVersion |
| 124 | + |
| 125 | + try: |
| 126 | + requirement = Requirement(requirement_text) |
| 127 | + if requirement.marker is not None and not requirement.marker.evaluate(): |
| 128 | + return None |
| 129 | + if requirement.url is not None: |
| 130 | + return (canonicalize_name(requirement.name), None, None) |
| 131 | + bounds = [] |
| 132 | + for specifier in requirement.specifier: |
| 133 | + if specifier.operator not in {"<", "<=", ">", ">=", "=="} or "*" in specifier.version: |
| 134 | + return (canonicalize_name(requirement.name), None, None) |
| 135 | + try: |
| 136 | + bounds.append((specifier.operator, Version(specifier.version))) |
| 137 | + except InvalidVersion: |
| 138 | + return (canonicalize_name(requirement.name), None, None) |
| 139 | + return (canonicalize_name(requirement.name), bounds, Specifier) |
| 140 | + except Exception: |
| 141 | + return ("<unknown>", None, None) |
| 142 | + |
| 143 | + |
| 144 | +def _constraint_status(bounds, specifier_type) -> str: |
| 145 | + lower = None |
| 146 | + upper = None |
| 147 | + exacts = [] |
| 148 | + specifiers = [specifier_type(f"{operator}{version}") for operator, version in bounds] |
| 149 | + for operator, version in bounds: |
| 150 | + if operator == "==": |
| 151 | + exacts.append(version) |
| 152 | + elif operator in {">", ">="}: |
| 153 | + candidate = (version, operator == ">=") |
| 154 | + if lower is None or candidate[0] > lower[0] or (candidate[0] == lower[0] and not candidate[1]): |
| 155 | + lower = candidate |
| 156 | + else: |
| 157 | + candidate = (version, operator == "<=") |
| 158 | + if upper is None or candidate[0] < upper[0] or (candidate[0] == upper[0] and not candidate[1]): |
| 159 | + upper = candidate |
| 160 | + if exacts: |
| 161 | + return "compatible" if any(all(specifier.contains(version, prereleases=True) |
| 162 | + for specifier in specifiers) for version in exacts) else "conflict" |
| 163 | + if lower is None or upper is None: |
| 164 | + return "compatible" |
| 165 | + if lower[0] > upper[0] or (lower[0] == upper[0] and not (lower[1] and upper[1])): |
| 166 | + return "conflict" |
| 167 | + if lower[0] == upper[0]: |
| 168 | + return "compatible" |
| 169 | + if not lower[1] or not upper[1]: |
| 170 | + # Strict bounds have special pre- and post-release exclusions under the |
| 171 | + # version-specifier rules. We need to check the actual specifiers to |
| 172 | + # prove compatibility. Otherwise, just conservatively say we don't know. |
| 173 | + version_type = type(lower[0]) |
| 174 | + candidates = [lower[0], upper[0]] |
| 175 | + for boundary in (lower[0], upper[0]): |
| 176 | + release = list(boundary.release) |
| 177 | + candidates.append(version_type(".".join(map(str, release[:-1] + [release[-1] + 1])))) |
| 178 | + for depth in range(1, 4): |
| 179 | + candidates.append(version_type(".".join(map(str, release + ([0] * depth) + [1])))) |
| 180 | + if any(all(specifier.contains(version, prereleases=True) for specifier in specifiers) |
| 181 | + for version in candidates): |
| 182 | + return "compatible" |
| 183 | + return "unknown_compatibility" |
| 184 | + return "compatible" |
| 185 | + |
| 186 | + |
| 187 | +def pip_dependency_conflicts(candidate_name: str, candidate_dependencies: bytes, |
| 188 | + installed_plugins: Iterable[tuple[str, bytes]]) -> List[DependencyConflict]: |
| 189 | + """Compare active requirements for a candidate plugin. |
| 190 | +
|
| 191 | + See the following for more on dependency and version specifiers: |
| 192 | +
|
| 193 | + - https://packaging.python.org/en/latest/specifications/dependency-specifiers/#dependency-specifiers |
| 194 | + - https://packaging.python.org/en/latest/specifications/version-specifiers/#version-specifiers |
| 195 | +
|
| 196 | + Markers that do not apply are ignored. Only a subset of the possible operators are |
| 197 | + handled and the result we hand back is conservative if we can't prove a conflict. |
| 198 | + """ |
| 199 | + def requirements(plugin_name: str, dependencies: bytes): |
| 200 | + try: |
| 201 | + return [(DependencyRequirement(plugin_name, text), _requirement_parts(text)) |
| 202 | + for text in pip_requirements_from_dependency_metadata(dependencies)] |
| 203 | + except Exception: |
| 204 | + return [(DependencyRequirement(plugin_name, dependencies.decode("utf-8", errors="replace")), |
| 205 | + ("<unknown>", None, None))] |
| 206 | + |
| 207 | + candidate = requirements(candidate_name, candidate_dependencies) |
| 208 | + installed_groups = [requirements(name, dependencies) for name, dependencies in installed_plugins] |
| 209 | + installed = [requirement for group in installed_groups for requirement in group] |
| 210 | + result = [] |
| 211 | + package_names = dict.fromkeys(parts[0] for _, parts in candidate if parts is not None) |
| 212 | + for package_name in package_names: |
| 213 | + candidate_same_package = [(requirement, parts) for requirement, parts in candidate |
| 214 | + if parts is not None and parts[0] == package_name] |
| 215 | + matching = [(requirement, parts) for requirement, parts in installed |
| 216 | + if parts is not None and parts[0] == package_name] |
| 217 | + candidate_unknown = any(parts[1] is None for _, parts in candidate_same_package) |
| 218 | + candidate_bounds = [bound for _, parts in candidate_same_package if parts[1] is not None for bound in parts[1]] |
| 219 | + specifier_type = next((parts[2] for _, parts in candidate_same_package + matching if parts[2] is not None), None) |
| 220 | + candidate_status = _constraint_status(candidate_bounds, specifier_type) |
| 221 | + if candidate_status == "conflict": |
| 222 | + result.append(DependencyConflict("proven_conflict", package_name, |
| 223 | + [requirement for requirement, _ in candidate_same_package], [])) |
| 224 | + continue |
| 225 | + proven = [] |
| 226 | + unknown = [] |
| 227 | + for installed_group in installed_groups: |
| 228 | + installed_same_plugin = [(requirement, parts) for requirement, parts in installed_group |
| 229 | + if parts is not None and parts[0] == package_name] |
| 230 | + if not installed_same_plugin: |
| 231 | + continue |
| 232 | + installed_bounds = [bound for _, parts in installed_same_plugin if parts[1] is not None for bound in parts[1]] |
| 233 | + installed_unknown = any(parts[1] is None for _, parts in installed_same_plugin) |
| 234 | + installed_status = _constraint_status(installed_bounds, specifier_type) |
| 235 | + combined_status = _constraint_status(candidate_bounds + installed_bounds, specifier_type) |
| 236 | + if installed_status != "conflict" and combined_status == "conflict": |
| 237 | + proven.extend(requirement for requirement, _ in installed_same_plugin) |
| 238 | + elif installed_unknown or combined_status == "unknown_compatibility": |
| 239 | + unknown.extend(requirement for requirement, _ in installed_same_plugin) |
| 240 | + if proven: |
| 241 | + result.append(DependencyConflict("proven_conflict", package_name, |
| 242 | + [requirement for requirement, _ in candidate_same_package], |
| 243 | + proven)) |
| 244 | + continue |
| 245 | + if candidate_unknown or candidate_status == "unknown_compatibility" or unknown: |
| 246 | + result.append(DependencyConflict("unknown_compatibility", package_name, |
| 247 | + [requirement for requirement, _ in candidate_same_package], |
| 248 | + unknown)) |
| 249 | + return result |
0 commit comments