yay/pkg/dep/depGraph.go

492 lines
11 KiB
Go
Raw Normal View History

2022-09-06 23:25:44 +02:00
package dep
import (
2022-11-08 01:32:21 +01:00
"context"
2022-09-06 23:25:44 +02:00
"fmt"
"io"
"os"
"strconv"
"github.com/Jguer/yay/v11/pkg/db"
aur "github.com/Jguer/yay/v11/pkg/query"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/yay/v11/pkg/topo"
2022-09-06 23:38:47 +02:00
2022-11-15 15:44:50 +01:00
aurc "github.com/Jguer/aur"
2022-11-16 00:25:17 +01:00
"github.com/Jguer/aur/metadata"
2022-09-06 23:38:47 +02:00
gosrc "github.com/Morganamilo/go-srcinfo"
2022-09-06 23:25:44 +02:00
"github.com/leonelquinteros/gotext"
)
2022-09-09 17:38:48 +02:00
type InstallInfo struct {
2022-09-11 23:15:31 +02:00
Source Source
Reason Reason
2022-09-20 00:01:19 +02:00
Version string
2022-09-11 23:15:31 +02:00
SrcinfoPath *string
2022-09-17 14:31:54 +02:00
AURBase *string
2022-09-20 00:44:06 +02:00
SyncDBName *string
2022-09-09 17:38:48 +02:00
}
func (i *InstallInfo) String() string {
return fmt.Sprintf("InstallInfo{Source: %v, Reason: %v}", i.Source, i.Reason)
}
type (
2022-09-09 17:38:48 +02:00
Reason int
Source int
)
2022-09-09 17:38:48 +02:00
func (r Reason) String() string {
2022-09-20 00:01:19 +02:00
return ReasonNames[r]
2022-09-09 17:38:48 +02:00
}
func (s Source) String() string {
2022-09-20 00:01:19 +02:00
return SourceNames[s]
2022-09-09 17:38:48 +02:00
}
const (
2022-09-09 17:38:48 +02:00
Explicit Reason = iota // 0
Dep // 1
MakeDep // 2
CheckDep // 3
)
2022-09-20 00:01:19 +02:00
var ReasonNames = map[Reason]string{
Explicit: gotext.Get("Explicit"),
Dep: gotext.Get("Dependency"),
MakeDep: gotext.Get("Make Dependency"),
CheckDep: gotext.Get("Check Dependency"),
2022-09-09 17:38:48 +02:00
}
const (
2022-09-09 17:38:48 +02:00
AUR Source = iota
Sync
Local
2022-09-09 17:38:48 +02:00
SrcInfo
Missing
)
2022-09-20 00:01:19 +02:00
var SourceNames = map[Source]string{
AUR: gotext.Get("AUR"),
Sync: gotext.Get("Sync"),
Local: gotext.Get("Local"),
SrcInfo: gotext.Get("SRCINFO"),
Missing: gotext.Get("Missing"),
2022-09-09 17:38:48 +02:00
}
var bgColorMap = map[Source]string{
AUR: "lightblue",
Sync: "lemonchiffon",
Local: "darkolivegreen1",
Missing: "tomato",
}
2022-09-09 17:38:48 +02:00
var colorMap = map[Reason]string{
Explicit: "black",
Dep: "deeppink",
MakeDep: "navyblue",
CheckDep: "forestgreen",
}
2022-09-06 23:25:44 +02:00
type Grapher struct {
dbExecutor db.Executor
2022-11-16 00:25:17 +01:00
aurCache *metadata.Client
2022-09-06 23:25:44 +02:00
fullGraph bool // If true, the graph will include all dependencies including already installed ones or repo
noConfirm bool
w io.Writer // output writer
providerCache map[string]*aur.Pkg
2022-09-06 23:25:44 +02:00
}
2022-11-16 00:25:17 +01:00
func NewGrapher(dbExecutor db.Executor, aurCache *metadata.Client,
2022-10-28 00:38:11 +02:00
fullGraph, noConfirm bool, output io.Writer,
) *Grapher {
2022-09-06 23:25:44 +02:00
return &Grapher{
dbExecutor: dbExecutor,
aurCache: aurCache,
fullGraph: fullGraph,
noConfirm: noConfirm,
w: output,
providerCache: make(map[string]*aurc.Pkg, 5),
2022-09-06 23:25:44 +02:00
}
}
2022-11-08 01:37:54 +01:00
func (g *Grapher) GraphFromTargets(ctx context.Context,
graph *topo.Graph[string, *InstallInfo], targets []string,
) (*topo.Graph[string, *InstallInfo], error) {
2022-11-02 00:37:27 +01:00
if graph == nil {
graph = topo.New[string, *InstallInfo]()
}
2022-09-07 00:01:23 +02:00
2022-10-28 00:38:11 +02:00
for _, targetString := range targets {
var (
err error
target = ToTarget(targetString)
)
switch target.DB {
case "": // unspecified db
2022-11-10 01:48:58 +01:00
if g.dbExecutor.SyncPackage(target.Name) != nil {
graph.AddNode(target.Name)
g.ValidateAndSetNodeInfo(graph, target.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit],
Background: bgColorMap[AUR],
Value: &InstallInfo{
Source: Sync,
Reason: Explicit,
Version: target.Version,
SyncDBName: &target.DB,
},
})
continue
}
fallthrough
2022-10-28 00:38:11 +02:00
case "aur":
2022-11-08 01:32:21 +01:00
graph, err = g.GraphFromAURCache(ctx, graph, []string{target.Name})
2022-10-28 00:38:11 +02:00
default:
graph.AddNode(target.Name)
g.ValidateAndSetNodeInfo(graph, target.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit],
Background: bgColorMap[AUR],
Value: &InstallInfo{
Source: Sync,
Reason: Explicit,
Version: target.Version,
SyncDBName: &target.DB,
},
})
}
if err != nil {
return nil, err
}
}
return graph, nil
}
2022-11-08 01:32:21 +01:00
func (g *Grapher) GraphFromSrcInfo(ctx context.Context, graph *topo.Graph[string, *InstallInfo], pkgBuildDir string,
2022-10-28 00:38:11 +02:00
pkgbuild *gosrc.Srcinfo,
) (*topo.Graph[string, *InstallInfo], error) {
if graph == nil {
graph = topo.New[string, *InstallInfo]()
}
2022-09-06 23:38:47 +02:00
aurPkgs, err := makeAURPKGFromSrcinfo(g.dbExecutor, pkgbuild)
if err != nil {
return nil, err
}
2022-11-15 15:44:50 +01:00
for i := range aurPkgs {
pkg := &aurPkgs[i]
2022-09-09 17:38:48 +02:00
2022-11-14 01:14:13 +01:00
graph.AddNode(pkg.Name)
2022-09-09 17:38:48 +02:00
g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit],
Background: bgColorMap[AUR],
Value: &InstallInfo{
2022-09-11 23:15:31 +02:00
Source: SrcInfo,
Reason: Explicit,
SrcinfoPath: &pkgBuildDir,
2022-09-17 14:31:54 +02:00
AURBase: &pkg.PackageBase,
2022-09-20 00:01:19 +02:00
Version: pkg.Version,
2022-09-09 17:38:48 +02:00
},
})
2022-11-15 15:44:50 +01:00
g.addDepNodes(ctx, pkg, graph)
2022-09-06 23:38:47 +02:00
}
return graph, nil
}
2022-11-08 01:32:21 +01:00
func (g *Grapher) addDepNodes(ctx context.Context, pkg *aur.Pkg, graph *topo.Graph[string, *InstallInfo]) {
if len(pkg.MakeDepends) > 0 {
2022-11-08 01:32:21 +01:00
g.addNodes(ctx, graph, pkg.Name, pkg.MakeDepends, MakeDep)
}
if !false && len(pkg.Depends) > 0 {
2022-11-08 01:32:21 +01:00
g.addNodes(ctx, graph, pkg.Name, pkg.Depends, Dep)
}
if !false && len(pkg.CheckDepends) > 0 {
2022-11-08 01:32:21 +01:00
g.addNodes(ctx, graph, pkg.Name, pkg.CheckDepends, CheckDep)
}
}
2022-11-10 01:48:58 +01:00
func (g *Grapher) GraphFromAURCache(ctx context.Context,
graph *topo.Graph[string, *InstallInfo],
targets []string,
) (*topo.Graph[string, *InstallInfo], error) {
2022-10-28 00:38:11 +02:00
if graph == nil {
graph = topo.New[string, *InstallInfo]()
}
2022-09-06 23:25:44 +02:00
for _, target := range targets {
2022-11-13 17:47:19 +01:00
aurPkgs, _ := g.aurCache.Get(ctx, &metadata.AURQuery{By: aurc.Name, Needles: []string{target}})
2022-10-28 00:38:11 +02:00
if len(aurPkgs) == 0 {
text.Errorln("No AUR package found for", target)
continue
}
2022-09-06 23:25:44 +02:00
pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm)
2022-11-14 01:14:13 +01:00
graph.AddNode(pkg.Name)
2022-09-09 17:38:48 +02:00
g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
Color: colorMap[Explicit],
Background: bgColorMap[AUR],
Value: &InstallInfo{
2022-09-17 14:31:54 +02:00
Source: AUR,
Reason: Explicit,
AURBase: &pkg.PackageBase,
2022-09-20 00:01:19 +02:00
Version: pkg.Version,
2022-09-09 17:38:48 +02:00
},
})
2022-11-08 01:32:21 +01:00
g.addDepNodes(ctx, pkg, graph)
2022-09-06 23:25:44 +02:00
}
return graph, nil
}
2022-09-09 17:38:48 +02:00
func (g *Grapher) ValidateAndSetNodeInfo(graph *topo.Graph[string, *InstallInfo],
node string, nodeInfo *topo.NodeInfo[*InstallInfo],
) {
info := graph.GetNodeInfo(node)
2022-09-17 14:31:54 +02:00
if info != nil && info.Value != nil {
2022-09-09 17:38:48 +02:00
if info.Value.Reason < nodeInfo.Value.Reason {
return // refuse to downgrade reason from explicit to dep
}
}
graph.SetNodeInfo(node, nodeInfo)
}
2022-09-06 23:25:44 +02:00
func (g *Grapher) addNodes(
2022-11-08 01:32:21 +01:00
ctx context.Context,
2022-09-09 17:38:48 +02:00
graph *topo.Graph[string, *InstallInfo],
2022-09-06 23:25:44 +02:00
parentPkgName string,
deps []string,
2022-09-09 17:38:48 +02:00
depType Reason,
2022-09-06 23:25:44 +02:00
) {
for _, depString := range deps {
depName, _, _ := splitDep(depString)
if g.dbExecutor.LocalSatisfierExists(depString) {
if g.fullGraph {
2022-09-09 17:38:48 +02:00
g.ValidateAndSetNodeInfo(
graph,
depName,
&topo.NodeInfo[*InstallInfo]{Color: colorMap[depType], Background: bgColorMap[Local]})
2022-09-06 23:25:44 +02:00
if err := graph.DependOn(depName, parentPkgName); err != nil {
text.Warnln(depName, parentPkgName, err)
}
}
2022-09-07 00:01:23 +02:00
2022-09-06 23:25:44 +02:00
continue
}
if graph.Exists(depName) {
if err := graph.DependOn(depName, parentPkgName); err != nil {
text.Warnln(depName, parentPkgName, err)
}
continue
}
// Check ALPM
if alpmPkg := g.dbExecutor.SyncSatisfier(depString); alpmPkg != nil {
if err := graph.DependOn(alpmPkg.Name(), parentPkgName); err != nil {
text.Warnln("repo dep warn:", depName, parentPkgName, err)
}
2022-09-20 00:44:06 +02:00
dbName := alpmPkg.DB().Name()
2022-09-09 17:38:48 +02:00
g.ValidateAndSetNodeInfo(
graph,
alpmPkg.Name(),
&topo.NodeInfo[*InstallInfo]{
Color: colorMap[depType],
Background: bgColorMap[Sync],
Value: &InstallInfo{
2022-09-20 00:44:06 +02:00
Source: Sync,
Reason: depType,
Version: alpmPkg.Version(),
SyncDBName: &dbName,
2022-09-09 17:38:48 +02:00
},
})
2022-09-06 23:25:44 +02:00
if newDeps := alpmPkg.Depends().Slice(); len(newDeps) != 0 && g.fullGraph {
newDepsSlice := make([]string, 0, len(newDeps))
for _, newDep := range newDeps {
newDepsSlice = append(newDepsSlice, newDep.Name)
}
2022-11-08 01:32:21 +01:00
g.addNodes(ctx, graph, alpmPkg.Name(), newDepsSlice, Dep)
2022-09-06 23:25:44 +02:00
}
continue
}
var aurPkgs []*aur.Pkg
if cachedProvidePkg, ok := g.providerCache[depName]; ok {
aurPkgs = []*aur.Pkg{cachedProvidePkg}
} else {
var errMeta error
aurPkgs, errMeta = g.aurCache.Get(ctx,
&metadata.AURQuery{
Needles: []string{depName},
By: aurc.None,
Contains: false,
})
if errMeta != nil {
text.Warnln("AUR cache error:", errMeta)
}
}
if len(aurPkgs) != 0 { // Check AUR
2022-09-06 23:25:44 +02:00
pkg := aurPkgs[0]
if len(aurPkgs) > 1 {
2022-09-09 17:38:48 +02:00
pkg = provideMenu(g.w, depName, aurPkgs, g.noConfirm)
g.providerCache[depName] = pkg
2022-09-06 23:25:44 +02:00
}
2022-09-17 14:31:54 +02:00
if err := graph.DependOn(pkg.Name, parentPkgName); err != nil {
text.Warnln("aur dep warn:", pkg.Name, parentPkgName, err)
2022-09-06 23:25:44 +02:00
}
2022-09-09 17:38:48 +02:00
graph.SetNodeInfo(
2022-09-17 14:31:54 +02:00
pkg.Name,
2022-09-09 17:38:48 +02:00
&topo.NodeInfo[*InstallInfo]{
Color: colorMap[depType],
Background: bgColorMap[AUR],
Value: &InstallInfo{
2022-09-17 14:31:54 +02:00
Source: AUR,
Reason: depType,
AURBase: &pkg.PackageBase,
2022-09-20 00:01:19 +02:00
Version: pkg.Version,
2022-09-09 17:38:48 +02:00
},
})
2022-11-08 01:32:21 +01:00
g.addDepNodes(ctx, pkg, graph)
2022-09-06 23:25:44 +02:00
continue
}
// no dep found. add as missing
2022-09-09 17:38:48 +02:00
graph.SetNodeInfo(depString, &topo.NodeInfo[*InstallInfo]{Color: colorMap[depType], Background: bgColorMap[Missing]})
2022-09-06 23:25:44 +02:00
}
}
func provideMenu(w io.Writer, dep string, options []*aur.Pkg, noConfirm bool) *aur.Pkg {
size := len(options)
if size == 1 {
return options[0]
}
str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
str += "\n"
size = 1
str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
for _, pkg := range options {
str += fmt.Sprintf("%d) %s ", size, pkg.Name)
size++
}
text.OperationInfoln(str)
for {
fmt.Fprintln(w, gotext.Get("\nEnter a number (default=1): "))
if noConfirm {
fmt.Fprintln(w, "1")
return options[0]
}
numberBuf, err := text.GetInput("", false)
if err != nil {
fmt.Fprintln(os.Stderr, err)
break
}
if numberBuf == "" {
return options[0]
}
num, err := strconv.Atoi(numberBuf)
if err != nil {
text.Errorln(gotext.Get("invalid number: %s", numberBuf))
continue
}
if num < 1 || num >= size {
text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
continue
}
return options[num-1]
}
return nil
}
2022-09-06 23:38:47 +02:00
func makeAURPKGFromSrcinfo(dbExecutor db.Executor, srcInfo *gosrc.Srcinfo) ([]aur.Pkg, error) {
pkgs := make([]aur.Pkg, 0, 1)
alpmArch, err := dbExecutor.AlpmArchitectures()
if err != nil {
return nil, err
}
alpmArch = append(alpmArch, "") // srcinfo assumes no value as ""
2022-11-15 15:44:50 +01:00
for i := range srcInfo.Packages {
pkg := &srcInfo.Packages[i]
2022-09-06 23:38:47 +02:00
pkgs = append(pkgs, aur.Pkg{
ID: 0,
Name: pkg.Pkgname,
PackageBaseID: 0,
PackageBase: srcInfo.Pkgbase,
Version: srcInfo.Version(),
Description: pkg.Pkgdesc,
URL: pkg.URL,
2022-09-07 00:01:23 +02:00
Depends: append(archStringToString(alpmArch, pkg.Depends),
archStringToString(alpmArch, srcInfo.Package.Depends)...),
MakeDepends: archStringToString(alpmArch, srcInfo.PackageBase.MakeDepends),
CheckDepends: archStringToString(alpmArch, srcInfo.PackageBase.CheckDepends),
Conflicts: append(archStringToString(alpmArch, pkg.Conflicts),
archStringToString(alpmArch, srcInfo.Package.Conflicts)...),
Provides: append(archStringToString(alpmArch, pkg.Provides),
archStringToString(alpmArch, srcInfo.Package.Provides)...),
Replaces: append(archStringToString(alpmArch, pkg.Replaces),
archStringToString(alpmArch, srcInfo.Package.Replaces)...),
OptDepends: []string{},
Groups: pkg.Groups,
License: pkg.License,
Keywords: []string{},
2022-09-06 23:38:47 +02:00
})
}
return pkgs, nil
}
func archStringToString(alpmArches []string, archString []gosrc.ArchString) []string {
pkgs := make([]string, 0, len(archString))
for _, arch := range archString {
if db.ArchIsSupported(alpmArches, arch.Arch) {
pkgs = append(pkgs, arch.Value)
}
}
return pkgs
}