dependencies.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. for _, pkg := range pkgs {
  117. dc.MakeOnly.remove(pkg)
  118. }
  119. dupes := make(map[*alpm.Package]struct{})
  120. filteredRepo := make([]*alpm.Package, 0)
  121. for _, pkg := range dc.Repo {
  122. _, ok := dupes[pkg]
  123. if ok {
  124. continue
  125. }
  126. dupes[pkg] = struct{}{}
  127. filteredRepo = append(filteredRepo, pkg)
  128. }
  129. dc.Repo = filteredRepo
  130. return dc, nil
  131. }
  132. func repoDepCatagoriesRecursive(pkg *alpm.Package, dc *depCatagories, dt *depTree, isMake bool) {
  133. pkg.Depends().ForEach(func(_dep alpm.Depend) error {
  134. dep := _dep.Name
  135. alpmpkg, exists := dt.Repo[dep]
  136. if exists {
  137. delete(dt.Repo, dep)
  138. repoDepCatagoriesRecursive(alpmpkg, dc, dt, isMake)
  139. if isMake {
  140. dc.MakeOnly.set(alpmpkg.Name())
  141. }
  142. dc.Repo = append(dc.Repo, alpmpkg)
  143. }
  144. return nil
  145. })
  146. }
  147. func depCatagoriesRecursive(_pkg *rpc.Pkg, dc *depCatagories, dt *depTree, isMake bool, seen stringSet) {
  148. for _, pkg := range dc.Bases[_pkg.PackageBase] {
  149. for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
  150. for _, _dep := range deps {
  151. dep := getNameFromDep(_dep)
  152. aurpkg, exists := dt.Aur[dep]
  153. if exists {
  154. delete(dt.Aur, dep)
  155. depCatagoriesRecursive(aurpkg, dc, dt, isMake, seen)
  156. if !seen.get(aurpkg.PackageBase) {
  157. dc.Aur = append(dc.Aur, aurpkg)
  158. seen.set(aurpkg.PackageBase)
  159. }
  160. if isMake {
  161. dc.MakeOnly.set(aurpkg.Name)
  162. }
  163. }
  164. alpmpkg, exists := dt.Repo[dep]
  165. if exists {
  166. delete(dt.Repo, dep)
  167. repoDepCatagoriesRecursive(alpmpkg, dc, dt, isMake)
  168. if isMake {
  169. dc.MakeOnly.set(alpmpkg.Name())
  170. }
  171. dc.Repo = append(dc.Repo, alpmpkg)
  172. }
  173. }
  174. isMake = true
  175. }
  176. }
  177. }
  178. // This is step one for dependency resolving. pkgs is a slice of the packages you
  179. // want to resolve the dependencies for. They can be a mix of aur and repo
  180. // dependencies. All unmet dependencies will be resolved.
  181. //
  182. // For Aur dependencies depends, makedepends and checkdepends are resolved but
  183. // for repo packages only depends are resolved as they are prebuilt.
  184. // The return will be split into three catagories: Repo, Aur and Missing.
  185. // The return is in no way ordered. This step is is just aimed at gathering the
  186. // packages we need.
  187. //
  188. // This has been designed to make the least amount of rpc requests as possible.
  189. // Web requests are probably going to be the bottleneck here so minimizing them
  190. // provides a nice speed boost.
  191. //
  192. // Here is a visual expample of the request system.
  193. // Remember only unsatisfied packages are requested, if a package is already
  194. // installed we dont bother.
  195. //
  196. // a
  197. // / \
  198. // b c
  199. // / \
  200. // d e
  201. //
  202. // We see a so we send a request for a
  203. // We see a wants b and c so we send a request for b and c
  204. // We see d and e so we send a request for d and e
  205. //
  206. // Thats 5 packages in 3 requests. The amount of requests needed should always be
  207. // the same as the height of the tree.
  208. // The example does not really do this justice, In the real world where packages
  209. // have 10+ dependencies each this is a very nice optimization.
  210. func getDepTree(pkgs []string) (*depTree, error) {
  211. dt := makeDepTree()
  212. localDb, err := alpmHandle.LocalDb()
  213. if err != nil {
  214. return dt, err
  215. }
  216. syncDb, err := alpmHandle.SyncDbs()
  217. if err != nil {
  218. return dt, err
  219. }
  220. for _, pkg := range pkgs {
  221. // If they explicitly asked for it still look for installed pkgs
  222. /*installedPkg, isInstalled := localDb.PkgCache().FindSatisfier(pkg)
  223. if isInstalled == nil {
  224. dt.Repo[installedPkg.Name()] = installedPkg
  225. continue
  226. }//*/
  227. // Check the repos for a matching dep
  228. repoPkg, inRepos := syncDb.FindSatisfier(pkg)
  229. if inRepos == nil {
  230. repoTreeRecursive(repoPkg, dt, localDb, syncDb)
  231. continue
  232. }
  233. _, isGroup := syncDb.PkgCachebyGroup(pkg)
  234. if isGroup == nil {
  235. continue
  236. }
  237. dt.ToProcess.set(pkg)
  238. }
  239. err = depTreeRecursive(dt, localDb, syncDb, false)
  240. return dt, err
  241. }
  242. // Takes a repo package,
  243. // gives all of the non installed deps,
  244. // repeats on each sub dep.
  245. func repoTreeRecursive(pkg *alpm.Package, dt *depTree, localDb *alpm.Db, syncDb alpm.DbList) (err error) {
  246. _, exists := dt.Repo[pkg.Name()]
  247. if exists {
  248. return
  249. }
  250. dt.Repo[pkg.Name()] = pkg
  251. (*pkg).Provides().ForEach(func(dep alpm.Depend) (err error) {
  252. dt.Repo[dep.Name] = pkg
  253. return nil
  254. })
  255. (*pkg).Depends().ForEach(func(dep alpm.Depend) (err error) {
  256. _, exists := dt.Repo[dep.Name]
  257. if exists {
  258. return
  259. }
  260. _, isInstalled := localDb.PkgCache().FindSatisfier(dep.String())
  261. if isInstalled == nil {
  262. return
  263. }
  264. repoPkg, inRepos := syncDb.FindSatisfier(dep.String())
  265. if inRepos == nil {
  266. repoTreeRecursive(repoPkg, dt, localDb, syncDb)
  267. return
  268. }
  269. dt.Missing.set(dep.String())
  270. return
  271. })
  272. return
  273. }
  274. func depTreeRecursive(dt *depTree, localDb *alpm.Db, syncDb alpm.DbList, isMake bool) (err error) {
  275. if len(dt.ToProcess) == 0 {
  276. return
  277. }
  278. nextProcess := make(stringSet)
  279. currentProcess := make(stringSet)
  280. // Strip version conditions
  281. for dep := range dt.ToProcess {
  282. currentProcess.set(getNameFromDep(dep))
  283. }
  284. // Assume toprocess only contains aur stuff we have not seen
  285. info, err := aurInfo(currentProcess.toSlice())
  286. if err != nil {
  287. return
  288. }
  289. // Cache the results
  290. for _, pkg := range info {
  291. // Copying to p fixes a bug.
  292. // Would rather not copy but cant find another way to fix.
  293. p := pkg
  294. dt.Aur[pkg.Name] = &p
  295. }
  296. // Loop through to process and check if we now have
  297. // each packaged cached.
  298. // If not cached, we assume it is missing.
  299. for pkgName := range currentProcess {
  300. pkg, exists := dt.Aur[pkgName]
  301. // Did not get it in the request.
  302. if !exists {
  303. dt.Missing.set(pkgName)
  304. continue
  305. }
  306. // for each dep and makedep
  307. for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
  308. for _, versionedDep := range deps {
  309. dep := getNameFromDep(versionedDep)
  310. _, exists = dt.Aur[dep]
  311. // We have it cached so skip.
  312. if exists {
  313. continue
  314. }
  315. _, exists = dt.Repo[dep]
  316. // We have it cached so skip.
  317. if exists {
  318. continue
  319. }
  320. _, exists = dt.Missing[dep]
  321. // We know it does not resolve so skip.
  322. if exists {
  323. continue
  324. }
  325. // Check if already installed.
  326. _, isInstalled := localDb.PkgCache().FindSatisfier(versionedDep)
  327. if isInstalled == nil {
  328. continue
  329. }
  330. // Check the repos for a matching dep.
  331. repoPkg, inRepos := syncDb.FindSatisfier(versionedDep)
  332. if inRepos == nil {
  333. repoTreeRecursive(repoPkg, dt, localDb, syncDb)
  334. continue
  335. }
  336. // If all else fails add it to next search.
  337. nextProcess.set(versionedDep)
  338. }
  339. }
  340. }
  341. dt.ToProcess = nextProcess
  342. depTreeRecursive(dt, localDb, syncDb, true)
  343. return
  344. }