Serge Bazanski | 77b87a6 | 2023-04-03 15:24:27 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "go.starlark.net/starlark" |
| 7 | ) |
| 8 | |
| 9 | // getBazelDNFFiles parses third_party/sandboxroot/repositories.bzl (at the given |
| 10 | // path) into a list of rpmDefs. It does so by loading the .bzl file into a |
| 11 | // minimal starlark interpreter that emulates enough of the Bazel internal API to |
| 12 | // get things going. |
| 13 | func getBazelDNFFiles(path string) ([]*rpmDef, error) { |
| 14 | var res []*rpmDef |
| 15 | |
| 16 | // rpm will be called any time the Starlark code calls rpm() from |
| 17 | // @bazeldnf//:deps.bzl. |
| 18 | rpm := func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 19 | var name, sha256 starlark.String |
| 20 | var urls *starlark.List |
| 21 | if err := starlark.UnpackArgs("rpm", args, kwargs, "name", &name, "sha256", &sha256, "urls", &urls); err != nil { |
| 22 | return nil, err |
| 23 | } |
| 24 | it := urls.Iterate() |
| 25 | defer it.Done() |
| 26 | |
| 27 | var urlsS []string |
| 28 | var url starlark.Value |
| 29 | for it.Next(&url) { |
| 30 | if url.Type() != "string" { |
| 31 | return nil, fmt.Errorf("urls must be a list of strings") |
| 32 | } |
| 33 | urlS := url.(starlark.String) |
| 34 | urlsS = append(urlsS, urlS.GoString()) |
| 35 | } |
| 36 | |
| 37 | ext, err := newRPMDef(name.GoString(), sha256.GoString(), urlsS) |
| 38 | if err != nil { |
| 39 | return nil, fmt.Errorf("invalid rpm: %v", err) |
| 40 | } |
| 41 | res = append(res, ext) |
| 42 | return starlark.None, nil |
| 43 | } |
| 44 | |
| 45 | thread := &starlark.Thread{ |
| 46 | Name: "fakebazel", |
| 47 | Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 48 | switch module { |
| 49 | case "@bazeldnf//:deps.bzl": |
| 50 | return map[string]starlark.Value{ |
| 51 | "rpm": starlark.NewBuiltin("rpm", rpm), |
| 52 | }, nil |
| 53 | } |
| 54 | return nil, fmt.Errorf("not implemented in fakebazel") |
| 55 | }, |
| 56 | } |
| 57 | globals, err := starlark.ExecFile(thread, path, nil, nil) |
| 58 | if err != nil { |
| 59 | return nil, fmt.Errorf("executing failed: %w", err) |
| 60 | } |
| 61 | if !globals.Has("sandbox_dependencies") { |
| 62 | return nil, fmt.Errorf("does not contain sandbox_dupendencies") |
| 63 | } |
| 64 | _, err = starlark.Call(thread, globals["sandbox_dependencies"], nil, nil) |
| 65 | if err != nil { |
| 66 | return nil, fmt.Errorf("failed to call sandbox_dependencies: %w", err) |
| 67 | } |
| 68 | return res, nil |
| 69 | } |