1
0
mirror of https://github.com/Jguer/yay synced 2024-07-01 07:56:37 +00:00
yay/query.go

258 lines
6.2 KiB
Go
Raw Permalink Normal View History

2017-04-29 17:12:12 +00:00
package main
import (
2021-05-13 05:27:24 +00:00
"context"
2017-04-29 17:12:12 +00:00
"fmt"
"io/fs"
"path/filepath"
2017-04-29 17:12:12 +00:00
2021-05-13 05:27:24 +00:00
aur "github.com/Jguer/aur"
2020-10-01 11:38:03 +00:00
alpm "github.com/Jguer/go-alpm/v2"
mapset "github.com/deckarep/golang-set/v2"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/runtime"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/parser"
"github.com/Jguer/yay/v12/pkg/text"
2017-04-29 17:12:12 +00:00
)
// SyncSearch presents a query to the local repos and to the AUR.
func syncSearch(ctx context.Context, pkgS []string,
dbExecutor db.Executor, queryBuilder query.Builder, verbose bool,
) error {
queryBuilder.Execute(ctx, dbExecutor, pkgS)
2017-04-29 17:12:12 +00:00
searchMode := query.Minimal
if verbose {
searchMode = query.Detailed
2018-04-03 05:49:41 +00:00
}
return queryBuilder.Results(dbExecutor, searchMode)
2017-04-29 17:12:12 +00:00
}
// SyncInfo serves as a pacman -Si for repo packages and AUR packages.
func syncInfo(ctx context.Context, run *runtime.Runtime,
cmdArgs *parser.Arguments, pkgS []string, dbExecutor db.Executor,
) error {
2021-08-11 18:13:28 +00:00
var (
info []aur.Pkg
2021-08-11 18:13:28 +00:00
err error
missing = false
)
pkgS = query.RemoveInvalidTargets(run.Logger, pkgS, run.Cfg.Mode)
aurS, repoS := packageSlices(pkgS, run.Cfg, dbExecutor)
2017-04-29 17:12:12 +00:00
if len(repoS) == 0 && len(aurS) == 0 {
if run.Cfg.Mode != parser.ModeRepo {
aurS = dbExecutor.InstalledRemotePackageNames()
}
if run.Cfg.Mode != parser.ModeAUR {
repoS = dbExecutor.InstalledSyncPackageNames()
}
}
if len(aurS) != 0 {
2019-02-04 16:56:02 +00:00
noDB := make([]string, 0, len(aurS))
for _, pkg := range aurS {
2020-07-10 00:36:45 +00:00
_, name := text.SplitDBFromName(pkg)
2019-02-04 16:56:02 +00:00
noDB = append(noDB, name)
}
info, err = run.AURClient.Get(ctx, &aur.Query{
Needles: noDB,
By: aur.Name,
})
if err != nil {
missing = true
2021-08-11 18:13:28 +00:00
run.Logger.Errorln(err)
}
}
if len(repoS) != 0 || (len(aurS) == 0 && len(repoS) == 0) {
2020-07-05 00:45:23 +00:00
arguments := cmdArgs.Copy()
arguments.ClearTargets()
arguments.AddTarget(repoS...)
2021-08-11 18:13:28 +00:00
err = run.CmdBuilder.Show(run.CmdBuilder.BuildPacmanCmd(ctx,
arguments, run.Cfg.Mode, settings.NoConfirm))
2018-01-06 13:50:54 +00:00
if err != nil {
return err
2018-01-06 13:50:54 +00:00
}
}
if len(aurS) != len(info) {
missing = true
}
for i := range info {
printInfo(run.Logger, run.Cfg, &info[i], cmdArgs.ExistsDouble("i"))
2017-04-29 17:12:12 +00:00
}
if missing {
err = fmt.Errorf("")
}
return err
2017-04-29 17:12:12 +00:00
}
2021-08-11 18:13:28 +00:00
// PackageSlices separates an input slice into aur and repo slices.
func packageSlices(toCheck []string, config *settings.Configuration, dbExecutor db.Executor) (aurNames, repoNames []string) {
New install algorithm I have replaced the old install and dependancy algorithms with a new design that attemps to be more pacaur like. Mostly in minimizing user input. Ask every thing first then do everything with no need for more user input. It is not yet fully complete but is finished enough so that it works, should not fail in most cases and provides a base for more contributors to help address the existing problems. The new install chain is as follows: Source info about the provided targets Fetch a list of all dependancies needed to install targets I put alot of effort into fetching the dependancy tree while making the least amount of aur requests as possible. I'm actually very happy with how it turned out and yay wil now resolve dependancies noticably faster than pacaur when there are many aur dependancies. Install repo targets by passing to pacman Print dependancy tree and ask to confirm Ask to clean build if directory already exists Download all pkgbuilds Ask to edit all pkgbuilds Ask to continue with the install Download the sources for each packagebuild Build and install every package using -s to get repo deps and -i to install Ask to remove make dependancies There are still a lot of things that need to be done for a fully working system. Here are the problems I found with this system, either new or existing: Formating I am not so good at formatting myself, I thought best to leave it until last so I could get feedback on how it should look and help implementing it. Dependancy tree The dependancy tree is usually correct although I have noticed times where it doesnt detect all the dependancies that it should. I have only noticed this when there are circular dependancies so i think this might be the cause. It's not a big deal currently because makepkg -i installed repo deps for us which handles the repo deps for us and will get the correct ones. So yay might not list all the dependancies. but they will get installed so I consider this a visual bug. I have yet to see any circular dependancies in the AUR so I can not say what will happend but I#m guessing that it will break. Versioned packages/dependencies Targets and dependancies with version constriants such as 'linux>=4.1' will not be checked on the aur side of things but will be checked on the repo side. Ignorepkg/Ignoregroup Currently I do not handle this in any way but it shouldn't be too hard to implement. Conflict checking This is not currently implemented either Split Paclages Split packages are not Handles properly. If we only specify one package so install from a split package makepkg -i ends up installing them all anyway. If we specify more than one (n) package it will actually build the package base n times and reinstall every split package n times. Makepkg To get things working I decided to keep using the makepkg -i method. I plan to eventually replace this with a pacman -U based method. This should allow passing args such as --dbpath and --config to aur packages aswell as help solve some problems such as the split packages. Clean build I plan to improve the clean build choice to be a little more smart and instead of check if the directory exists, check if the package is already build and if so skip the build all together.
2018-01-17 21:48:23 +00:00
for _, _pkg := range toCheck {
dbName, name := text.SplitDBFromName(_pkg)
if dbName == "aur" || config.Mode == parser.ModeAUR {
2021-05-13 05:27:24 +00:00
aurNames = append(aurNames, _pkg)
continue
} else if dbName != "" || config.Mode == parser.ModeRepo {
2021-05-13 05:27:24 +00:00
repoNames = append(repoNames, _pkg)
continue
}
2020-12-15 00:29:05 +00:00
if dbExecutor.SyncSatisfierExists(name) ||
len(dbExecutor.PackagesFromGroup(name)) != 0 {
2021-05-13 05:27:24 +00:00
repoNames = append(repoNames, _pkg)
New install algorithm I have replaced the old install and dependancy algorithms with a new design that attemps to be more pacaur like. Mostly in minimizing user input. Ask every thing first then do everything with no need for more user input. It is not yet fully complete but is finished enough so that it works, should not fail in most cases and provides a base for more contributors to help address the existing problems. The new install chain is as follows: Source info about the provided targets Fetch a list of all dependancies needed to install targets I put alot of effort into fetching the dependancy tree while making the least amount of aur requests as possible. I'm actually very happy with how it turned out and yay wil now resolve dependancies noticably faster than pacaur when there are many aur dependancies. Install repo targets by passing to pacman Print dependancy tree and ask to confirm Ask to clean build if directory already exists Download all pkgbuilds Ask to edit all pkgbuilds Ask to continue with the install Download the sources for each packagebuild Build and install every package using -s to get repo deps and -i to install Ask to remove make dependancies There are still a lot of things that need to be done for a fully working system. Here are the problems I found with this system, either new or existing: Formating I am not so good at formatting myself, I thought best to leave it until last so I could get feedback on how it should look and help implementing it. Dependancy tree The dependancy tree is usually correct although I have noticed times where it doesnt detect all the dependancies that it should. I have only noticed this when there are circular dependancies so i think this might be the cause. It's not a big deal currently because makepkg -i installed repo deps for us which handles the repo deps for us and will get the correct ones. So yay might not list all the dependancies. but they will get installed so I consider this a visual bug. I have yet to see any circular dependancies in the AUR so I can not say what will happend but I#m guessing that it will break. Versioned packages/dependencies Targets and dependancies with version constriants such as 'linux>=4.1' will not be checked on the aur side of things but will be checked on the repo side. Ignorepkg/Ignoregroup Currently I do not handle this in any way but it shouldn't be too hard to implement. Conflict checking This is not currently implemented either Split Paclages Split packages are not Handles properly. If we only specify one package so install from a split package makepkg -i ends up installing them all anyway. If we specify more than one (n) package it will actually build the package base n times and reinstall every split package n times. Makepkg To get things working I decided to keep using the makepkg -i method. I plan to eventually replace this with a pacman -U based method. This should allow passing args such as --dbpath and --config to aur packages aswell as help solve some problems such as the split packages. Clean build I plan to improve the clean build choice to be a little more smart and instead of check if the directory exists, check if the package is already build and if so skip the build all together.
2018-01-17 21:48:23 +00:00
} else {
2021-05-13 05:27:24 +00:00
aurNames = append(aurNames, _pkg)
}
}
2021-05-13 05:27:24 +00:00
return aurNames, repoNames
}
// MapSetMap is a Map of Sets.
type mapSetMap[T comparable] map[T]mapset.Set[T]
// Add adds a new value to the Map.
// If n is already in the map, then v is appended to the StringSet under that key.
// Otherwise a new Set is created containing v.
func (mss mapSetMap[T]) Add(n, v T) {
if _, ok := mss[n]; !ok {
mss[n] = mapset.NewSet[T]()
}
mss[n].Add(v)
}
// HangingPackages returns a list of packages installed as deps
// and unneeded by the system
2021-08-11 18:13:28 +00:00
// removeOptional decides whether optional dependencies are counted or not.
2020-08-16 21:41:38 +00:00
func hangingPackages(removeOptional bool, dbExecutor db.Executor) (hanging []string) {
2018-03-23 23:45:46 +00:00
// 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(mapSetMap[string])
2018-03-23 23:45:46 +00:00
2020-08-07 16:55:19 +00:00
packages := dbExecutor.LocalPackages()
2018-03-23 23:45:46 +00:00
// Mark explicit dependencies and enumerate the provides list
2020-08-07 16:55:19 +00:00
for _, pkg := range packages {
2018-03-23 23:45:46 +00:00
if pkg.Reason() == alpm.PkgReasonExplicit {
safePackages[pkg.Name()] = 1
} else {
safePackages[pkg.Name()] = 0
}
2020-08-07 16:55:19 +00:00
for _, dep := range dbExecutor.PackageProvides(pkg) {
provides.Add(dep.Name, pkg.Name())
2020-08-07 16:55:19 +00:00
}
2018-03-23 23:45:46 +00:00
}
iterateAgain := true
2020-08-07 16:55:19 +00:00
for iterateAgain {
iterateAgain = false
2021-08-11 18:13:28 +00:00
2020-08-07 16:55:19 +00:00
for _, pkg := range packages {
if state := safePackages[pkg.Name()]; state == 0 || state == 2 {
continue
}
safePackages[pkg.Name()] = 2
deps := dbExecutor.PackageDepends(pkg)
2021-08-11 18:13:28 +00:00
2020-08-07 16:55:19 +00:00
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.Iter() {
2020-08-07 16:55:19 +00:00
if safePackages[p] == 0 {
iterateAgain = true
safePackages[p] = 1
}
2018-03-24 12:37:31 +00:00
}
}
2021-08-11 18:13:28 +00:00
2020-08-07 16:55:19 +00:00
continue
2018-03-23 23:45:46 +00:00
}
2020-08-07 16:55:19 +00:00
if state == 0 {
iterateAgain = true
safePackages[dep.Name] = 1
}
2018-03-23 23:45:46 +00:00
}
}
}
// Build list of packages to be removed
2020-08-07 16:55:19 +00:00
for _, pkg := range packages {
2018-03-23 23:45:46 +00:00
if safePackages[pkg.Name()] == 0 {
hanging = append(hanging, pkg.Name())
}
2020-08-07 16:55:19 +00:00
}
2018-03-23 23:45:46 +00:00
2020-08-07 16:55:19 +00:00
return hanging
}
func getFolderSize(path string) (size int64) {
_ = filepath.WalkDir(path, func(p string, entry fs.DirEntry, err error) error {
info, _ := entry.Info()
size += info.Size()
return nil
})
2021-08-11 18:13:28 +00:00
return size
}
2020-08-04 20:00:07 +00:00
// Statistics returns statistics about packages installed in system.
func statistics(run *runtime.Runtime, dbExecutor db.Executor) (res struct {
Totaln int
Expln int
TotalSize int64
pacmanCaches map[string]int64
yayCache int64
},
) {
for _, pkg := range dbExecutor.LocalPackages() {
res.TotalSize += pkg.ISize()
res.Totaln++
2021-08-11 18:13:28 +00:00
2020-08-04 20:00:07 +00:00
if pkg.Reason() == alpm.PkgReasonExplicit {
res.Expln++
}
}
res.pacmanCaches = make(map[string]int64)
for _, path := range run.PacmanConf.CacheDir {
res.pacmanCaches[path] = getFolderSize(path)
}
res.yayCache = getFolderSize(run.Cfg.BuildDir)
return
}