-
Notifications
You must be signed in to change notification settings - Fork 156
/
interpreter.go
199 lines (179 loc) · 5.45 KB
/
interpreter.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package starlark
import (
"bytes"
"hash/fnv"
"io/fs"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"go.starlark.net/starlark"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/config"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/data"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/install"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/io"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/runtime"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/universe"
"github.com/tensorchord/envd/pkg/util/fileutil"
)
type Interpreter interface {
Eval(script string) (interface{}, error)
ExecFile(filename string, funcname string) (interface{}, error)
}
type entry struct {
globals starlark.StringDict
err error
}
// generalInterpreter is the interpreter implementation for Starlark.
// Please refer to https://github.com/google/starlark-go
type generalInterpreter struct {
predeclared starlark.StringDict
buildContextDir string
cache map[string]*entry
}
func NewInterpreter(buildContextDir string) Interpreter {
// Register envd rules and built-in variables to Starlark.
universe.RegisterEnvdRules()
universe.RegisterBuildContext(buildContextDir)
return &generalInterpreter{
predeclared: starlark.StringDict{
"install": install.Module,
"config": config.Module,
"io": io.Module,
"runtime": runtime.Module,
"data": data.Module,
},
buildContextDir: buildContextDir,
cache: make(map[string]*entry),
}
}
func (s *generalInterpreter) NewThread(module string) *starlark.Thread {
thread := &starlark.Thread{
Name: module,
Load: s.load,
}
return thread
}
func (s *generalInterpreter) load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
return s.exec(thread, module)
}
func (s *generalInterpreter) exec(thread *starlark.Thread, module string) (starlark.StringDict, error) {
e, ok := s.cache[module]
if e != nil {
return e.globals, e.err
}
if ok {
return nil, errors.Newf("Detect cycling import during parsing %s", module)
}
s.cache[module] = nil
if !strings.HasPrefix(module, universe.GitPrefix) {
var data interface{}
globals, err := starlark.ExecFile(thread, module, data, s.predeclared)
e = &entry{globals, err}
} else {
// exec remote git repo
url := module[len(universe.GitPrefix):]
path, err := fileutil.DownloadOrUpdateGitRepo(url)
if err != nil {
return nil, err
}
globals, err := s.loadGitModule(thread, path)
e = &entry{globals, err}
}
return e.globals, e.err
}
func (s *generalInterpreter) loadGitModule(thread *starlark.Thread, path string) (globals starlark.StringDict, err error) {
var src interface{}
globals = starlark.StringDict{}
logger := logrus.WithField("file", thread.Name)
logger.Debugf("load git module from: %s", path)
err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.HasSuffix(d.Name(), ".envd") {
return nil
}
dict, err := starlark.ExecFile(thread, path, src, s.predeclared)
if err != nil {
return err
}
for key, val := range dict {
if _, exist := globals[key]; exist {
return errors.Newf("found duplicated object name: %s in %s", key, path)
}
if !strings.HasPrefix(key, "_") {
globals[key] = val
}
}
return nil
})
return
}
func (s generalInterpreter) ExecFile(filename string, funcname string) (interface{}, error) {
logrus.WithField("filename", filename).Debug("interprete the file")
thread := s.NewThread(filename)
globals, err := s.exec(thread, filename)
if err != nil {
return nil, err
}
if funcname != "" {
logrus.Debugf("Execute %s func", funcname)
if globals.Has(funcname) {
buildVar := globals[funcname]
if fn, ok := buildVar.(*starlark.Function); ok {
_, err := starlark.Call(thread, fn, nil, nil)
if err != nil {
return nil, errors.Wrapf(err, "Exception when exec %s func", funcname)
}
} else {
return nil, errors.Errorf("%s is not a function", funcname)
}
} else {
return nil, errors.Errorf("envd file doesn't has %s function", funcname)
}
}
return globals, nil
}
func (s generalInterpreter) Eval(script string) (interface{}, error) {
thread := s.NewThread(script)
return starlark.ExecFile(thread, "", script, s.predeclared)
}
func GetEnvdProgramHash(filename string) (string, error) {
envdSrc, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
// No Check builtin or predeclared for now
funcAlwaysHas := func(x string) bool {
return true
}
_, prog, err := starlark.SourceProgram(filename, envdSrc, funcAlwaysHas)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
err = prog.Write(buf)
if err != nil {
return "", err
}
h := fnv.New64a()
h.Write(buf.Bytes())
hashsum := h.Sum64()
return strconv.FormatUint(hashsum, 16), nil
}