forked from jedie/DragonPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·71 lines (53 loc) · 1.65 KB
/
cli.py
File metadata and controls
executable file
·71 lines (53 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""
`./cli.py` is for normal usage.
Just call this file, and the magic happens ;)
The `uv` tool is required to run the development CLI.
e.g.: Install `uv` via `pipx`
apt-get install pipx
pipx install uv
"""
import os
import shlex
import shutil
import subprocess
import sys
from pathlib import Path
assert sys.version_info >= (3, 12), f'Python version {sys.version_info} is too old!'
# Create and use "/.venv-app/" for virtualenv:
VIRTUAL_ENV = '.venv-app'
def print_uv_error_and_exit():
print('\nError: "uv" command not found in PATH. Please install "uv" first!\n')
print('Hint:')
print('\tapt-get install pipx\n')
print('\tpipx install uv\n')
sys.exit(1)
def verbose_check_call(*popen_args, **extra_env):
print(f'\n+ {shlex.join(str(arg) for arg in popen_args)}\n')
env = {
'VIRTUAL_ENV': VIRTUAL_ENV,
'UV_VENV': VIRTUAL_ENV,
**os.environ,
**extra_env,
}
return subprocess.check_call(
popen_args,
env=env,
cwd=Path(__file__).parent, # Needed if called from other working directory
)
def main(argv):
uv_bin = shutil.which('uv') # Ensure 'uv' is available in PATH
if not uv_bin:
print_uv_error_and_exit()
if not Path(VIRTUAL_ENV).is_dir():
verbose_check_call(uv_bin, 'venv', VIRTUAL_ENV)
# Call our entry point CLI:
try:
verbose_check_call(uv_bin, 'run', '--active', '-m', 'dragonpy', *argv[1:])
except subprocess.CalledProcessError as err:
sys.exit(err.returncode)
except KeyboardInterrupt:
print('Bye!')
sys.exit(130)
if __name__ == '__main__':
main(sys.argv)