dependencies.go 10 KB

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