depPool.go 12 KB

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