dependencies.go 12 KB

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