dep.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. alpm "github.com/Jguer/go-alpm"
  6. rpc "github.com/mikkeloscar/aur"
  7. )
  8. type providers struct {
  9. lookfor string
  10. Pkgs []*rpc.Pkg
  11. }
  12. func makeProviders(name string) providers {
  13. return providers{
  14. name,
  15. make([]*rpc.Pkg, 0),
  16. }
  17. }
  18. func (q providers) Len() int {
  19. return len(q.Pkgs)
  20. }
  21. func (q providers) Less(i, j int) bool {
  22. if q.lookfor == q.Pkgs[i].Name {
  23. return true
  24. }
  25. if q.lookfor == q.Pkgs[j].Name {
  26. return false
  27. }
  28. return LessRunes([]rune(q.Pkgs[i].Name), []rune(q.Pkgs[j].Name))
  29. }
  30. func (q providers) Swap(i, j int) {
  31. q.Pkgs[i], q.Pkgs[j] = q.Pkgs[j], q.Pkgs[i]
  32. }
  33. func splitDep(dep string) (string, string, string) {
  34. mod := ""
  35. split := strings.FieldsFunc(dep, func(c rune) bool {
  36. match := c == '>' || c == '<' || c == '='
  37. if match {
  38. mod += string(c)
  39. }
  40. return match
  41. })
  42. if len(split) == 0 {
  43. return "", "", ""
  44. }
  45. if len(split) == 1 {
  46. return split[0], "", ""
  47. }
  48. return split[0], mod, split[1]
  49. }
  50. func pkgSatisfies(name, version, dep string) bool {
  51. depName, depMod, depVersion := splitDep(dep)
  52. if depName != name {
  53. return false
  54. }
  55. return verSatisfies(version, depMod, depVersion)
  56. }
  57. func provideSatisfies(provide, dep string) bool {
  58. depName, depMod, depVersion := splitDep(dep)
  59. provideName, provideMod, provideVersion := splitDep(provide)
  60. if provideName != depName {
  61. return false
  62. }
  63. // Unversioned provieds can not satisfy a versioned dep
  64. if provideMod == "" && depMod != "" {
  65. return false
  66. }
  67. return verSatisfies(provideVersion, depMod, depVersion)
  68. }
  69. func verSatisfies(ver1, mod, ver2 string) bool {
  70. switch mod {
  71. case "=":
  72. return alpm.VerCmp(ver1, ver2) == 0
  73. case "<":
  74. return alpm.VerCmp(ver1, ver2) < 0
  75. case "<=":
  76. return alpm.VerCmp(ver1, ver2) <= 0
  77. case ">":
  78. return alpm.VerCmp(ver1, ver2) > 0
  79. case ">=":
  80. return alpm.VerCmp(ver1, ver2) >= 0
  81. }
  82. return true
  83. }
  84. func satisfiesAur(dep string, pkg *rpc.Pkg) bool {
  85. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  86. return true
  87. }
  88. for _, provide := range pkg.Provides {
  89. if provideSatisfies(provide, dep) {
  90. return true
  91. }
  92. }
  93. return false
  94. }
  95. func satisfiesRepo(dep string, pkg *alpm.Package) bool {
  96. if pkgSatisfies(pkg.Name(), pkg.Version(), dep) {
  97. return true
  98. }
  99. if pkg.Provides().ForEach(func(provide alpm.Depend) error {
  100. if provideSatisfies(provide.String(), dep) {
  101. return fmt.Errorf("")
  102. }
  103. return nil
  104. }) != nil {
  105. return true
  106. }
  107. return false
  108. }
  109. //split apart db/package to db and package
  110. func splitDBFromName(pkg string) (string, string) {
  111. split := strings.SplitN(pkg, "/", 2)
  112. if len(split) == 2 {
  113. return split[0], split[1]
  114. }
  115. return "", split[0]
  116. }
  117. func getBases(pkgs []*rpc.Pkg) []Base {
  118. basesMap := make(map[string]Base)
  119. for _, pkg := range pkgs {
  120. basesMap[pkg.PackageBase] = append(basesMap[pkg.PackageBase], pkg)
  121. }
  122. bases := make([]Base, 0, len(basesMap))
  123. for _, base := range basesMap {
  124. bases = append(bases, base)
  125. }
  126. return bases
  127. }
  128. func isDevelName(name string) bool {
  129. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly"} {
  130. if strings.HasSuffix(name, "-"+suffix) {
  131. return true
  132. }
  133. }
  134. return strings.Contains(name, "-always-")
  135. }
  136. func isDevelPackage(pkg alpm.Package) bool {
  137. return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
  138. }