PatchSysconfig patches the python sysconfig variable prefix.
(ctx *gcp.Context, layer *libcnb.Layer)
| 523 | } |
| 524 | |
| 525 | // isBothPyprojectAndRequirementsPresent checks if both pyproject.toml and requirements.txt are present. |
| 526 | func isBothPyprojectAndRequirementsPresent(ctx *gcp.Context) bool { |
| 527 | pyprojectTomlExists, _ := ctx.FileExists(pyprojectToml) |
| 528 | requirementsTxtExists, _ := ctx.FileExists(requirements) |
| 529 | return pyprojectTomlExists && requirementsTxtExists |
| 530 | } |
| 531 | |
| 532 | // PatchSysconfig patches the python sysconfig variable prefix. |
| 533 | func PatchSysconfig(ctx *gcp.Context, layer *libcnb.Layer) error { |
| 534 | if cap := ctx.Capability(SysconfigPatcherCapability); cap != nil { |
| 535 | p, ok := cap.(sysconfigPatcher) |
| 536 | if !ok { |
| 537 | return gcp.InternalErrorf("capability %q must implement sysconfigPatcher", SysconfigPatcherCapability) |
| 538 | } |
| 539 | return p.PatchSysconfig(ctx, layer) |
| 540 | } |
| 541 | // replace python sysconfig variable prefix from "/opt/python" to "/layers/google.python.runtime/python/" which is the layer.Path |
| 542 | // python is installed in /layers/google.python.runtime/python/ for unified builder, |
| 543 | // while the python downloaded from debs is installed in "/opt/python". |
| 544 | sysconfig, err := ctx.Exec([]string{filepath.Join(layer.Path, "bin/python3"), "-m", "sysconfig"}) |
| 545 | if err != nil { |
| 546 | ctx.Warnf("Getting python sysconfig: %v", err) |
| 547 | } |
| 548 | execPrefix, err := parseExecPrefix(sysconfig.Stdout) |
| 549 | if err != nil { |
| 550 | return err |
| 551 | } |
| 552 | result, err := ctx.Exec([]string{ |
| 553 | "grep", |
| 554 | "-rlI", |
| 555 | execPrefix, |
| 556 | layer.Path, |
| 557 | }) |
| 558 | if err != nil { |
| 559 | ctx.Warnf("Grep failed: %v", err) |
| 560 | } |
| 561 | paths := strings.Split(result.Stdout, "\n") |
| 562 | for _, path := range paths { |
| 563 | if _, err := ctx.Exec([]string{ |
| 564 | "sed", |
| 565 | "-i", |
| 566 | "s|" + execPrefix + "|" + layer.Path + "|g", |
| 567 | path, |
| 568 | }); err != nil { |
no test coverage detected