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)
| 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. |
| 359 | func 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. |
| 406 | func compileBytecode(ctx *gcp.Context, path string) error { |