Skip to content

Commit

Permalink
Allow creation of scar binary with pyinstaller
Browse files Browse the repository at this point in the history
* Update copied files to work with pyinstaller
* Add udocker tarball to binary
  • Loading branch information
Alfonso Pérez authored Jul 19, 2018
1 parent 0581118 commit 3635347
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/providers/aws/functioncode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3635347

Please sign in to comment.