depPool.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 Target struct {
  21. DB string
  22. Name string
  23. Mod string
  24. Version string
  25. }
  26. func ToTarget(pkg string) Target {
  27. dbName, depString := text.SplitDBFromName(pkg)
  28. name, mod, depVersion := splitDep(depString)
  29. return Target{
  30. DB: dbName,
  31. Name: name,
  32. Mod: mod,
  33. Version: depVersion,
  34. }
  35. }
  36. func (t Target) DepString() string {
  37. return t.Name + t.Mod + t.Version
  38. }
  39. func (t Target) String() string {
  40. if t.DB != "" {
  41. return t.DB + "/" + t.DepString()
  42. }
  43. return t.DepString()
  44. }
  45. type Pool struct {
  46. Targets []Target
  47. Explicit stringset.StringSet
  48. Repo map[string]db.IPackage
  49. Aur map[string]*query.Pkg
  50. AurCache map[string]*query.Pkg
  51. Groups []string
  52. AlpmExecutor db.Executor
  53. Warnings *query.AURWarnings
  54. aurClient *aur.Client
  55. }
  56. func newPool(dbExecutor db.Executor, aurClient *aur.Client) *Pool {
  57. dp := &Pool{
  58. Targets: []Target{},
  59. Explicit: map[string]struct{}{},
  60. Repo: map[string]alpm.IPackage{},
  61. Aur: map[string]*aur.Pkg{},
  62. AurCache: map[string]*aur.Pkg{},
  63. Groups: []string{},
  64. AlpmExecutor: dbExecutor,
  65. Warnings: nil,
  66. aurClient: aurClient,
  67. }
  68. return dp
  69. }
  70. // Includes db/ prefixes and group installs.
  71. func (dp *Pool) ResolveTargets(ctx context.Context, pkgs []string,
  72. mode parser.TargetMode,
  73. ignoreProviders, noConfirm, provides bool, rebuild string, splitN int, noDeps, noCheckDeps bool, assumeInstalled []string,
  74. ) 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,
  246. ) error {
  247. newPackages := make(stringset.StringSet)
  248. newAURPackages := make(stringset.StringSet)
  249. err := dp.cacheAURPackages(ctx, pkgs, provides, splitN)
  250. if err != nil {
  251. return err
  252. }
  253. if len(pkgs) == 0 {
  254. return nil
  255. }
  256. for name := range pkgs {
  257. _, ok := dp.Aur[name]
  258. if ok {
  259. continue
  260. }
  261. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  262. if pkg == nil {
  263. continue
  264. }
  265. if explicit {
  266. dp.Explicit.Set(pkg.Name)
  267. }
  268. dp.Aur[pkg.Name] = pkg
  269. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  270. for _, deps := range combinedDepList {
  271. for _, dep := range deps {
  272. newPackages.Set(dep)
  273. }
  274. }
  275. }
  276. for dep := range newPackages {
  277. if dp.hasSatisfier(dep) {
  278. continue
  279. }
  280. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  281. hm := settings.HideMenus
  282. settings.HideMenus = isInstalled
  283. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  284. settings.HideMenus = hm
  285. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  286. continue
  287. }
  288. if repoPkg != nil {
  289. dp.ResolveRepoDependency(repoPkg, false)
  290. continue
  291. }
  292. // assume it's in the aur
  293. // ditch the versioning because the RPC can't handle it
  294. newAURPackages.Set(dep)
  295. }
  296. err = dp.resolveAURPackages(ctx, newAURPackages, false, ignoreProviders,
  297. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  298. return err
  299. }
  300. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  301. dp.Repo[pkg.Name()] = pkg
  302. if noDeps {
  303. return
  304. }
  305. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  306. if dp.hasSatisfier(dep.String()) {
  307. continue
  308. }
  309. // has satisfier installed: skip
  310. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  311. continue
  312. }
  313. // has satisfier in repo: fetch it
  314. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  315. dp.ResolveRepoDependency(repoPkg, noDeps)
  316. }
  317. }
  318. }
  319. func GetPool(ctx context.Context, pkgs []string,
  320. warnings *query.AURWarnings,
  321. dbExecutor db.Executor,
  322. aurClient *aur.Client,
  323. mode parser.TargetMode,
  324. ignoreProviders, noConfirm, provides bool,
  325. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string,
  326. ) (*Pool, error) {
  327. dp := newPool(dbExecutor, aurClient)
  328. dp.Warnings = warnings
  329. err := dp.ResolveTargets(ctx, pkgs, mode, ignoreProviders, noConfirm, provides,
  330. rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  331. return dp, err
  332. }
  333. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  334. for _, pkg := range dp.Aur {
  335. if satisfiesAur(dep, pkg) {
  336. return pkg
  337. }
  338. }
  339. return nil
  340. }
  341. // This is mostly used to promote packages from the cache
  342. // to the Install list
  343. // Provide a pacman style provider menu if there's more than one candidate
  344. // This acts slightly differently from Pacman, It will give
  345. // a menu even if a package with a matching name exists. I believe this
  346. // method is better because most of the time you are choosing between
  347. // foo and foo-git.
  348. // Using Pacman's ways trying to install foo would never give you
  349. // a menu.
  350. // TODO: maybe intermix repo providers in the menu.
  351. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  352. depName, _, _ := splitDep(dep)
  353. seen := make(stringset.StringSet)
  354. providerSlice := makeProviders(depName)
  355. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  356. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  357. return pkg
  358. }
  359. }
  360. if ignoreProviders {
  361. for _, pkg := range dp.AurCache {
  362. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  363. for _, target := range dp.Targets {
  364. if target.Name == pkg.Name {
  365. return pkg
  366. }
  367. }
  368. }
  369. }
  370. }
  371. for _, pkg := range dp.AurCache {
  372. if seen.Get(pkg.Name) {
  373. continue
  374. }
  375. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  376. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  377. seen.Set(pkg.Name)
  378. continue
  379. }
  380. for _, provide := range pkg.Provides {
  381. if provideSatisfies(provide, dep, pkg.Version) {
  382. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  383. seen.Set(pkg.Name)
  384. continue
  385. }
  386. }
  387. }
  388. if !provides && providerSlice.Len() >= 1 {
  389. return providerSlice.Pkgs[0]
  390. }
  391. if providerSlice.Len() == 1 {
  392. return providerSlice.Pkgs[0]
  393. }
  394. if providerSlice.Len() > 1 {
  395. sort.Sort(providerSlice)
  396. return providerMenu(dep, providerSlice, noConfirm)
  397. }
  398. return nil
  399. }
  400. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  401. for _, pkg := range dp.Repo {
  402. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  403. return pkg
  404. }
  405. }
  406. return nil
  407. }
  408. func (dp *Pool) hasSatisfier(dep string) bool {
  409. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  410. }
  411. func (dp *Pool) hasPackage(name string) bool {
  412. for _, pkg := range dp.Repo {
  413. if pkg.Name() == name {
  414. return true
  415. }
  416. }
  417. for _, pkg := range dp.Aur {
  418. if pkg.Name == name {
  419. return true
  420. }
  421. }
  422. for _, pkg := range dp.Groups {
  423. if pkg == name {
  424. return true
  425. }
  426. }
  427. return false
  428. }
  429. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  430. for _, pkgAndVersion := range assumeInstalled {
  431. assumeName, _, _ := splitDep(pkgAndVersion)
  432. depName, _, _ := splitDep(name)
  433. if assumeName == depName {
  434. return true
  435. }
  436. }
  437. return false
  438. }
  439. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  440. size := providers.Len()
  441. str := text.Bold(gotext.Get("There are %d providers available for %s:", size, dep))
  442. str += "\n"
  443. size = 1
  444. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  445. for _, pkg := range providers.Pkgs {
  446. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  447. size++
  448. }
  449. text.OperationInfoln(str)
  450. for {
  451. fmt.Println(gotext.Get("\nEnter a number (default=1): "))
  452. if noConfirm {
  453. fmt.Println("1")
  454. return providers.Pkgs[0]
  455. }
  456. numberBuf, err := text.GetInput("", false)
  457. if err != nil {
  458. fmt.Fprintln(os.Stderr, err)
  459. break
  460. }
  461. if numberBuf == "" {
  462. return providers.Pkgs[0]
  463. }
  464. num, err := strconv.Atoi(numberBuf)
  465. if err != nil {
  466. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  467. continue
  468. }
  469. if num < 1 || num >= size {
  470. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  471. continue
  472. }
  473. return providers.Pkgs[num-1]
  474. }
  475. return nil
  476. }