Coverage for src/governed_harness/profiles/detectors.py: 97%

52 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-12 16:06 +0000

1from __future__ import annotations 

2 

3from collections.abc import Iterable 

4from dataclasses import dataclass 

5from pathlib import Path 

6from typing import TYPE_CHECKING 

7 

8if TYPE_CHECKING: # the configuration package imports this module 

9 from governed_harness.configuration.models import TechnologyProfileDefinition 

10 

11 

12@dataclass(frozen=True) 

13class DetectionResult: 

14 profile_id: str 

15 technology: str 

16 confidence: float 

17 evidence: tuple[str, ...] 

18 warnings: tuple[str, ...] = () 

19 

20 

21class MarkerDetector: 

22 profile_id: str 

23 technology: str 

24 markers: tuple[tuple[str, float], ...] 

25 

26 def detect(self, workspace: Path) -> DetectionResult: 

27 root = workspace.resolve(strict=True) 

28 evidence: list[str] = [] 

29 score = 0.0 

30 for marker, weight in self.markers: 

31 if (root / marker).exists(): 

32 evidence.append(marker) 

33 score += weight 

34 return DetectionResult( 

35 profile_id=self.profile_id, 

36 technology=self.technology, 

37 confidence=min(1.0, score), 

38 evidence=tuple(evidence), 

39 ) 

40 

41 

42class PythonDetector(MarkerDetector): 

43 profile_id = "python_default" 

44 technology = "python" 

45 markers = ( 

46 ("pyproject.toml", 0.80), 

47 ("requirements.txt", 0.45), 

48 ("setup.py", 0.35), 

49 ("pytest.ini", 0.30), 

50 ("tox.ini", 0.25), 

51 ) 

52 

53 

54class NodeDetector(MarkerDetector): 

55 profile_id = "node_default" 

56 technology = "node" 

57 markers = ( 

58 ("package.json", 0.80), 

59 ("package-lock.json", 0.35), 

60 ("pnpm-lock.yaml", 0.35), 

61 ("yarn.lock", 0.35), 

62 ) 

63 

64 def detect(self, workspace: Path) -> DetectionResult: 

65 result = super().detect(workspace) 

66 locks = [ 

67 name 

68 for name in ("package-lock.json", "pnpm-lock.yaml", "yarn.lock") 

69 if (workspace / name).exists() 

70 ] 

71 warnings = (f"ambiguous package managers: {', '.join(locks)}",) if len(locks) > 1 else () 

72 return DetectionResult( 

73 profile_id=result.profile_id, 

74 technology=result.technology, 

75 confidence=result.confidence, 

76 evidence=result.evidence, 

77 warnings=warnings, 

78 ) 

79 

80 

81class DeclaredProfileDetector: 

82 """Detection of a project profile (``toolchain.profilePaths``) by the markers its YAML 

83 declares. A profile that declares no marker applies to every workspace.""" 

84 

85 def __init__(self, profile: TechnologyProfileDefinition) -> None: 

86 self.profile = profile 

87 

88 def detect(self, workspace: Path) -> DetectionResult: 

89 root = workspace.resolve(strict=True) 

90 if not self.profile.detectors: 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true

91 return DetectionResult( 

92 profile_id=self.profile.profile_id, 

93 technology=self.profile.technology, 

94 confidence=1.0, 

95 evidence=("no detectors declared",), 

96 ) 

97 evidence = [item.marker for item in self.profile.detectors if (root / item.marker).exists()] 

98 score = sum(item.weight for item in self.profile.detectors if item.marker in evidence) 

99 return DetectionResult( 

100 profile_id=self.profile.profile_id, 

101 technology=self.profile.technology, 

102 confidence=min(1.0, score), 

103 evidence=tuple(evidence), 

104 ) 

105 

106 

107def detect_profiles( 

108 workspace: Path, extra: Iterable[TechnologyProfileDefinition] = () 

109) -> list[DetectionResult]: 

110 """The built-in detections and, since 2.0, those of the project profiles in ``extra``.""" 

111 results = [PythonDetector().detect(workspace), NodeDetector().detect(workspace)] 

112 results.extend(DeclaredProfileDetector(profile).detect(workspace) for profile in extra) 

113 return sorted(results, key=lambda item: (item.confidence, item.profile_id), reverse=True)