in samcli/lib/build/build_strategy.py [0:0]
def build_single_function_definition(self, build_definition: FunctionBuildDefinition) -> Dict[str, str]:
"""
Builds single function definition with caching
"""
if build_definition.packagetype == IMAGE:
return self._delegate_build_strategy.build_single_function_definition(build_definition)
code_dir = str(pathlib.Path(self._base_dir, cast(str, build_definition.codeuri)).resolve())
source_hash = dir_checksum(code_dir, ignore_list=[".aws-sam"], hash_generator=hashlib.sha256())
cache_function_dir = pathlib.Path(self._cache_dir, build_definition.uuid)
function_build_results = {}
if not cache_function_dir.exists() or build_definition.source_hash != source_hash:
LOG.info(
"Cache is invalid, running build and copying resources for following functions (%s)",
build_definition.get_resource_full_paths(),
)
build_result = self._delegate_build_strategy.build_single_function_definition(build_definition)
function_build_results.update(build_result)
if cache_function_dir.exists():
shutil.rmtree(str(cache_function_dir))
build_definition.source_hash = source_hash
# Since all the build contents are same for a build definition, just copy any one of them into the cache
for _, value in build_result.items():
osutils.copytree(value, str(cache_function_dir))
break
else:
LOG.info(
"Valid cache found, copying previously built resources for following functions (%s)",
build_definition.get_resource_full_paths(),
)
if is_experimental_enabled(ExperimentalFlag.BuildPerformance):
first_function_artifacts_dir: Optional[str] = None
for function in build_definition.functions:
if not first_function_artifacts_dir:
# artifacts directory will be created by the builder
artifacts_dir = build_definition.get_build_dir(self._build_dir)
LOG.debug("Linking artifacts from %s to %s", cache_function_dir, artifacts_dir)
osutils.create_symlink_or_copy(str(cache_function_dir), artifacts_dir)
function_build_results[function.full_path] = artifacts_dir
first_function_artifacts_dir = artifacts_dir
else:
LOG.debug(
"Function (%s) build folder is updated to %s",
function.full_path,
first_function_artifacts_dir,
)
function_build_results[function.full_path] = first_function_artifacts_dir
else:
for function in build_definition.functions:
# artifacts directory will be created by the builder
artifacts_dir = function.get_build_dir(self._build_dir)
LOG.debug("Copying artifacts from %s to %s", cache_function_dir, artifacts_dir)
osutils.copytree(str(cache_function_dir), artifacts_dir)
function_build_results[function.full_path] = artifacts_dir
return function_build_results