1
0

check updates, refactor slightly

This commit is contained in:
2024-12-24 11:15:27 +01:00
parent baa4453eb0
commit ba02ef502c
+32 -41
View File
@@ -1,5 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Check if all scripts and their dependencies are properly configured.""" """
Check if all script dependencies are available.
Does not go into venvs, dependencies need to be installed globally.
Prints a list of which commands can be run and which can't.
Basic binaries such as gnu coreutils are expected and not checked for.
"""
import os import os
import shutil import shutil
@@ -9,28 +14,33 @@ from pathlib import Path
from typing import Dict, List, Tuple from typing import Dict, List, Tuple
import importlib import importlib
# Unicode emoji
CHECK = "✅" CHECK = "✅"
CROSS = "❌" CROSS = "❌"
def check_command(cmd: str) -> Tuple[bool, str]: def check_command(cmd: str) -> Tuple[bool, str|None]:
"""Check if a command is available in PATH.""" if shutil.which(cmd) is not None:
return (shutil.which(cmd) is not None, f"Command '{cmd}' not found") return (True, None)
else:
return (False, f"Command '{cmd}' not found")
def check_env_var(var: str) -> Tuple[bool, str]: def check_env_var(var: str) -> Tuple[bool, str|None]:
"""Check if an environment variable is set.""" if var in os.environ:
return (var in os.environ, f"Environment variable '{var}' not set") return (True, None)
else:
return (False, f"Environment variable '{var}' not set")
def check_python_lib(lib: str) -> Tuple[bool, str]: def check_python_lib(lib: str) -> Tuple[bool, str|None]:
"""Check if a Python library is available."""
try: try:
importlib.import_module(lib.split('.')[0]) importlib.import_module(lib)
return (True, f"Python library '{lib}' found") return (True, None)
except ImportError: except ImportError:
return (False, f"Python library '{lib}' not found") return (False, f"Python library '{lib}' not found")
def check_stats_commands() -> List[Tuple[bool, str]]: def check_stats_commands() -> List[Tuple[bool, str]]:
"""Check if all stat commands in config can run.""" """
Checks if all the commands that are configured for the stats daemon
can be executed.
"""
results = [] results = []
config_home = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")) config_home = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
config_path = Path(config_home) / "stats/config.ini" config_path = Path(config_home) / "stats/config.ini"
@@ -46,25 +56,13 @@ def check_stats_commands() -> List[Tuple[bool, str]]:
cmd = config[section]['command'] cmd = config[section]['command']
try: try:
subprocess.run(cmd, shell=True, capture_output=True, check=True) subprocess.run(cmd, shell=True, capture_output=True, check=True)
results.append((True, f"Command '{cmd}' runs successfully")) results.append((True, None))
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
results.append((False, f"Command '{cmd}' failed to run")) results.append((False, f"Command '{cmd}' failed to run"))
return results return results
def check_stats_collector_config() -> List[Tuple[bool, str]]: def check_scripts() -> Dict[str, List[Tuple[bool, str|None]]]:
"""Check if stats collector config exists."""
results = []
config_home = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
config_path = Path(config_home) / "stats-collector/config.ini"
if config_path.exists():
results.append((True, "Stats collector config found"))
else:
results.append((False, f"Stats collector config not found at {config_path}"))
return results
def check_scripts() -> Dict[str, List[Tuple[bool, str]]]:
"""Run all checks for each script.""" """Run all checks for each script."""
checks = { checks = {
"e-ink": [ "e-ink": [
@@ -78,7 +76,7 @@ def check_scripts() -> Dict[str, List[Tuple[bool, str]]]:
check_command("webpmux"), check_command("webpmux"),
check_command("gifski"), check_command("gifski"),
], ],
"mover": [], # No specific dependencies "mover": [],
"pushover": [ "pushover": [
check_env_var("PUSHOVER_APP_TOKEN"), check_env_var("PUSHOVER_APP_TOKEN"),
check_env_var("PUSHOVER_USER_TOKEN"), check_env_var("PUSHOVER_USER_TOKEN"),
@@ -93,14 +91,13 @@ def check_scripts() -> Dict[str, List[Tuple[bool, str]]]:
check_command("free"), check_command("free"),
check_command("df"), check_command("df"),
check_python_lib("configparser"), check_python_lib("configparser"),
check_python_lib("http.server"), check_python_lib("http"),
*check_stats_commands(), *check_stats_commands(),
], ],
"stats-collector": [ "stats-collector": [
check_command("python3"), check_command("python3"),
check_python_lib("sqlalchemy"), check_python_lib("sqlalchemy"),
check_python_lib("requests"), check_python_lib("requests"),
*check_stats_collector_config(),
], ],
"syncwall": [ "syncwall": [
check_command("wget"), check_command("wget"),
@@ -132,21 +129,15 @@ def check_scripts() -> Dict[str, List[Tuple[bool, str]]]:
def main(): def main():
results = check_scripts() results = check_scripts()
print("Script Status Check")
print("==================")
for script, checks in results.items(): for script, checks in results.items():
all_passed = all(result[0] for result in checks) if checks else True all_passed = all(result[0] for result in checks) if checks else True
status = CHECK if all_passed else CROSS status = CHECK if all_passed else CROSS
print(f"\n{status} {script}") print(f"{status} {script}")
if not checks: for passed, message in checks:
print(" No specific requirements") status = CHECK if passed else CROSS
else: if message:
for passed, message in checks: print(f"\t{status} {message}")
status = CHECK if passed else CROSS
if not passed:
print(f" {status} {message}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()