MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/buildpacks / prepareDependenciesLayer

Function prepareDependenciesLayer

pkg/python/python.go:359–403  ·  view source on GitHub ↗

prepareDependenciesLayer is a helper function that handles the common logic for preparing a dependency layer. It checks for empty requirements, manages the cache, and clears the layer if necessary. It returns a boolean indicating whether the installation should proceed.

(ctx *gcp.Context, l *libcnb.Layer, installerName string, reqs ...string)

Source from the content-addressed store, hash-verified

357// It checks for empty requirements, manages the cache, and clears the layer if necessary.
358// It returns a boolean indicating whether the installation should proceed.
359func prepareDependenciesLayer(ctx *gcp.Context, l *libcnb.Layer, installerName string, reqs ...string) (bool, error) {
360 // Defensive check
361 if len(reqs) == 0 {
362 ctx.Debugf("No requirements files to install, clearing layer.")
363 if err := ctx.ClearLayer(l); err != nil {
364 return false, fmt.Errorf("clearing layer %q: %w", l.Name, err)
365 }
366 return false, nil
367 }
368
369 // Caching logic
370 currentPythonVersion, err := Version(ctx)
371 if err != nil {
372 return false, err
373 }
374 hash, cached, err := cache.HashAndCheck(ctx, l, dependencyHashKey,
375 cache.WithFiles(reqs...),
376 cache.WithStrings(currentPythonVersion, installerName))
377 if err != nil {
378 return false, err
379 }
380
381 // Check cache expiration to pick up new versions of dependencies that are not pinned.
382 expired := cacheExpired(ctx, l)
383
384 if cached && !expired {
385 ctx.CacheHit(l.Name)
386 return false, nil
387 }
388 ctx.CacheMiss(l.Name)
389
390 if expired {
391 ctx.Debugf("Dependencies cache expired, clearing layer.")
392 }
393 if err := ctx.ClearLayer(l); err != nil {
394 return false, fmt.Errorf("clearing layer %q: %w", l.Name, err)
395 }
396
397 // Update layer metadata for caching
398 cache.Add(ctx, l, dependencyHashKey, hash)
399 ctx.SetMetadata(l, pythonVersionKey, currentPythonVersion)
400 ctx.SetMetadata(l, expiryTimestampKey, time.Now().Add(expirationTime).Format(dateFormat))
401
402 return true, nil
403}
404
405// compileBytecode is a helper that generates deterministic hash-based pyc files for faster startup.
406func compileBytecode(ctx *gcp.Context, path string) error {

Callers 3

UVInstallRequirementsFunction · 0.85
PIPInstallRequirementsFunction · 0.85

Calls 12

HashAndCheckFunction · 0.92
WithFilesFunction · 0.92
WithStringsFunction · 0.92
AddFunction · 0.92
cacheExpiredFunction · 0.85
DebugfMethod · 0.80
ClearLayerMethod · 0.80
CacheHitMethod · 0.80
CacheMissMethod · 0.80
SetMetadataMethod · 0.80
VersionFunction · 0.70
AddMethod · 0.45

Tested by 1