init
This commit is contained in:
24 files changed
+3822
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const sizeBatch = 16
|
||||
|
||||
// QuerySizes asks `nix path-info -S` for closure and NAR sizes in batches,
|
||||
// calling cb with each batch's results ([closure, nar]) and batch length.
|
||||
// Paths that are not valid get -2 so they are not retried.
|
||||
func QuerySizes(ctx context.Context, cfg Config, paths []string, cb func(map[string][2]int64, int)) {
|
||||
batches := make(chan []string)
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < 4; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for b := range batches {
|
||||
cb(pathInfo(ctx, cfg, b), len(b))
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := 0; i < len(paths); i += sizeBatch {
|
||||
end := min(i+sizeBatch, len(paths))
|
||||
select {
|
||||
case batches <- paths[i:end]:
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
close(batches)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
type pathInfoEntry struct {
|
||||
Path string `json:"path"`
|
||||
Valid *bool `json:"valid"`
|
||||
ClosureSize int64 `json:"closureSize"`
|
||||
NarSize int64 `json:"narSize"`
|
||||
}
|
||||
|
||||
func pathInfo(ctx context.Context, cfg Config, paths []string) map[string][2]int64 {
|
||||
res := map[string][2]int64{}
|
||||
for _, p := range paths {
|
||||
res[p] = [2]int64{-2, -2}
|
||||
}
|
||||
args := append([]string{"--extra-experimental-features", "nix-command", "path-info", "--json", "-S"}, paths...)
|
||||
// path-info exits non-zero when some paths are invalid but still prints
|
||||
// JSON for the rest, so the exit status is ignored.
|
||||
out, _ := exec.CommandContext(ctx, cfg.NixBin, args...).Output()
|
||||
for p, e := range parsePathInfo(out) {
|
||||
res[p] = [2]int64{e.ClosureSize, e.NarSize}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// parsePathInfo accepts both the object-keyed-by-path format (Nix >= 2.19)
|
||||
// and the older array format.
|
||||
func parsePathInfo(out []byte) map[string]pathInfoEntry {
|
||||
res := map[string]pathInfoEntry{}
|
||||
var obj map[string]*pathInfoEntry
|
||||
if err := json.Unmarshal(out, &obj); err == nil {
|
||||
for p, e := range obj {
|
||||
if e != nil {
|
||||
res[p] = *e
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
var arr []pathInfoEntry
|
||||
if err := json.Unmarshal(out, &arr); err == nil {
|
||||
for _, e := range arr {
|
||||
if e.Path != "" && (e.Valid == nil || *e.Valid) {
|
||||
res[e.Path] = e
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
Reference in new issue
Block a user