-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathpython.go
More file actions
145 lines (124 loc) · 3.13 KB
/
python.go
File metadata and controls
145 lines (124 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package langs
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
type PythonLangHelper struct {
BaseHelper
Version string
}
func (h *PythonLangHelper) DefaultFormat() string {
return "json"
}
func (h *PythonLangHelper) HasBoilerplate() bool { return true }
func (h *PythonLangHelper) GenerateBoilerplate() error {
wd, err := os.Getwd()
if err != nil {
return err
}
codeFile := filepath.Join(wd, "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
}
testFile := filepath.Join(wd, "test.json")
if exists(testFile) {
fmt.Println("test.json already exists, skipping")
} else {
if err := ioutil.WriteFile(testFile, []byte(pythonTestBoilerPlate), 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", "python3.6"}
}
func (h *PythonLangHelper) Extensions() []string {
return []string{".py"}
}
func (h *PythonLangHelper) BuildFromImage() (string, error) {
return fmt.Sprintf("python:%v", h.Version), nil
}
func (h *PythonLangHelper) RunFromImage() (string, error) {
return fmt.Sprintf("fnproject/python:%v", h.Version), nil
}
func (h *PythonLangHelper) Entrypoint() (string, error) {
return "python3 func.py", nil
}
func (h *PythonLangHelper) DockerfileBuildCmds() []string {
pip := "pip3"
r := []string{}
if exists("requirements.txt") {
r = append(r,
"ADD requirements.txt /function/",
fmt.Sprintf("RUN %v install -r requirements.txt", pip),
)
}
r = append(r, "ADD . /function/")
return r
}
func (h *PythonLangHelper) IsMultiStage() bool {
return false
}
const (
helloPythonSrcBoilerplate = `
import fdk
import json
def handler(ctx, data=None, loop=None):
body = json.loads(data) if len(data) > 0 else {"name": "World"}
return "Hello {0}".format(body.get("name"))
if __name__ == "__main__":
fdk.handle(handler)
`
reqsPythonSrcBoilerplate = `fdk`
pythonTestBoilerPlate = `{
"tests": [
{
"input": {
"body": {
"name": "Johnny"
}
},
"output": {
"body": {
"message": "Hello Johnny"
}
}
},
{
"input": {
"body": ""
},
"output": {
"body": {
"message": "Hello World"
}
}
}
]
}
`
)
// The multi-stage build didn't work, pip seems to be required for it to load the modules
// func (h *PythonLangHelper) DockerfileCopyCmds() []string {
// return []string{
// "ADD . /function/",
// "COPY --from=build-stage /root/.cache/pip/ /root/.cache/pip/",
// }
// }