2020-07-10 02:36:45 +02:00
|
|
|
package dep
|
2018-05-08 06:06:25 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/db"
|
|
|
|
aur "github.com/Jguer/yay/v11/pkg/query"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2018-05-08 06:06:25 +02:00
|
|
|
)
|
|
|
|
|
2018-05-10 03:22:21 +02:00
|
|
|
type providers struct {
|
|
|
|
lookfor string
|
2021-05-13 07:27:24 +02:00
|
|
|
Pkgs []*aur.Pkg
|
2018-05-10 03:22:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeProviders(name string) providers {
|
|
|
|
return providers{
|
|
|
|
name,
|
2021-05-13 07:27:24 +02:00
|
|
|
make([]*aur.Pkg, 0),
|
2018-05-10 03:22:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q providers) Len() int {
|
|
|
|
return len(q.Pkgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q providers) Less(i, j int) bool {
|
|
|
|
if q.lookfor == q.Pkgs[i].Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.lookfor == q.Pkgs[j].Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-10 02:36:45 +02:00
|
|
|
return text.LessRunes([]rune(q.Pkgs[i].Name), []rune(q.Pkgs[j].Name))
|
2018-05-10 03:22:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q providers) Swap(i, j int) {
|
|
|
|
q.Pkgs[i], q.Pkgs[j] = q.Pkgs[j], q.Pkgs[i]
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
func splitDep(dep string) (pkg, mod, ver string) {
|
2018-05-08 06:06:25 +02:00
|
|
|
split := strings.FieldsFunc(dep, func(c rune) bool {
|
|
|
|
match := c == '>' || c == '<' || c == '='
|
|
|
|
|
|
|
|
if match {
|
|
|
|
mod += string(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return match
|
|
|
|
})
|
|
|
|
|
2018-08-05 01:25:27 +02:00
|
|
|
if len(split) == 0 {
|
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:06:25 +02:00
|
|
|
if len(split) == 1 {
|
|
|
|
return split[0], "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return split[0], mod, split[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func pkgSatisfies(name, version, dep string) bool {
|
|
|
|
depName, depMod, depVersion := splitDep(dep)
|
|
|
|
|
|
|
|
if depName != name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return verSatisfies(version, depMod, depVersion)
|
|
|
|
}
|
|
|
|
|
2020-10-06 06:25:18 +02:00
|
|
|
func provideSatisfies(provide, dep, pkgVersion string) bool {
|
2018-05-08 06:06:25 +02:00
|
|
|
depName, depMod, depVersion := splitDep(dep)
|
|
|
|
provideName, provideMod, provideVersion := splitDep(provide)
|
|
|
|
|
|
|
|
if provideName != depName {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-17 14:45:07 +02:00
|
|
|
// Unversioned provides can not satisfy a versioned dep
|
2018-05-08 06:06:25 +02:00
|
|
|
if provideMod == "" && depMod != "" {
|
2020-10-06 06:25:18 +02:00
|
|
|
provideVersion = pkgVersion // Example package: pagure
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return verSatisfies(provideVersion, depMod, depVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verSatisfies(ver1, mod, ver2 string) bool {
|
|
|
|
switch mod {
|
|
|
|
case "=":
|
2021-02-16 16:49:38 +01:00
|
|
|
return db.VerCmp(ver1, ver2) == 0
|
2018-05-08 06:06:25 +02:00
|
|
|
case "<":
|
2021-02-16 16:49:38 +01:00
|
|
|
return db.VerCmp(ver1, ver2) < 0
|
2018-05-08 06:06:25 +02:00
|
|
|
case "<=":
|
2021-02-16 16:49:38 +01:00
|
|
|
return db.VerCmp(ver1, ver2) <= 0
|
2018-05-08 06:06:25 +02:00
|
|
|
case ">":
|
2021-02-16 16:49:38 +01:00
|
|
|
return db.VerCmp(ver1, ver2) > 0
|
2018-05-08 06:06:25 +02:00
|
|
|
case ">=":
|
2021-02-16 16:49:38 +01:00
|
|
|
return db.VerCmp(ver1, ver2) >= 0
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-13 07:27:24 +02:00
|
|
|
func satisfiesAur(dep string, pkg *aur.Pkg) bool {
|
2018-05-08 06:06:25 +02:00
|
|
|
if pkgSatisfies(pkg.Name, pkg.Version, dep) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, provide := range pkg.Provides {
|
2020-10-06 06:25:18 +02:00
|
|
|
if provideSatisfies(provide, dep, pkg.Version) {
|
2018-05-08 06:06:25 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-16 16:49:38 +01:00
|
|
|
func satisfiesRepo(dep string, pkg db.IPackage, dbExecutor db.Executor) bool {
|
2018-05-08 06:06:25 +02:00
|
|
|
if pkgSatisfies(pkg.Name(), pkg.Version(), dep) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-16 23:41:38 +02:00
|
|
|
for _, provided := range dbExecutor.PackageProvides(pkg) {
|
2020-10-06 06:25:18 +02:00
|
|
|
if provideSatisfies(provided.String(), dep, pkg.Version()) {
|
2020-07-29 01:53:25 +02:00
|
|
|
return true
|
2018-05-08 06:06:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|