Skip to content

Commit fab501f

Browse files
committed
CHANGES:
- run.py now detects, if libraries are missing and installs them automatically.
1 parent ad15b1b commit fab501f

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

core/commandline.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
parser.add_argument('-n', '--node', help='Node name', default=platform.node())
2424
parser.add_argument('-r', '--no-restart', action='store_true', default=False,
2525
help="don't start DCSServerBot after the update")
26+
parser.add_argument('-i', '--install', action='store_true', default=False,
27+
help='Install requirements.txt only')
2628
elif program == 'install.py':
2729
parser.add_argument('-n', '--node', help='Node name', default=platform.node())
2830
parser.add_argument('-u', '--user', help='Database username', default='dcsserverbot')

core/data/impl/nodeimpl.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,23 @@ def __init__(self, name: str, config_dir: Optional[str] = 'config'):
9696
self.before_update: dict[str, Callable[[], Awaitable[Any]]] = {}
9797
self.after_update: dict[str, Callable[[], Awaitable[Any]]] = {}
9898
self.locals = self.read_locals()
99+
self.db_version = None
100+
self.pool: Optional[ConnectionPool] = None
101+
self.apool: Optional[AsyncConnectionPool] = None
102+
self._master = None
103+
self.listen_address = self.locals.get('listen_address', '127.0.0.1')
104+
if self.listen_address != '127.0.0.1':
105+
self.log.warning(
106+
'Please consider changing the listen_address in your nodes.yaml to 127.0.0.1 for security reasons!')
107+
self.listen_port = self.locals.get('listen_port', 10042)
108+
109+
async def __aenter__(self):
99110
if sys.platform == 'win32':
100111
from os import system
101112
system(f"title DCSServerBot v{self.bot_version}.{self.sub_version} - {self.node.name}")
102113
self.log.info(f'DCSServerBot v{self.bot_version}.{self.sub_version} starting up ...')
103114
self.log.info(f'- Python version {platform.python_version()} detected.')
104-
self.install_plugins()
115+
await asyncio.to_thread(self.install_plugins)
105116
self.plugins: list[str] = [x.lower() for x in self.config.get('plugins', DEFAULT_PLUGINS)]
106117
for plugin in [x.lower() for x in self.config.get('opt_plugins', [])]:
107118
if plugin not in self.plugins:
@@ -110,17 +121,6 @@ def __init__(self, name: str, config_dir: Optional[str] = 'config'):
110121
if 'cloud' in self.plugins:
111122
self.plugins.remove('cloud')
112123
self.plugins.append('cloud')
113-
self.db_version = None
114-
self.pool: Optional[ConnectionPool] = None
115-
self.apool: Optional[AsyncConnectionPool] = None
116-
self._master = None
117-
self.listen_address = self.locals.get('listen_address', '127.0.0.1')
118-
if self.listen_address != '127.0.0.1':
119-
self.log.warning(
120-
'Please consider changing the listen_address in your nodes.yaml to 127.0.0.1 for security reasons!')
121-
self.listen_port = self.locals.get('listen_port', 10042)
122-
123-
async def __aenter__(self):
124124
return self
125125

126126
async def __aexit__(self, type, value, traceback):

run.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
# Default imports
34
import asyncio
45
import certifi
56
import discord
@@ -11,20 +12,29 @@
1112
import sys
1213
import time
1314

14-
from core import (
15-
NodeImpl, ServiceRegistry, ServiceInstallationError, utils, YAMLError, FatalException, COMMAND_LINE_ARGS,
16-
CloudRotatingFileHandler
17-
)
1815
from datetime import datetime
19-
from pid import PidFile, PidFileError
20-
from rich import print
21-
from rich.console import Console
22-
from rich.logging import RichHandler
23-
from rich.text import Text
2416

25-
# ruamel YAML support
26-
from ruamel.yaml import YAML
27-
yaml = YAML()
17+
# DCSServerBot imports
18+
try:
19+
from core import (
20+
NodeImpl, ServiceRegistry, ServiceInstallationError, utils, YAMLError, FatalException, COMMAND_LINE_ARGS,
21+
CloudRotatingFileHandler
22+
)
23+
from pid import PidFile, PidFileError
24+
from rich import print
25+
from rich.console import Console
26+
from rich.logging import RichHandler
27+
from rich.text import Text
28+
29+
# ruamel YAML support
30+
from ruamel.yaml import YAML
31+
yaml = YAML()
32+
except ModuleNotFoundError as ex:
33+
import subprocess
34+
35+
print(f"Module {ex.name} is not installed, fixing ...")
36+
subprocess.run([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
37+
exit(-1)
2838

2939
LOGLEVEL = {
3040
'DEBUG': logging.DEBUG,

update.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from version import __version__
1818

1919

20+
def install_requirements() -> int:
21+
return subprocess.run([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
22+
23+
2024
def do_update_git() -> int:
2125
import git
2226

@@ -38,7 +42,7 @@ def do_update_git() -> int:
3842
print(' => DCSServerBot updated to the latest version.')
3943
if modules:
4044
print(' => requirements.txt has changed. Installing missing modules...')
41-
rc = subprocess.run([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
45+
rc = install_requirements()
4246
if rc.returncode:
4347
print(' => Autoupdate failed!')
4448
print(' Please run update.cmd manually.')
@@ -142,7 +146,7 @@ def do_update_github() -> int:
142146

143147
# move file
144148
shutil.copy2(old_file_path, new_file_path)
145-
rc = subprocess.run([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
149+
rc = install_requirements()
146150
if rc.returncode:
147151
print(' => Autoupdate failed!')
148152
return -1
@@ -156,7 +160,15 @@ def do_update_github() -> int:
156160
# get the command line args from core
157161
args = COMMAND_LINE_ARGS
158162
try:
159-
rc = do_update_git()
163+
if args.install:
164+
rc = install_requirements()
165+
if rc.returncode:
166+
print("Unable to install or update 'requirements.txt'. Please try installing it manually.")
167+
print("To do so, open 'cmd.exe' in the DCSServerBot installation directory, and type:")
168+
print('"%USERPROFILE%\\.dcssb\\Script\\pip" install -r requirements.txt')
169+
exit(-2)
170+
else:
171+
rc = do_update_git()
160172
except ImportError:
161173
rc = do_update_github()
162174
if args.no_restart:

0 commit comments

Comments
 (0)