dependencies.go 12 KB

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