dependencies.go 12 KB

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