From ea78346eb9a49fd9b5d43e7f81277612e2c99fad Mon Sep 17 00:00:00 2001 From: Mathieu Carette Date: Tue, 24 May 2016 10:14:54 +0200 Subject: [PATCH] Use complete conda environment, not just site-packages --- zappa/zappa.py | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/zappa/zappa.py b/zappa/zappa.py index 6146f455f..503dcc59e 100755 --- a/zappa/zappa.py +++ b/zappa/zappa.py @@ -178,6 +178,8 @@ ZIP_EXCLUDES = ['*.exe', '*.DS_Store', '*.Python', '*.git', '*.zip', '*.tar.gz','*.hg'] +STANDARD_CONDA_PACKAGES = ['openssl','pip','python','readline','sqlite','wheel'] + ## # Classes ## @@ -236,7 +238,7 @@ def __init__(self, boto_session=None, profile_name=None): ## def create_lambda_zip(self, prefix='lambda_package', handler_file=None, - minify=True, exclude=None, use_precompiled_packages=True, include=None, venv=None): + minify=True, exclude=None, use_precompiled_packages=True, include=None, venv=None, exclude_conda_packages=STANDARD_CONDA_PACKAGES): """ Creates a Lambda-ready zip file of the current virtualenvironment and working directory. @@ -307,15 +309,38 @@ def splitpath(path): # Then, do the site-packages.. # TODO Windows: %VIRTUAL_ENV%\Lib\site-packages temp_package_path = os.path.join(temp_zappa_folder, 'package') - site_packages = os.path.join(venv, 'lib', 'python2.7', 'site-packages') + if not USE_CONDA: + site_packages = os.path.join(venv, 'lib', 'python2.7', 'site-packages') - if minify: - excludes = ZIP_EXCLUDES + exclude - shutil.copytree(site_packages, temp_package_path, symlinks=False, ignore=shutil.ignore_patterns(*excludes)) - else: - shutil.copytree(site_packages, temp_package_path, symlinks=False) + if minify: + excludes = ZIP_EXCLUDES + exclude + shutil.copytree(site_packages, temp_package_path, symlinks=False, ignore=shutil.ignore_patterns(*excludes)) + else: + shutil.copytree(site_packages, temp_package_path, symlinks=False) - copy_tree(temp_package_path, temp_project_path, update=True) + copy_tree(temp_package_path, temp_project_path, update=True) + else: + temp_package_path = os.path.join(new_temp_folder,'conda_env') + site_packages = os.path.join(temp_package_path, 'lib', 'python2.7', 'site-packages') + if minify: + excludes = ZIP_EXCLUDES + exclude + shutil.copytree(conda_env, temp_package_path, symlinks=True, ignore=shutil.ignore_patterns(*excludes)) + # Use conda cli to remove standard packages like python, pip, ... + subprocess.call(['conda','remove','-p',temp_package_path,'--force','--yes','--json']+exclude_conda_packages) + else: + shutil.copytree(conda_env, temp_package_path, symlinks=True) + + # Extracts all egg files (e.g. setuptools) + egg_files = [f for f in os.listdir(site_packages) if os.path.isfile(os.path.join(site_packages, f)) and f.split('.')[-1]=='egg'] + for egg_file in egg_files: + print('Extracting '+ egg_file) + with zipfile.ZipFile(os.path.join(site_packages,egg_file)) as zf: + zf.extractall(os.path.join(site_packages)) + os.remove(os.path.join(site_packages,egg_file)) + # Put site-packages at the root of the environment + copy_tree(site_packages, temp_package_path, update=True) + shutil.rmtree(site_packages) + copy_tree(temp_package_path, temp_project_path, update=True) # Then the pre-compiled packages.. if use_precompiled_packages: