depPool.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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/v10/pkg/db"
  15. "github.com/Jguer/yay/v10/pkg/query"
  16. "github.com/Jguer/yay/v10/pkg/settings"
  17. "github.com/Jguer/yay/v10/pkg/settings/parser"
  18. "github.com/Jguer/yay/v10/pkg/stringset"
  19. "github.com/Jguer/yay/v10/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(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(aurTargets, true, ignoreProviders, 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(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(context.Background(), 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(_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(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(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(pkgs stringset.StringSet,
  242. explicit, ignoreProviders, noConfirm, provides bool,
  243. rebuild string, splitN int, noDeps, noCheckDeps bool) error {
  244. newPackages := make(stringset.StringSet)
  245. newAURPackages := make(stringset.StringSet)
  246. err := dp.cacheAURPackages(pkgs, provides, splitN)
  247. if err != nil {
  248. return err
  249. }
  250. if len(pkgs) == 0 {
  251. return nil
  252. }
  253. for name := range pkgs {
  254. _, ok := dp.Aur[name]
  255. if ok {
  256. continue
  257. }
  258. pkg := dp.findSatisfierAurCache(name, ignoreProviders, noConfirm, provides)
  259. if pkg == nil {
  260. continue
  261. }
  262. if explicit {
  263. dp.Explicit.Set(pkg.Name)
  264. }
  265. dp.Aur[pkg.Name] = pkg
  266. combinedDepList := ComputeCombinedDepList(pkg, noDeps, noCheckDeps)
  267. for _, deps := range combinedDepList {
  268. for _, dep := range deps {
  269. newPackages.Set(dep)
  270. }
  271. }
  272. }
  273. for dep := range newPackages {
  274. if dp.hasSatisfier(dep) {
  275. continue
  276. }
  277. isInstalled := dp.AlpmExecutor.LocalSatisfierExists(dep)
  278. hm := settings.HideMenus
  279. settings.HideMenus = isInstalled
  280. repoPkg := dp.AlpmExecutor.SyncSatisfier(dep) // has satisfier in repo: fetch it
  281. settings.HideMenus = hm
  282. if isInstalled && (rebuild != "tree" || repoPkg != nil) {
  283. continue
  284. }
  285. if repoPkg != nil {
  286. dp.ResolveRepoDependency(repoPkg, false)
  287. continue
  288. }
  289. // assume it's in the aur
  290. // ditch the versioning because the RPC can't handle it
  291. newAURPackages.Set(dep)
  292. }
  293. err = dp.resolveAURPackages(newAURPackages, false, ignoreProviders, noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps)
  294. return err
  295. }
  296. func (dp *Pool) ResolveRepoDependency(pkg db.IPackage, noDeps bool) {
  297. dp.Repo[pkg.Name()] = pkg
  298. if noDeps {
  299. return
  300. }
  301. for _, dep := range dp.AlpmExecutor.PackageDepends(pkg) {
  302. if dp.hasSatisfier(dep.String()) {
  303. continue
  304. }
  305. // has satisfier installed: skip
  306. if dp.AlpmExecutor.LocalSatisfierExists(dep.String()) {
  307. continue
  308. }
  309. // has satisfier in repo: fetch it
  310. if repoPkg := dp.AlpmExecutor.SyncSatisfier(dep.String()); repoPkg != nil {
  311. dp.ResolveRepoDependency(repoPkg, noDeps)
  312. }
  313. }
  314. }
  315. func GetPool(pkgs []string,
  316. warnings *query.AURWarnings,
  317. dbExecutor db.Executor,
  318. aurClient *aur.Client,
  319. mode parser.TargetMode,
  320. ignoreProviders, noConfirm, provides bool,
  321. rebuild string, splitN int, noDeps bool, noCheckDeps bool, assumeInstalled []string) (*Pool, error) {
  322. dp := makePool(dbExecutor, aurClient)
  323. dp.Warnings = warnings
  324. err := dp.ResolveTargets(pkgs, mode, ignoreProviders, noConfirm, provides, rebuild, splitN, noDeps, noCheckDeps, assumeInstalled)
  325. return dp, err
  326. }
  327. func (dp *Pool) findSatisfierAur(dep string) *query.Pkg {
  328. for _, pkg := range dp.Aur {
  329. if satisfiesAur(dep, pkg) {
  330. return pkg
  331. }
  332. }
  333. return nil
  334. }
  335. // This is mostly used to promote packages from the cache
  336. // to the Install list
  337. // Provide a pacman style provider menu if there's more than one candidate
  338. // This acts slightly differently from Pacman, It will give
  339. // a menu even if a package with a matching name exists. I believe this
  340. // method is better because most of the time you are choosing between
  341. // foo and foo-git.
  342. // Using Pacman's ways trying to install foo would never give you
  343. // a menu.
  344. // TODO: maybe intermix repo providers in the menu.
  345. func (dp *Pool) findSatisfierAurCache(dep string, ignoreProviders, noConfirm, provides bool) *query.Pkg {
  346. depName, _, _ := splitDep(dep)
  347. seen := make(stringset.StringSet)
  348. providerSlice := makeProviders(depName)
  349. if dp.AlpmExecutor.LocalPackage(depName) != nil {
  350. if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
  351. return pkg
  352. }
  353. }
  354. if ignoreProviders {
  355. for _, pkg := range dp.AurCache {
  356. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  357. for _, target := range dp.Targets {
  358. if target.Name == pkg.Name {
  359. return pkg
  360. }
  361. }
  362. }
  363. }
  364. }
  365. for _, pkg := range dp.AurCache {
  366. if seen.Get(pkg.Name) {
  367. continue
  368. }
  369. if pkgSatisfies(pkg.Name, pkg.Version, dep) {
  370. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  371. seen.Set(pkg.Name)
  372. continue
  373. }
  374. for _, provide := range pkg.Provides {
  375. if provideSatisfies(provide, dep, pkg.Version) {
  376. providerSlice.Pkgs = append(providerSlice.Pkgs, pkg)
  377. seen.Set(pkg.Name)
  378. continue
  379. }
  380. }
  381. }
  382. if !provides && providerSlice.Len() >= 1 {
  383. return providerSlice.Pkgs[0]
  384. }
  385. if providerSlice.Len() == 1 {
  386. return providerSlice.Pkgs[0]
  387. }
  388. if providerSlice.Len() > 1 {
  389. sort.Sort(providerSlice)
  390. return providerMenu(dep, providerSlice, noConfirm)
  391. }
  392. return nil
  393. }
  394. func (dp *Pool) findSatisfierRepo(dep string) db.IPackage {
  395. for _, pkg := range dp.Repo {
  396. if satisfiesRepo(dep, pkg, dp.AlpmExecutor) {
  397. return pkg
  398. }
  399. }
  400. return nil
  401. }
  402. func (dp *Pool) hasSatisfier(dep string) bool {
  403. return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
  404. }
  405. func (dp *Pool) hasPackage(name string) bool {
  406. for _, pkg := range dp.Repo {
  407. if pkg.Name() == name {
  408. return true
  409. }
  410. }
  411. for _, pkg := range dp.Aur {
  412. if pkg.Name == name {
  413. return true
  414. }
  415. }
  416. for _, pkg := range dp.Groups {
  417. if pkg == name {
  418. return true
  419. }
  420. }
  421. return false
  422. }
  423. func isInAssumeInstalled(name string, assumeInstalled []string) bool {
  424. for _, pkgAndVersion := range assumeInstalled {
  425. assumeName, _, _ := splitDep(pkgAndVersion)
  426. depName, _, _ := splitDep(name)
  427. if assumeName == depName {
  428. return true
  429. }
  430. }
  431. return false
  432. }
  433. func providerMenu(dep string, providers providers, noConfirm bool) *query.Pkg {
  434. size := providers.Len()
  435. str := text.Bold(gotext.Get("There are %d providers available for %s:\n", size, dep))
  436. size = 1
  437. str += text.SprintOperationInfo(gotext.Get("Repository AUR"), "\n ")
  438. for _, pkg := range providers.Pkgs {
  439. str += fmt.Sprintf("%d) %s ", size, pkg.Name)
  440. size++
  441. }
  442. text.OperationInfoln(str)
  443. for {
  444. fmt.Print(gotext.Get("\nEnter a number (default=1): "))
  445. if noConfirm {
  446. fmt.Println("1")
  447. return providers.Pkgs[0]
  448. }
  449. reader := bufio.NewReader(os.Stdin)
  450. numberBuf, overflow, err := reader.ReadLine()
  451. if err != nil {
  452. fmt.Fprintln(os.Stderr, err)
  453. break
  454. }
  455. if overflow {
  456. text.Errorln(gotext.Get("input too long"))
  457. continue
  458. }
  459. if string(numberBuf) == "" {
  460. return providers.Pkgs[0]
  461. }
  462. num, err := strconv.Atoi(string(numberBuf))
  463. if err != nil {
  464. text.Errorln(gotext.Get("invalid number: %s", string(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. }