depPool.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package main
  2. import (
  3. "sort"
  4. "strings"
  5. "sync"
  6. alpm "github.com/jguer/go-alpm"
  7. rpc "github.com/mikkeloscar/aur"
  8. )
  9. type target struct {
  10. Db string
  11. Name string
  12. Mod string
  13. Version string
  14. }
  15. func toTarget(pkg string) target {
  16. db, dep := splitDbFromName(pkg)
  17. name, mod, version := splitDep(dep)
  18. return target{
  19. db,
  20. name,
  21. mod,
  22. version,
  23. }
  24. }
  25. func (t target) DepString() string {
  26. return t.Name + t.Mod + t.Version
  27. }
  28. func (t target) String() string {
  29. if t.Db != "" {
  30. return t.Db + "/" + t.DepString()
  31. }
  32. return t.DepString()
  33. }
  34. type depPool struct {
  35. Targets []target
  36. Explicit stringSet
  37. Repo map[string]*alpm.Package
  38. Aur map[string]*rpc.Pkg
  39. AurCache map[string]*rpc.Pkg
  40. Groups []string
  41. LocalDb *alpm.Db
  42. SyncDb alpm.DbList
  43. Warnings *aurWarnings
  44. }
  45. func makeDepPool() (*depPool, error) {
  46. localDb, err := alpmHandle.LocalDb()
  47. if err != nil {
  48. return nil, err
  49. }
  50. syncDb, err := alpmHandle.SyncDbs()
  51. if err != nil {
  52. return nil, err
  53. }
  54. dp := &depPool{
  55. make([]target, 0),
  56. make(stringSet),
  57. make(map[string]*alpm.Package),
  58. make(map[string]*rpc.Pkg),
  59. make(map[string]*rpc.Pkg),
  60. make([]string, 0),
  61. localDb,
  62. syncDb,
  63. nil,
  64. }
  65. return dp, nil
  66. }
  67. // Includes db/ prefixes and group installs
  68. func (dp *depPool) ResolveTargets(pkgs []string) error {
  69. // RPC requests are slow
  70. // Combine as many AUR package requests as possible into a single RPC
  71. // call
  72. aurTargets := make(stringSet)
  73. for _, pkg := range pkgs {
  74. var err error
  75. target := toTarget(pkg)
  76. // skip targets already satisfied
  77. // even if the user enters db/pkg and aur/pkg the latter will
  78. // still get skiped even if it's from a different database to
  79. // the one specified
  80. // this is how pacman behaves
  81. if dp.hasPackage(target.DepString()) {
  82. continue
  83. }
  84. var foundPkg *alpm.Package
  85. var singleDb *alpm.Db
  86. // aur/ prefix means we only check the aur
  87. if target.Db == "aur" {
  88. dp.Targets = append(dp.Targets, target)
  89. aurTargets.set(target.DepString())
  90. continue
  91. }
  92. // if theres a different priefix only look in that repo
  93. if target.Db != "" {
  94. singleDb, err = alpmHandle.SyncDbByName(target.Db)
  95. if err != nil {
  96. return err
  97. }
  98. foundPkg, err = singleDb.PkgCache().FindSatisfier(target.DepString())
  99. //otherwise find it in any repo
  100. } else {
  101. foundPkg, err = dp.SyncDb.FindSatisfier(target.DepString())
  102. }
  103. if err == nil {
  104. dp.Targets = append(dp.Targets, target)
  105. dp.Explicit.set(foundPkg.Name())
  106. dp.ResolveRepoDependency(foundPkg)
  107. continue
  108. } else {
  109. //check for groups
  110. //currently we dont resolve the packages in a group
  111. //only check if the group exists
  112. //would be better to check the groups from singleDb if
  113. //the user specified a db but theres no easy way to do
  114. //it without making alpm_lists so dont bother for now
  115. //db/group is probably a rare use case
  116. group, err := dp.SyncDb.PkgCachebyGroup(target.Name)
  117. if err == nil {
  118. dp.Groups = append(dp.Groups, target.String())
  119. group.ForEach(func(pkg alpm.Package) error {
  120. dp.Explicit.set(pkg.Name())
  121. return nil
  122. })
  123. continue
  124. }
  125. }
  126. //if there was no db prefix check the aur
  127. if target.Db == "" {
  128. aurTargets.set(target.DepString())
  129. }
  130. dp.Targets = append(dp.Targets, target)
  131. }
  132. if len(aurTargets) > 0 {
  133. return dp.resolveAURPackages(aurTargets, true)
  134. }
  135. return nil
  136. }
  137. // Pseudo provides finder.
  138. // Try to find provides by performing a search of the package name
  139. // This effectively performs -Ss on each package
  140. // then runs -Si on each result to cache the information.
  141. //
  142. // For example if you were to -S yay then yay -Ss would give:
  143. // yay-git yay-bin yay realyog pacui pacui-git ruby-yard
  144. // These packages will all be added to the cache incase they are needed later
  145. // Ofcouse only the first three packages provide yay, the rest are just false
  146. // positives.
  147. //
  148. // This method increases dependency resolve time
  149. func (dp *depPool) findProvides(pkgs stringSet) error {
  150. var mux sync.Mutex
  151. var wg sync.WaitGroup
  152. doSearch := func(pkg string) {
  153. defer wg.Done()
  154. var err error
  155. var results []rpc.Pkg
  156. // Hack for a bigger search result, if the user wants
  157. // java-envronment we can search for just java instead and get
  158. // more hits.
  159. words := strings.Split(pkg, "-")
  160. for i := range words {
  161. results, err = rpc.SearchByNameDesc(strings.Join(words[:i+1], "-"))
  162. if err == nil {
  163. break
  164. }
  165. }
  166. if err != nil {
  167. return
  168. }
  169. for _, result := range results {
  170. mux.Lock()
  171. if _, ok := dp.AurCache[result.Name]; !ok {
  172. pkgs.set(result.Name)
  173. }
  174. mux.Unlock()
  175. }
  176. }
  177. for pkg := range pkgs {
  178. if _, err := dp.LocalDb.PkgByName(pkg); err == nil {
  179. continue
  180. }
  181. wg.Add(1)
  182. go doSearch(pkg)
  183. }
  184. wg.Wait()
  185. return nil
  186. }
  187. func (dp *depPool) cacheAURPackages(_pkgs stringSet) error {
  188. pkgs := _pkgs.copy()
  189. query := make([]string, 0)
  190. for pkg := range pkgs {
  191. if _, ok := dp.AurCache[pkg]; ok {
  192. pkgs.remove(pkg)
  193. }
  194. }
  195. if len(pkgs) == 0 {
  196. return nil
  197. }
  198. if config.Provides {
  199. err := dp.findProvides(pkgs)
  200. if err != nil {
  201. return err
  202. }
  203. }
  204. for pkg := range pkgs {
  205. if _, ok := dp.AurCache[pkg]; !ok {
  206. name, _, _ := splitDep(pkg)
  207. query = append(query, name)
  208. }
  209. }
  210. info, err := aurInfo(query, dp.Warnings)
  211. if err != nil {
  212. return err
  213. }
  214. for _, pkg := range info {
  215. // Dump everything in cache just in case we need it later
  216. dp.AurCache[pkg.Name] = pkg
  217. }
  218. return nil
  219. }
  220. func (dp *depPool) resolveAURPackages(pkgs stringSet, explicit bool) error {
  221. newPackages := make(stringSet)
  222. newAURPackages := make(stringSet)
  223. err := dp.cacheAURPackages(pkgs)
  224. if err != nil {
  225. return err
  226. }
  227. if len(pkgs) == 0 {
  228. return nil
  229. }
  230. for name := range pkgs {
  231. _, ok := dp.Aur[name]
  232. if ok {
  233. continue
  234. }
  235. pkg := dp.findSatisfierAurCache(name)
  236. if pkg == nil {
  237. continue
  238. }
  239. if explicit {
  240. dp.Explicit.set(pkg.Name)
  241. }
  242. dp.Aur[pkg.Name] = pkg
  243. for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
  244. for _, dep := range deps {
  245. newPackages.set(dep)
  246. }
  247. }
  248. }
  249. for dep := range newPackages {
  250. if dp.hasSatisfier(dep) {
  251. continue
  252. }
  253. //has satisfier installed: skip
  254. _, isInstalled := dp.LocalDb.PkgCache().FindSatisfier(dep)
  255. if isInstalled == nil {
  256. continue
  257. }
  258. //has satisfier in repo: fetch it
  259. repoPkg, inRepos := dp.SyncDb.FindSatisfier(dep)
  260. if inRepos == nil {
  261. dp.ResolveRepoDependency(repoPkg)
  262. continue
  263. }
  264. //assume it's in the aur
  265. //ditch the versioning because the RPC cant handle it
  266. newAURPackages.set(dep)
  267. }
  268. err = dp.resolveAURPackages(newAURPackages, false)
  269. return err
  270. }
  271. func (dp *depPool) ResolveRepoDependency(pkg *alpm.Package) {
  272. dp.Repo[pkg.Name()] = pkg
  273. pkg.Depends().ForEach(func(dep alpm.Depend) (err error) {
  274. //have satisfier in dep tree: skip
  275. if dp.hasSatisfier(dep.String()) {
  276. return
  277. }
  278. //has satisfier installed: skip
  279. _, isInstalled := dp.LocalDb.PkgCache().FindSatisfier(dep.String())
  280. if isInstalled == nil {
  281. return
  282. }
  283. //has satisfier in repo: fetch it
  284. repoPkg, inRepos := dp.SyncDb.FindSatisfier(dep.String())
  285. if inRepos != nil {
  286. return
  287. }
  288. dp.ResolveRepoDependency(repoPkg)
  289. return nil
  290. })
  291. }
  292. func getDepPool(pkgs []string, warnings *aurWarnings) (*depPool, error) {
  293. dp, err := makeDepPool()
  294. if err != nil {
  295. return nil, err
  296. }
  297. dp.Warnings = warnings
  298. err = dp.ResolveTargets(pkgs)
  299. return dp, err
  300. }
  301. func (dp *depPool) findSatisfierAur(dep string) *rpc.Pkg {
  302. for _, pkg := range dp.Aur {
  303. if satisfiesAur(dep, pkg) {
  304. return pkg
  305. }
  306. }
  307. return nil
  308. }
  309. // This is mostly used to promote packages from the cache
  310. // to the Install list
  311. // Provide a pacman style provider menu if theres more than one candidate
  312. // TODO: maybe intermix repo providers in the menu
  313. func (dp *depPool) findSatisfierAurCache(dep string) *rpc.Pkg {
  314. depName, _, _ := splitDep(dep)
  315. seen := make(stringSet)
  316. providers := makeProviders(depName)
  317. if _, err := dp.LocalDb.PkgByName(depName); err == nil {
  318. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  319. return pkg
  320. }
  321. }
  322. //this version prioratizes name over provides
  323. //if theres a direct match for a package return
  324. //that instead of using the menu
  325. //
  326. //providers := make(rpcPkgs, 0)
  327. //for _, pkg := range dp.AurCache {
  328. // if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  329. // return pkg
  330. // }
  331. //}
  332. //for _, pkg := range dp.AurCache {
  333. // for _, provide := range pkg.Provides {
  334. // if provideSatisfies(provide, dep) {
  335. // providers = append(providers, pkg)
  336. // }
  337. // }
  338. //}
  339. // This version acts slightly differenly from Pacman, It will give
  340. // a menu even if a package with a matching name exists. I believe this
  341. // method is better because most of the time you are choosing between
  342. // foo and foo-git.
  343. // Using Pacman's ways trying to install foo would never give you
  344. // a menu.
  345. for _, pkg := range dp.AurCache {
  346. if seen.get(pkg.Name) {
  347. continue
  348. }
  349. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  350. providers.Pkgs = append(providers.Pkgs, pkg)
  351. seen.set(pkg.Name)
  352. continue
  353. }
  354. for _, provide := range pkg.Provides {
  355. if provideSatisfies(provide, dep) {
  356. providers.Pkgs = append(providers.Pkgs, pkg)
  357. seen.set(pkg.Name)
  358. continue
  359. }
  360. }
  361. }
  362. if providers.Len() == 1 {
  363. return providers.Pkgs[0]
  364. }
  365. if providers.Len() > 1 {
  366. sort.Sort(providers)
  367. return providerMenu(dep, providers)
  368. }
  369. return nil
  370. }
  371. func (dp *depPool) findSatisfierRepo(dep string) *alpm.Package {
  372. for _, pkg := range dp.Repo {
  373. if satisfiesRepo(dep, pkg) {
  374. return pkg
  375. }
  376. }
  377. return nil
  378. }
  379. func (dp *depPool) hasSatisfier(dep string) bool {
  380. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  381. }
  382. func (dp *depPool) hasPackage(name string) bool {
  383. for _, pkg := range dp.Repo {
  384. if pkg.Name() == name {
  385. return true
  386. }
  387. }
  388. for _, pkg := range dp.Aur {
  389. if pkg.Name == name {
  390. return true
  391. }
  392. }
  393. for _, pkg := range dp.Groups {
  394. if pkg == name {
  395. return true
  396. }
  397. }
  398. return false
  399. }