depGraph.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. package dep
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strconv"
  8. aurc "github.com/Jguer/aur"
  9. "github.com/Jguer/yay/v11/pkg/db"
  10. "github.com/Jguer/yay/v11/pkg/metadata"
  11. aur "github.com/Jguer/yay/v11/pkg/query"
  12. "github.com/Jguer/yay/v11/pkg/text"
  13. "github.com/Jguer/yay/v11/pkg/topo"
  14. gosrc "github.com/Morganamilo/go-srcinfo"
  15. "github.com/leonelquinteros/gotext"
  16. )
  17. type InstallInfo struct {
  18. Source Source
  19. Reason Reason
  20. Version string
  21. SrcinfoPath *string
  22. AURBase *string
  23. SyncDBName *string
  24. }
  25. func (i *InstallInfo) String() string {
  26. return fmt.Sprintf("InstallInfo{Source: %v, Reason: %v}", i.Source, i.Reason)
  27. }
  28. type (
  29. Reason int
  30. Source int
  31. )
  32. func (r Reason) String() string {
  33. return ReasonNames[r]
  34. }
  35. func (s Source) String() string {
  36. return SourceNames[s]
  37. }
  38. const (
  39. Explicit Reason = iota // 0
  40. Dep // 1
  41. MakeDep // 2
  42. CheckDep // 3
  43. )
  44. var ReasonNames = map[Reason]string{
  45. Explicit: gotext.Get("Explicit"),
  46. Dep: gotext.Get("Dependency"),
  47. MakeDep: gotext.Get("Make Dependency"),
  48. CheckDep: gotext.Get("Check Dependency"),
  49. }
  50. const (
  51. AUR Source = iota
  52. Sync
  53. Local
  54. SrcInfo
  55. Missing
  56. )
  57. var SourceNames = map[Source]string{
  58. AUR: gotext.Get("AUR"),
  59. Sync: gotext.Get("Sync"),
  60. Local: gotext.Get("Local"),
  61. SrcInfo: gotext.Get("SRCINFO"),
  62. Missing: gotext.Get("Missing"),
  63. }
  64. var bgColorMap = map[Source]string{
  65. AUR: "lightblue",
  66. Sync: "lemonchiffon",
  67. Local: "darkolivegreen1",
  68. Missing: "tomato",
  69. }
  70. var colorMap = map[Reason]string{
  71. Explicit: "black",
  72. Dep: "deeppink",
  73. MakeDep: "navyblue",
  74. CheckDep: "forestgreen",
  75. }
  76. type Grapher struct {
  77. dbExecutor db.Executor
  78. aurCache *metadata.AURCache
  79. fullGraph bool // If true, the graph will include all dependencies including already installed ones or repo
  80. noConfirm bool
  81. w io.Writer // output writer
  82. }
  83. func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache,
  84. fullGraph, noConfirm bool, output io.Writer,
  85. ) *Grapher {
  86. return &Grapher{
  87. dbExecutor: dbExecutor,
  88. aurCache: aurCache,
  89. fullGraph: fullGraph,
  90. noConfirm: noConfirm,
  91. w: output,
  92. }
  93. }
  94. func (g *Grapher) GraphFromTargets(ctx context.Context,
  95. graph *topo.Graph[string, *InstallInfo], targets []string,
  96. ) (*topo.Graph[string, *InstallInfo], error) {
  97. if graph == nil {
  98. graph = topo.New[string, *InstallInfo]()
  99. }
  100. for _, targetString := range targets {
  101. var (
  102. err error
  103. target = ToTarget(targetString)
  104. )
  105. switch target.DB {
  106. case "": // unspecified db
  107. if g.dbExecutor.SyncPackage(target.Name) != nil {
  108. graph.AddNode(target.Name)
  109. g.ValidateAndSetNodeInfo(graph, target.Name, &topo.NodeInfo[*InstallInfo]{
  110. Color: colorMap[Explicit],
  111. Background: bgColorMap[AUR],
  112. Value: &InstallInfo{
  113. Source: Sync,
  114. Reason: Explicit,
  115. Version: target.Version,
  116. SyncDBName: &target.DB,
  117. },
  118. })
  119. continue
  120. }
  121. fallthrough
  122. case "aur":
  123. graph, err = g.GraphFromAURCache(ctx, graph, []string{target.Name})
  124. default:
  125. graph.AddNode(target.Name)
  126. g.ValidateAndSetNodeInfo(graph, target.Name, &topo.NodeInfo[*InstallInfo]{
  127. Color: colorMap[Explicit],
  128. Background: bgColorMap[AUR],
  129. Value: &InstallInfo{
  130. Source: Sync,
  131. Reason: Explicit,
  132. Version: target.Version,
  133. SyncDBName: &target.DB,
  134. },
  135. })
  136. }
  137. if err != nil {
  138. return nil, err
  139. }
  140. }
  141. return graph, nil
  142. }
  143. func (g *Grapher) GraphFromSrcInfo(ctx context.Context, graph *topo.Graph[string, *InstallInfo], pkgBuildDir string,
  144. pkgbuild *gosrc.Srcinfo,
  145. ) (*topo.Graph[string, *InstallInfo], error) {
  146. if graph == nil {
  147. graph = topo.New[string, *InstallInfo]()
  148. }
  149. aurPkgs, err := makeAURPKGFromSrcinfo(g.dbExecutor, pkgbuild)
  150. if err != nil {
  151. return nil, err
  152. }
  153. for _, pkg := range aurPkgs {
  154. pkg := pkg
  155. g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
  156. Color: colorMap[Explicit],
  157. Background: bgColorMap[AUR],
  158. Value: &InstallInfo{
  159. Source: SrcInfo,
  160. Reason: Explicit,
  161. SrcinfoPath: &pkgBuildDir,
  162. AURBase: &pkg.PackageBase,
  163. Version: pkg.Version,
  164. },
  165. })
  166. g.addDepNodes(ctx, &pkg, graph)
  167. }
  168. return graph, nil
  169. }
  170. func (g *Grapher) addDepNodes(ctx context.Context, pkg *aur.Pkg, graph *topo.Graph[string, *InstallInfo]) {
  171. if len(pkg.MakeDepends) > 0 {
  172. g.addNodes(ctx, graph, pkg.Name, pkg.MakeDepends, MakeDep)
  173. }
  174. if !false && len(pkg.Depends) > 0 {
  175. g.addNodes(ctx, graph, pkg.Name, pkg.Depends, Dep)
  176. }
  177. if !false && len(pkg.CheckDepends) > 0 {
  178. g.addNodes(ctx, graph, pkg.Name, pkg.CheckDepends, CheckDep)
  179. }
  180. }
  181. func (g *Grapher) GraphFromAURCache(ctx context.Context,
  182. graph *topo.Graph[string, *InstallInfo],
  183. targets []string,
  184. ) (*topo.Graph[string, *InstallInfo], error) {
  185. if graph == nil {
  186. graph = topo.New[string, *InstallInfo]()
  187. }
  188. for _, target := range targets {
  189. aurPkgs, _ := g.aurCache.Get(ctx, &metadata.AURQuery{By: aurc.Name, Needles: []string{target}})
  190. if len(aurPkgs) == 0 {
  191. text.Errorln("No AUR package found for", target)
  192. continue
  193. }
  194. pkg := provideMenu(g.w, target, aurPkgs, g.noConfirm)
  195. g.ValidateAndSetNodeInfo(graph, pkg.Name, &topo.NodeInfo[*InstallInfo]{
  196. Color: colorMap[Explicit],
  197. Background: bgColorMap[AUR],
  198. Value: &InstallInfo{
  199. Source: AUR,
  200. Reason: Explicit,
  201. AURBase: &pkg.PackageBase,
  202. Version: pkg.Version,
  203. },
  204. })
  205. graph.AddNode(pkg.Name)
  206. g.addDepNodes(ctx, pkg, graph)
  207. }
  208. return graph, nil
  209. }
  210. func (g *Grapher) ValidateAndSetNodeInfo(graph *topo.Graph[string, *InstallInfo],
  211. node string, nodeInfo *topo.NodeInfo[*InstallInfo],
  212. ) {
  213. info := graph.GetNodeInfo(node)
  214. if info != nil && info.Value != nil {
  215. if info.Value.Reason < nodeInfo.Value.Reason {
  216. return // refuse to downgrade reason from explicit to dep
  217. }
  218. }
  219. graph.SetNodeInfo(node, nodeInfo)
  220. }
  221. func (g *Grapher) addNodes(
  222. ctx context.Context,
  223. graph *topo.Graph[string, *InstallInfo],
  224. parentPkgName string,
  225. deps []string,
  226. depType Reason,
  227. ) {
  228. for _, depString := range deps {
  229. depName, _, _ := splitDep(depString)
  230. if g.dbExecutor.LocalSatisfierExists(depString) {
  231. if g.fullGraph {
  232. g.ValidateAndSetNodeInfo(
  233. graph,
  234. depName,
  235. &topo.NodeInfo[*InstallInfo]{Color: colorMap[depType], Background: bgColorMap[Local]})
  236. if err := graph.DependOn(depName, parentPkgName); err != nil {
  237. text.Warnln(depName, parentPkgName, err)
  238. }
  239. }
  240. continue
  241. }
  242. if graph.Exists(depName) {
  243. if err := graph.DependOn(depName, parentPkgName); err != nil {
  244. text.Warnln(depName, parentPkgName, err)
  245. }
  246. continue
  247. }
  248. // Check ALPM
  249. if alpmPkg := g.dbExecutor.SyncSatisfier(depString); alpmPkg != nil {
  250. if err := graph.DependOn(alpmPkg.Name(), parentPkgName); err != nil {
  251. text.Warnln("repo dep warn:", depName, parentPkgName, err)
  252. }
  253. dbName := alpmPkg.DB().Name()
  254. g.ValidateAndSetNodeInfo(
  255. graph,
  256. alpmPkg.Name(),
  257. &topo.NodeInfo[*InstallInfo]{
  258. Color: colorMap[depType],
  259. Background: bgColorMap[Sync],
  260. Value: &InstallInfo{
  261. Source: Sync,
  262. Reason: depType,
  263. Version: alpmPkg.Version(),
  264. SyncDBName: &dbName,
  265. },
  266. })
  267. if newDeps := alpmPkg.Depends().Slice(); len(newDeps) != 0 && g.fullGraph {
  268. newDepsSlice := make([]string, 0, len(newDeps))
  269. for _, newDep := range newDeps {
  270. newDepsSlice = append(newDepsSlice, newDep.Name)
  271. }
  272. g.addNodes(ctx, graph, alpmPkg.Name(), newDepsSlice, Dep)
  273. }
  274. continue
  275. }
  276. if aurPkgs, _ := g.aurCache.FindPackage(ctx, depName); len(aurPkgs) != 0 { // Check AUR
  277. pkg := aurPkgs[0]
  278. if len(aurPkgs) > 1 {
  279. pkg = provideMenu(g.w, depName, aurPkgs, g.noConfirm)
  280. g.aurCache.SetProvideCache(depName, []*aur.Pkg{pkg})
  281. }
  282. if err := graph.DependOn(pkg.Name, parentPkgName); err != nil {
  283. text.Warnln("aur dep warn:", pkg.Name, parentPkgName, err)
  284. }
  285. graph.SetNodeInfo(
  286. pkg.Name,
  287. &topo.NodeInfo[*InstallInfo]{
  288. Color: colorMap[depType],
  289. Background: bgColorMap[AUR],
  290. Value: &InstallInfo{
  291. Source: AUR,
  292. Reason: depType,
  293. AURBase: &pkg.PackageBase,
  294. Version: pkg.Version,
  295. },
  296. })
  297. g.addDepNodes(ctx, pkg, graph)
  298. continue
  299. }
  300. // no dep found. add as missing
  301. graph.SetNodeInfo(depString, &topo.NodeInfo[*InstallInfo]{Color: colorMap[depType], Background: bgColorMap[Missing]})
  302. }
  303. }
  304. func provideMenu(w io.Writer, dep string, options []*aur.Pkg, noConfirm bool) *aur.Pkg {
  305. size := len(options)
  306. if size == 1 {
  307. return options[0]
  308. }
  309. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  310. str += "\n"
  311. size = 1
  312. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  313. for _, pkg := range options {
  314. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  315. size++
  316. }
  317. text.OperationInfoln(str)
  318. for {
  319. fmt.Fprintln(w, gotext.Get("\nEnter a number (default=1): "))
  320. if noConfirm {
  321. fmt.Fprintln(w, "1")
  322. return options[0]
  323. }
  324. numberBuf, err := text.GetInput("", false)
  325. if err != nil {
  326. fmt.Fprintln(os.Stderr, err)
  327. break
  328. }
  329. if numberBuf == "" {
  330. return options[0]
  331. }
  332. num, err := strconv.Atoi(numberBuf)
  333. if err != nil {
  334. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  335. continue
  336. }
  337. if num < 1 || num >= size {
  338. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  339. continue
  340. }
  341. return options[num-1]
  342. }
  343. return nil
  344. }
  345. func makeAURPKGFromSrcinfo(dbExecutor db.Executor, srcInfo *gosrc.Srcinfo) ([]aur.Pkg, error) {
  346. pkgs := make([]aur.Pkg, 0, 1)
  347. alpmArch, err := dbExecutor.AlpmArchitectures()
  348. if err != nil {
  349. return nil, err
  350. }
  351. alpmArch = append(alpmArch, "") // srcinfo assumes no value as ""
  352. for _, pkg := range srcInfo.Packages {
  353. pkgs = append(pkgs, aur.Pkg{
  354. ID: 0,
  355. Name: pkg.Pkgname,
  356. PackageBaseID: 0,
  357. PackageBase: srcInfo.Pkgbase,
  358. Version: srcInfo.Version(),
  359. Description: pkg.Pkgdesc,
  360. URL: pkg.URL,
  361. Depends: append(archStringToString(alpmArch, pkg.Depends),
  362. archStringToString(alpmArch, srcInfo.Package.Depends)...),
  363. MakeDepends: archStringToString(alpmArch, srcInfo.PackageBase.MakeDepends),
  364. CheckDepends: archStringToString(alpmArch, srcInfo.PackageBase.CheckDepends),
  365. Conflicts: append(archStringToString(alpmArch, pkg.Conflicts),
  366. archStringToString(alpmArch, srcInfo.Package.Conflicts)...),
  367. Provides: append(archStringToString(alpmArch, pkg.Provides),
  368. archStringToString(alpmArch, srcInfo.Package.Provides)...),
  369. Replaces: append(archStringToString(alpmArch, pkg.Replaces),
  370. archStringToString(alpmArch, srcInfo.Package.Replaces)...),
  371. OptDepends: []string{},
  372. Groups: pkg.Groups,
  373. License: pkg.License,
  374. Keywords: []string{},
  375. })
  376. }
  377. return pkgs, nil
  378. }
  379. func archStringToString(alpmArches []string, archString []gosrc.ArchString) []string {
  380. pkgs := make([]string, 0, len(archString))
  381. for _, arch := range archString {
  382. if db.ArchIsSupported(alpmArches, arch.Arch) {
  383. pkgs = append(pkgs, arch.Value)
  384. }
  385. }
  386. return pkgs
  387. }