dependencies.go 12 KB

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