Coverage for src/system/commands.py: 33.93%

56 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 23:52 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from pathlib import Path 

5from shutil import which 

6import subprocess 

7from typing import List, Optional 

8 

9# Commands 

10class Commands: 

11 

12 # Exec 

13 @staticmethod 

14 def exec(binary: str, arguments: List[str]) -> int: 

15 

16 # Run system command 

17 try: 

18 process = subprocess.run( 

19 args=[binary] + arguments, 

20 cwd=None, 

21 check=False, 

22 shell=False, 

23 ) 

24 return process.returncode 

25 except subprocess.CalledProcessError as e: 

26 print(f"Error executing command: {e}") 

27 return 1 

28 

29 # Exists 

30 @staticmethod 

31 def exists(binary: str) -> bool: 

32 

33 # Check system binary exists 

34 return which(binary) is not None 

35 

36 # Grep 

37 @staticmethod 

38 def grep(file: Path, string: str) -> bool: 

39 

40 # Ignore missing file 

41 if not file.exists(): 

42 return False 

43 

44 # Check file contains string 

45 with open( 

46 file, 

47 encoding='utf8', 

48 mode='r', 

49 ) as f: 

50 for line in f.readlines(): 

51 

52 # Find string in line 

53 if string in line: 

54 return True 

55 

56 # Fallback 

57 return False # pragma: no cover 

58 

59 # Mise 

60 @staticmethod 

61 def mise(arguments: List[str], check: bool = True) -> bool: 

62 

63 # Run mise 

64 return Commands.run( 

65 'mise', 

66 arguments, 

67 check, 

68 ) 

69 

70 # Output 

71 @staticmethod 

72 def output( 

73 binary: str, 

74 arguments: List[str], 

75 quiet: bool = False, 

76 errors: bool = False, 

77 ) -> str: 

78 

79 # Variables 

80 stderr: Optional[int] = None 

81 

82 # Redirect errors 

83 if errors: 

84 stderr = subprocess.STDOUT 

85 elif quiet: 

86 stderr = subprocess.DEVNULL 

87 

88 # Get system output 

89 try: 

90 return subprocess.check_output( 

91 args=[binary] + arguments, 

92 cwd=None, 

93 shell=False, 

94 stderr=stderr, 

95 ).strip().decode() 

96 except subprocess.CalledProcessError as err: 

97 return str(err.output.decode()) 

98 except FileNotFoundError: 

99 return '' 

100 

101 # Pip 

102 @staticmethod 

103 def pip(arguments: List[str]) -> bool: 

104 

105 # Run with pipx 

106 if Commands.exists('pipx'): 

107 return Commands.run( 

108 'pipx', 

109 arguments, 

110 ) 

111 

112 # Run with pip # pragma: no cover 

113 return Commands.run( 

114 'sudo', 

115 ['pip'] + arguments, 

116 ) 

117 

118 # Run 

119 @staticmethod 

120 def run(binary: str, arguments: List[str], check: bool = True) -> bool: 

121 

122 # Variables 

123 stderr: Optional[int] = None if check else subprocess.DEVNULL 

124 stdout: Optional[int] = None if check else subprocess.DEVNULL 

125 

126 # Run system command 

127 try: 

128 process = subprocess.run( 

129 args=[binary] + arguments, 

130 cwd=None, 

131 check=check, 

132 shell=False, 

133 stderr=stderr, 

134 stdout=stdout, 

135 ) 

136 return process.returncode == 0 

137 except subprocess.CalledProcessError as e: 

138 if check: 

139 print(f"Error executing command: {e}") 

140 return False