depPool.go 12 KB

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