depPool.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 makePool(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) error {
  74. // RPC requests are slow
  75. // Combine as many AUR package requests as possible into a single RPC call
  76. aurTargets := make(stringset.StringSet)
  77. pkgs = query.RemoveInvalidTargets(pkgs, mode)
  78. for _, pkg := range pkgs {
  79. target := ToTarget(pkg)
  80. // skip targets already satisfied
  81. // even if the user enters db/pkg and aur/pkg the latter will
  82. // still get skipped even if it's from a different database to
  83. // the one specified
  84. // this is how pacman behaves
  85. if dp.hasPackage(target.DepString()) || isInAssumeInstalled(target.DepString(), assumeInstalled) {
  86. continue
  87. }
  88. var foundPkg db.IPackage
  89. // aur/ prefix means we only check the aur
  90. if target.DB == "aur" || mode == parser.ModeAUR {
  91. dp.Targets = append(dp.Targets, target)
  92. aurTargets.Set(target.DepString())
  93. continue
  94. }
  95. // If there's a different prefix only look in that repo
  96. if target.DB != "" {
  97. foundPkg = dp.AlpmExecutor.SatisfierFromDB(target.DepString(), target.DB)
  98. } else {
  99. // otherwise find it in any repo
  100. foundPkg = dp.AlpmExecutor.SyncSatisfier(target.DepString())
  101. }
  102. if foundPkg != nil {
  103. dp.Targets = append(dp.Targets, target)
  104. dp.Explicit.Set(foundPkg.Name())
  105. dp.ResolveRepoDependency(foundPkg, noDeps)
  106. continue
  107. } else {
  108. // check for groups
  109. // currently we don't resolve the packages in a group
  110. // only check if the group exists
  111. // would be better to check the groups from singleDB if
  112. // the user specified a db but there's no easy way to do
  113. // it without making alpm_lists so don't bother for now
  114. // db/group is probably a rare use case
  115. groupPackages := dp.AlpmExecutor.PackagesFromGroup(target.Name)
  116. if len(groupPackages) > 0 {
  117. dp.Groups = append(dp.Groups, target.String())
  118. for _, pkg := range groupPackages {
  119. dp.Explicit.Set(pkg.Name())
  120. }
  121. continue
  122. }
  123. }
  124. // if there was no db prefix check the aur
  125. if target.DB == "" {
  126. aurTargets.Set(target.DepString())
  127. }
  128. dp.Targets = append(dp.Targets, target)
  129. }
  130. if len(aurTargets) > 0 && mode.AtLeastAUR() {
  131. return dp.resolveAURPackages(ctx, aurTargets, true, ignoreProviders,
  132. noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  133. }
  134. return nil
  135. }
  136. // Pseudo provides finder.
  137. // Try to find provides by performing a search of the package name
  138. // This effectively performs -Ss on each package
  139. // then runs -Si on each result to cache the information.
  140. //
  141. // For example if you were to -S yay then yay -Ss would give:
  142. // yay-git yay-bin yay realyog pacui pacui-git ruby-yard
  143. // These packages will all be added to the cache in case they are needed later
  144. // Ofcouse only the first three packages provide yay, the rest are just false
  145. // positives.
  146. //
  147. // This method increases dependency resolve time.
  148. func (dp *Pool) findProvides(ctx context.Context, pkgs stringset.StringSet) error {
  149. var (
  150. mux sync.Mutex
  151. wg sync.WaitGroup
  152. )
  153. doSearch := func(pkg string) {
  154. defer wg.Done()
  155. var (
  156. err error
  157. results []query.Pkg
  158. )
  159. // Hack for a bigger search result, if the user wants
  160. // java-envronment we can search for just java instead and get
  161. // more hits.
  162. pkg, _, _ = splitDep(pkg) // openimagedenoise-git > ispc-git #1234
  163. words := strings.Split(pkg, "-")
  164. for i := range words {
  165. results, err = dp.aurClient.Search(ctx, strings.Join(words[:i+1], "-"), aur.None)
  166. if err == nil {
  167. break
  168. }
  169. }
  170. if err != nil {
  171. return
  172. }
  173. for iR := range results {
  174. mux.Lock()
  175. if _, ok := dp.AurCache[results[iR].Name]; !ok {
  176. pkgs.Set(results[iR].Name)
  177. }
  178. mux.Unlock()
  179. }
  180. }
  181. for pkg := range pkgs {
  182. if dp.AlpmExecutor.LocalPackage(pkg) != nil {
  183. continue
  184. }
  185. wg.Add(1)
  186. go doSearch(pkg)
  187. }
  188. wg.Wait()
  189. return nil
  190. }
  191. func (dp *Pool) cacheAURPackages(ctx context.Context, _pkgs stringset.StringSet, provides bool, splitN int) error {
  192. pkgs := _pkgs.Copy()
  193. toQuery := make([]string, 0)
  194. for pkg := range pkgs {
  195. if _, ok := dp.AurCache[pkg]; ok {
  196. pkgs.Remove(pkg)
  197. }
  198. }
  199. if len(pkgs) == 0 {
  200. return nil
  201. }
  202. if provides {
  203. err := dp.findProvides(ctx, pkgs)
  204. if err != nil {
  205. return err
  206. }
  207. }
  208. for pkg := range pkgs {
  209. if _, ok := dp.AurCache[pkg]; !ok {
  210. name, _, ver := splitDep(pkg)
  211. if ver != "" {
  212. toQuery = append(toQuery, name, name+"-"+ver)
  213. } else {
  214. toQuery = append(toQuery, name)
  215. }
  216. }
  217. }
  218. info, err := query.AURInfo(ctx, dp.aurClient, toQuery, dp.Warnings, splitN)
  219. if err != nil {
  220. return err
  221. }
  222. for _, pkg := range info {
  223. // Dump everything in cache just in case we need it later
  224. dp.AurCache[pkg.Name] = pkg
  225. }
  226. return nil
  227. }
  228. // Compute dependency lists used in Package dep searching and ordering.
  229. // Order sensitive TOFIX.
  230. func ComputeCombinedDepList(pkg *aur.Pkg, noDeps, noCheckDeps bool) [][]string {
  231. combinedDepList := make([][]string, 0, 3)
  232. if !noDeps {
  233. combinedDepList = append(combinedDepList, pkg.Depends)
  234. }
  235. combinedDepList = append(combinedDepList, pkg.MakeDepends)
  236. if !noCheckDeps {
  237. combinedDepList = append(combinedDepList, pkg.CheckDepends)
  238. }
  239. return combinedDepList
  240. }
  241. func (dp *Pool) resolveAURPackages(ctx context.Context,
  242. pkgs stringset.StringSet,
  243. explicit, ignoreProviders, noConfirm, provides bool,
  244. rebuild string, splitN int, noDeps, noCheckDeps bool) error {
  245. newPackages := make(stringset.StringSet)
  246. newAURPackages := make(stringset.StringSet)
  247. err := dp.cacheAURPackages(ctx, pkgs, provides, splitN)
  248. if err != nil {
  249. return err
  250. }
  251. if len(pkgs) == 0 {
  252. return nil
  253. }
  254. for name := range pkgs {
  255. _, ok := dp.Aur[name]
  256. if ok {
  257. continue
  258. }
  259. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  260. if pkg == nil {
  261. continue
  262. }
  263. if explicit {
  264. dp.Explicit.Set(pkg.Name)
  265. }
  266. dp.Aur[pkg.Name] = pkg
  267. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  268. for _, deps := range combinedDepList {
  269. for _, dep := range deps {
  270. newPackages.Set(dep)
  271. }
  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) (*Pool, error) {
  324. dp := makePool(dbExecutor, aurClient)
  325. dp.Warnings = warnings
  326. err := dp.ResolveTargets(ctx, pkgs, mode, ignoreProviders, noConfirm, provides,
  327. rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  328. return dp, err
  329. }
  330. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  331. for _, pkg := range dp.Aur {
  332. if satisfiesAur(dep, pkg) {
  333. return pkg
  334. }
  335. }
  336. return nil
  337. }
  338. // This is mostly used to promote packages from the cache
  339. // to the Install list
  340. // Provide a pacman style provider menu if there's more than one candidate
  341. // This acts slightly differently from Pacman, It will give
  342. // a menu even if a package with a matching name exists. I believe this
  343. // method is better because most of the time you are choosing between
  344. // foo and foo-git.
  345. // Using Pacman's ways trying to install foo would never give you
  346. // a menu.
  347. // TODO: maybe intermix repo providers in the menu.
  348. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  349. depName, _, _ := splitDep(dep)
  350. seen := make(stringset.StringSet)
  351. providerSlice := makeProviders(depName)
  352. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  353. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  354. return pkg
  355. }
  356. }
  357. if ignoreProviders {
  358. for _, pkg := range dp.AurCache {
  359. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  360. for _, target := range dp.Targets {
  361. if target.Name == pkg.Name {
  362. return pkg
  363. }
  364. }
  365. }
  366. }
  367. }
  368. for _, pkg := range dp.AurCache {
  369. if seen.Get(pkg.Name) {
  370. continue
  371. }
  372. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  373. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  374. seen.Set(pkg.Name)
  375. continue
  376. }
  377. for _, provide := range pkg.Provides {
  378. if provideSatisfies(provide, dep, pkg.Version) {
  379. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  380. seen.Set(pkg.Name)
  381. continue
  382. }
  383. }
  384. }
  385. if !provides && providerSlice.Len() >= 1 {
  386. return providerSlice.Pkgs[0]
  387. }
  388. if providerSlice.Len() == 1 {
  389. return providerSlice.Pkgs[0]
  390. }
  391. if providerSlice.Len() > 1 {
  392. sort.Sort(providerSlice)
  393. return providerMenu(dep, providerSlice, noConfirm)
  394. }
  395. return nil
  396. }
  397. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  398. for _, pkg := range dp.Repo {
  399. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  400. return pkg
  401. }
  402. }
  403. return nil
  404. }
  405. func (dp *Pool) hasSatisfier(dep string) bool {
  406. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  407. }
  408. func (dp *Pool) hasPackage(name string) bool {
  409. for _, pkg := range dp.Repo {
  410. if pkg.Name() == name {
  411. return true
  412. }
  413. }
  414. for _, pkg := range dp.Aur {
  415. if pkg.Name == name {
  416. return true
  417. }
  418. }
  419. for _, pkg := range dp.Groups {
  420. if pkg == name {
  421. return true
  422. }
  423. }
  424. return false
  425. }
  426. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  427. for _, pkgAndVersion := range assumeInstalled {
  428. assumeName, _, _ := splitDep(pkgAndVersion)
  429. depName, _, _ := splitDep(name)
  430. if assumeName == depName {
  431. return true
  432. }
  433. }
  434. return false
  435. }
  436. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  437. size := providers.Len()
  438. str := text.Bold(gotext.Get("There are %d providers available for %s:\n", size, dep))
  439. size = 1
  440. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  441. for _, pkg := range providers.Pkgs {
  442. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  443. size++
  444. }
  445. text.OperationInfoln(str)
  446. for {
  447. fmt.Print(gotext.Get("\nEnter a number (default=1): "))
  448. if noConfirm {
  449. fmt.Println("1")
  450. return providers.Pkgs[0]
  451. }
  452. numberBuf, err := text.GetInput("", false)
  453. if err != nil {
  454. fmt.Fprintln(os.Stderr, err)
  455. break
  456. }
  457. if numberBuf == "" {
  458. return providers.Pkgs[0]
  459. }
  460. num, err := strconv.Atoi(numberBuf)
  461. if err != nil {
  462. text.Errorln(gotext.Get("invalid number: %s", numberBuf))
  463. continue
  464. }
  465. if num < 1 || num >= size {
  466. text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size-1))
  467. continue
  468. }
  469. return providers.Pkgs[num-1]
  470. }
  471. return nil
  472. }