-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcon-shell.py
executable file
·115 lines (93 loc) · 3.51 KB
/
rcon-shell.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
"""
Python Rcon shell
~~~~~~~~~~~~~~~~~
Just call this file, and the magic happens ;)
"""
import argparse
import signal
import subprocess
import sys
import venv
from pathlib import Path
try:
import ensurepip # noqa
except ModuleNotFoundError as err:
print(err)
print('-'*100)
print('Error: Pip not available!')
print('Hint: "apt-get install python3-venv"\n')
raise
# rcon needs python >= 3.8 -> https://github.com/conqp/rcon/blob/master/setup.py
assert sys.version_info >= (3, 8), 'Python version is too old!'
if sys.platform == 'win32': # wtf
# Files under Windows, e.g.: .../.venv/Scripts/python3.exe
BIN_NAME = 'Scripts'
FILE_EXT = '.exe'
else:
# Files under Linux/Mac and all other than Windows, e.g.: .../.venv/bin/python3
BIN_NAME = 'bin'
FILE_EXT = ''
VENV_PATH = Path('.venv')
BIN_PATH = VENV_PATH / BIN_NAME
PYTHON_PATH = BIN_PATH / f'python3{FILE_EXT}'
PIP_PATH = BIN_PATH / f'pip{FILE_EXT}'
POETRY_PATH = BIN_PATH / f'poetry{FILE_EXT}'
# script file defined in pyproject.toml as [tool.poetry.scripts]
# (Under Windows: ".exe" not added!)
PROJECT_SHELL_SCRIPT = BIN_PATH / 'devshell'
def verbose_check_call(*popenargs):
popenargs = [str(arg) for arg in popenargs] # e.g.: Path() -> str for python 3.7
print(f'\n\n+ {" ".join(popenargs)}\n')
return subprocess.check_call(popenargs)
def noop_signal_handler(signal_num, frame):
"""
Signal handler that does nothing: Used to ignore "Ctrl-C" signals
"""
pass
if __name__ == '__main__':
if '--update' in sys.argv or '--help' in sys.argv:
parser = argparse.ArgumentParser(
prog=Path(__file__).name,
description='Developer shell',
epilog='...live long and prosper...'
)
parser.add_argument(
'--update', default=False, action='store_true',
help='Force create/upgrade virtual environment'
)
parser.add_argument(
'command_args',
nargs=argparse.ZERO_OR_MORE,
help='arguments to pass to dev-setup shell/cli',
)
options = parser.parse_args()
force_update = options.update
extra_args = sys.argv[2:]
else:
force_update = False
extra_args = sys.argv[1:]
# Create virtual env in ".../.venv/":
if not PYTHON_PATH.is_file() or force_update:
print('Create virtual env here:', VENV_PATH.absolute())
builder = venv.EnvBuilder(symlinks=True, upgrade=True, with_pip=True)
builder.create(env_dir=VENV_PATH)
# install/update "pip" and "poetry":
if not POETRY_PATH.is_file() or force_update:
# Note: Under Windows pip.exe can't replace this own .exe file, so use the module way:
verbose_check_call(PYTHON_PATH, '-m', 'pip', 'install', '-U', 'pip')
verbose_check_call(PIP_PATH, 'install', 'poetry')
# install / update via poetry
if not PROJECT_SHELL_SCRIPT.is_file():
verbose_check_call(POETRY_PATH, 'install')
elif force_update:
verbose_check_call(POETRY_PATH, 'update')
print('\nUpdate done.')
# The cmd2 shell should not abort on Ctrl-C => ignore "Interrupt from keyboard" signal:
signal.signal(signal.SIGINT, noop_signal_handler)
# Run project cmd shell via "setup.py" entrypoint:
# (Call it via python, because Windows sucks calling the file direct)
try:
verbose_check_call(PYTHON_PATH, PROJECT_SHELL_SCRIPT, *extra_args)
except subprocess.CalledProcessError as err:
sys.exit(err.returncode)