Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package rbcrun |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Sasha Smundak | 6b795dc | 2021-08-18 16:32:19 -0700 | [diff] [blame] | 19 | "io/fs" |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 20 | "os" |
| 21 | "os/exec" |
| 22 | "path/filepath" |
Cole Faust | c7b8b6e | 2022-04-26 12:03:19 -0700 | [diff] [blame] | 23 | "sort" |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | |
| 26 | "go.starlark.net/starlark" |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 27 | "go.starlark.net/starlarkjson" |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 28 | "go.starlark.net/starlarkstruct" |
| 29 | ) |
| 30 | |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 31 | type ExecutionMode int |
| 32 | const ( |
| 33 | ExecutionModeRbc ExecutionMode = iota |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 34 | ExecutionModeScl ExecutionMode = iota |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 35 | ) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 36 | |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 37 | const allowExternalEntrypointKey = "allowExternalEntrypoint" |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 38 | const callingFileKey = "callingFile" |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 39 | const executionModeKey = "executionMode" |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 40 | const shellKey = "shell" |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 41 | |
| 42 | type modentry struct { |
| 43 | globals starlark.StringDict |
| 44 | err error |
| 45 | } |
| 46 | |
| 47 | var moduleCache = make(map[string]*modentry) |
| 48 | |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 49 | var rbcBuiltins starlark.StringDict = starlark.StringDict{ |
| 50 | "struct": starlark.NewBuiltin("struct", starlarkstruct.Make), |
| 51 | // To convert find-copy-subdir and product-copy-files-by pattern |
| 52 | "rblf_find_files": starlark.NewBuiltin("rblf_find_files", find), |
| 53 | // To convert makefile's $(shell cmd) |
| 54 | "rblf_shell": starlark.NewBuiltin("rblf_shell", shell), |
| 55 | // Output to stderr |
| 56 | "rblf_log": starlark.NewBuiltin("rblf_log", log), |
| 57 | // To convert makefile's $(wildcard foo*) |
| 58 | "rblf_wildcard": starlark.NewBuiltin("rblf_wildcard", wildcard), |
| 59 | } |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 60 | |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 61 | var sclBuiltins starlark.StringDict = starlark.StringDict{ |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 62 | "struct": starlark.NewBuiltin("struct", starlarkstruct.Make), |
| 63 | "json": starlarkjson.Module, |
| 64 | } |
| 65 | |
Cole Faust | ccd2680 | 2023-11-09 14:28:34 -0800 | [diff] [blame^] | 66 | func isSymlink(filepath string) (bool, error) { |
| 67 | if info, err := os.Lstat(filepath); err == nil { |
| 68 | return info.Mode() & os.ModeSymlink != 0, nil |
| 69 | } else { |
| 70 | return false, err |
| 71 | } |
| 72 | } |
| 73 | |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 74 | // Takes a module name (the first argument to the load() function) and returns the path |
| 75 | // it's trying to load, stripping out leading //, and handling leading :s. |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 76 | func cleanModuleName(moduleName string, callerDir string, allowExternalPaths bool) (string, error) { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 77 | if strings.Count(moduleName, ":") > 1 { |
| 78 | return "", fmt.Errorf("at most 1 colon must be present in starlark path: %s", moduleName) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 79 | } |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 80 | |
| 81 | // We don't have full support for external repositories, but at least support skylib's dicts. |
| 82 | if moduleName == "@bazel_skylib//lib:dicts.bzl" { |
| 83 | return "external/bazel-skylib/lib/dicts.bzl", nil |
| 84 | } |
| 85 | |
| 86 | localLoad := false |
| 87 | if strings.HasPrefix(moduleName, "@//") { |
| 88 | moduleName = moduleName[3:] |
| 89 | } else if strings.HasPrefix(moduleName, "//") { |
| 90 | moduleName = moduleName[2:] |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 91 | } else if strings.HasPrefix(moduleName, ":") { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 92 | moduleName = moduleName[1:] |
| 93 | localLoad = true |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 94 | } else if !allowExternalPaths { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 95 | return "", fmt.Errorf("load path must start with // or :") |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 96 | } |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 97 | |
| 98 | if ix := strings.LastIndex(moduleName, ":"); ix >= 0 { |
| 99 | moduleName = moduleName[:ix] + string(os.PathSeparator) + moduleName[ix+1:] |
| 100 | } |
| 101 | |
| 102 | if filepath.Clean(moduleName) != moduleName { |
| 103 | return "", fmt.Errorf("load path must be clean, found: %s, expected: %s", moduleName, filepath.Clean(moduleName)) |
| 104 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 105 | if !allowExternalPaths { |
| 106 | if strings.HasPrefix(moduleName, "../") { |
| 107 | return "", fmt.Errorf("load path must not start with ../: %s", moduleName) |
| 108 | } |
| 109 | if strings.HasPrefix(moduleName, "/") { |
| 110 | return "", fmt.Errorf("load path starts with /, use // for a absolute path: %s", moduleName) |
| 111 | } |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if localLoad { |
| 115 | return filepath.Join(callerDir, moduleName), nil |
| 116 | } |
| 117 | |
| 118 | return moduleName, nil |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // loader implements load statement. The format of the loaded module URI is |
| 122 | // [//path]:base[|symbol] |
| 123 | // The file path is $ROOT/path/base if path is present, <caller_dir>/base otherwise. |
| 124 | // The presence of `|symbol` indicates that the loader should return a single 'symbol' |
| 125 | // bound to None if file is missing. |
| 126 | func loader(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 127 | mode := thread.Local(executionModeKey).(ExecutionMode) |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 128 | allowExternalEntrypoint := thread.Local(allowExternalEntrypointKey).(bool) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 129 | var defaultSymbol string |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 130 | mustLoad := true |
| 131 | if mode == ExecutionModeRbc { |
| 132 | pipePos := strings.LastIndex(module, "|") |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 133 | if pipePos >= 0 { |
| 134 | mustLoad = false |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 135 | defaultSymbol = module[pipePos+1:] |
| 136 | module = module[:pipePos] |
| 137 | } |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 138 | } |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 139 | callingFile := thread.Local(callingFileKey).(string) |
| 140 | modulePath, err := cleanModuleName(module, filepath.Dir(callingFile), allowExternalEntrypoint) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | e, ok := moduleCache[modulePath] |
| 145 | if e == nil { |
| 146 | if ok { |
| 147 | return nil, fmt.Errorf("cycle in load graph") |
| 148 | } |
| 149 | |
| 150 | // Add a placeholder to indicate "load in progress". |
| 151 | moduleCache[modulePath] = nil |
| 152 | |
| 153 | // Decide if we should load. |
| 154 | if !mustLoad { |
| 155 | if _, err := os.Stat(modulePath); err == nil { |
| 156 | mustLoad = true |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Load or return default |
| 161 | if mustLoad { |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 162 | if strings.HasSuffix(callingFile, ".scl") && !strings.HasSuffix(modulePath, ".scl") { |
| 163 | return nil, fmt.Errorf(".scl files can only load other .scl files: %q loads %q", callingFile, modulePath) |
| 164 | } |
| 165 | // Switch into scl mode from here on |
| 166 | if strings.HasSuffix(modulePath, ".scl") { |
| 167 | mode = ExecutionModeScl |
| 168 | } |
Cole Faust | ccd2680 | 2023-11-09 14:28:34 -0800 | [diff] [blame^] | 169 | |
| 170 | if sym, err := isSymlink(modulePath); sym && err == nil { |
| 171 | return nil, fmt.Errorf("symlinks to starlark files are not allowed. Instead, load the target file and re-export its symbols: %s", modulePath) |
| 172 | } else if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 176 | childThread := &starlark.Thread{Name: "exec " + module, Load: thread.Load} |
| 177 | // Cheating for the sake of testing: |
| 178 | // propagate starlarktest's Reporter key, otherwise testing |
| 179 | // the load function may cause panic in starlarktest code. |
| 180 | const testReporterKey = "Reporter" |
| 181 | if v := thread.Local(testReporterKey); v != nil { |
| 182 | childThread.SetLocal(testReporterKey, v) |
| 183 | } |
| 184 | |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 185 | // Only the entrypoint starlark file allows external loads. |
| 186 | childThread.SetLocal(allowExternalEntrypointKey, false) |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 187 | childThread.SetLocal(callingFileKey, modulePath) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 188 | childThread.SetLocal(executionModeKey, mode) |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 189 | childThread.SetLocal(shellKey, thread.Local(shellKey)) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 190 | if mode == ExecutionModeRbc { |
| 191 | globals, err := starlark.ExecFile(childThread, modulePath, nil, rbcBuiltins) |
| 192 | e = &modentry{globals, err} |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 193 | } else if mode == ExecutionModeScl { |
| 194 | globals, err := starlark.ExecFile(childThread, modulePath, nil, sclBuiltins) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 195 | e = &modentry{globals, err} |
| 196 | } else { |
| 197 | return nil, fmt.Errorf("unknown executionMode %d", mode) |
| 198 | } |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 199 | } else { |
| 200 | e = &modentry{starlark.StringDict{defaultSymbol: starlark.None}, nil} |
| 201 | } |
| 202 | |
| 203 | // Update the cache. |
| 204 | moduleCache[modulePath] = e |
| 205 | } |
| 206 | return e.globals, e.err |
| 207 | } |
| 208 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 209 | // wildcard(pattern, top=None) expands shell's glob pattern. If 'top' is present, |
| 210 | // the 'top/pattern' is globbed and then 'top/' prefix is removed. |
| 211 | func wildcard(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, |
| 212 | kwargs []starlark.Tuple) (starlark.Value, error) { |
| 213 | var pattern string |
| 214 | var top string |
| 215 | |
| 216 | if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 1, &pattern, &top); err != nil { |
| 217 | return starlark.None, err |
| 218 | } |
| 219 | |
| 220 | var files []string |
| 221 | var err error |
| 222 | if top == "" { |
| 223 | if files, err = filepath.Glob(pattern); err != nil { |
| 224 | return starlark.None, err |
| 225 | } |
| 226 | } else { |
| 227 | prefix := top + string(filepath.Separator) |
| 228 | if files, err = filepath.Glob(prefix + pattern); err != nil { |
| 229 | return starlark.None, err |
| 230 | } |
| 231 | for i := range files { |
| 232 | files[i] = strings.TrimPrefix(files[i], prefix) |
| 233 | } |
| 234 | } |
Cole Faust | c7b8b6e | 2022-04-26 12:03:19 -0700 | [diff] [blame] | 235 | // Kati uses glob(3) with no flags, which means it's sorted |
| 236 | // because GLOB_NOSORT is not passed. Go's glob is not |
| 237 | // guaranteed to sort the results. |
| 238 | sort.Strings(files) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 239 | return makeStringList(files), nil |
| 240 | } |
| 241 | |
Sasha Smundak | 6b795dc | 2021-08-18 16:32:19 -0700 | [diff] [blame] | 242 | // find(top, pattern, only_files = 0) returns all the paths under 'top' |
| 243 | // whose basename matches 'pattern' (which is a shell's glob pattern). |
| 244 | // If 'only_files' is non-zero, only the paths to the regular files are |
| 245 | // returned. The returned paths are relative to 'top'. |
| 246 | func find(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, |
| 247 | kwargs []starlark.Tuple) (starlark.Value, error) { |
| 248 | var top, pattern string |
| 249 | var onlyFiles int |
| 250 | if err := starlark.UnpackArgs(b.Name(), args, kwargs, |
| 251 | "top", &top, "pattern", &pattern, "only_files?", &onlyFiles); err != nil { |
| 252 | return starlark.None, err |
| 253 | } |
| 254 | top = filepath.Clean(top) |
| 255 | pattern = filepath.Clean(pattern) |
| 256 | // Go's filepath.Walk is slow, consider using OS's find |
| 257 | var res []string |
| 258 | err := filepath.WalkDir(top, func(path string, d fs.DirEntry, err error) error { |
| 259 | if err != nil { |
| 260 | if d != nil && d.IsDir() { |
| 261 | return fs.SkipDir |
| 262 | } else { |
| 263 | return nil |
| 264 | } |
| 265 | } |
| 266 | relPath := strings.TrimPrefix(path, top) |
| 267 | if len(relPath) > 0 && relPath[0] == os.PathSeparator { |
| 268 | relPath = relPath[1:] |
| 269 | } |
| 270 | // Do not return top-level dir |
| 271 | if len(relPath) == 0 { |
| 272 | return nil |
| 273 | } |
| 274 | if matched, err := filepath.Match(pattern, d.Name()); err == nil && matched && (onlyFiles == 0 || d.Type().IsRegular()) { |
| 275 | res = append(res, relPath) |
| 276 | } |
| 277 | return nil |
| 278 | }) |
| 279 | return makeStringList(res), err |
| 280 | } |
| 281 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 282 | // shell(command) runs OS shell with given command and returns back |
| 283 | // its output the same way as Make's $(shell ) function. The end-of-lines |
| 284 | // ("\n" or "\r\n") are replaced with " " in the result, and the trailing |
| 285 | // end-of-line is removed. |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 286 | func shell(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 287 | kwargs []starlark.Tuple) (starlark.Value, error) { |
| 288 | var command string |
| 289 | if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 1, &command); err != nil { |
| 290 | return starlark.None, err |
| 291 | } |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 292 | shellPath := thread.Local(shellKey).(string) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 293 | if shellPath == "" { |
| 294 | return starlark.None, |
Sasha Smundak | 57bb508 | 2021-04-01 15:51:56 -0700 | [diff] [blame] | 295 | fmt.Errorf("cannot run shell, /bin/sh is missing (running on Windows?)") |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 296 | } |
| 297 | cmd := exec.Command(shellPath, "-c", command) |
| 298 | // We ignore command's status |
| 299 | bytes, _ := cmd.Output() |
| 300 | output := string(bytes) |
| 301 | if strings.HasSuffix(output, "\n") { |
| 302 | output = strings.TrimSuffix(output, "\n") |
| 303 | } else { |
| 304 | output = strings.TrimSuffix(output, "\r\n") |
| 305 | } |
| 306 | |
| 307 | return starlark.String( |
| 308 | strings.ReplaceAll( |
| 309 | strings.ReplaceAll(output, "\r\n", " "), |
| 310 | "\n", " ")), nil |
| 311 | } |
| 312 | |
| 313 | func makeStringList(items []string) *starlark.List { |
| 314 | elems := make([]starlark.Value, len(items)) |
| 315 | for i, item := range items { |
| 316 | elems[i] = starlark.String(item) |
| 317 | } |
| 318 | return starlark.NewList(elems) |
| 319 | } |
| 320 | |
Sasha Smundak | e8652d4 | 2021-09-24 08:25:17 -0700 | [diff] [blame] | 321 | func log(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 322 | sep := " " |
| 323 | if err := starlark.UnpackArgs("print", nil, kwargs, "sep?", &sep); err != nil { |
| 324 | return nil, err |
| 325 | } |
| 326 | for i, v := range args { |
| 327 | if i > 0 { |
| 328 | fmt.Fprint(os.Stderr, sep) |
| 329 | } |
| 330 | if s, ok := starlark.AsString(v); ok { |
| 331 | fmt.Fprint(os.Stderr, s) |
| 332 | } else if b, ok := v.(starlark.Bytes); ok { |
| 333 | fmt.Fprint(os.Stderr, string(b)) |
| 334 | } else { |
| 335 | fmt.Fprintf(os.Stderr, "%s", v) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | fmt.Fprintln(os.Stderr) |
| 340 | return starlark.None, nil |
| 341 | } |
| 342 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 343 | // Parses, resolves, and executes a Starlark file. |
| 344 | // filename and src parameters are as for starlark.ExecFile: |
| 345 | // * filename is the name of the file to execute, |
| 346 | // and the name that appears in error messages; |
| 347 | // * src is an optional source of bytes to use instead of filename |
| 348 | // (it can be a string, or a byte array, or an io.Reader instance) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 349 | // Returns the top-level starlark variables, the list of starlark files loaded, and an error |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 350 | func Run(filename string, src interface{}, mode ExecutionMode, allowExternalEntrypoint bool) (starlark.StringDict, []string, error) { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 351 | // NOTE(asmundak): OS-specific. Behave similar to Linux `system` call, |
| 352 | // which always uses /bin/sh to run the command |
| 353 | shellPath := "/bin/sh" |
| 354 | if _, err := os.Stat(shellPath); err != nil { |
| 355 | shellPath = "" |
| 356 | } |
| 357 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 358 | mainThread := &starlark.Thread{ |
| 359 | Name: "main", |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 360 | Print: func(_ *starlark.Thread, msg string) { |
| 361 | if mode == ExecutionModeRbc { |
| 362 | // In rbc mode, rblf_log is used to print to stderr |
| 363 | fmt.Println(msg) |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 364 | } else if mode == ExecutionModeScl { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 365 | fmt.Fprintln(os.Stderr, msg) |
| 366 | } |
| 367 | }, |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 368 | Load: loader, |
| 369 | } |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 370 | filename, err := filepath.Abs(filename) |
| 371 | if err != nil { |
| 372 | return nil, nil, err |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 373 | } |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 374 | if wd, err := os.Getwd(); err == nil { |
| 375 | filename, err = filepath.Rel(wd, filename) |
| 376 | if err != nil { |
| 377 | return nil, nil, err |
| 378 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 379 | if !allowExternalEntrypoint && strings.HasPrefix(filename, "../") { |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 380 | return nil, nil, fmt.Errorf("path could not be made relative to workspace root: %s", filename) |
| 381 | } |
| 382 | } else { |
| 383 | return nil, nil, err |
| 384 | } |
| 385 | |
Cole Faust | ccd2680 | 2023-11-09 14:28:34 -0800 | [diff] [blame^] | 386 | if sym, err := isSymlink(filename); sym && err == nil { |
| 387 | return nil, nil, fmt.Errorf("symlinks to starlark files are not allowed. Instead, load the target file and re-export its symbols: %s", filename) |
| 388 | } else if err != nil { |
| 389 | return nil, nil, err |
| 390 | } |
| 391 | |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 392 | // Add top-level file to cache for cycle detection purposes |
| 393 | moduleCache[filename] = nil |
| 394 | |
| 395 | var results starlark.StringDict |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 396 | mainThread.SetLocal(allowExternalEntrypointKey, allowExternalEntrypoint) |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 397 | mainThread.SetLocal(callingFileKey, filename) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 398 | mainThread.SetLocal(executionModeKey, mode) |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 399 | mainThread.SetLocal(shellKey, shellPath) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 400 | if mode == ExecutionModeRbc { |
| 401 | results, err = starlark.ExecFile(mainThread, filename, src, rbcBuiltins) |
Cole Faust | 5b8dda0 | 2023-11-07 11:14:58 -0800 | [diff] [blame] | 402 | } else if mode == ExecutionModeScl { |
| 403 | results, err = starlark.ExecFile(mainThread, filename, src, sclBuiltins) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 404 | } else { |
| 405 | return results, nil, fmt.Errorf("unknown executionMode %d", mode) |
| 406 | } |
| 407 | loadedStarlarkFiles := make([]string, 0, len(moduleCache)) |
| 408 | for file := range moduleCache { |
| 409 | loadedStarlarkFiles = append(loadedStarlarkFiles, file) |
| 410 | } |
| 411 | sort.Strings(loadedStarlarkFiles) |
| 412 | |
| 413 | return results, loadedStarlarkFiles, err |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 414 | } |