Coverage for /builds/svesch/cikit/src/cikit/ci/runner.py: 70%

21 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-10-09 14:17 +0000

1# SPDX-License-Identifier: GPL-3.0-or-later 

2"""Common helpers for CI command modules. 

3 

4This module contains shared setup and execution logic used by all CI 

5subcommands. It centralizes logging, environment initialization, status 

6handling, result output, and top-level exception handling. 

7 

8Author: 

9 Sven Schlender 

10Date: 

11 2026-05-27 

12""" 

13 

14import sys 

15from collections.abc import Callable 

16 

17from cikit import CiRunStatus, CiSection, CiToolsError, log_dict_compact, setup_logging 

18from cikit.macros import init_env 

19 

20 

21def _resolve_run_name(default_name: str, args=None) -> str: 

22 """Resolve the effective run name. 

23 

24 Args: 

25 default_name: Default logical run name provided by the command. 

26 args: Optional parsed command arguments. 

27 

28 Returns: 

29 Effective logical run name. 

30 """ 

31 return getattr(args, "name", None) or default_name 

32 

33 

34def run_ci_script(default_name: str, callback: Callable, args=None) -> int: 

35 """Run a CI command with common setup and error handling. 

36 

37 This helper initializes logging, creates the shared run status, resolves 

38 the CI run environment, executes the command callback, and prints the final 

39 run data result. Top-level ``CiToolsError`` exceptions are converted into 

40 process exit codes. 

41 

42 Args: 

43 default_name: Default logical run name used for the run environment. 

44 callback: Function called as ``callback(env, status)``. 

45 args: Optional parsed command arguments. If it provides ``verbose``, 

46 that value controls logging verbosity. If it provides ``name``, 

47 that value overrides ``default_name``. 

48 

49 Returns: 

50 Process exit code. 

51 """ 

52 try: 

53 setup_logging(verbose=getattr(args, "verbose", False)) 

54 

55 status = CiRunStatus() 

56 run_name = getattr(args, "name", None) or default_name 

57 

58 with CiSection("Getting env", status=status): 

59 env = init_env(run_name) 

60 

61 callback(env, status) 

62 

63 if env.data: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 with CiSection("Result", status=status): 

65 log_dict_compact(env.data) 

66 

67 return status.exit_code 

68 

69 except CiToolsError as exp: 

70 print(str(exp), file=sys.stderr) 

71 return exp.exit_code