dep.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. type Base []*rpc.Pkg
  34. func (b Base) Pkgbase() string {
  35. return b[0].PackageBase
  36. }
  37. func (b Base) Version() string {
  38. return b[0].Version
  39. }
  40. func (b Base) URLPath() string {
  41. return b[0].URLPath
  42. }
  43. type target struct {
  44. Db string
  45. Name string
  46. Mod string
  47. Version string
  48. }
  49. func toTarget(pkg string) target {
  50. db, dep := splitDbFromName(pkg)
  51. name, mod, version := splitDep(dep)
  52. return target{
  53. db,
  54. name,
  55. mod,
  56. version,
  57. }
  58. }
  59. func (t target) DepString() string {
  60. return t.Name + t.Mod + t.Version
  61. }
  62. func (t target) String() string {
  63. if t.Db != "" {
  64. return t.Db + "/" + t.DepString()
  65. }
  66. return t.DepString()
  67. }
  68. func splitDep(dep string) (string, string, string) {
  69. mod := ""
  70. split := strings.FieldsFunc(dep, func(c rune) bool {
  71. match := c == '>' || c == '<' || c == '='
  72. if match {
  73. mod += string(c)
  74. }
  75. return match
  76. })
  77. if len(split) == 0 {
  78. return "", "", ""
  79. }
  80. if len(split) == 1 {
  81. return split[0], "", ""
  82. }
  83. return split[0], mod, split[1]
  84. }
  85. func pkgSatisfies(name, version, dep string) bool {
  86. depName, depMod, depVersion := splitDep(dep)
  87. if depName != name {
  88. return false
  89. }
  90. return verSatisfies(version, depMod, depVersion)
  91. }
  92. func provideSatisfies(provide, dep string) bool {
  93. depName, depMod, depVersion := splitDep(dep)
  94. provideName, provideMod, provideVersion := splitDep(provide)
  95. if provideName != depName {
  96. return false
  97. }
  98. // Unversioned provieds can not satisfy a versioned dep
  99. if provideMod == "" && depMod != "" {
  100. return false
  101. }
  102. return verSatisfies(provideVersion, depMod, depVersion)
  103. }
  104. func verSatisfies(ver1, mod, ver2 string) bool {
  105. switch mod {
  106. case "=":
  107. return alpm.VerCmp(ver1, ver2) == 0
  108. case "<":
  109. return alpm.VerCmp(ver1, ver2) < 0
  110. case "<=":
  111. return alpm.VerCmp(ver1, ver2) <= 0
  112. case ">":
  113. return alpm.VerCmp(ver1, ver2) > 0
  114. case ">=":
  115. return alpm.VerCmp(ver1, ver2) >= 0
  116. }
  117. return true
  118. }
  119. func satisfiesAur(dep string, pkg *rpc.Pkg) bool {
  120. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  121. return true
  122. }
  123. for _, provide := range pkg.Provides {
  124. if provideSatisfies(provide, dep) {
  125. return true
  126. }
  127. }
  128. return false
  129. }
  130. func satisfiesRepo(dep string, pkg *alpm.Package) bool {
  131. if pkgSatisfies(pkg.Name(), pkg.Version(), dep) {
  132. return true
  133. }
  134. if pkg.Provides().ForEach(func(provide alpm.Depend) error {
  135. if provideSatisfies(provide.String(), dep) {
  136. return fmt.Errorf("")
  137. }
  138. return nil
  139. }) != nil {
  140. return true
  141. }
  142. return false
  143. }
  144. //split apart db/package to db and package
  145. func splitDbFromName(pkg string) (string, string) {
  146. split := strings.SplitN(pkg, "/", 2)
  147. if len(split) == 2 {
  148. return split[0], split[1]
  149. }
  150. return "", split[0]
  151. }
  152. func getBases(pkgs []*rpc.Pkg) []Base {
  153. basesMap := make(map[string]Base)
  154. for _, pkg := range pkgs {
  155. basesMap[pkg.PackageBase] = append(basesMap[pkg.PackageBase], pkg)
  156. }
  157. bases := make([]Base, 0, len(basesMap))
  158. for _, base := range basesMap {
  159. bases = append(bases, base)
  160. }
  161. return bases
  162. }
  163. func isDevelName(name string) bool {
  164. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly"} {
  165. if strings.HasSuffix(name, "-"+suffix) {
  166. return true
  167. }
  168. }
  169. return strings.Contains(name, "-always-")
  170. }