dependencies.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. rpc "github.com/mikkeloscar/aur"
  6. )
  7. // BuildDependencies finds packages, on the second run
  8. // compares with a baselist and avoids searching those
  9. func buildDependencies(baselist []string) func(toCheck []string, isBaseList bool, last bool) (repo []string, notFound []string) {
  10. localDb, err := alpmHandle.LocalDb()
  11. if err != nil {
  12. panic(err)
  13. }
  14. dbList, err := alpmHandle.SyncDbs()
  15. if err != nil {
  16. panic(err)
  17. }
  18. f := func(c rune) bool {
  19. return c == '>' || c == '<' || c == '=' || c == ' '
  20. }
  21. return func(toCheck []string, isBaseList bool, close bool) (repo []string, notFound []string) {
  22. if close {
  23. return
  24. }
  25. Loop:
  26. for _, dep := range toCheck {
  27. if !isBaseList {
  28. for _, base := range baselist {
  29. if base == dep {
  30. continue Loop
  31. }
  32. }
  33. }
  34. if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
  35. continue
  36. } else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
  37. repo = append(repo, pkg.Name())
  38. } else {
  39. field := strings.FieldsFunc(dep, f)
  40. notFound = append(notFound, field[0])
  41. }
  42. }
  43. return
  44. }
  45. }
  46. // DepSatisfier receives a string slice, returns a slice of packages found in
  47. // repos and one of packages not found in repos. Leaves out installed packages.
  48. func depSatisfier(toCheck []string) (repo []string, notFound []string, err error) {
  49. localDb, err := alpmHandle.LocalDb()
  50. if err != nil {
  51. return
  52. }
  53. dbList, err := alpmHandle.SyncDbs()
  54. if err != nil {
  55. return
  56. }
  57. f := func(c rune) bool {
  58. return c == '>' || c == '<' || c == '=' || c == ' '
  59. }
  60. for _, dep := range toCheck {
  61. if _, erp := localDb.PkgCache().FindSatisfier(dep); erp == nil {
  62. continue
  63. } else if pkg, erp := dbList.FindSatisfier(dep); erp == nil {
  64. repo = append(repo, pkg.Name())
  65. } else {
  66. field := strings.FieldsFunc(dep, f)
  67. notFound = append(notFound, field[0])
  68. }
  69. }
  70. err = nil
  71. return
  72. }
  73. // PkgDependencies returns package dependencies not installed belonging to AUR
  74. // 0 is Repo, 1 is Foreign.
  75. func pkgDependencies(a *rpc.Pkg) (runDeps [2][]string, makeDeps [2][]string, err error) {
  76. var q aurQuery
  77. if len(a.Depends) == 0 && len(a.MakeDepends) == 0 {
  78. q, err = rpc.Info([]string{a.Name})
  79. if len(q) == 0 || err != nil {
  80. err = fmt.Errorf("Unable to search dependencies, %s", err)
  81. return
  82. }
  83. } else {
  84. q = append(q, *a)
  85. }
  86. depSearch := buildDependencies(a.Depends)
  87. if len(a.Depends) != 0 {
  88. runDeps[0], runDeps[1] = depSearch(q[0].Depends, true, false)
  89. if len(runDeps[0]) != 0 || len(runDeps[1]) != 0 {
  90. fmt.Println("\x1b[1;32m=>\x1b[1;33m Run Dependencies: \x1b[0m")
  91. printDeps(runDeps[0], runDeps[1])
  92. }
  93. }
  94. if len(a.MakeDepends) != 0 {
  95. makeDeps[0], makeDeps[1] = depSearch(q[0].MakeDepends, false, false)
  96. if len(makeDeps[0]) != 0 || len(makeDeps[1]) != 0 {
  97. fmt.Println("\x1b[1;32m=>\x1b[1;33m Make Dependencies: \x1b[0m")
  98. printDeps(makeDeps[0], makeDeps[1])
  99. }
  100. }
  101. depSearch(a.MakeDepends, false, true)
  102. err = nil
  103. return
  104. }