|
| 1 | +# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). |
| 2 | +# Licensed under the Apache License, Version 2.0 (see LICENSE). |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import itertools |
| 7 | +import json |
| 8 | +import logging |
| 9 | +from dataclasses import dataclass |
| 10 | +from typing import TYPE_CHECKING, Any, Mapping |
| 11 | + |
| 12 | +from packaging.version import parse |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + # We seem to get a version of `packaging` that doesn't have `LegacyVersion` when running |
| 16 | + # pytest.. |
| 17 | + from packaging.version import LegacyVersion, Version |
| 18 | + |
| 19 | +from pants.backend.python.util_rules.pex_requirements import ( |
| 20 | + LoadedLockfile, |
| 21 | + LoadedLockfileRequest, |
| 22 | + Lockfile, |
| 23 | + LockfileContent, |
| 24 | +) |
| 25 | +from pants.base.exceptions import EngineError |
| 26 | +from pants.core.goals.generate_lockfiles import LockfileDiff, LockfilePackages, PackageName |
| 27 | +from pants.engine.fs import Digest, DigestContents |
| 28 | +from pants.engine.rules import Get, rule_helper |
| 29 | +from pants.util.frozendict import FrozenDict |
| 30 | + |
| 31 | +logger = logging.getLogger(__name__) |
| 32 | + |
| 33 | + |
| 34 | +@dataclass(frozen=True, order=True) |
| 35 | +class PythonRequirementVersion: |
| 36 | + _parsed: LegacyVersion | Version |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def parse(cls, version: str) -> PythonRequirementVersion: |
| 40 | + return cls(parse(version)) |
| 41 | + |
| 42 | + def __str__(self) -> str: |
| 43 | + return str(self._parsed) |
| 44 | + |
| 45 | + def __getattr__(self, key: str) -> Any: |
| 46 | + return getattr(self._parsed, key) |
| 47 | + |
| 48 | + |
| 49 | +def _pex_lockfile_requirements( |
| 50 | + lockfile_data: Mapping[str, Any] | None, path: str | None = None |
| 51 | +) -> LockfilePackages: |
| 52 | + if not lockfile_data: |
| 53 | + return LockfilePackages({}) |
| 54 | + |
| 55 | + try: |
| 56 | + # Setup generators |
| 57 | + locked_resolves = ( |
| 58 | + ( |
| 59 | + (PackageName(r["project_name"]), PythonRequirementVersion.parse(r["version"])) |
| 60 | + for r in resolve["locked_requirements"] |
| 61 | + ) |
| 62 | + for resolve in lockfile_data["locked_resolves"] |
| 63 | + ) |
| 64 | + requirements = dict(itertools.chain.from_iterable(locked_resolves)) |
| 65 | + except KeyError as e: |
| 66 | + if path: |
| 67 | + logger.warning(f"{path}: Failed to parse lockfile: {e}") |
| 68 | + |
| 69 | + requirements = {} |
| 70 | + |
| 71 | + return LockfilePackages(requirements) |
| 72 | + |
| 73 | + |
| 74 | +@rule_helper |
| 75 | +async def _parse_lockfile(lockfile: Lockfile | LockfileContent) -> FrozenDict[str, Any] | None: |
| 76 | + try: |
| 77 | + loaded = await Get( |
| 78 | + LoadedLockfile, |
| 79 | + LoadedLockfileRequest(lockfile), |
| 80 | + ) |
| 81 | + fc = await Get(DigestContents, Digest, loaded.lockfile_digest) |
| 82 | + parsed_lockfile = json.loads(fc[0].content) |
| 83 | + return FrozenDict.deep_freeze(parsed_lockfile) |
| 84 | + except EngineError: |
| 85 | + # May fail in case the file doesn't exist, which is expected when parsing the "old" lockfile |
| 86 | + # the first time a new lockfile is generated. |
| 87 | + return None |
| 88 | + except json.JSONDecodeError as e: |
| 89 | + file_path = ( |
| 90 | + lockfile.file_path if isinstance(lockfile, Lockfile) else lockfile.file_content.path |
| 91 | + ) |
| 92 | + logger.debug(f"{file_path}: Failed to parse lockfile contents: {e}") |
| 93 | + return None |
| 94 | + |
| 95 | + |
| 96 | +@rule_helper |
| 97 | +async def _generate_python_lockfile_diff( |
| 98 | + digest: Digest, resolve_name: str, path: str |
| 99 | +) -> LockfileDiff: |
| 100 | + new_content = await Get(DigestContents, Digest, digest) |
| 101 | + new = await _parse_lockfile( |
| 102 | + LockfileContent( |
| 103 | + file_content=next(c for c in new_content if c.path == path), |
| 104 | + resolve_name=resolve_name, |
| 105 | + ) |
| 106 | + ) |
| 107 | + old = await _parse_lockfile( |
| 108 | + Lockfile( |
| 109 | + file_path=path, |
| 110 | + file_path_description_of_origin="generated lockfile", |
| 111 | + resolve_name=resolve_name, |
| 112 | + ) |
| 113 | + ) |
| 114 | + return LockfileDiff.create( |
| 115 | + path=path, |
| 116 | + resolve_name=resolve_name, |
| 117 | + old=_pex_lockfile_requirements(old), |
| 118 | + new=_pex_lockfile_requirements(new, path), |
| 119 | + ) |
0 commit comments