def __init__(self, ctx, args, cache_version_str): super(Python, self).__init__(ctx, _PYTHON_NAMESPACE, args, cache_version_str, [_REQUIREMENTS_TXT]) self._venv_dir = ftl_util.gen_tmp_dir(_VENV_DIR) self._wheel_dir = ftl_util.gen_tmp_dir(_WHEEL_DIR) self._python_cmd = args.python_cmd.split(" ") self._pip_cmd = args.pip_cmd.split(" ") self._venv_cmd = args.venv_cmd.split(" ")
def Build(self): lyr_imgs = [] lyr_imgs.append(self._base_image) if ftl_util.has_pkg_descriptor(self._descriptor_files, self._ctx): # build interpreter layer interpreter_builder = package_builder.InterpreterLayerBuilder( venv_dir=self._venv_dir, python_cmd=self._python_cmd, venv_cmd=self._venv_cmd, cache=self._cache) interpreter_builder.BuildLayer() lyr_imgs.append(interpreter_builder.GetImage()) if self._is_phase2: # do a phase 2 build of the package layers w/ Pipfile.lock # iterate over package/version Pipfile.lock pkgs = self._parse_pipfile_pkgs() for pkg in pkgs: pipfile_builder = package_builder.PipfileLayerBuilder( ctx=self._ctx, descriptor_files=self._descriptor_files, pkg_descriptor=pkg, pkg_dir=None, wheel_dir=ftl_util.gen_tmp_dir(constants.WHEEL_DIR), venv_dir=self._venv_dir, pip_cmd=self._pip_cmd, venv_cmd=self._venv_cmd, dep_img_lyr=interpreter_builder, cache=self._cache) pipfile_builder.BuildLayer() lyr_imgs.append(pipfile_builder.GetImage()) else: # do a phase 1 build of the package layers w/ requirements.txt req_txt_builder = package_builder.RequirementsLayerBuilder( ctx=self._ctx, descriptor_files=self._descriptor_files, pkg_dir=None, wheel_dir=self._wheel_dir, venv_dir=self._venv_dir, python_cmd=self._python_cmd, pip_cmd=self._pip_cmd, venv_cmd=self._venv_cmd, dep_img_lyr=interpreter_builder, cache=self._cache) req_txt_builder.BuildLayer() lyr_imgs.append(req_txt_builder.GetImage()) app = base_builder.AppLayerBuilder( ctx=self._ctx, destination_path=self._args.destination_path, entrypoint=self._args.entrypoint, exposed_ports=self._args.exposed_ports) app.BuildLayer() lyr_imgs.append(app.GetImage()) ftl_image = ftl_util.AppendLayersIntoImage(lyr_imgs) self.StoreImage(ftl_image)
def _build_pkg(self, pkg, interpreter_builder, lyr_imgs): pipfile_builder = package_builder.PipfileLayerBuilder( ctx=self._ctx, descriptor_files=self._descriptor_files, pkg_descriptor=pkg, pkg_dir=None, wheel_dir=ftl_util.gen_tmp_dir(constants.WHEEL_DIR), venv_dir=self._venv_dir, pip_cmd=self._pip_cmd, venv_cmd=self._venv_cmd, dep_img_lyr=interpreter_builder, cache=self._cache) pipfile_builder.BuildLayer() lyr_imgs.append(pipfile_builder.GetImage())
def __init__(self, ctx, args): super(Python, self).__init__( ctx, constants.PYTHON_CACHE_NAMESPACE, args, [ constants.PIPFILE_LOCK, constants.PIPFILE, # not supported rn constants.REQUIREMENTS_TXT ]) self._venv_dir = args.venv_dir self._wheel_dir = ftl_util.gen_tmp_dir(constants.WHEEL_DIR) self._python_cmd = args.python_cmd.split(" ") self._pip_cmd = args.pip_cmd.split(" ") self._venv_cmd = args.venv_cmd.split(" ") self._is_phase2 = ctx.Contains(constants.PIPFILE_LOCK)
def setUp(self, mock_from): mock_from.return_value.__enter__.return_value = None self.ctx = context.Memory() self.ctx.AddFile("app.py", _APP) args = mock.Mock() args.name = 'gcr.io/test/test:latest' args.base = 'gcr.io/google-appengine/python:latest' args.python_cmd = 'python2.7' args.pip_cmd = 'pip' args.venv_cmd = 'virtualenv' args.tar_base_image_path = None self.builder = builder.Python(self.ctx, args) # constants.VENV_DIR.replace('/', '') is used as the default path # will give permissions errors in some build environments (eg: kokoro) self.interpreter_builder = layer_builder.InterpreterLayerBuilder( ftl_util.gen_tmp_dir(constants.VENV_DIR.replace('/', '')), self.builder._python_cmd, self.builder._venv_cmd) self.interpreter_builder._setup_venv = mock.Mock() self.builder._pip_download_wheels = mock.Mock()