Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions odev/common/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(?P<name>[\w_-]+)
(?:
(?P<op>[~<>=]+)
(?P<version>[\d.*]+)
(?P<version>[a-zA-Z\d.*+!_-]+)
)?
(?:
\s*;\s*
Expand Down Expand Up @@ -86,6 +86,9 @@ class PythonEnv:
pip_freeze_cache: ClassVar[TTLCache] = TTLCache(ttl=60)
"""Cache for pip freeze output."""

installed_packages_cache: ClassVar[TTLCache] = TTLCache(ttl=60)
"""Cache for parsed installed packages."""

def __init__(self, path: Path | str | None = None, version: str | None = None):
"""Initialize a python environment.

Expand Down Expand Up @@ -381,8 +384,8 @@ def __package_spec(self, package: str) -> tuple[str, str]:
# Consider:
# - Package name: odoo_upgrade
# - Version: aaa1f0fee6870075e25cb5e6744e4c589bb32b46
package_name, package_version = package.split(" @ ") if " @ " in package else (package, package)
package_version = package_name.split("@")[-1] if "@" in package_name else "1.0.0"
package_name, package_url = package.split(" @ ") if " @ " in package else (package, package)
package_version = package_url.split("@")[-1] if "@" in package_url else "1.0.0"
else:
raise ValueError(f"Invalid package specification {package!r}")

Expand All @@ -394,6 +397,9 @@ def installed_packages(self) -> Mapping[str, Version | str]:
:return: The result of the pip command execution.
:rtype: CompletedProcess
"""
if (installed := self.installed_packages_cache.get(self.path.as_posix())) is not None:
return installed

freeze = self.__pip_freeze_all()
packages = freeze.stdout.decode().splitlines()
installed: MutableMapping[str, Version | str] = {}
Expand All @@ -408,6 +414,7 @@ def installed_packages(self) -> Mapping[str, Version | str]:

installed[package_name.strip().lower()] = package_version

self.installed_packages_cache.set(self.path.as_posix(), installed)
return installed

def missing_requirements(self, path: Path | str, raise_if_error: bool = True) -> Generator[str, None, None]: # noqa: PLR0912
Expand Down Expand Up @@ -437,6 +444,11 @@ def missing_requirements(self, path: Path | str, raise_if_error: bool = True) ->
continue

if "git+" in line:
if " @ " not in line:
logger.warning(
f"Git requirement {line!r} is not named. "
"Please use the format 'package_name @ git+url@version' to avoid issues."
)
package_name, package_version = self.__package_spec(line)
installed_version = installed_packages.get(package_name)

Expand Down
Loading