Coverage for src/system/commands.py: 100.00%
5 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-29 22:06 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-29 22:06 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from pathlib import Path
5from shutil import which
6import subprocess
7from typing import List, Optional
9# Commands
10class Commands:
12 # Exec
13 @staticmethod
14 def exec(binary: str, arguments: List[str]) -> int: # pragma: no cover
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
29 # Exists
30 @staticmethod
31 def exists(binary: str) -> bool: # pragma: no cover
33 # Check system binary exists
34 return which(binary) is not None
36 # Grep
37 @staticmethod
38 def grep(file: Path, string: str) -> bool: # pragma: no cover
40 # Ignore missing file
41 if not file.exists():
42 return False
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():
52 # Find string in line
53 if string in line:
54 return True
56 # Fallback
57 return False # pragma: no cover
59 # Mise
60 @staticmethod
61 def mise(arguments: List[str], check: bool = True) -> bool: # pragma: no cover
63 # Run mise
64 return Commands.run(
65 'mise',
66 arguments,
67 check,
68 )
70 # Output
71 @staticmethod
72 def output(
73 binary: str,
74 arguments: List[str],
75 quiet: bool = False,
76 errors: bool = False,
77 ) -> str: # pragma: no cover
79 # Variables
80 stderr: Optional[int] = None
82 # Redirect errors
83 if errors:
84 stderr = subprocess.STDOUT
85 elif quiet:
86 stderr = subprocess.DEVNULL
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 ''
101 # Pip
102 @staticmethod
103 def pip(arguments: List[str]) -> bool: # pragma: no cover
105 # Run with pipx
106 if Commands.exists('pipx'):
107 return Commands.run(
108 'pipx',
109 arguments,
110 )
112 # Run with pip # pragma: no cover
113 return Commands.run(
114 'sudo',
115 ['pip'] + arguments,
116 )
118 # Run
119 @staticmethod
120 def run(binary: str, arguments: List[str],
121 check: bool = True) -> bool: # pragma: no cover
123 # Variables
124 stderr: Optional[int] = None if check else subprocess.DEVNULL
125 stdout: Optional[int] = None if check else subprocess.DEVNULL
127 # Run system command
128 try:
129 process = subprocess.run(
130 args=[binary] + arguments,
131 cwd=None,
132 check=check,
133 shell=False,
134 stderr=stderr,
135 stdout=stdout,
136 )
137 return process.returncode == 0
138 except subprocess.CalledProcessError as e:
139 if check:
140 print(f"Error executing command: {e}")
141 return False