From 36353479149cd76cac686d6f206c34bcd796964d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20P=C3=A9rez?= Date: Thu, 19 Jul 2018 15:50:07 +0200 Subject: [PATCH] Allow creation of scar binary with pyinstaller * Update copied files to work with pyinstaller * Add udocker tarball to binary --- src/providers/aws/functioncode.py | 25 ++++++++++++++++--------- src/utils.py | 12 +++++++++++- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/providers/aws/functioncode.py b/src/providers/aws/functioncode.py index 893ad176..5c0b4b18 100644 --- a/src/providers/aws/functioncode.py +++ b/src/providers/aws/functioncode.py @@ -80,16 +80,22 @@ def prepare_lambda_code(self): def add_mandatory_files(self): os.makedirs(self.scar_temporal_folder, exist_ok=True) - shutil.copy(self.supervisor_source, self.supervisor_dest) - shutil.copy(self.udocker_source, self.udocker_dest) + shutil.copy(utils.resource_path(self.supervisor_source), self.supervisor_dest) + shutil.copy(utils.resource_path(self.udocker_source), self.udocker_dest) os.makedirs(utils.join_paths(self.scar_temporal_folder, "src"), exist_ok=True) - shutil.copy(utils.join_paths(self.lambda_code_files_path, "__init__.py"), - utils.join_paths(self.scar_temporal_folder, "src/__init__.py")) - shutil.copy(utils.join_paths(self.src_path, "utils.py"), - utils.join_paths(self.scar_temporal_folder, "src/utils.py")) - shutil.copy(utils.join_paths(self.src_path, "exceptions.py"), - utils.join_paths(self.scar_temporal_folder, "src/exceptions.py")) + + initpy_source = utils.resource_path(utils.join_paths(self.lambda_code_files_path, "__init__.py")) + initpy_dest = utils.join_paths(self.scar_temporal_folder, "src/__init__.py") + shutil.copy(initpy_source, initpy_dest) + + utils_source = utils.resource_path(utils.join_paths(self.src_path, "utils.py")) + utils_dest = utils.join_paths(self.scar_temporal_folder, "src/utils.py") + shutil.copy(utils_source, utils_dest) + + exceptions_source = utils.resource_path(utils.join_paths(self.src_path, "exceptions.py")) + exceptions_dest = utils.join_paths(self.scar_temporal_folder, "src/exceptions.py") + shutil.copy(exceptions_source, exceptions_dest) self.set_environment_variable('UDOCKER_DIR', "/tmp/home/udocker") self.set_environment_variable('UDOCKER_LIB', "/var/task/udocker/lib/") @@ -138,7 +144,8 @@ def save_tmp_udocker_env(cls): if utils.is_value_in_dict(os.environ, 'UDOCKER_DIR'): cls.udocker_dir = os.environ['UDOCKER_DIR'] # Set temporal global vars - utils.set_environment_variable('UDOCKER_TARBALL', utils.join_paths(cls.lambda_code_files_path, "udocker-1.1.0-RC2.tar.gz")) + udocker_tarball = utils.resource_path(utils.join_paths(cls.lambda_code_files_path, "udocker-1.1.0-RC2.tar.gz")) + utils.set_environment_variable('UDOCKER_TARBALL', udocker_tarball) utils.set_environment_variable('UDOCKER_DIR', utils.join_paths(cls.scar_temporal_folder, "udocker")) @classmethod diff --git a/src/utils.py b/src/utils.py index fba8931d..9deef542 100644 --- a/src/utils.py +++ b/src/utils.py @@ -22,6 +22,16 @@ import tarfile import tempfile import uuid +import sys + +def resource_path(relative_path): + """ Get absolute path to resource, works for dev and for PyInstaller """ + try: + # PyInstaller creates a temp folder and stores path in _MEIPASS + base_path = sys._MEIPASS + except Exception: + base_path = os.path.abspath(".") + return os.path.join(base_path, relative_path) def join_paths(*paths): return os.path.join(*paths) @@ -127,7 +137,7 @@ def create_tar_gz(files_to_archive, destination_tar_path): for file_path in files_to_archive: tar.add(file_path, arcname=os.path.basename(file_path)) return destination_tar_path - + def extract_tar_gz(tar_path, destination_path): with tarfile.open(tar_path, "r:gz") as tar: tar.extractall(path=destination_path)