depPool.go 13 KB

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