Repository navigation
Expand file tree
/
Copy pathpython.go
More file actions
125 lines (104 loc) · 3.13 KB
/
Copy pathpython.go
File metadata and controls
125 lines (104 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package langs
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// PythonLangHelper - python-specific init helper
type PythonLangHelper struct {
BaseHelper
Version string
}
// CustomMemory - python is a hungry beast so specify a higher base memory here.
func (h *PythonLangHelper) CustomMemory() uint64 {
return 256
}
// HasBoilerplate - yep, we have boilerplate...
func (h *PythonLangHelper) HasBoilerplate() bool { return true }
// GenerateBoilerplate - ...and here it is.
func (h *PythonLangHelper) GenerateBoilerplate(path string) error {
codeFile := filepath.Join(path, "func.py")
if exists(codeFile) {
return errors.New("func.py already exists, canceling init")
}
if err := ioutil.WriteFile(codeFile, []byte(helloPythonSrcBoilerplate), os.FileMode(0644)); err != nil {
return err
}
depFile := "requirements.txt"
if err := ioutil.WriteFile(depFile, []byte(reqsPythonSrcBoilerplate), os.FileMode(0644)); err != nil {
return err
}
return nil
}
func (h *PythonLangHelper) Handles(lang string) bool {
return defaultHandles(h, lang)
}
func (h *PythonLangHelper) Runtime() string {
return h.LangStrings()[0]
}
func (h *PythonLangHelper) LangStrings() []string {
return []string{"python", fmt.Sprintf("python%s", h.Version)}
}
func (h *PythonLangHelper) Extensions() []string {
return []string{".py"}
}
func (h *PythonLangHelper) BuildFromImage() (string, error) {
return fmt.Sprintf("fnproject/python:%s-dev", h.Version), nil
}
func (h *PythonLangHelper) RunFromImage() (string, error) {
return fmt.Sprintf("fnproject/python:%s", h.Version), nil
}
func (h *PythonLangHelper) Entrypoint() (string, error) {
return "/python/bin/fdk /function/func.py handler", nil
}
func (h *PythonLangHelper) DockerfileBuildCmds() []string {
var r []string
if exists("requirements.txt") {
pip_cmd := `RUN pip3 install --target /python/ --no-cache --no-cache-dir`
if exists(".pip_cache") {
r = append(r, "ADD .pip_cache /function/.pip_cache")
pip_cmd += " --no-index --find-links /function/.pip_cache"
}
r = append(r, "ADD requirements.txt /function/")
r = append(r, fmt.Sprintf(`
%v -r requirements.txt &&\
rm -fr ~/.cache/pip /tmp* requirements.txt func.yaml Dockerfile .venv`, pip_cmd))
}
r = append(r, "ADD . /function/")
if exists("setup.py") {
r = append(r, "python setup.py install")
}
r = append(r, "RUN rm -fr /function/.pip_cache")
return r
}
func (h *PythonLangHelper) IsMultiStage() bool {
return true
}
const (
helloPythonSrcBoilerplate = `import io
import json
from fdk import response
def handler(ctx, data: io.BytesIO=None):
name = "World"
try:
body = json.loads(data.getvalue())
name = body.get("name")
except (Exception, ValueError) as ex:
print(str(ex))
return response.Response(
ctx, response_data=json.dumps(
{"message": "Hello {0}".format(name)}),
headers={"Content-Type": "application/json"}
)
`
reqsPythonSrcBoilerplate = `fdk`
)
func (h *PythonLangHelper) DockerfileCopyCmds() []string {
return []string{
"COPY --from=build-stage /function /function",
"COPY --from=build-stage /python /python",
"ENV PYTHONPATH=/python",
}
}