depPool.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. package dep
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "github.com/Jguer/aur"
  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 aur.ClientInterface
  30. }
  31. func newPool(dbExecutor db.Executor, aurClient aur.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. // Hack for a bigger search result, if the user wants
  136. // java-envronment we can search for just java instead and get
  137. // more hits.
  138. pkg, _, _ = splitDep(pkg) // openimagedenoise-git > ispc-git #1234
  139. words := strings.Split(pkg, "-")
  140. for i := range words {
  141. results, err = dp.aurClient.Search(ctx, strings.Join(words[:i+1], "-"), aur.None)
  142. if err == nil {
  143. break
  144. }
  145. }
  146. if err != nil {
  147. return
  148. }
  149. for iR := range results {
  150. mux.Lock()
  151. if _, ok := dp.AurCache[results[iR].Name]; !ok {
  152. pkgs.Set(results[iR].Name)
  153. }
  154. mux.Unlock()
  155. }
  156. }
  157. for pkg := range pkgs {
  158. if dp.AlpmExecutor.LocalPackage(pkg) != nil {
  159. continue
  160. }
  161. wg.Add(1)
  162. go doSearch(pkg)
  163. }
  164. wg.Wait()
  165. return nil
  166. }
  167. func (dp *Pool) cacheAURPackages(ctx context.Context, _pkgs stringset.StringSet, provides bool, splitN int) error {
  168. pkgs := _pkgs.Copy()
  169. toQuery := make([]string, 0)
  170. for pkg := range pkgs {
  171. if _, ok := dp.AurCache[pkg]; ok {
  172. pkgs.Remove(pkg)
  173. }
  174. }
  175. if len(pkgs) == 0 {
  176. return nil
  177. }
  178. if provides {
  179. err := dp.findProvides(ctx, pkgs)
  180. if err != nil {
  181. return err
  182. }
  183. }
  184. for pkg := range pkgs {
  185. if _, ok := dp.AurCache[pkg]; !ok {
  186. name, _, ver := splitDep(pkg)
  187. if ver != "" {
  188. toQuery = append(toQuery, name, name+"-"+ver)
  189. } else {
  190. toQuery = append(toQuery, name)
  191. }
  192. }
  193. }
  194. info, err := query.AURInfo(ctx, dp.aurClient, toQuery, dp.Warnings, splitN)
  195. if err != nil {
  196. return err
  197. }
  198. for _, pkg := range info {
  199. // Dump everything in cache just in case we need it later
  200. dp.AurCache[pkg.Name] = pkg
  201. }
  202. return nil
  203. }
  204. // Compute dependency lists used in Package dep searching and ordering.
  205. // Order sensitive TOFIX.
  206. func ComputeCombinedDepList(pkg *aur.Pkg, noDeps, noCheckDeps bool) []string {
  207. combinedDepList := make([]string, 0, len(pkg.Depends)+len(pkg.MakeDepends)+len(pkg.CheckDepends))
  208. if !noDeps {
  209. combinedDepList = append(combinedDepList, pkg.Depends...)
  210. }
  211. combinedDepList = append(combinedDepList, pkg.MakeDepends...)
  212. if !noCheckDeps {
  213. combinedDepList = append(combinedDepList, pkg.CheckDepends...)
  214. }
  215. return combinedDepList
  216. }
  217. func (dp *Pool) resolveAURPackages(ctx context.Context,
  218. pkgs stringset.StringSet,
  219. explicit, ignoreProviders, noConfirm, provides bool,
  220. rebuild string, splitN int, noDeps, noCheckDeps bool,
  221. ) error {
  222. newPackages := make(stringset.StringSet)
  223. newAURPackages := make(stringset.StringSet)
  224. err := dp.cacheAURPackages(ctx, pkgs, provides, splitN)
  225. if err != nil {
  226. return err
  227. }
  228. if len(pkgs) == 0 {
  229. return nil
  230. }
  231. for name := range pkgs {
  232. _, ok := dp.Aur[name]
  233. if ok {
  234. continue
  235. }
  236. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  237. if pkg == nil {
  238. continue
  239. }
  240. if explicit {
  241. dp.Explicit.Set(pkg.Name)
  242. }
  243. dp.Aur[pkg.Name] = pkg
  244. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  245. for _, dep := range combinedDepList {
  246. newPackages.Set(dep)
  247. }
  248. }
  249. for dep := range newPackages {
  250. if dp.hasSatisfier(dep) {
  251. continue
  252. }
  253. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  254. hm := settings.HideMenus
  255. settings.HideMenus = isInstalled
  256. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  257. settings.HideMenus = hm
  258. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  259. continue
  260. }
  261. if repoPkg != nil {
  262. dp.ResolveRepoDependency(repoPkg, false)
  263. continue
  264. }
  265. // assume it's in the aur
  266. // ditch the versioning because the RPC can't handle it
  267. newAURPackages.Set(dep)
  268. }
  269. err = dp.resolveAURPackages(ctx, newAURPackages, false, ignoreProviders,
  270. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  271. return err
  272. }
  273. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  274. dp.Repo[pkg.Name()] = pkg
  275. if noDeps {
  276. return
  277. }
  278. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  279. if dp.hasSatisfier(dep.String()) {
  280. continue
  281. }
  282. // has satisfier installed: skip
  283. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  284. continue
  285. }
  286. // has satisfier in repo: fetch it
  287. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  288. dp.ResolveRepoDependency(repoPkg, noDeps)
  289. }
  290. }
  291. }
  292. func GetPool(ctx context.Context, pkgs []string,
  293. warnings *query.AURWarnings,
  294. dbExecutor db.Executor,
  295. aurClient aur.ClientInterface,
  296. mode parser.TargetMode,
  297. ignoreProviders, noConfirm, provides bool,
  298. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string,
  299. ) (*Pool, error) {
  300. dp := newPool(dbExecutor, aurClient)
  301. dp.Warnings = warnings
  302. err := dp.ResolveTargets(ctx, pkgs, mode, ignoreProviders, noConfirm, provides,
  303. rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  304. return dp, err
  305. }
  306. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  307. for _, pkg := range dp.Aur {
  308. if satisfiesAur(dep, pkg) {
  309. return pkg
  310. }
  311. }
  312. return nil
  313. }
  314. // This is mostly used to promote packages from the cache
  315. // to the Install list
  316. // Provide a pacman style provider menu if there's more than one candidate
  317. // This acts slightly differently from Pacman, It will give
  318. // a menu even if a package with a matching name exists. I believe this
  319. // method is better because most of the time you are choosing between
  320. // foo and foo-git.
  321. // Using Pacman's ways trying to install foo would never give you
  322. // a menu.
  323. // TODO: maybe intermix repo providers in the menu.
  324. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  325. depName, _, _ := splitDep(dep)
  326. seen := make(stringset.StringSet)
  327. providerSlice := makeProviders(depName)
  328. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  329. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  330. return pkg
  331. }
  332. }
  333. if ignoreProviders {
  334. for _, pkg := range dp.AurCache {
  335. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  336. for _, target := range dp.Targets {
  337. if target.Name == pkg.Name {
  338. return pkg
  339. }
  340. }
  341. }
  342. }
  343. }
  344. for _, pkg := range dp.AurCache {
  345. if seen.Get(pkg.Name) {
  346. continue
  347. }
  348. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  349. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  350. seen.Set(pkg.Name)
  351. continue
  352. }
  353. for _, provide := range pkg.Provides {
  354. if provideSatisfies(provide, dep, pkg.Version) {
  355. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  356. seen.Set(pkg.Name)
  357. continue
  358. }
  359. }
  360. }
  361. if !provides && providerSlice.Len() >= 1 {
  362. return providerSlice.Pkgs[0]
  363. }
  364. if providerSlice.Len() == 1 {
  365. return providerSlice.Pkgs[0]
  366. }
  367. if providerSlice.Len() > 1 {
  368. sort.Sort(providerSlice)
  369. return providerMenu(dep, providerSlice, noConfirm)
  370. }
  371. return nil
  372. }
  373. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  374. for _, pkg := range dp.Repo {
  375. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  376. return pkg
  377. }
  378. }
  379. return nil
  380. }
  381. func (dp *Pool) hasSatisfier(dep string) bool {
  382. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  383. }
  384. func (dp *Pool) hasPackage(name string) bool {
  385. for _, pkg := range dp.Repo {
  386. if pkg.Name() == name {
  387. return true
  388. }
  389. }
  390. for _, pkg := range dp.Aur {
  391. if pkg.Name == name {
  392. return true
  393. }
  394. }
  395. for _, pkg := range dp.Groups {
  396. if pkg == name {
  397. return true
  398. }
  399. }
  400. return false
  401. }
  402. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  403. for _, pkgAndVersion := range assumeInstalled {
  404. assumeName, _, _ := splitDep(pkgAndVersion)
  405. depName, _, _ := splitDep(name)
  406. if assumeName == depName {
  407. return true
  408. }
  409. }
  410. return false
  411. }
  412. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  413. size := providers.Len()
  414. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  415. str += "\n"
  416. size = 1
  417. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  418. for _, pkg := range providers.Pkgs {
  419. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  420. size++
  421. }
  422. text.OperationInfoln(str)
  423. for {
  424. fmt.Println(gotext.Get("\nEnter a number (default=1): "))
  425. if noConfirm {
  426. fmt.Println("1")
  427. return providers.Pkgs[0]
  428. }
  429. numberBuf, err := text.GetInput("", false)
  430. if err != nil {
  431. fmt.Fprintln(os.Stderr, err)
  432. break
  433. }
  434. if numberBuf == "" {
  435. return providers.Pkgs[0]
  436. }
  437. num, err := strconv.Atoi(numberBuf)
  438. if err != nil {
  439. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  440. continue
  441. }
  442. if num < 1 || num >= size {
  443. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  444. continue
  445. }
  446. return providers.Pkgs[num-1]
  447. }
  448. return nil
  449. }