2018-05-08 06:06:25 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-10 03:22:21 +02:00
|
|
|
"sort"
|
2018-05-08 06:06:25 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
alpm "github.com/jguer/go-alpm"
|
|
|
|
rpc "github.com/mikkeloscar/aur"
|
|
|
|
)
|
|
|
|
|
|
|
|
type depPool struct {
|
|
|
|
Targets []target
|
2018-05-29 17:47:43 +02:00
|
|
|
Explicit stringSet
|
2018-05-08 06:06:25 +02:00
|
|
|
Repo map[string]*alpm.Package
|
|
|
|
Aur map[string]*rpc.Pkg
|
|
|
|
AurCache map[string]*rpc.Pkg
|
|
|
|
Groups []string
|
|
|
|
LocalDb *alpm.Db
|
|
|
|
SyncDb alpm.DbList
|
|
|
|
Warnings *aurWarnings
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeDepPool() (*depPool, error) {
|
|
|
|
localDb, err := alpmHandle.LocalDb()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
syncDb, err := alpmHandle.SyncDbs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dp := &depPool{
|
|
|
|
make([]target, 0),
|
2018-05-29 17:47:43 +02:00
|
|
|
make(stringSet),
|
2018-05-08 06:06:25 +02:00
|
|
|
make(map[string]*alpm.Package),
|
|
|
|
make(map[string]*rpc.Pkg),
|
|
|
|
make(map[string]*rpc.Pkg),
|
|
|
|
make([]string, 0),
|
|
|
|
localDb,
|
|
|
|
syncDb,
|
2018-05-10 15:36:38 +02:00
|
|
|
nil,
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return dp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Includes db/ prefixes and group installs
|
|
|
|
func (dp *depPool) ResolveTargets(pkgs []string) error {
|
|
|
|
// RPC requests are slow
|
|
|
|
// Combine as many AUR package requests as possible into a single RPC
|
|
|
|
// call
|
|
|
|
aurTargets := make(stringSet)
|
|
|
|
|
2018-06-01 05:20:49 +02:00
|
|
|
pkgs = removeInvalidTargets(pkgs)
|
|
|
|
|
2018-05-30 05:41:59 +02:00
|
|
|
for _, pkg := range pkgs {
|
|
|
|
var err error
|
|
|
|
target := toTarget(pkg)
|
2018-05-08 06:06:25 +02:00
|
|
|
|
|
|
|
// skip targets already satisfied
|
|
|
|
// even if the user enters db/pkg and aur/pkg the latter will
|
2018-06-04 21:12:26 +02:00
|
|
|
// still get skipped even if it's from a different database to
|
2018-05-08 06:06:25 +02:00
|
|
|
// the one specified
|
|
|
|
// this is how pacman behaves
|
2018-05-10 03:22:21 +02:00
|
|
|
if dp.hasPackage(target.DepString()) {
|
2018-05-08 06:06:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundPkg *alpm.Package
|
|
|
|
var singleDb *alpm.Db
|
|
|
|
|
|
|
|
// aur/ prefix means we only check the aur
|
2018-06-01 05:20:49 +02:00
|
|
|
if target.Db == "aur" || mode == ModeAUR {
|
2018-05-30 05:41:59 +02:00
|
|
|
dp.Targets = append(dp.Targets, target)
|
2018-05-08 06:06:25 +02:00
|
|
|
aurTargets.set(target.DepString())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:12:26 +02:00
|
|
|
// If there'ss a different priefix only look in that repo
|
2018-05-08 06:06:25 +02:00
|
|
|
if target.Db != "" {
|
|
|
|
singleDb, err = alpmHandle.SyncDbByName(target.Db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
foundPkg, err = singleDb.PkgCache().FindSatisfier(target.DepString())
|
|
|
|
//otherwise find it in any repo
|
|
|
|
} else {
|
|
|
|
foundPkg, err = dp.SyncDb.FindSatisfier(target.DepString())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2018-05-30 05:41:59 +02:00
|
|
|
dp.Targets = append(dp.Targets, target)
|
2018-05-29 17:47:43 +02:00
|
|
|
dp.Explicit.set(foundPkg.Name())
|
2018-05-08 06:06:25 +02:00
|
|
|
dp.ResolveRepoDependency(foundPkg)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
//check for groups
|
2018-06-04 21:12:26 +02:00
|
|
|
//currently we don't resolve the packages in a group
|
2018-05-08 06:06:25 +02:00
|
|
|
//only check if the group exists
|
|
|
|
//would be better to check the groups from singleDb if
|
2018-06-04 21:12:26 +02:00
|
|
|
//the user specified a db but there's no easy way to do
|
|
|
|
//it without making alpm_lists so don't bother for now
|
2018-05-08 06:06:25 +02:00
|
|
|
//db/group is probably a rare use case
|
2018-05-30 05:41:59 +02:00
|
|
|
group, err := dp.SyncDb.PkgCachebyGroup(target.Name)
|
2018-05-08 06:06:25 +02:00
|
|
|
if err == nil {
|
|
|
|
dp.Groups = append(dp.Groups, target.String())
|
2018-05-30 05:41:59 +02:00
|
|
|
group.ForEach(func(pkg alpm.Package) error {
|
|
|
|
dp.Explicit.set(pkg.Name())
|
|
|
|
return nil
|
|
|
|
})
|
2018-05-08 06:06:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if there was no db prefix check the aur
|
|
|
|
if target.Db == "" {
|
|
|
|
aurTargets.set(target.DepString())
|
|
|
|
}
|
2018-05-30 05:41:59 +02:00
|
|
|
|
|
|
|
dp.Targets = append(dp.Targets, target)
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 17:23:15 +02:00
|
|
|
if len(aurTargets) > 0 && (mode == ModeAny || mode == ModeAUR) {
|
2018-05-30 05:41:59 +02:00
|
|
|
return dp.resolveAURPackages(aurTargets, true)
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:41:59 +02:00
|
|
|
return nil
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pseudo provides finder.
|
|
|
|
// Try to find provides by performing a search of the package name
|
|
|
|
// This effectively performs -Ss on each package
|
|
|
|
// then runs -Si on each result to cache the information.
|
|
|
|
//
|
|
|
|
// For example if you were to -S yay then yay -Ss would give:
|
|
|
|
// yay-git yay-bin yay realyog pacui pacui-git ruby-yard
|
2018-06-04 21:12:26 +02:00
|
|
|
// These packages will all be added to the cache in case they are needed later
|
2018-05-08 06:06:25 +02:00
|
|
|
// Ofcouse only the first three packages provide yay, the rest are just false
|
|
|
|
// positives.
|
|
|
|
//
|
|
|
|
// This method increases dependency resolve time
|
|
|
|
func (dp *depPool) findProvides(pkgs stringSet) error {
|
|
|
|
var mux sync.Mutex
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
doSearch := func(pkg string) {
|
|
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
var results []rpc.Pkg
|
|
|
|
|
|
|
|
// Hack for a bigger search result, if the user wants
|
|
|
|
// java-envronment we can search for just java instead and get
|
|
|
|
// more hits.
|
|
|
|
words := strings.Split(pkg, "-")
|
|
|
|
|
|
|
|
for i := range words {
|
|
|
|
results, err = rpc.SearchByNameDesc(strings.Join(words[:i+1], "-"))
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
mux.Lock()
|
|
|
|
if _, ok := dp.AurCache[result.Name]; !ok {
|
|
|
|
pkgs.set(result.Name)
|
|
|
|
}
|
|
|
|
mux.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for pkg := range pkgs {
|
2018-05-30 04:31:21 +02:00
|
|
|
if _, err := dp.LocalDb.PkgByName(pkg); err == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-08 06:06:25 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go doSearch(pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *depPool) cacheAURPackages(_pkgs stringSet) error {
|
|
|
|
pkgs := _pkgs.copy()
|
|
|
|
query := make([]string, 0)
|
|
|
|
|
|
|
|
for pkg := range pkgs {
|
|
|
|
if _, ok := dp.AurCache[pkg]; ok {
|
|
|
|
pkgs.remove(pkg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pkgs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-29 18:39:38 +02:00
|
|
|
if config.Provides {
|
2018-05-08 06:06:25 +02:00
|
|
|
err := dp.findProvides(pkgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for pkg := range pkgs {
|
|
|
|
if _, ok := dp.AurCache[pkg]; !ok {
|
|
|
|
name, _, _ := splitDep(pkg)
|
|
|
|
query = append(query, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := aurInfo(query, dp.Warnings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range info {
|
|
|
|
// Dump everything in cache just in case we need it later
|
|
|
|
dp.AurCache[pkg.Name] = pkg
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:47:43 +02:00
|
|
|
func (dp *depPool) resolveAURPackages(pkgs stringSet, explicit bool) error {
|
2018-05-08 06:06:25 +02:00
|
|
|
newPackages := make(stringSet)
|
|
|
|
newAURPackages := make(stringSet)
|
|
|
|
|
|
|
|
err := dp.cacheAURPackages(pkgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pkgs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for name := range pkgs {
|
|
|
|
_, ok := dp.Aur[name]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg := dp.findSatisfierAurCache(name)
|
|
|
|
if pkg == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:47:43 +02:00
|
|
|
if explicit {
|
|
|
|
dp.Explicit.set(pkg.Name)
|
|
|
|
}
|
2018-05-08 06:06:25 +02:00
|
|
|
dp.Aur[pkg.Name] = pkg
|
|
|
|
|
|
|
|
for _, deps := range [3][]string{pkg.Depends, pkg.MakeDepends, pkg.CheckDepends} {
|
|
|
|
for _, dep := range deps {
|
|
|
|
newPackages.set(dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for dep := range newPackages {
|
|
|
|
if dp.hasSatisfier(dep) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-04 21:01:49 +02:00
|
|
|
_, isInstalled := dp.LocalDb.PkgCache().FindSatisfier(dep) //has satisfier installed: skip
|
2018-09-25 15:05:45 +02:00
|
|
|
hm := hideMenus
|
|
|
|
hideMenus = isInstalled == nil
|
|
|
|
repoPkg, inRepos := dp.SyncDb.FindSatisfier(dep) //has satisfier in repo: fetch it
|
|
|
|
hideMenus = hm
|
2018-09-04 21:01:49 +02:00
|
|
|
if isInstalled == nil && (config.ReBuild != "tree" || inRepos == nil) {
|
2018-05-08 06:06:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if inRepos == nil {
|
|
|
|
dp.ResolveRepoDependency(repoPkg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//assume it's in the aur
|
2018-06-04 21:12:26 +02:00
|
|
|
//ditch the versioning because the RPC can't handle it
|
2018-05-08 06:06:25 +02:00
|
|
|
newAURPackages.set(dep)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:47:43 +02:00
|
|
|
err = dp.resolveAURPackages(newAURPackages, false)
|
2018-05-08 06:06:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *depPool) ResolveRepoDependency(pkg *alpm.Package) {
|
|
|
|
dp.Repo[pkg.Name()] = pkg
|
|
|
|
|
|
|
|
pkg.Depends().ForEach(func(dep alpm.Depend) (err error) {
|
|
|
|
//have satisfier in dep tree: skip
|
|
|
|
if dp.hasSatisfier(dep.String()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//has satisfier installed: skip
|
|
|
|
_, isInstalled := dp.LocalDb.PkgCache().FindSatisfier(dep.String())
|
|
|
|
if isInstalled == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//has satisfier in repo: fetch it
|
|
|
|
repoPkg, inRepos := dp.SyncDb.FindSatisfier(dep.String())
|
|
|
|
if inRepos != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dp.ResolveRepoDependency(repoPkg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:36:38 +02:00
|
|
|
func getDepPool(pkgs []string, warnings *aurWarnings) (*depPool, error) {
|
2018-05-08 06:06:25 +02:00
|
|
|
dp, err := makeDepPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:36:38 +02:00
|
|
|
dp.Warnings = warnings
|
2018-05-08 06:06:25 +02:00
|
|
|
err = dp.ResolveTargets(pkgs)
|
|
|
|
|
|
|
|
return dp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *depPool) findSatisfierAur(dep string) *rpc.Pkg {
|
|
|
|
for _, pkg := range dp.Aur {
|
|
|
|
if satisfiesAur(dep, pkg) {
|
|
|
|
return pkg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is mostly used to promote packages from the cache
|
|
|
|
// to the Install list
|
2018-06-04 21:12:26 +02:00
|
|
|
// Provide a pacman style provider menu if there's more than one candidate
|
|
|
|
// This acts slightly differently from Pacman, It will give
|
2018-06-02 19:27:26 +02:00
|
|
|
// a menu even if a package with a matching name exists. I believe this
|
|
|
|
// method is better because most of the time you are choosing between
|
|
|
|
// foo and foo-git.
|
|
|
|
// Using Pacman's ways trying to install foo would never give you
|
|
|
|
// a menu.
|
2018-05-08 06:06:25 +02:00
|
|
|
// TODO: maybe intermix repo providers in the menu
|
|
|
|
func (dp *depPool) findSatisfierAurCache(dep string) *rpc.Pkg {
|
2018-05-10 03:22:21 +02:00
|
|
|
depName, _, _ := splitDep(dep)
|
|
|
|
seen := make(stringSet)
|
|
|
|
providers := makeProviders(depName)
|
|
|
|
|
|
|
|
if _, err := dp.LocalDb.PkgByName(depName); err == nil {
|
|
|
|
if pkg, ok := dp.AurCache[dep]; ok && pkgSatisfies(pkg.Name, pkg.Version, dep) {
|
2018-05-08 06:06:25 +02:00
|
|
|
return pkg
|
|
|
|
}
|
2018-06-02 19:27:26 +02:00
|
|
|
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
2018-06-02 19:27:26 +02:00
|
|
|
if cmdArgs.op == "Y" || cmdArgs.op == "yay" {
|
|
|
|
for _, pkg := range dp.AurCache {
|
|
|
|
if pkgSatisfies(pkg.Name, pkg.Version, dep) {
|
|
|
|
for _, target := range dp.Targets {
|
|
|
|
if target.Name == pkg.Name {
|
|
|
|
return pkg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 03:22:21 +02:00
|
|
|
|
2018-05-08 06:06:25 +02:00
|
|
|
for _, pkg := range dp.AurCache {
|
2018-05-10 03:22:21 +02:00
|
|
|
if seen.get(pkg.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:36:38 +02:00
|
|
|
if pkgSatisfies(pkg.Name, pkg.Version, dep) {
|
2018-05-10 03:22:21 +02:00
|
|
|
providers.Pkgs = append(providers.Pkgs, pkg)
|
|
|
|
seen.set(pkg.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:06:25 +02:00
|
|
|
for _, provide := range pkg.Provides {
|
|
|
|
if provideSatisfies(provide, dep) {
|
2018-05-10 03:22:21 +02:00
|
|
|
providers.Pkgs = append(providers.Pkgs, pkg)
|
|
|
|
seen.set(pkg.Name)
|
|
|
|
continue
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:22:21 +02:00
|
|
|
if providers.Len() == 1 {
|
|
|
|
return providers.Pkgs[0]
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 03:22:21 +02:00
|
|
|
if providers.Len() > 1 {
|
|
|
|
sort.Sort(providers)
|
2018-05-08 06:06:25 +02:00
|
|
|
return providerMenu(dep, providers)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *depPool) findSatisfierRepo(dep string) *alpm.Package {
|
|
|
|
for _, pkg := range dp.Repo {
|
|
|
|
if satisfiesRepo(dep, pkg) {
|
|
|
|
return pkg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *depPool) hasSatisfier(dep string) bool {
|
|
|
|
return dp.findSatisfierRepo(dep) != nil || dp.findSatisfierAur(dep) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *depPool) hasPackage(name string) bool {
|
|
|
|
for _, pkg := range dp.Repo {
|
|
|
|
if pkg.Name() == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range dp.Aur {
|
|
|
|
if pkg.Name == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range dp.Groups {
|
|
|
|
if pkg == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|