dependencies.go 13 KB

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