depPool.go 12 KB

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