From d4c8d674332e96cf0c34d430cbde54aaaa8d4d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Tue, 10 Dec 2019 16:26:47 -0300 Subject: [PATCH] pre-downloading electron files during bauh initialization --- bauh/gems/web/__init__.py | 5 ++- bauh/gems/web/controller.py | 5 ++- bauh/gems/web/environment.py | 78 +++++++++++++++++++++++++++++++++--- bauh/view/core/downloader.py | 2 +- 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/bauh/gems/web/__init__.py b/bauh/gems/web/__init__.py index 3727b55f..dea2326d 100644 --- a/bauh/gems/web/__init__.py +++ b/bauh/gems/web/__init__.py @@ -9,4 +9,7 @@ NODE_BIN_PATH = '{}/bin/node'.format(NODE_DIR_PATH) NPM_BIN_PATH = '{}/bin/npm'.format(NODE_DIR_PATH) NODE_MODULES_PATH = '{}/node_modules'.format(BIN_PATH) NATIVEFIER_BIN_PATH = '{}/.bin/nativefier'.format(NODE_MODULES_PATH) - +ELECTRON_PATH = '{}/.cache/electron'.format(HOME_PATH) +ELECTRON_DOWNLOAD_URL = 'https://github.com/electron/electron/releases/download/v{version}/electron-v{version}-linux-{arch}.zip' +ELECTRON_SHA256_URL = 'https://github.com/electron/electron/releases/download/v{version}/SHASUMS256.txt' +ELECTRON_VERSION = '0.5.12' diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index 576a3e14..2dbb6dc4 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -96,9 +96,12 @@ class WebApplicationManager(SoftwareManager): def requires_root(self, action: str, pkg: SoftwarePackage): return False + def _update_environment(self): + self.node_updater.update_environment(self.context.is_system_x86_64()) + def prepare(self): if bool(int(os.getenv('BAUH_WEB_UPDATE_NODE', 1))): - Thread(daemon=True, target=self.node_updater.update_environment).start() + Thread(daemon=True, target=self._update_environment).start() def list_updates(self, internet_available: bool) -> List[PackageUpdate]: pass diff --git a/bauh/gems/web/environment.py b/bauh/gems/web/environment.py index 0c357697..848b5b64 100644 --- a/bauh/gems/web/environment.py +++ b/bauh/gems/web/environment.py @@ -9,8 +9,9 @@ from bauh.api.abstract.download import FileDownloader from bauh.api.abstract.handler import ProcessWatcher from bauh.api.exception import NoInternetException from bauh.commons import system -from bauh.commons.system import new_subprocess, SimpleProcess, ProcessHandler -from bauh.gems.web import BIN_PATH, NODE_DIR_PATH, NODE_BIN_PATH, NPM_BIN_PATH, NODE_MODULES_PATH, NATIVEFIER_BIN_PATH +from bauh.commons.system import SimpleProcess, ProcessHandler, run_cmd +from bauh.gems.web import BIN_PATH, NODE_DIR_PATH, NODE_BIN_PATH, NPM_BIN_PATH, NODE_MODULES_PATH, NATIVEFIER_BIN_PATH, \ + ELECTRON_PATH, ELECTRON_DOWNLOAD_URL, ELECTRON_SHA256_URL, ELECTRON_VERSION from bauh.view.util.translation import I18n @@ -135,8 +136,75 @@ class NodeUpdater: self.logger.info("Nativefier is already installed") return True - def update_environment(self, handler: ProcessHandler = None): - if not self.update_node(handler.watcher if handler else None): + def _download_electron(self, version: str, is_x86_x64_arch: bool, watcher: ProcessWatcher) -> bool: + self.logger.info("Downloading Electron {}".format(version)) + arch = 'ia32' if not is_x86_x64_arch else 'x64' + + Path(ELECTRON_PATH).mkdir(parents=True, exist_ok=True) + + electron_url = ELECTRON_DOWNLOAD_URL.format(arch=arch, version=version) + electron_path = '{}/{}'.format(ELECTRON_PATH, electron_url.split('/')[-1]) + + return self.file_downloader.download(file_url=electron_url, watcher=watcher, output_path=electron_path,cwd=ELECTRON_PATH) + + def _download_electron_sha256(self, version: str, watcher: ProcessWatcher) -> bool: + self.logger.info("Downloading Electron {} sha526".format(version)) + + Path(ELECTRON_PATH).mkdir(parents=True, exist_ok=True) + + sha256_url = ELECTRON_SHA256_URL.format(version=version) + sha256_path = '{}/{}'.format(ELECTRON_PATH, sha256_url.split('/')[-1]) + return self.file_downloader.download(file_url=sha256_url, watcher=watcher, output_path=sha256_path, cwd=ELECTRON_PATH) + + def install_electron(self, version: str, is_x86_x64_arch: bool, watcher: ProcessWatcher) -> bool: + self.logger.info("Checking installed Electron") + + if not os.path.exists(ELECTRON_PATH): + self.logger.info("Electron is not installed") + self._download_electron(version=version, is_x86_x64_arch=is_x86_x64_arch, watcher=watcher) + else: + files = run_cmd('ls', print_error=False, cwd=ELECTRON_PATH) + + if files: + file_list = files.split('\n') + file_name = ELECTRON_DOWNLOAD_URL.format(version=version, arch='x64' if is_x86_x64_arch else 'ia32').split('/')[-1] + electron_file = [f for f in file_list if f == file_name] + + if electron_file: + self.logger.info("Electron {} already downloaded".format(version)) + else: + if not self._download_electron(version=version, is_x86_x64_arch=is_x86_x64_arch, watcher=watcher): + return False + + file_name = ELECTRON_SHA256_URL.split('/')[-1] + sha256_file = [f for f in file_list if f == file_name] + + if sha256_file: + self.logger.info("Electron {} sha256 already downloaded".format(version)) + return True + else: + return self._download_electron_sha256(version=version, watcher=watcher) + + self.logger.info('No Electron file found') + if self._download_electron(version=version, is_x86_x64_arch=is_x86_x64_arch, watcher=watcher): + return self._download_electron_sha256(version=version, watcher=watcher) + return False - return self.install_nativefier(handler=handler) + def update_environment(self, is_x86_x64_arch: bool, handler: ProcessHandler = None): + if not self.update_node(handler.watcher if handler else None): + self.logger.warning('Could not update the environment') + return False + + if not self.install_nativefier(handler=handler): + self.logger.warning('Could not update the environment') + return False + + res = self.install_electron(version=ELECTRON_VERSION, is_x86_x64_arch=is_x86_x64_arch, watcher=handler.watcher if handler else None) + + if res: + self.logger.info('Environment updated') + else: + self.logger.warning('Could not update the environment') + + return res diff --git a/bauh/view/core/downloader.py b/bauh/view/core/downloader.py index fc5f45d1..2b5f0a48 100644 --- a/bauh/view/core/downloader.py +++ b/bauh/view/core/downloader.py @@ -65,7 +65,7 @@ class AdaptableFileDownloader(FileDownloader): self.logger.info('Removing downloaded file {}'.format(to_delete)) os.remove(to_delete) - def download(self, file_url: str, watcher: ProcessWatcher, output_path: str, cwd: str) -> bool: + def download(self, file_url: str, watcher: ProcessWatcher, output_path: str = None, cwd: str = None) -> bool: self.logger.info('Downloading {}'.format(file_url)) handler = ProcessHandler(watcher) file_name = file_url.split('/')[-1]