depPool.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. package dep
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "strconv"
  8. "sync"
  9. "github.com/Jguer/aur"
  10. "github.com/Jguer/aur/rpc"
  11. alpm "github.com/Jguer/go-alpm/v2"
  12. "github.com/leonelquinteros/gotext"
  13. "github.com/Jguer/yay/v11/pkg/db"
  14. "github.com/Jguer/yay/v11/pkg/query"
  15. "github.com/Jguer/yay/v11/pkg/settings"
  16. "github.com/Jguer/yay/v11/pkg/settings/parser"
  17. "github.com/Jguer/yay/v11/pkg/stringset"
  18. "github.com/Jguer/yay/v11/pkg/text"
  19. )
  20. type Pool struct {
  21. Targets []Target
  22. Explicit stringset.StringSet
  23. Repo map[string]db.IPackage
  24. Aur map[string]*query.Pkg
  25. AurCache map[string]*query.Pkg
  26. Groups []string
  27. AlpmExecutor db.Executor
  28. Warnings *query.AURWarnings
  29. aurClient rpc.ClientInterface
  30. }
  31. func newPool(dbExecutor db.Executor, aurClient rpc.ClientInterface) *Pool {
  32. dp := &Pool{
  33. Targets: []Target{},
  34. Explicit: map[string]struct{}{},
  35. Repo: map[string]alpm.IPackage{},
  36. Aur: map[string]*aur.Pkg{},
  37. AurCache: map[string]*aur.Pkg{},
  38. Groups: []string{},
  39. AlpmExecutor: dbExecutor,
  40. Warnings: nil,
  41. aurClient: aurClient,
  42. }
  43. return dp
  44. }
  45. // Includes db/ prefixes and group installs.
  46. func (dp *Pool) ResolveTargets(ctx context.Context, pkgs []string,
  47. mode parser.TargetMode,
  48. ignoreProviders, noConfirm, provides bool, rebuild string, splitN int, noDeps, noCheckDeps bool, assumeInstalled []string,
  49. ) error {
  50. // RPC requests are slow
  51. // Combine as many AUR package requests as possible into a single RPC call
  52. aurTargets := make(stringset.StringSet)
  53. pkgs = query.RemoveInvalidTargets(pkgs, mode)
  54. for _, pkg := range pkgs {
  55. target := ToTarget(pkg)
  56. // skip targets already satisfied
  57. // even if the user enters db/pkg and aur/pkg the latter will
  58. // still get skipped even if it's from a different database to
  59. // the one specified
  60. // this is how pacman behaves
  61. if dp.hasPackage(target.DepString()) || isInAssumeInstalled(target.DepString(), assumeInstalled) {
  62. continue
  63. }
  64. var foundPkg db.IPackage
  65. // aur/ prefix means we only check the aur
  66. if target.DB == "aur" || mode == parser.ModeAUR {
  67. dp.Targets = append(dp.Targets, target)
  68. aurTargets.Set(target.DepString())
  69. continue
  70. }
  71. // If there's a different prefix only look in that repo
  72. if target.DB != "" {
  73. foundPkg = dp.AlpmExecutor.SatisfierFromDB(target.DepString(), target.DB)
  74. } else {
  75. // otherwise find it in any repo
  76. foundPkg = dp.AlpmExecutor.SyncSatisfier(target.DepString())
  77. }
  78. if foundPkg != nil {
  79. dp.Targets = append(dp.Targets, target)
  80. dp.Explicit.Set(foundPkg.Name())
  81. dp.ResolveRepoDependency(foundPkg, noDeps)
  82. continue
  83. } else {
  84. // check for groups
  85. // currently we don't resolve the packages in a group
  86. // only check if the group exists
  87. // would be better to check the groups from singleDB if
  88. // the user specified a db but there's no easy way to do
  89. // it without making alpm_lists so don't bother for now
  90. // db/group is probably a rare use case
  91. groupPackages := dp.AlpmExecutor.PackagesFromGroup(target.Name)
  92. if len(groupPackages) > 0 {
  93. dp.Groups = append(dp.Groups, target.String())
  94. for _, pkg := range groupPackages {
  95. dp.Explicit.Set(pkg.Name())
  96. }
  97. continue
  98. }
  99. }
  100. // if there was no db prefix check the aur
  101. if target.DB == "" {
  102. aurTargets.Set(target.DepString())
  103. }
  104. dp.Targets = append(dp.Targets, target)
  105. }
  106. if len(aurTargets) > 0 && mode.AtLeastAUR() {
  107. return dp.resolveAURPackages(ctx, aurTargets, true, ignoreProviders,
  108. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  109. }
  110. return nil
  111. }
  112. // Pseudo provides finder.
  113. // Try to find provides by performing a search of the package name
  114. // This effectively performs -Ss on each package
  115. // then runs -Si on each result to cache the information.
  116. //
  117. // For example if you were to -S yay then yay -Ss would give:
  118. // yay-git yay-bin yay realyog pacui pacui-git ruby-yard
  119. // These packages will all be added to the cache in case they are needed later
  120. // Ofcouse only the first three packages provide yay, the rest are just false
  121. // positives.
  122. //
  123. // This method increases dependency resolve time.
  124. func (dp *Pool) findProvides(ctx context.Context, pkgs stringset.StringSet) error {
  125. var (
  126. mux sync.Mutex
  127. wg sync.WaitGroup
  128. )
  129. doSearch := func(pkg string) {
  130. defer wg.Done()
  131. var (
  132. err error
  133. results []query.Pkg
  134. )
  135. results, err = dp.aurClient.Search(ctx, pkg, aur.Provides)
  136. if err != nil {
  137. return
  138. }
  139. for iR := range results {
  140. mux.Lock()
  141. if _, ok := dp.AurCache[results[iR].Name]; !ok {
  142. pkgs.Set(results[iR].Name)
  143. }
  144. mux.Unlock()
  145. }
  146. }
  147. for pkg := range pkgs {
  148. if dp.AlpmExecutor.LocalPackage(pkg) != nil {
  149. continue
  150. }
  151. wg.Add(1)
  152. text.Debugln("AUR RPC Search:", pkg)
  153. go doSearch(pkg)
  154. }
  155. wg.Wait()
  156. return nil
  157. }
  158. func (dp *Pool) cacheAURPackages(ctx context.Context, _pkgs stringset.StringSet, provides bool, splitN int) error {
  159. pkgs := _pkgs.Copy()
  160. toQuery := make([]string, 0)
  161. for pkg := range pkgs {
  162. if _, ok := dp.AurCache[pkg]; ok {
  163. pkgs.Remove(pkg)
  164. }
  165. }
  166. if len(pkgs) == 0 {
  167. return nil
  168. }
  169. if provides {
  170. err := dp.findProvides(ctx, pkgs)
  171. if err != nil {
  172. return err
  173. }
  174. }
  175. for pkg := range pkgs {
  176. if _, ok := dp.AurCache[pkg]; !ok {
  177. name, _, ver := splitDep(pkg)
  178. if ver != "" {
  179. toQuery = append(toQuery, name, name+"-"+ver)
  180. } else {
  181. toQuery = append(toQuery, name)
  182. }
  183. }
  184. }
  185. info, err := query.AURInfo(ctx, dp.aurClient, toQuery, dp.Warnings, splitN)
  186. if err != nil {
  187. return err
  188. }
  189. for i := range info {
  190. // Dump everything in cache just in case we need it later
  191. pkg := &info[i]
  192. dp.AurCache[pkg.Name] = pkg
  193. }
  194. return nil
  195. }
  196. // Compute dependency lists used in Package dep searching and ordering.
  197. // Order sensitive TOFIX.
  198. func ComputeCombinedDepList(pkg *aur.Pkg, noDeps, noCheckDeps bool) []string {
  199. combinedDepList := make([]string, 0, len(pkg.Depends)+len(pkg.MakeDepends)+len(pkg.CheckDepends))
  200. if !noDeps {
  201. combinedDepList = append(combinedDepList, pkg.Depends...)
  202. }
  203. combinedDepList = append(combinedDepList, pkg.MakeDepends...)
  204. if !noCheckDeps {
  205. combinedDepList = append(combinedDepList, pkg.CheckDepends...)
  206. }
  207. return combinedDepList
  208. }
  209. func (dp *Pool) resolveAURPackages(ctx context.Context,
  210. pkgs stringset.StringSet,
  211. explicit, ignoreProviders, noConfirm, provides bool,
  212. rebuild string, splitN int, noDeps, noCheckDeps bool,
  213. ) error {
  214. newPackages := make(stringset.StringSet)
  215. newAURPackages := make(stringset.StringSet)
  216. err := dp.cacheAURPackages(ctx, pkgs, provides, splitN)
  217. if err != nil {
  218. return err
  219. }
  220. if len(pkgs) == 0 {
  221. return nil
  222. }
  223. for name := range pkgs {
  224. _, ok := dp.Aur[name]
  225. if ok {
  226. continue
  227. }
  228. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  229. if pkg == nil {
  230. continue
  231. }
  232. if explicit {
  233. dp.Explicit.Set(pkg.Name)
  234. }
  235. dp.Aur[pkg.Name] = pkg
  236. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  237. for _, dep := range combinedDepList {
  238. newPackages.Set(dep)
  239. }
  240. }
  241. for dep := range newPackages {
  242. if dp.hasSatisfier(dep) {
  243. continue
  244. }
  245. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  246. hm := settings.HideMenus
  247. settings.HideMenus = isInstalled
  248. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  249. settings.HideMenus = hm
  250. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  251. continue
  252. }
  253. if repoPkg != nil {
  254. dp.ResolveRepoDependency(repoPkg, false)
  255. continue
  256. }
  257. // assume it's in the aur
  258. // ditch the versioning because the RPC can't handle it
  259. newAURPackages.Set(dep)
  260. }
  261. err = dp.resolveAURPackages(ctx, newAURPackages, false, ignoreProviders,
  262. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  263. return err
  264. }
  265. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  266. dp.Repo[pkg.Name()] = pkg
  267. if noDeps {
  268. return
  269. }
  270. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  271. if dp.hasSatisfier(dep.String()) {
  272. continue
  273. }
  274. // has satisfier installed: skip
  275. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  276. continue
  277. }
  278. // has satisfier in repo: fetch it
  279. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  280. dp.ResolveRepoDependency(repoPkg, noDeps)
  281. }
  282. }
  283. }
  284. func GetPool(ctx context.Context, pkgs []string,
  285. warnings *query.AURWarnings,
  286. dbExecutor db.Executor,
  287. aurClient rpc.ClientInterface,
  288. mode parser.TargetMode,
  289. ignoreProviders, noConfirm, provides bool,
  290. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string,
  291. ) (*Pool, error) {
  292. dp := newPool(dbExecutor, aurClient)
  293. dp.Warnings = warnings
  294. err := dp.ResolveTargets(ctx, pkgs, mode, ignoreProviders, noConfirm, provides,
  295. rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  296. return dp, err
  297. }
  298. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  299. for _, pkg := range dp.Aur {
  300. if satisfiesAur(dep, pkg) {
  301. return pkg
  302. }
  303. }
  304. return nil
  305. }
  306. // This is mostly used to promote packages from the cache
  307. // to the Install list
  308. // Provide a pacman style provider menu if there's more than one candidate
  309. // This acts slightly differently from Pacman, It will give
  310. // a menu even if a package with a matching name exists. I believe this
  311. // method is better because most of the time you are choosing between
  312. // foo and foo-git.
  313. // Using Pacman's ways trying to install foo would never give you
  314. // a menu.
  315. // TODO: maybe intermix repo providers in the menu.
  316. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  317. depName, _, _ := splitDep(dep)
  318. seen := make(stringset.StringSet)
  319. providerSlice := makeProviders(depName)
  320. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  321. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  322. return pkg
  323. }
  324. }
  325. if ignoreProviders {
  326. for _, pkg := range dp.AurCache {
  327. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  328. for _, target := range dp.Targets {
  329. if target.Name == pkg.Name {
  330. return pkg
  331. }
  332. }
  333. }
  334. }
  335. }
  336. for _, pkg := range dp.AurCache {
  337. if seen.Get(pkg.Name) {
  338. continue
  339. }
  340. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  341. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  342. seen.Set(pkg.Name)
  343. continue
  344. }
  345. for _, provide := range pkg.Provides {
  346. if provideSatisfies(provide, dep, pkg.Version) {
  347. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  348. seen.Set(pkg.Name)
  349. continue
  350. }
  351. }
  352. }
  353. if !provides && providerSlice.Len() >= 1 {
  354. return providerSlice.Pkgs[0]
  355. }
  356. if providerSlice.Len() == 1 {
  357. return providerSlice.Pkgs[0]
  358. }
  359. if providerSlice.Len() > 1 {
  360. sort.Sort(providerSlice)
  361. return providerMenu(dep, providerSlice, noConfirm)
  362. }
  363. return nil
  364. }
  365. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  366. for _, pkg := range dp.Repo {
  367. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  368. return pkg
  369. }
  370. }
  371. return nil
  372. }
  373. func (dp *Pool) hasSatisfier(dep string) bool {
  374. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  375. }
  376. func (dp *Pool) hasPackage(name string) bool {
  377. for _, pkg := range dp.Repo {
  378. if pkg.Name() == name {
  379. return true
  380. }
  381. }
  382. for _, pkg := range dp.Aur {
  383. if pkg.Name == name {
  384. return true
  385. }
  386. }
  387. for _, pkg := range dp.Groups {
  388. if pkg == name {
  389. return true
  390. }
  391. }
  392. return false
  393. }
  394. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  395. for _, pkgAndVersion := range assumeInstalled {
  396. assumeName, _, _ := splitDep(pkgAndVersion)
  397. depName, _, _ := splitDep(name)
  398. if assumeName == depName {
  399. return true
  400. }
  401. }
  402. return false
  403. }
  404. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  405. size := providers.Len()
  406. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  407. str += "\n"
  408. size = 1
  409. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  410. for _, pkg := range providers.Pkgs {
  411. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  412. size++
  413. }
  414. text.OperationInfoln(str)
  415. for {
  416. fmt.Println(gotext.Get("\nEnter a number (default=1): "))
  417. if noConfirm {
  418. fmt.Println("1")
  419. return providers.Pkgs[0]
  420. }
  421. numberBuf, err := text.GetInput(os.Stdin, "", false)
  422. if err != nil {
  423. fmt.Fprintln(os.Stderr, err)
  424. break
  425. }
  426. if numberBuf == "" {
  427. return providers.Pkgs[0]
  428. }
  429. num, err := strconv.Atoi(numberBuf)
  430. if err != nil {
  431. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  432. continue
  433. }
  434. if num < 1 || num >= size {
  435. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  436. continue
  437. }
  438. return providers.Pkgs[num-1]
  439. }
  440. return nil
  441. }