Coverage for sparkle/CLI/cli.py: 0%
27 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-05 14:48 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-05 14:48 +0000
1#!/usr/bin/env python3
2"""Sparkle CLI entry point."""
3import sys
4import os
5from pathlib import Path
8def commands() -> list[str]:
9 """Get list of available commands."""
10 module_path = Path(__file__).parent.resolve()
11 self_name = Path(__file__).name
12 return [path.stem for path in module_path.iterdir()
13 if path.is_file() and path.suffix == ".py" and path.name != self_name]
16def main() -> None:
17 """Pass through command to launch CLI commands."""
18 module_path = Path(__file__).parent.resolve()
19 max_space = max([path.name.count("_") for path in module_path.iterdir()
20 if path.is_file()])
21 if len(sys.argv) < 2:
22 print("Usage: sparkle <command>")
23 sys.exit(1)
24 # Support spaces instead of _
25 possible_commands = commands()
26 for i in range(1, min(max_space, len(sys.argv))):
27 command = "_".join(sys.argv[1:i + 1])
28 args = sys.argv[i + 1:]
29 command_file = module_path / f"{command}.py"
30 if command in possible_commands:
31 break
32 if command_file.is_file():
33 if not os.access(command_file, os.X_OK): # Pip installation changes exec rights
34 command_file.chmod(0o755)
35 os.system(f"{command_file} {' '.join(args)}")
36 else:
37 print(f"Does not understand command {command}")
40if __name__ == "__main__":
41 main()