Coverage for lintro / tools / core / install_strategies / environment.py: 60%
20 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Install environment detection for strategy-based tool installation.
3Provides a slim, frozen data object describing what package managers are
4available on the current system. Strategy classes receive this instead of
5the full ``RuntimeContext`` so they stay decoupled from CI detection,
6platform labels, and other unrelated concerns.
7"""
9from __future__ import annotations
11import shutil
12from dataclasses import dataclass
14from lintro.enums.install_context import InstallContext, PackageManager
17@dataclass(frozen=True)
18class InstallEnvironment:
19 """Available package managers and install context.
21 Attributes:
22 install_context: How lintro was installed.
23 available_managers: Set of package manager identifiers found on PATH.
24 """
26 install_context: InstallContext
27 available_managers: frozenset[PackageManager]
29 def has(self, manager: PackageManager) -> bool:
30 """Check if a package manager is available.
32 Args:
33 manager: Package manager to check.
35 Returns:
36 True if the manager was found on PATH.
37 """
38 return manager in self.available_managers
40 @classmethod
41 def detect(cls, install_context: InstallContext) -> InstallEnvironment:
42 """Detect available package managers from PATH.
44 Args:
45 install_context: How lintro was installed (passed in from
46 the existing ``_detect_install_context`` helper).
48 Returns:
49 InstallEnvironment with detected values.
50 """
51 managers: set[PackageManager] = set()
52 for pm in PackageManager:
53 if pm == PackageManager.PIP:
54 # Accept pip3 as pip fallback
55 if shutil.which("pip") is not None or shutil.which("pip3") is not None:
56 managers.add(pm)
57 elif shutil.which(pm) is not None:
58 managers.add(pm)
59 return cls(
60 install_context=install_context,
61 available_managers=frozenset(managers),
62 )