-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
292 lines (254 loc) · 7.44 KB
/
main.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/spf13/afero"
starcmdownload "github.com/discentem/starcm/functions/download"
starcmshard "github.com/discentem/starcm/functions/shard"
starcmshell "github.com/discentem/starcm/functions/shell"
starcmtemplate "github.com/discentem/starcm/functions/template"
starcmwrite "github.com/discentem/starcm/functions/write"
"github.com/discentem/starcm/internal/loading"
"github.com/discentem/starcm/libraries/logging"
starcmshelllib "github.com/discentem/starcm/libraries/shell"
starlarkhelpers "github.com/discentem/starcm/starlark-helpers"
"github.com/google/deck"
"github.com/google/deck/backends/logger"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"go.starlark.net/syntax"
)
func LoadFromFile(ctx context.Context, fpath string, src interface{}, load starlarkhelpers.LoaderFunc) error {
logging.Log("LoadFromFile", deck.V(2), "info", "loading file %q", fpath)
thread := &starlark.Thread{
Load: load,
Name: "my_program_main_thread",
Print: func(_ *starlark.Thread, msg string) { fmt.Println(msg) },
}
var currentDir string
if len(thread.CallStack()) > 0 {
currentDir = filepath.Dir(thread.CallStack().At(0).Pos.Filename())
} else {
// Fallback if there are no call frames, assuming the initial script directory
currentDir = fpath
}
logging.Log("LoadFromFile", deck.V(3), "info", "current starlark execution dir %q", currentDir)
if currentDir != fpath {
fpath = filepath.Join(currentDir, fpath)
}
if _, err := starlark.ExecFileOptions(
&syntax.FileOptions{
// Allow if statements and for loops to be top-level in the module.
TopLevelControl: true,
},
thread,
fpath,
src, nil,
); err != nil {
if evalErr, ok := err.(*starlark.EvalError); ok {
return fmt.Errorf(evalErr.Backtrace())
}
return fmt.Errorf("load at path: %q: %s", fpath, err)
}
return nil
}
// Loader handles module loading for starlark modules.
type Loader struct {
// Predeclared is used for builtin modules which are not loaded from a path.
Predeclared func(module string) (starlark.StringDict, error)
// WorkspacePath specifies the path to the source directory.
WorkspacePath string
// Fsys is the filesystem to use for loading modules.
Fsys afero.Fs
}
// Sequential implements sequential module loading.
// Module paths starting with "//" will be loaded from WorkspacePath, which should be the mount path to the workspace source directory.
func (l *Loader) Sequential(ctx context.Context) func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
type entry struct {
globals starlark.StringDict
err error
}
var cache = make(map[string]*entry)
// load is set for the thread.Load when caching a new entry.
var load func(_ *starlark.Thread, module string) (starlark.StringDict, error)
load = func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
e, ok := cache[module]
if e == nil {
if ok {
return nil, fmt.Errorf("cycle in load graph %q", module)
}
cache[module] = nil
builtin, err := l.Predeclared(module)
if builtin != nil || err != nil {
e = &entry{builtin, err}
} else {
if path.Ext(module) != ".star" {
return nil, fmt.Errorf("module %q is not valid, modules should have a .star extension", module)
}
// shorthand for a workspace path
modulepath := module
if strings.HasPrefix(module, "//") {
modulepath = path.Join(l.WorkspacePath, module)
}
// if we hit a load statement in a .star file
// load the next module relative to the current module
if len(thread.CallStack()) > 0 {
modulepath = filepath.Dir(thread.CallStack().At(0).Pos.Filename())
modulepath = path.Join(modulepath, module)
}
data, err := afero.ReadFile(l.Fsys, modulepath)
if err != nil {
return nil, fmt.Errorf("loading module %q: %s", modulepath, err)
}
// create a thread for the module and set Load
thread := &starlark.Thread{Name: "exec " + module, Load: load}
globals, err := starlark.ExecFileOptions(
&syntax.FileOptions{
// Allow if statements and for loops to be top-level in the module.
TopLevelControl: true,
},
thread,
module,
data,
nil,
)
if err != nil {
return nil, fmt.Errorf("executing module %q: %s", module, err)
}
// Step 2: Extract relevant information from the function
e = &entry{globals, err}
}
cache[module] = e
}
return e.globals, e.err
}
return load
}
type LoaderOption func(*Loader)
func WithWorkspacePath(workspacePath string) LoaderOption {
return func(l *Loader) {
l.WorkspacePath = workspacePath
}
}
func WithPredeclared(predeclared func(module string) (starlark.StringDict, error)) LoaderOption {
return func(l *Loader) {
l.Predeclared = predeclared
}
}
func WithFsys(fsys afero.Fs) LoaderOption {
return func(l *Loader) {
l.Fsys = fsys
}
}
func NewLoader(ctx context.Context, opts ...LoaderOption) Loader {
l := Loader{}
for _, opt := range opts {
opt(&l)
}
return l
}
func defaultLoader(ctx context.Context, fsys afero.Fs, ex starcmshelllib.Executor, workspacePath string) Loader {
l := NewLoader(
ctx,
WithWorkspacePath(workspacePath),
WithFsys(fsys),
WithPredeclared(func(module string) (starlark.StringDict, error) {
switch module {
case "starcm":
return starlark.StringDict{
"download": starlark.NewBuiltin(
"download",
starcmdownload.New(
ctx,
*http.DefaultClient,
fsys,
).Function(),
),
"write": starlark.NewBuiltin(
"write",
starcmwrite.New(ctx, os.Stdout).Function(),
),
"exec": starlark.NewBuiltin(
"exec",
starcmshell.New(
ctx,
ex,
).Function(),
),
"template": starlark.NewBuiltin(
"template",
starcmtemplate.New(ctx, fsys).Function(),
),
"shard": starlark.NewBuiltin(
"shard",
starcmshard.New(ctx).Function(),
),
"load_dynamic": starlark.NewBuiltin("load_dynamic", loading.DynamicLoadfunc()),
}, nil
case "starlarkstdlib":
return starlark.StringDict{
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
}, nil
default:
// set both to nil to allow the loader to load a .star file from a path.
return nil, nil
}
}),
)
return l
}
func main() {
f := flag.String(
"root_file",
"",
"path to the first starlark file to run",
)
timestamps := flag.Bool("timestamps", true, "include timestamps in logs")
verbosity := flag.Int("v", 1, "verbosity level")
inmemfs := flag.Bool("inmem_fs", false, "use in-memory filesystem")
flag.Parse()
l := log.Default()
l.SetOutput(os.Stdout)
if !*timestamps {
l.SetFlags(log.LUTC)
}
deck.Add(logger.Init(l.Writer(), l.Flags()))
deck.Info("starting starcm...")
deck.SetVerbosity(*verbosity)
ctx := context.Background()
fsys := afero.Fs(nil)
if *inmemfs {
fsys = afero.NewMemMapFs()
} else {
fsys = afero.NewOsFs()
}
loader := defaultLoader(
ctx,
fsys,
&starcmshelllib.RealExecutor{},
filepath.Dir(*f),
)
b, err := afero.ReadFile(fsys, *f)
if err != nil {
log.Fatal(err)
}
err = LoadFromFile(
context.Background(),
*f,
// If src is bytes, starlark-go will just execute it directly
// without any additional processing.
// https://github.com/google/starlark-go/blob/42030a7cedcee8b1fe3dc9309d4f545f6104715d/syntax/scan.go#L282
b,
loader.Sequential(context.Background()),
)
if err != nil {
log.Fatal(err)
}
}