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

Function PipInstallPyproject

pkg/python/pyproject.go:551–622  ·  view source on GitHub ↗

PipInstallPyproject installs dependencies from a pyproject.toml file in the current directory.

(ctx *gcp.Context, l *libcnb.Layer)

Source from the content-addressed store, hash-verified

549 return err
550 }
551 hash, cached, err := cache.HashAndCheck(ctx, l, dependencyHashKey,
552 cache.WithFiles("pyproject.toml"),
553 cache.WithStrings(currentPythonVersion))
554 if err != nil {
555 return err
556 }
557
558 // Check cache expiration to pick up new versions of dependencies that are not pinned.
559 expired := cacheExpired(ctx, l)
560
561 if cached && !expired {
562 ctx.Logf("Dependencies cached and not expired. Skipping installation.")
563 return nil
564 }
565
566 if expired {
567 ctx.Debugf("Dependencies cache expired, clearing layer.")
568 }
569
570 if err := ctx.ClearLayer(l); err != nil {
571 return fmt.Errorf("clearing layer %q: %w", l.Name, err)
572 }
573
574 cache.Add(ctx, l, dependencyHashKey, hash)
575 // Update the layer metadata.
576 ctx.SetMetadata(l, pythonVersionKey, currentPythonVersion)
577 ctx.SetMetadata(l, expiryTimestampKey, time.Now().Add(expirationTime).Format(dateFormat))
578
579 if err := ar.GeneratePythonConfig(ctx); err != nil {
580 return fmt.Errorf("generating Artifact Registry credentials: %w", err)
581 }
582
583 l.SharedEnvironment.Default("PYTHONUSERBASE", l.Path)
584 if err := ctx.Setenv("PYTHONUSERBASE", l.Path); err != nil {
585 return err
586 }
587 binDir := filepath.Join(l.Path, "bin")
588 l.SharedEnvironment.Prepend("PATH", string(os.PathListSeparator), binDir)
589 if err := ctx.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH")); err != nil {
590 return err
591 }
592
593 cmd := []string{
594 "python3", "-m", "pip", "install",
595 ".",
596 "--user",
597 "--upgrade",
598 "--upgrade-strategy", "only-if-needed",
599 "--no-warn-script-location", // bin is added at run time by lifecycle.
600 "--disable-pip-version-check", // If we were going to upgrade pip, we would have done it already in the runtime buildpack.
601 "--force-reinstall", // Some dependencies may be in the build image but not run image. Later requirements.txt should override earlier.
602 "--no-compile", // Prevent default timestamp-based bytecode compilation. Deterministic pycs are generated in a second step below.
603 "--no-cache-dir", // We don't want http caching of pypi requests.
604 }
605 vendorDir, isVendored := os.LookupEnv(VendorPipDepsEnv)
606 if isVendored {
607 cmd = append(cmd, "--no-index", "--find-links", vendorDir)
608 buildermetrics.GlobalBuilderMetrics().GetCounter(buildermetrics.PipVendorDependenciesCounterID).Increment(1)

Callers 1

BuildFnFunction · 0.92

Calls 15

HashAndCheckFunction · 0.92
WithFilesFunction · 0.92
WithStringsFunction · 0.92
AddFunction · 0.92
GeneratePythonConfigFunction · 0.92
GlobalBuilderMetricsFunction · 0.92
cacheExpiredFunction · 0.85
LogfMethod · 0.80
DebugfMethod · 0.80
ClearLayerMethod · 0.80
SetMetadataMethod · 0.80
SetenvMethod · 0.80

Tested by

no test coverage detected