depGraph.go 11 KB

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