dependencies.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package main
  2. import (
  3. "strings"
  4. alpm "github.com/jguer/go-alpm"
  5. rpc "github.com/mikkeloscar/aur"
  6. )
  7. type depTree struct {
  8. ToProcess stringSet
  9. Repo map[string]*alpm.Package
  10. Aur map[string]*rpc.Pkg
  11. Missing stringSet
  12. }
  13. type depCatagories struct {
  14. Repo []*alpm.Package
  15. Aur []*rpc.Pkg
  16. MakeOnly stringSet
  17. Bases map[string][]*rpc.Pkg
  18. }
  19. func makeDepTree() *depTree {
  20. dt := depTree{
  21. make(stringSet),
  22. make(map[string]*alpm.Package),
  23. make(map[string]*rpc.Pkg),
  24. make(stringSet),
  25. }
  26. return &dt
  27. }
  28. func makeDependCatagories() *depCatagories {
  29. dc := depCatagories{
  30. make([]*alpm.Package, 0),
  31. make([]*rpc.Pkg, 0),
  32. make(stringSet),
  33. make(map[string][]*rpc.Pkg),
  34. }
  35. return &dc
  36. }
  37. // Cut the version requirement from a dependency leaving just the name.
  38. func getNameFromDep(dep string) string {
  39. return strings.FieldsFunc(dep, func(c rune) bool {
  40. return c == '>' || c == '<' || c == '='
  41. })[0]
  42. }
  43. // Step two of dependency resolving. We already have all the information on the
  44. // packages we need, now it's just about ordering them correctly.
  45. // pkgs is a list of targets, the packages we want to install. Dependencies are
  46. // not included.
  47. // For each package we want we iterate down the tree until we hit the bottom.
  48. // This is done recursively for each branch.
  49. // The start of the tree is defined as the package we want.
  50. // When we hit the bottom of the branch we know thats the first package
  51. // we need to install so we add it to the start of the to install
  52. // list (dc.Aur and dc.Repo).
  53. // We work our way up until there is another branch to go down and do it all
  54. // again.
  55. //
  56. // Here is a visual example:
  57. //
  58. // a
  59. // / \
  60. // b c
  61. // / \
  62. // d e
  63. //
  64. // We see a and it needs b and c
  65. // We see b and it needs d and e
  66. // We see d - it needs nothing so we add d to our list and move up
  67. // We see e - it needs nothing so we add e to our list and move up
  68. // We see c - it needs nothing so we add c to our list and move up
  69. //
  70. // The final install order would come out as debca
  71. //
  72. // There is a little more to this, handling provides, multiple packages wanting the
  73. // same dependencies, etc. This is just the basic premise.
  74. func getDepCatagories(pkgs []string, dt *depTree) (*depCatagories, error) {
  75. dc := makeDependCatagories()
  76. seen := make(stringSet)
  77. for _, pkg := range dt.Aur {
  78. _, ok := dc.Bases[pkg.PackageBase]
  79. if !ok {
  80. dc.Bases[pkg.PackageBase] = make([]*rpc.Pkg, 0)
  81. }
  82. dc.Bases[pkg.PackageBase] = append(dc.Bases[pkg.PackageBase], pkg)
  83. }
  84. for _, pkg := range pkgs {
  85. dep := getNameFromDep(pkg)
  86. alpmpkg, exists := dt.Repo[dep]
  87. if exists {
  88. repoDepCatagoriesRecursive(alpmpkg, dc, dt, false)
  89. dc.Repo = append(dc.Repo, alpmpkg)
  90. delete(dt.Repo, dep)
  91. }
  92. aurpkg, exists := dt.Aur[dep]
  93. if exists {
  94. depCatagoriesRecursive(aurpkg, dc, dt, false, seen)
  95. if !seen.get(aurpkg.PackageBase) {
  96. dc.Aur = append(dc.Aur, aurpkg)
  97. seen.set(aurpkg.PackageBase)
  98. }
  99. delete(dt.Aur, dep)
  100. }
  101. }
  102. for _, base := range dc.Bases {
  103. for _, pkg := range base {
  104. for _, dep := range pkg.Depends {
  105. dc.MakeOnly.remove(dep)
  106. }
  107. }
  108. }
  109. for _, pkg := range dc.Repo {
  110. pkg.Depends().ForEach(func(_dep alpm.Depend) error {
  111. dep := _dep.Name
  112. dc.MakeOnly.remove(dep)
  113. return nil
  114. })
  115. }
  116. dupes := make(map[*alpm.Package]struct{})
  117. filteredRepo := make([]*alpm.Package, 0)
  118. for _, pkg := range dc.Repo {
  119. _, ok := dupes[pkg]
  120. if ok {
  121. continue
  122. }
  123. dupes[pkg] = struct{}{}
  124. filteredRepo = append(filteredRepo, pkg)
  125. }
  126. dc.Repo = filteredRepo
  127. return dc, nil
  128. }
  129. func repoDepCatagoriesRecursive(pkg *alpm.Package, dc *depCatagories, dt *depTree, isMake bool) {
  130. pkg.Depends().ForEach(func(_dep alpm.Depend) error {
  131. dep := _dep.Name
  132. alpmpkg, exists := dt.Repo[dep]
  133. if exists {
  134. delete(dt.Repo, dep)
  135. repoDepCatagoriesRecursive(alpmpkg, dc, dt, isMake)
  136. if isMake {
  137. dc.MakeOnly.set(alpmpkg.Name())
  138. }
  139. dc.Repo = append(dc.Repo, alpmpkg)
  140. }
  141. return nil
  142. })
  143. }
  144. func depCatagoriesRecursive(_pkg *rpc.Pkg, dc *depCatagories, dt *depTree, isMake bool, seen stringSet) {
  145. for _, pkg := range dc.Bases[_pkg.PackageBase] {
  146. for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
  147. for _, _dep := range deps {
  148. dep := getNameFromDep(_dep)
  149. aurpkg, exists := dt.Aur[dep]
  150. if exists {
  151. delete(dt.Aur, dep)
  152. depCatagoriesRecursive(aurpkg, dc, dt, isMake, seen)
  153. if !seen.get(aurpkg.PackageBase) {
  154. dc.Aur = append(dc.Aur, aurpkg)
  155. seen.set(aurpkg.PackageBase)
  156. }
  157. if isMake {
  158. dc.MakeOnly.set(aurpkg.Name)
  159. }
  160. }
  161. alpmpkg, exists := dt.Repo[dep]
  162. if exists {
  163. delete(dt.Repo, dep)
  164. repoDepCatagoriesRecursive(alpmpkg, dc, dt, isMake)
  165. if isMake {
  166. dc.MakeOnly.set(alpmpkg.Name())
  167. }
  168. dc.Repo = append(dc.Repo, alpmpkg)
  169. }
  170. }
  171. isMake = true
  172. }
  173. }
  174. }
  175. // This is step one for dependency resolving. pkgs is a slice of the packages you
  176. // want to resolve the dependencies for. They can be a mix of aur and repo
  177. // dependencies. All unmet dependencies will be resolved.
  178. //
  179. // For Aur dependencies depends, makedepends and checkdepends are resolved but
  180. // for repo packages only depends are resolved as they are prebuilt.
  181. // The return will be split into three catagories: Repo, Aur and Missing.
  182. // The return is in no way ordered. This step is is just aimed at gathering the
  183. // packages we need.
  184. //
  185. // This has been designed to make the least amount of rpc requests as possible.
  186. // Web requests are probably going to be the bottleneck here so minimizing them
  187. // provides a nice speed boost.
  188. //
  189. // Here is a visual expample of the request system.
  190. // Remember only unsatisfied packages are requested, if a package is already
  191. // installed we dont bother.
  192. //
  193. // a
  194. // / \
  195. // b c
  196. // / \
  197. // d e
  198. //
  199. // We see a so we send a request for a
  200. // We see a wants b and c so we send a request for b and c
  201. // We see d and e so we send a request for d and e
  202. //
  203. // Thats 5 packages in 3 requests. The amount of requests needed should always be
  204. // the same as the height of the tree.
  205. // The example does not really do this justice, In the real world where packages
  206. // have 10+ dependencies each this is a very nice optimization.
  207. func getDepTree(pkgs []string) (*depTree, error) {
  208. dt := makeDepTree()
  209. localDb, err := alpmHandle.LocalDb()
  210. if err != nil {
  211. return dt, err
  212. }
  213. syncDb, err := alpmHandle.SyncDbs()
  214. if err != nil {
  215. return dt, err
  216. }
  217. for _, pkg := range pkgs {
  218. // If they explicitly asked for it still look for installed pkgs
  219. /*installedPkg, isInstalled := localDb.PkgCache().FindSatisfier(pkg)
  220. if isInstalled == nil {
  221. dt.Repo[installedPkg.Name()] = installedPkg
  222. continue
  223. }//*/
  224. // Check the repos for a matching dep
  225. repoPkg, inRepos := syncDb.FindSatisfier(pkg)
  226. if inRepos == nil {
  227. repoTreeRecursive(repoPkg, dt, localDb, syncDb)
  228. continue
  229. }
  230. _, isGroup := syncDb.PkgCachebyGroup(pkg)
  231. if isGroup == nil {
  232. continue
  233. }
  234. dt.ToProcess.set(pkg)
  235. }
  236. err = depTreeRecursive(dt, localDb, syncDb, false)
  237. return dt, err
  238. }
  239. // Takes a repo package,
  240. // gives all of the non installed deps,
  241. // repeats on each sub dep.
  242. func repoTreeRecursive(pkg *alpm.Package, dt *depTree, localDb *alpm.Db, syncDb alpm.DbList) (err error) {
  243. _, exists := dt.Repo[pkg.Name()]
  244. if exists {
  245. return
  246. }
  247. dt.Repo[pkg.Name()] = pkg
  248. (*pkg).Provides().ForEach(func(dep alpm.Depend) (err error) {
  249. dt.Repo[dep.Name] = pkg
  250. return nil
  251. })
  252. (*pkg).Depends().ForEach(func(dep alpm.Depend) (err error) {
  253. _, exists := dt.Repo[dep.Name]
  254. if exists {
  255. return
  256. }
  257. _, isInstalled := localDb.PkgCache().FindSatisfier(dep.String())
  258. if isInstalled == nil {
  259. return
  260. }
  261. repoPkg, inRepos := syncDb.FindSatisfier(dep.String())
  262. if inRepos == nil {
  263. repoTreeRecursive(repoPkg, dt, localDb, syncDb)
  264. return
  265. }
  266. dt.Missing.set(dep.String())
  267. return
  268. })
  269. return
  270. }
  271. func depTreeRecursive(dt *depTree, localDb *alpm.Db, syncDb alpm.DbList, isMake bool) (err error) {
  272. if len(dt.ToProcess) == 0 {
  273. return
  274. }
  275. nextProcess := make(stringSet)
  276. currentProcess := make(stringSet)
  277. // Strip version conditions
  278. for dep := range dt.ToProcess {
  279. currentProcess.set(getNameFromDep(dep))
  280. }
  281. // Assume toprocess only contains aur stuff we have not seen
  282. info, err := aurInfo(currentProcess.toSlice())
  283. if err != nil {
  284. return
  285. }
  286. // Cache the results
  287. for _, pkg := range info {
  288. // Copying to p fixes a bug.
  289. // Would rather not copy but cant find another way to fix.
  290. p := pkg
  291. dt.Aur[pkg.Name] = &p
  292. }
  293. // Loop through to process and check if we now have
  294. // each packaged cached.
  295. // If not cached, we assume it is missing.
  296. for pkgName := range currentProcess {
  297. pkg, exists := dt.Aur[pkgName]
  298. // Did not get it in the request.
  299. if !exists {
  300. dt.Missing.set(pkgName)
  301. continue
  302. }
  303. // for each dep and makedep
  304. for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
  305. for _, versionedDep := range deps {
  306. dep := getNameFromDep(versionedDep)
  307. _, exists = dt.Aur[dep]
  308. // We have it cached so skip.
  309. if exists {
  310. continue
  311. }
  312. _, exists = dt.Repo[dep]
  313. // We have it cached so skip.
  314. if exists {
  315. continue
  316. }
  317. _, exists = dt.Missing[dep]
  318. // We know it does not resolve so skip.
  319. if exists {
  320. continue
  321. }
  322. // Check if already installed.
  323. _, isInstalled := localDb.PkgCache().FindSatisfier(versionedDep)
  324. if isInstalled == nil {
  325. continue
  326. }
  327. // Check the repos for a matching dep.
  328. repoPkg, inRepos := syncDb.FindSatisfier(versionedDep)
  329. if inRepos == nil {
  330. repoTreeRecursive(repoPkg, dt, localDb, syncDb)
  331. continue
  332. }
  333. // If all else fails add it to next search.
  334. nextProcess.set(versionedDep)
  335. }
  336. }
  337. }
  338. dt.ToProcess = nextProcess
  339. depTreeRecursive(dt, localDb, syncDb, true)
  340. return
  341. }