Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/builders/buildpacks/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf

if f.Runtime == "python" {
if fi, _ := os.Lstat(filepath.Join(f.Root, "Procfile")); fi == nil {
cli = pyScaffoldInjector{cli}
cli = pyScaffoldInjector{cli, f.Invoke}
}
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/builders/buildpacks/scaffolding_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"runtime"
"strings"
Expand All @@ -17,6 +18,7 @@ import (
// It basically moves content of /workspace to /workspace/fn and then setup scaffolding code directly in /workspace.
type pyScaffoldInjector struct {
client.CommonAPIClient
invoke string
}

func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string, r io.Reader, opts container.CopyToContainerOptions) error {
Expand Down Expand Up @@ -61,7 +63,7 @@ func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string,
return
}
}
err = writePythonScaffolding(tw)
err = writePythonScaffolding(tw, s.invoke)
if err != nil {
return
}
Expand All @@ -71,7 +73,7 @@ func (s pyScaffoldInjector) CopyToContainer(ctx context.Context, ctr, p string,
return s.CommonAPIClient.CopyToContainer(ctx, ctr, p, pr, opts)
}

func writePythonScaffolding(tw *tar.Writer) error {
func writePythonScaffolding(tw *tar.Writer, invoke string) error {
for _, f := range []struct {
path string
content string
Expand All @@ -82,7 +84,7 @@ func writePythonScaffolding(tw *tar.Writer) error {
},
{
path: "service/main.py",
content: serviceMain,
content: serviceMain(invoke),
},
{
path: "service/__init__.py",
Expand Down Expand Up @@ -133,14 +135,15 @@ python = ">=3.9,<4.0"
script = "service.main:main"
`

const serviceMain = `"""
func serviceMain(invoke string) string {
template := `"""
This code is glue between a user's Function and the middleware which will
expose it as a network service. This code is written on-demand when a
Function is being built, deployed or run. This will be included in the
final container.
"""
import logging
from func_python.cloudevent import serve
from func_python.%s import serve

logging.basicConfig(level=logging.INFO)

Expand All @@ -161,3 +164,5 @@ def main():
if __name__ == "__main__":
main()
`
return fmt.Sprintf(template, invoke)
}
Loading