mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 04:44:15 +02:00
[web.environment] refactoring: String formatting method
This commit is contained in:
@@ -50,48 +50,48 @@ class EnvironmentUpdater:
|
|||||||
self.taskman = taskman
|
self.taskman = taskman
|
||||||
|
|
||||||
def _download_and_install(self, version: str, version_url: str, watcher: ProcessWatcher) -> bool:
|
def _download_and_install(self, version: str, version_url: str, watcher: ProcessWatcher) -> bool:
|
||||||
self.logger.info("Downloading NodeJS {}: {}".format(version, version_url))
|
self.logger.info(f"Downloading NodeJS {version}: {version_url}")
|
||||||
|
|
||||||
tarf_path = '{}/{}'.format(ENV_PATH, version_url.split('/')[-1])
|
tarf_path = f"{ENV_PATH}/{version_url.split('/')[-1]}"
|
||||||
downloaded = self.file_downloader.download(version_url, watcher=watcher, output_path=tarf_path, cwd=ENV_PATH)
|
downloaded = self.file_downloader.download(version_url, watcher=watcher, output_path=tarf_path, cwd=ENV_PATH)
|
||||||
|
|
||||||
if not downloaded:
|
if not downloaded:
|
||||||
self.logger.error("Could not download '{}'. Aborting...".format(version_url))
|
self.logger.error(f"Could not download '{version_url}'. Aborting...")
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
tf = tarfile.open(tarf_path)
|
tf = tarfile.open(tarf_path)
|
||||||
tf.extractall(path=ENV_PATH)
|
tf.extractall(path=ENV_PATH)
|
||||||
|
|
||||||
extracted_file = '{}/{}'.format(ENV_PATH, tf.getnames()[0])
|
extracted_file = f'{ENV_PATH}/{tf.getnames()[0]}'
|
||||||
|
|
||||||
if os.path.exists(NODE_DIR_PATH):
|
if os.path.exists(NODE_DIR_PATH):
|
||||||
self.logger.info("Removing old NodeJS version installation dir -> {}".format(NODE_DIR_PATH))
|
self.logger.info(f"Removing old NodeJS version installation dir -> {NODE_DIR_PATH}")
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(NODE_DIR_PATH)
|
shutil.rmtree(NODE_DIR_PATH)
|
||||||
except:
|
except:
|
||||||
self.logger.error("Could not delete old NodeJS version dir -> {}".format(NODE_DIR_PATH))
|
self.logger.error(f"Could not delete old NodeJS version dir -> {NODE_DIR_PATH}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.rename(extracted_file, NODE_DIR_PATH)
|
os.rename(extracted_file, NODE_DIR_PATH)
|
||||||
except:
|
except:
|
||||||
self.logger.error("Could not rename the NodeJS version file {} as {}".format(extracted_file, NODE_DIR_PATH))
|
self.logger.error(f"Could not rename the NodeJS version file {extracted_file} as {NODE_DIR_PATH}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if os.path.exists(NODE_MODULES_PATH):
|
if os.path.exists(NODE_MODULES_PATH):
|
||||||
self.logger.info('Deleting {}'.format(NODE_MODULES_PATH))
|
self.logger.info(f'Deleting {NODE_MODULES_PATH}')
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(NODE_MODULES_PATH)
|
shutil.rmtree(NODE_MODULES_PATH)
|
||||||
except:
|
except:
|
||||||
self.logger.error("Could not delete the directory {}".format(NODE_MODULES_PATH))
|
self.logger.error(f"Could not delete the directory {NODE_MODULES_PATH}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
except:
|
except:
|
||||||
self.logger.error('Could not extract {}'.format(tarf_path))
|
self.logger.error(f'Could not extract {tarf_path}')
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False
|
return False
|
||||||
finally:
|
finally:
|
||||||
@@ -99,13 +99,13 @@ class EnvironmentUpdater:
|
|||||||
try:
|
try:
|
||||||
os.remove(tarf_path)
|
os.remove(tarf_path)
|
||||||
except:
|
except:
|
||||||
self.logger.error('Could not delete file {}'.format(tarf_path))
|
self.logger.error(f'Could not delete file {tarf_path}')
|
||||||
|
|
||||||
def check_node_installed(self, version: str) -> bool:
|
def check_node_installed(self, version: str) -> bool:
|
||||||
if not os.path.exists(NODE_DIR_PATH):
|
if not os.path.exists(NODE_DIR_PATH):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
installed_version = system.run_cmd('{} --version'.format(NODE_BIN_PATH), print_error=False)
|
installed_version = system.run_cmd(f'{NODE_BIN_PATH} --version', print_error=False)
|
||||||
|
|
||||||
if installed_version:
|
if installed_version:
|
||||||
installed_version = installed_version.strip()
|
installed_version = installed_version.strip()
|
||||||
@@ -113,7 +113,7 @@ class EnvironmentUpdater:
|
|||||||
if installed_version.startswith('v'):
|
if installed_version.startswith('v'):
|
||||||
installed_version = installed_version[1:]
|
installed_version = installed_version[1:]
|
||||||
|
|
||||||
self.logger.info('Node versions: installed ({}), cloud ({})'.format(installed_version, version))
|
self.logger.info(f'Node versions: installed ({installed_version}), cloud ({version})')
|
||||||
|
|
||||||
if version != installed_version:
|
if version != installed_version:
|
||||||
self.logger.info("The NodeJs installed version is different from the Cloud.")
|
self.logger.info("The NodeJs installed version is different from the Cloud.")
|
||||||
@@ -139,7 +139,7 @@ class EnvironmentUpdater:
|
|||||||
if installed_version.startswith('v'):
|
if installed_version.startswith('v'):
|
||||||
installed_version = installed_version[1:]
|
installed_version = installed_version[1:]
|
||||||
|
|
||||||
self.logger.info('Node versions: installed ({}), cloud ({})'.format(installed_version, version))
|
self.logger.info(f'Node versions: installed ({installed_version}), cloud ({version})')
|
||||||
|
|
||||||
if version != installed_version:
|
if version != installed_version:
|
||||||
self.logger.info("The NodeJs installed version is different from the Cloud.")
|
self.logger.info("The NodeJs installed version is different from the Cloud.")
|
||||||
@@ -149,17 +149,17 @@ class EnvironmentUpdater:
|
|||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
self.logger.warning("Could not determine the current NodeJS installed version")
|
self.logger.warning("Could not determine the current NodeJS installed version")
|
||||||
self.logger.info("Removing {}".format(NODE_DIR_PATH))
|
self.logger.info(f"Removing {NODE_DIR_PATH}")
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(NODE_DIR_PATH)
|
shutil.rmtree(NODE_DIR_PATH)
|
||||||
return self._download_and_install(version=version, version_url=version_url, watcher=watcher)
|
return self._download_and_install(version=version, version_url=version_url, watcher=watcher)
|
||||||
except:
|
except:
|
||||||
self.logger.error('Could not delete the dir {}'.format(NODE_DIR_PATH))
|
self.logger.error(f'Could not delete the dir {NODE_DIR_PATH}')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _install_node_lib(self, name: str, version: str, handler: ProcessHandler):
|
def _install_node_lib(self, name: str, version: str, handler: ProcessHandler):
|
||||||
lib_repr = '{}{}'.format(name, '@{}'.format(version) if version else '')
|
lib_repr = f"{name}{'@{}'.format(version) if version else ''}"
|
||||||
self.logger.info("Installing {}".format(lib_repr))
|
self.logger.info(f"Installing {lib_repr}")
|
||||||
|
|
||||||
if handler and handler.watcher:
|
if handler and handler.watcher:
|
||||||
handler.watcher.change_substatus(self.i18n['web.environment.install'].format(bold(lib_repr)))
|
handler.watcher.change_substatus(self.i18n['web.environment.install'].format(bold(lib_repr)))
|
||||||
@@ -169,15 +169,15 @@ class EnvironmentUpdater:
|
|||||||
installed = handler.handle_simple(proc)[0]
|
installed = handler.handle_simple(proc)[0]
|
||||||
|
|
||||||
if installed:
|
if installed:
|
||||||
self.logger.info("{} successfully installed".format(lib_repr))
|
self.logger.info(f"{lib_repr} successfully installed")
|
||||||
|
|
||||||
return installed
|
return installed
|
||||||
|
|
||||||
def _install_nativefier(self, version: str, url: str, handler: ProcessHandler) -> bool:
|
def _install_nativefier(self, version: str, url: str, handler: ProcessHandler) -> bool:
|
||||||
self.logger.info("Checking if nativefier@{} exists".format(version))
|
self.logger.info(f"Checking if nativefier@{version} exists")
|
||||||
|
|
||||||
if not url or not self.http_client.exists(url):
|
if not url or not self.http_client.exists(url):
|
||||||
self.logger.warning("The file {} seems not to exist".format(url))
|
self.logger.warning(f"The file {url} seems not to exist")
|
||||||
handler.watcher.show_message(title=self.i18n['message.file.not_exist'],
|
handler.watcher.show_message(title=self.i18n['message.file.not_exist'],
|
||||||
body=self.i18n['message.file.not_exist.body'].format(bold(url)),
|
body=self.i18n['message.file.not_exist.body'].format(bold(url)),
|
||||||
type_=MessageType.ERROR)
|
type_=MessageType.ERROR)
|
||||||
@@ -192,12 +192,12 @@ class EnvironmentUpdater:
|
|||||||
return os.path.exists(NATIVEFIER_BIN_PATH)
|
return os.path.exists(NATIVEFIER_BIN_PATH)
|
||||||
|
|
||||||
def download_electron(self, version: str, url: str, widevine: bool, watcher: ProcessWatcher) -> bool:
|
def download_electron(self, version: str, url: str, widevine: bool, watcher: ProcessWatcher) -> bool:
|
||||||
self.logger.info("Downloading Electron {}".format(version))
|
self.logger.info(f"Downloading Electron {version}")
|
||||||
|
|
||||||
electron_path = self._get_electron_file_path(url=url, relative=False)
|
electron_path = self._get_electron_file_path(url=url, relative=False)
|
||||||
|
|
||||||
if not self.http_client.exists(url):
|
if not self.http_client.exists(url):
|
||||||
self.logger.warning("The file {} seems not to exist".format(url))
|
self.logger.warning(f"The file {url} seems not to exist")
|
||||||
watcher.show_message(title=self.i18n['message.file.not_exist'],
|
watcher.show_message(title=self.i18n['message.file.not_exist'],
|
||||||
body=self.i18n['message.file.not_exist.body'].format(bold(url)),
|
body=self.i18n['message.file.not_exist.body'].format(bold(url)),
|
||||||
type_=MessageType.ERROR)
|
type_=MessageType.ERROR)
|
||||||
@@ -206,12 +206,12 @@ class EnvironmentUpdater:
|
|||||||
return self.file_downloader.download(file_url=url, watcher=watcher, output_path=electron_path, cwd=ELECTRON_CACHE_DIR)
|
return self.file_downloader.download(file_url=url, watcher=watcher, output_path=electron_path, cwd=ELECTRON_CACHE_DIR)
|
||||||
|
|
||||||
def download_electron_sha256(self, version: str, url: str, widevine: bool, watcher: ProcessWatcher) -> bool:
|
def download_electron_sha256(self, version: str, url: str, widevine: bool, watcher: ProcessWatcher) -> bool:
|
||||||
self.logger.info("Downloading Electron {} sha526".format(version))
|
self.logger.info(f"Downloading Electron {version} sha526")
|
||||||
|
|
||||||
sha256_path = self._get_electron_file_path(url=url, relative=False)
|
sha256_path = self._get_electron_file_path(url=url, relative=False)
|
||||||
|
|
||||||
if not self.http_client.exists(url):
|
if not self.http_client.exists(url):
|
||||||
self.logger.warning("The file {} seems not to exist".format(url))
|
self.logger.warning(f"The file {url} seems not to exist")
|
||||||
watcher.show_message(title=self.i18n['message.file.not_exist'],
|
watcher.show_message(title=self.i18n['message.file.not_exist'],
|
||||||
body=self.i18n['message.file.not_exist.body'].format(bold(url)),
|
body=self.i18n['message.file.not_exist.body'].format(bold(url)),
|
||||||
type_=MessageType.ERROR)
|
type_=MessageType.ERROR)
|
||||||
@@ -237,7 +237,7 @@ class EnvironmentUpdater:
|
|||||||
return f'{ELECTRON_CACHE_DIR}/{file_path}' if not relative else file_path
|
return f'{ELECTRON_CACHE_DIR}/{file_path}' if not relative else file_path
|
||||||
|
|
||||||
def check_electron_installed(self, version: str, is_x86_x64_arch: bool, widevine: bool) -> Dict[str, bool]:
|
def check_electron_installed(self, version: str, is_x86_x64_arch: bool, widevine: bool) -> Dict[str, bool]:
|
||||||
self.logger.info("Checking if Electron {} (widevine={}) is installed".format(version, widevine))
|
self.logger.info(f"Checking if Electron {version} (widevine={widevine}) is installed")
|
||||||
res = {'electron': False, 'sha256': False}
|
res = {'electron': False, 'sha256': False}
|
||||||
|
|
||||||
if not os.path.exists(ELECTRON_CACHE_DIR):
|
if not os.path.exists(ELECTRON_CACHE_DIR):
|
||||||
@@ -262,7 +262,7 @@ class EnvironmentUpdater:
|
|||||||
|
|
||||||
for att in ('electron', 'sha256'):
|
for att in ('electron', 'sha256'):
|
||||||
if res[att]:
|
if res[att]:
|
||||||
self.logger.info('{} ({}) already downloaded'.format(att, version))
|
self.logger.info(f'{att} ({version}) already downloaded')
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@@ -275,7 +275,7 @@ class EnvironmentUpdater:
|
|||||||
try:
|
try:
|
||||||
settings_exp = int(web_config['environment']['cache_exp'])
|
settings_exp = int(web_config['environment']['cache_exp'])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.logger.error("Could not parse settings property 'environment.cache_exp': {}".format(web_config['environment']['cache_exp']))
|
self.logger.error(f"Could not parse settings property 'environment.cache_exp': {web_config['environment']['cache_exp']}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if settings_exp <= 0:
|
if settings_exp <= 0:
|
||||||
@@ -298,7 +298,7 @@ class EnvironmentUpdater:
|
|||||||
try:
|
try:
|
||||||
env_timestamp = datetime.fromtimestamp(float(env_ts_str))
|
env_timestamp = datetime.fromtimestamp(float(env_ts_str))
|
||||||
except:
|
except:
|
||||||
self.logger.error("Could not parse environment settings file timestamp: {}".format(env_ts_str))
|
self.logger.error(f"Could not parse environment settings file timestamp: {env_ts_str}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
expired = env_timestamp + timedelta(hours=settings_exp) <= datetime.utcnow()
|
expired = env_timestamp + timedelta(hours=settings_exp) <= datetime.utcnow()
|
||||||
@@ -318,7 +318,7 @@ class EnvironmentUpdater:
|
|||||||
try:
|
try:
|
||||||
return yaml.safe_load(cached_settings_str)
|
return yaml.safe_load(cached_settings_str)
|
||||||
except yaml.YAMLError:
|
except yaml.YAMLError:
|
||||||
self.logger.error('Could not parse the cache environment settings file: {}'.format(cached_settings_str))
|
self.logger.error(f'Could not parse the cache environment settings file: {cached_settings_str}')
|
||||||
|
|
||||||
def read_settings(self, web_config: dict, cache: bool = True) -> Optional[dict]:
|
def read_settings(self, web_config: dict, cache: bool = True) -> Optional[dict]:
|
||||||
if self.taskman:
|
if self.taskman:
|
||||||
@@ -345,7 +345,7 @@ class EnvironmentUpdater:
|
|||||||
try:
|
try:
|
||||||
settings = yaml.safe_load(res.content)
|
settings = yaml.safe_load(res.content)
|
||||||
except yaml.YAMLError:
|
except yaml.YAMLError:
|
||||||
self.logger.error('Could not parse environment settings: {}'.format(res.text))
|
self.logger.error(f'Could not parse environment settings: {res.text}')
|
||||||
self._finish_task_download_settings()
|
self._finish_task_download_settings()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -355,7 +355,7 @@ class EnvironmentUpdater:
|
|||||||
try:
|
try:
|
||||||
Path(cache_dir).mkdir(parents=True, exist_ok=True)
|
Path(cache_dir).mkdir(parents=True, exist_ok=True)
|
||||||
except OSError:
|
except OSError:
|
||||||
self.logger.error("Could not create Web cache directory: {}".format(cache_dir))
|
self.logger.error(f"Could not create Web cache directory: {cache_dir}")
|
||||||
self.logger.info('Finished')
|
self.logger.info('Finished')
|
||||||
self._finish_task_download_settings()
|
self._finish_task_download_settings()
|
||||||
return
|
return
|
||||||
@@ -379,11 +379,11 @@ class EnvironmentUpdater:
|
|||||||
electron_version = env['electron-wvvmp' if widevine else 'electron']['version']
|
electron_version = env['electron-wvvmp' if widevine else 'electron']['version']
|
||||||
|
|
||||||
if not widevine and pkg.version and pkg.version != electron_version: # this feature does not support custom widevine electron at the moment
|
if not widevine and pkg.version and pkg.version != electron_version: # this feature does not support custom widevine electron at the moment
|
||||||
self.logger.info('A preset Electron version is defined for {}: {}'.format(pkg.url, pkg.version))
|
self.logger.info(f'A preset Electron version is defined for {pkg.url}: {pkg.version}')
|
||||||
electron_version = pkg.version
|
electron_version = pkg.version
|
||||||
|
|
||||||
if not widevine and local_config['environment']['electron']['version']:
|
if not widevine and local_config['environment']['electron']['version']:
|
||||||
self.logger.warning("A custom Electron version will be used {} to install {}".format(electron_version, pkg.url))
|
self.logger.warning(f"A custom Electron version will be used {electron_version} to install {pkg.url}")
|
||||||
electron_version = local_config['environment']['electron']['version']
|
electron_version = local_config['environment']['electron']['version']
|
||||||
|
|
||||||
electron_status = self.check_electron_installed(version=electron_version, is_x86_x64_arch=x86_x64, widevine=widevine)
|
electron_status = self.check_electron_installed(version=electron_version, is_x86_x64_arch=x86_x64, widevine=widevine)
|
||||||
@@ -426,7 +426,7 @@ class EnvironmentUpdater:
|
|||||||
|
|
||||||
def _check_nativefier_installed(self, nativefier_settings: dict) -> bool:
|
def _check_nativefier_installed(self, nativefier_settings: dict) -> bool:
|
||||||
if not os.path.exists(NODE_MODULES_PATH):
|
if not os.path.exists(NODE_MODULES_PATH):
|
||||||
self.logger.info('Node modules path {} not found'.format(NODE_MODULES_PATH))
|
self.logger.info(f'Node modules path {NODE_MODULES_PATH} not found')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
if not self._is_nativefier_installed():
|
if not self._is_nativefier_installed():
|
||||||
@@ -437,7 +437,7 @@ class EnvironmentUpdater:
|
|||||||
if installed_version:
|
if installed_version:
|
||||||
installed_version = installed_version.strip()
|
installed_version = installed_version.strip()
|
||||||
|
|
||||||
self.logger.info("Nativefier versions: installed ({}), cloud ({})".format(installed_version, nativefier_settings['version']))
|
self.logger.info(f"Nativefier versions: installed ({installed_version}), cloud ({nativefier_settings['version']})")
|
||||||
|
|
||||||
if nativefier_settings['version'] != installed_version:
|
if nativefier_settings['version'] != installed_version:
|
||||||
self.logger.info("Installed nativefier version is different from cloud's. Changing version.")
|
self.logger.info("Installed nativefier version is different from cloud's. Changing version.")
|
||||||
@@ -449,11 +449,11 @@ class EnvironmentUpdater:
|
|||||||
def _map_nativefier_file(self, nativefier_settings: dict) -> EnvironmentComponent:
|
def _map_nativefier_file(self, nativefier_settings: dict) -> EnvironmentComponent:
|
||||||
base_url = nativefier_settings.get('url')
|
base_url = nativefier_settings.get('url')
|
||||||
if not base_url:
|
if not base_url:
|
||||||
self.logger.warning("'url' not found in nativefier environment settings. Using hardcoded URL '{}'".format(NATIVEFIER_BASE_URL))
|
self.logger.warning(f"'url' not found in nativefier environment settings. Using hardcoded URL '{NATIVEFIER_BASE_URL}'")
|
||||||
base_url = NATIVEFIER_BASE_URL
|
base_url = NATIVEFIER_BASE_URL
|
||||||
|
|
||||||
url = base_url.format(version=nativefier_settings['version'])
|
url = base_url.format(version=nativefier_settings['version'])
|
||||||
return EnvironmentComponent(name='nativefier@{}'.format(nativefier_settings['version']),
|
return EnvironmentComponent(name=f"nativefier@{nativefier_settings['version']}",
|
||||||
url=url,
|
url=url,
|
||||||
size=self.http_client.get_content_length(url),
|
size=self.http_client.get_content_length(url),
|
||||||
version=nativefier_settings['version'],
|
version=nativefier_settings['version'],
|
||||||
@@ -471,7 +471,7 @@ class EnvironmentUpdater:
|
|||||||
system_env = local_config['environment'].get('system', False)
|
system_env = local_config['environment'].get('system', False)
|
||||||
|
|
||||||
if system_env:
|
if system_env:
|
||||||
self.logger.warning("Using system's nativefier to install {}".format(app.url))
|
self.logger.warning(f"Using system's nativefier to install {app.url}")
|
||||||
else:
|
else:
|
||||||
node_check = Thread(target=self._check_and_fill_node, args=(env, components))
|
node_check = Thread(target=self._check_and_fill_node, args=(env, components))
|
||||||
node_check.start()
|
node_check.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user