-
Notifications
You must be signed in to change notification settings - Fork 18
/
loader.go
197 lines (167 loc) · 4.79 KB
/
loader.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
package loader
import (
"context"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/pkg/larker/builtin"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"github.com/cirruslabs/cirrus-cli/pkg/larker/resolver"
"github.com/qri-io/starlib/encoding/base64"
"github.com/qri-io/starlib/encoding/yaml"
"github.com/qri-io/starlib/hash"
"github.com/qri-io/starlib/http"
"github.com/qri-io/starlib/re"
"github.com/qri-io/starlib/zipfile"
starlarkjson "go.starlark.net/lib/json"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"os"
"path/filepath"
"strings"
)
var (
ErrCycle = errors.New("import cycle detected")
)
type CacheEntry struct {
globals starlark.StringDict
err error
}
type Loader struct {
ctx context.Context
cache map[string]*CacheEntry
fs fs.FileSystem
env map[string]string
affectedFiles []string
isTest bool
}
func NewLoader(
ctx context.Context,
fs fs.FileSystem,
env map[string]string,
affectedFiles []string,
isTest bool,
) *Loader {
return &Loader{
ctx: ctx,
cache: make(map[string]*CacheEntry),
fs: fs,
env: env,
affectedFiles: affectedFiles,
isTest: isTest,
}
}
func (loader *Loader) ResolveFS(currentFS fs.FileSystem, locator string) (fs.FileSystem, string, error) {
return resolver.FindModuleFS(loader.ctx, currentFS, loader.env, locator)
}
func (loader *Loader) LoadFunc(
frameFS fs.FileSystem,
) func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
return func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
// Lookup cache
entry, ok := loader.cache[module]
if ok {
// Even through we've found the requested module in the cache,
// a canary might indicate that it's still being loaded, which
// means we've hit an import cycle
if entry == nil {
return nil, ErrCycle
}
// Return cached results
return entry.globals, entry.err
}
// A special case for loading Cirrus-provided builtins (e.g. load("cirrus", "fs"))
if module == "cirrus" {
return loader.loadCirrusModule()
}
moduleFS, path, err := loader.ResolveFS(frameFS, module)
if err != nil {
return nil, err
}
source, err := moduleFS.Get(loader.ctx, path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
var hint string
if strings.Contains(module, ".start") {
hint = ", perhaps you've meant the .star extension instead of the .start?"
}
return nil, fmt.Errorf("%w: module '%s' at path '%s' not found%s",
resolver.ErrRetrievalFailed, module, path, hint)
}
return nil, err
}
// Place a canary to indicate the commencing load and detect cycles
loader.cache[module] = nil
// Load the module and cache results
oldLoad := thread.Load
thread.Load = loader.LoadFunc(moduleFS)
globals, err := starlark.ExecFile(thread, filepath.Base(module), source, nil)
thread.Load = oldLoad
loader.cache[module] = &CacheEntry{
globals: globals,
err: err,
}
return globals, err
}
}
func (loader *Loader) loadCirrusModule() (starlark.StringDict, error) {
result := make(starlark.StringDict)
starlarkEnv := starlark.NewDict(len(loader.env))
for key, value := range loader.env {
if err := starlarkEnv.SetKey(starlark.String(key), starlark.String(value)); err != nil {
return nil, err
}
}
result["env"] = starlarkEnv
result["is_test"] = starlark.Bool(loader.isTest)
result["changes_include"] = generateChangesIncludeBuiltin(loader.affectedFiles)
result["changes_include_only"] = generateChangesIncludeOnlyBuiltin(loader.affectedFiles)
result["fs"] = &starlarkstruct.Module{
Name: "fs",
Members: builtin.FS(loader.ctx, func(locator string) (fs.FileSystem, string, error) {
return loader.ResolveFS(loader.fs, locator)
}),
}
httpModule, err := http.LoadModule()
if err != nil {
return nil, err
}
result["http"] = httpModule["http"]
hashModule, err := hash.LoadModule()
if err != nil {
return nil, err
}
result["hash"] = hashModule["hash"]
base64Module, err := base64.LoadModule()
if err != nil {
return nil, err
}
result["base64"] = base64Module["base64"]
// Work around https://github.com/qri-io/starlib/pull/70
fixedJSONModule := &starlarkstruct.Module{
Name: "json",
Members: starlark.StringDict{
"loads": starlarkjson.Module.Members["decode"],
"dumps": starlarkjson.Module.Members["encode"],
},
}
result["json"] = fixedJSONModule
yamlModule, err := yaml.LoadModule()
if err != nil {
return nil, err
}
result["yaml"] = yamlModule["yaml"]
reModule, err := re.LoadModule()
if err != nil {
return nil, err
}
result["re"] = reModule["re"]
zipfileModule, err := zipfile.LoadModule()
if err != nil {
return nil, err
}
result["zipfile"] = &starlarkstruct.Module{
Name: "zipfile",
Members: zipfileModule,
}
return result, nil
}