Coverage for /Users/les/Projects/crackerjack/crackerjack/managers/async_hook_manager.py: 0%

69 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:39 -0700

1import asyncio 

2import typing as t 

3from pathlib import Path 

4 

5from rich.console import Console 

6 

7from crackerjack.config.hooks import HookConfigLoader 

8from crackerjack.executors.async_hook_executor import AsyncHookExecutor 

9from crackerjack.models.task import HookResult 

10 

11 

12class AsyncHookManager: 

13 def __init__( 

14 self, 

15 console: Console, 

16 pkg_path: Path, 

17 max_concurrent: int = 3, 

18 ) -> None: 

19 self.console = console 

20 self.pkg_path = pkg_path 

21 self.async_executor = AsyncHookExecutor( 

22 console, 

23 pkg_path, 

24 max_concurrent=max_concurrent, 

25 quiet=True, 

26 ) 

27 self.config_loader = HookConfigLoader() 

28 

29 async def run_fast_hooks_async(self) -> list[HookResult]: 

30 strategy = self.config_loader.load_strategy("fast") 

31 

32 strategy.parallel = False 

33 

34 execution_result = await self.async_executor.execute_strategy(strategy) 

35 return execution_result.results 

36 

37 async def run_comprehensive_hooks_async(self) -> list[HookResult]: 

38 strategy = self.config_loader.load_strategy("comprehensive") 

39 

40 strategy.parallel = True 

41 strategy.max_workers = 3 

42 

43 execution_result = await self.async_executor.execute_strategy(strategy) 

44 return execution_result.results 

45 

46 def run_fast_hooks(self) -> list[HookResult]: 

47 return asyncio.run(self.run_fast_hooks_async()) 

48 

49 def run_comprehensive_hooks(self) -> list[HookResult]: 

50 return asyncio.run(self.run_comprehensive_hooks_async()) 

51 

52 async def install_hooks_async(self) -> bool: 

53 try: 

54 process = await asyncio.create_subprocess_exec( 

55 "uv", 

56 "run", 

57 "pre-commit", 

58 "install", 

59 cwd=self.pkg_path, 

60 stdout=asyncio.subprocess.PIPE, 

61 stderr=asyncio.subprocess.PIPE, 

62 ) 

63 

64 _, stderr = await asyncio.wait_for(process.communicate(), timeout=30) 

65 

66 if process.returncode == 0: 

67 self.console.print("[green]✅[/ green] Pre-commit hooks installed") 

68 return True 

69 error_msg = stderr.decode() if stderr else "Unknown error" 

70 self.console.print( 

71 f"[red]❌[/ red] Failed to install hooks: {error_msg}", 

72 ) 

73 return False 

74 

75 except TimeoutError: 

76 self.console.print("[red]❌[/ red] Hook installation timed out") 

77 return False 

78 except Exception as e: 

79 self.console.print(f"[red]❌[/ red] Error installing hooks: {e}") 

80 return False 

81 

82 def install_hooks(self) -> bool: 

83 return asyncio.run(self.install_hooks_async()) 

84 

85 async def update_hooks_async(self) -> bool: 

86 try: 

87 process = await asyncio.create_subprocess_exec( 

88 "uv", 

89 "run", 

90 "pre-commit", 

91 "autoupdate", 

92 cwd=self.pkg_path, 

93 stdout=asyncio.subprocess.PIPE, 

94 stderr=asyncio.subprocess.PIPE, 

95 ) 

96 

97 _, stderr = await asyncio.wait_for(process.communicate(), timeout=60) 

98 

99 if process.returncode == 0: 

100 self.console.print("[green]✅[/ green] Pre-commit hooks updated") 

101 return True 

102 error_msg = stderr.decode() if stderr else "Unknown error" 

103 self.console.print(f"[red]❌[/ red] Failed to update hooks: {error_msg}") 

104 return False 

105 

106 except TimeoutError: 

107 self.console.print("[red]❌[/ red] Hook update timed out") 

108 return False 

109 except Exception as e: 

110 self.console.print(f"[red]❌[/ red] Error updating hooks: {e}") 

111 return False 

112 

113 def update_hooks(self) -> bool: 

114 return asyncio.run(self.update_hooks_async()) 

115 

116 def get_hook_summary(self, results: list[HookResult]) -> dict[str, t.Any]: 

117 if not results: 

118 return { 

119 "total": 0, 

120 "passed": 0, 

121 "failed": 0, 

122 "errors": 0, 

123 "total_duration": 0, 

124 "success_rate": 0, 

125 } 

126 

127 passed = sum(1 for r in results if r.status == "passed") 

128 failed = sum(1 for r in results if r.status == "failed") 

129 errors = sum(1 for r in results if r.status in ("timeout", "error")) 

130 total_duration = sum(r.duration for r in results) 

131 

132 return { 

133 "total": len(results), 

134 "passed": passed, 

135 "failed": failed, 

136 "errors": errors, 

137 "total_duration": total_duration, 

138 "success_rate": (passed / len(results)) * 100 if results else 0, 

139 }