depPool.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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, len(pkg.Depends)+len(pkg.MakeDepends)+len(pkg.CheckDepends))
  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 _, dep := range combinedDepList {
  271. newPackages.Set(dep)
  272. }
  273. }
  274. for dep := range newPackages {
  275. if dp.hasSatisfier(dep) {
  276. continue
  277. }
  278. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  279. hm := settings.HideMenus
  280. settings.HideMenus = isInstalled
  281. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  282. settings.HideMenus = hm
  283. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  284. continue
  285. }
  286. if repoPkg != nil {
  287. dp.ResolveRepoDependency(repoPkg, false)
  288. continue
  289. }
  290. // assume it's in the aur
  291. // ditch the versioning because the RPC can't handle it
  292. newAURPackages.Set(dep)
  293. }
  294. err = dp.resolveAURPackages(ctx, newAURPackages, false, ignoreProviders,
  295. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  296. return err
  297. }
  298. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  299. dp.Repo[pkg.Name()] = pkg
  300. if noDeps {
  301. return
  302. }
  303. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  304. if dp.hasSatisfier(dep.String()) {
  305. continue
  306. }
  307. // has satisfier installed: skip
  308. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  309. continue
  310. }
  311. // has satisfier in repo: fetch it
  312. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  313. dp.ResolveRepoDependency(repoPkg, noDeps)
  314. }
  315. }
  316. }
  317. func GetPool(ctx context.Context, pkgs []string,
  318. warnings *query.AURWarnings,
  319. dbExecutor db.Executor,
  320. aurClient *aur.Client,
  321. mode parser.TargetMode,
  322. ignoreProviders, noConfirm, provides bool,
  323. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string,
  324. ) (*Pool, error) {
  325. dp := newPool(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:", size, dep))
  440. str += "\n"
  441. size = 1
  442. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  443. for _, pkg := range providers.Pkgs {
  444. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  445. size++
  446. }
  447. text.OperationInfoln(str)
  448. for {
  449. fmt.Println(gotext.Get("\nEnter a number (default=1): "))
  450. if noConfirm {
  451. fmt.Println("1")
  452. return providers.Pkgs[0]
  453. }
  454. numberBuf, err := text.GetInput("", false)
  455. if err != nil {
  456. fmt.Fprintln(os.Stderr, err)
  457. break
  458. }
  459. if numberBuf == "" {
  460. return providers.Pkgs[0]
  461. }
  462. num, err := strconv.Atoi(numberBuf)
  463. if err != nil {
  464. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  465. continue
  466. }
  467. if num < 1 || num >= size {
  468. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  469. continue
  470. }
  471. return providers.Pkgs[num-1]
  472. }
  473. return nil
  474. }