dep.go 4.1 KB

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