def create_deployment_package(self, project_dir): # type: (str) -> str print "Creating deployment package." # pip install -t doesn't work out of the box with homebrew and # python, so we're using virtualenvs instead which works in # more cases. venv_dir = os.path.join(project_dir, '.chalice', 'venv') self._create_virtualenv(venv_dir) pip_exe = compat.pip_script_in_venv(venv_dir) assert os.path.isfile(pip_exe) # Next install any requirements specified by the app. requirements_file = os.path.join(project_dir, 'requirements.txt') deployment_package_filename = self.deployment_package_filename( project_dir) if self._has_at_least_one_package(requirements_file) and not \ os.path.isfile(deployment_package_filename): p = subprocess.Popen([pip_exe, 'install', '-r', requirements_file], stdout=subprocess.PIPE) p.communicate() deps_dir = compat.site_packages_dir_in_venv(venv_dir) assert os.path.isdir(deps_dir) # Now we need to create a zip file and add in the site-packages # dir first, followed by the app_dir contents next. if not os.path.isdir(os.path.dirname(deployment_package_filename)): os.makedirs(os.path.dirname(deployment_package_filename)) with zipfile.ZipFile(deployment_package_filename, 'w', compression=zipfile.ZIP_DEFLATED) as z: self._add_py_deps(z, deps_dir) self._add_app_files(z, project_dir) return deployment_package_filename
def create_deployment_package(self, project_dir, package_filename=None): # type: (str, Optional[str]) -> str print("Creating deployment package.") # pip install -t doesn't work out of the box with homebrew and # python, so we're using virtualenvs instead which works in # more cases. venv_dir = os.path.join(project_dir, '.chalice', 'venv') self._create_virtualenv(venv_dir) pip_exe = compat.pip_script_in_venv(venv_dir) assert os.path.isfile(pip_exe) # Next install any requirements specified by the app. requirements_file = os.path.join(project_dir, 'requirements.txt') deployment_package_filename = self.deployment_package_filename( project_dir) if package_filename is None: package_filename = deployment_package_filename if self._has_at_least_one_package(requirements_file) and not \ os.path.isfile(package_filename): p = subprocess.Popen([pip_exe, 'install', '-r', requirements_file], stdout=subprocess.PIPE) p.communicate() deps_dir = compat.site_packages_dir_in_venv(venv_dir) assert os.path.isdir(deps_dir) # Now we need to create a zip file and add in the site-packages # dir first, followed by the app_dir contents next. dirname = os.path.dirname(os.path.abspath(package_filename)) if not os.path.isdir(dirname): os.makedirs(dirname) with zipfile.ZipFile(package_filename, 'w', compression=zipfile.ZIP_DEFLATED) as z: self._add_py_deps(z, deps_dir) self._add_app_files(z, project_dir) self._add_vendor_files(z, os.path.join(project_dir, self._VENDOR_DIR)) return package_filename