123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- package main
- import (
- "context"
- "errors"
- "fmt"
- "os"
- "sort"
- "strings"
- aur "github.com/Jguer/aur"
- alpm "github.com/Jguer/go-alpm/v2"
- "github.com/leonelquinteros/gotext"
- "github.com/Jguer/yay/v10/pkg/db"
- "github.com/Jguer/yay/v10/pkg/query"
- "github.com/Jguer/yay/v10/pkg/settings"
- "github.com/Jguer/yay/v10/pkg/settings/parser"
- "github.com/Jguer/yay/v10/pkg/stringset"
- "github.com/Jguer/yay/v10/pkg/text"
- )
- // Query is a collection of Results.
- type aurQuery []aur.Pkg
- // Query holds the results of a repository search.
- type repoQuery []alpm.IPackage
- func (s repoQuery) Reverse() {
- for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
- s[i], s[j] = s[j], s[i]
- }
- }
- func (q aurQuery) Len() int {
- return len(q)
- }
- func (q aurQuery) Less(i, j int) bool {
- var result bool
- switch config.SortBy {
- case "votes":
- result = q[i].NumVotes > q[j].NumVotes
- case "popularity":
- result = q[i].Popularity > q[j].Popularity
- case "name":
- result = text.LessRunes([]rune(q[i].Name), []rune(q[j].Name))
- case "base":
- result = text.LessRunes([]rune(q[i].PackageBase), []rune(q[j].PackageBase))
- case "submitted":
- result = q[i].FirstSubmitted < q[j].FirstSubmitted
- case "modified":
- result = q[i].LastModified < q[j].LastModified
- case "id":
- result = q[i].ID < q[j].ID
- case "baseid":
- result = q[i].PackageBaseID < q[j].PackageBaseID
- }
- if config.SortMode == settings.BottomUp {
- return !result
- }
- return result
- }
- func (q aurQuery) Swap(i, j int) {
- q[i], q[j] = q[j], q[i]
- }
- func getSearchBy(value string) aur.By {
- switch value {
- case "name":
- return aur.Name
- case "maintainer":
- return aur.Maintainer
- case "depends":
- return aur.Depends
- case "makedepends":
- return aur.MakeDepends
- case "optdepends":
- return aur.OptDepends
- case "checkdepends":
- return aur.CheckDepends
- default:
- return aur.NameDesc
- }
- }
- // NarrowSearch searches AUR and narrows based on subarguments.
- func narrowSearch(aurClient *aur.Client, pkgS []string, sortS bool) (aurQuery, error) {
- var (
- r []aur.Pkg
- err error
- usedIndex int
- )
- by := getSearchBy(config.SearchBy)
- if len(pkgS) == 0 {
- return nil, nil
- }
- for i, word := range pkgS {
- r, err = aurClient.Search(context.Background(), word, by)
- if err == nil {
- usedIndex = i
- break
- }
- }
- if err != nil {
- return nil, err
- }
- if len(pkgS) == 1 {
- if sortS {
- sort.Sort(aurQuery(r))
- }
- return r, err
- }
- var (
- aq aurQuery
- n int
- )
- for i := range r {
- match := true
- for j, pkgN := range pkgS {
- if usedIndex == j {
- continue
- }
- if !(strings.Contains(r[i].Name, pkgN) || strings.Contains(strings.ToLower(r[i].Description), pkgN)) {
- match = false
- break
- }
- }
- if match {
- n++
- aq = append(aq, r[i])
- }
- }
- if sortS {
- sort.Sort(aq)
- }
- return aq, err
- }
- // SyncSearch presents a query to the local repos and to the AUR.
- func syncSearch(pkgS []string, aurClient *aur.Client, dbExecutor db.Executor) (err error) {
- pkgS = query.RemoveInvalidTargets(pkgS, config.Runtime.Mode)
- var (
- aurErr error
- aq aurQuery
- pq repoQuery
- )
- if config.Runtime.Mode.AtLeastAUR() {
- aq, aurErr = narrowSearch(aurClient, pkgS, true)
- }
- if config.Runtime.Mode.AtLeastRepo() {
- pq = queryRepo(pkgS, dbExecutor)
- }
- switch config.SortMode {
- case settings.TopDown:
- if config.Runtime.Mode.AtLeastRepo() {
- pq.printSearch(dbExecutor)
- }
- if config.Runtime.Mode.AtLeastAUR() {
- aq.printSearch(1, dbExecutor)
- }
- case settings.BottomUp:
- if config.Runtime.Mode.AtLeastAUR() {
- aq.printSearch(1, dbExecutor)
- }
- if config.Runtime.Mode.AtLeastRepo() {
- pq.printSearch(dbExecutor)
- }
- default:
- return errors.New(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
- }
- if aurErr != nil {
- text.Errorln(gotext.Get("error during AUR search: %s", aurErr))
- text.Warnln(gotext.Get("Showing repo packages only"))
- }
- return nil
- }
- // SyncInfo serves as a pacman -Si for repo packages and AUR packages.
- func syncInfo(cmdArgs *parser.Arguments, pkgS []string, dbExecutor db.Executor) error {
- var (
- info []*aur.Pkg
- err error
- missing = false
- )
- pkgS = query.RemoveInvalidTargets(pkgS, config.Runtime.Mode)
- aurS, repoS := packageSlices(pkgS, dbExecutor)
- if len(aurS) != 0 {
- noDB := make([]string, 0, len(aurS))
- for _, pkg := range aurS {
- _, name := text.SplitDBFromName(pkg)
- noDB = append(noDB, name)
- }
- info, err = query.AURInfoPrint(config.Runtime.AURClient, noDB, config.RequestSplitN)
- if err != nil {
- missing = true
- fmt.Fprintln(os.Stderr, err)
- }
- }
- // Repo always goes first
- if len(repoS) != 0 {
- arguments := cmdArgs.Copy()
- arguments.ClearTargets()
- arguments.AddTarget(repoS...)
- err = config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(
- cmdArgs, config.Runtime.Mode, settings.NoConfirm))
- if err != nil {
- return err
- }
- }
- if len(aurS) != len(info) {
- missing = true
- }
- if len(info) != 0 {
- for _, pkg := range info {
- PrintInfo(pkg, cmdArgs.ExistsDouble("i"))
- }
- }
- if missing {
- err = fmt.Errorf("")
- }
- return err
- }
- // Search handles repo searches. Creates a RepoSearch struct.
- func queryRepo(pkgInputN []string, dbExecutor db.Executor) repoQuery {
- s := repoQuery(dbExecutor.SyncPackages(pkgInputN...))
- if config.SortMode == settings.BottomUp {
- s.Reverse()
- }
- return s
- }
- // PackageSlices separates an input slice into aur and repo slices.
- func packageSlices(toCheck []string, dbExecutor db.Executor) (aurNames, repoNames []string) {
- for _, _pkg := range toCheck {
- dbName, name := text.SplitDBFromName(_pkg)
- if dbName == "aur" || config.Runtime.Mode == parser.ModeAUR {
- aurNames = append(aurNames, _pkg)
- continue
- } else if dbName != "" || config.Runtime.Mode == parser.ModeRepo {
- repoNames = append(repoNames, _pkg)
- continue
- }
- if dbExecutor.SyncSatisfierExists(name) ||
- len(dbExecutor.PackagesFromGroup(name)) != 0 {
- repoNames = append(repoNames, _pkg)
- } else {
- aurNames = append(aurNames, _pkg)
- }
- }
- return aurNames, repoNames
- }
- // HangingPackages returns a list of packages installed as deps
- // and unneeded by the system
- // removeOptional decides whether optional dependencies are counted or not.
- func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []string) {
- // safePackages represents every package in the system in one of 3 states
- // State = 0 - Remove package from the system
- // State = 1 - Keep package in the system; need to iterate over dependencies
- // State = 2 - Keep package and have iterated over dependencies
- safePackages := make(map[string]uint8)
- // provides stores a mapping from the provides name back to the original package name
- provides := make(stringset.MapStringSet)
- packages := dbExecutor.LocalPackages()
- // Mark explicit dependencies and enumerate the provides list
- for _, pkg := range packages {
- if pkg.Reason() == alpm.PkgReasonExplicit {
- safePackages[pkg.Name()] = 1
- } else {
- safePackages[pkg.Name()] = 0
- }
- for _, dep := range dbExecutor.PackageProvides(pkg) {
- provides.Add(dep.Name, pkg.Name())
- }
- }
- iterateAgain := true
- for iterateAgain {
- iterateAgain = false
- for _, pkg := range packages {
- if state := safePackages[pkg.Name()]; state == 0 || state == 2 {
- continue
- }
- safePackages[pkg.Name()] = 2
- deps := dbExecutor.PackageDepends(pkg)
- if !removeOptional {
- deps = append(deps, dbExecutor.PackageOptionalDepends(pkg)...)
- }
- // Update state for dependencies
- for _, dep := range deps {
- // Don't assume a dependency is installed
- state, ok := safePackages[dep.Name]
- if !ok {
- // Check if dep is a provides rather than actual package name
- if pset, ok2 := provides[dep.Name]; ok2 {
- for p := range pset {
- if safePackages[p] == 0 {
- iterateAgain = true
- safePackages[p] = 1
- }
- }
- }
- continue
- }
- if state == 0 {
- iterateAgain = true
- safePackages[dep.Name] = 1
- }
- }
- }
- }
- // Build list of packages to be removed
- for _, pkg := range packages {
- if safePackages[pkg.Name()] == 0 {
- hanging = append(hanging, pkg.Name())
- }
- }
- return hanging
- }
- // Statistics returns statistics about packages installed in system.
- func statistics(dbExecutor db.Executor) *struct {
- Totaln int
- Expln int
- TotalSize int64
- } {
- var (
- totalSize int64
- localPackages = dbExecutor.LocalPackages()
- totalInstalls = 0
- explicitInstalls = 0
- )
- for _, pkg := range localPackages {
- totalSize += pkg.ISize()
- totalInstalls++
- if pkg.Reason() == alpm.PkgReasonExplicit {
- explicitInstalls++
- }
- }
- info := &struct {
- Totaln int
- Expln int
- TotalSize int64
- }{
- totalInstalls, explicitInstalls, totalSize,
- }
- return info
- }
|