2018-09-21 21:29:50 +00:00
|
|
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
2019-11-11 13:58:12 +00:00
|
|
|
"crypto/sha256"
|
2019-10-31 11:34:28 +00:00
|
|
|
"fmt"
|
2019-11-15 17:11:26 +00:00
|
|
|
"sort"
|
|
|
|
"unicode"
|
|
|
|
|
2019-06-05 17:13:09 +00:00
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
2020-11-03 16:20:52 +00:00
|
|
|
"github.com/mudler/topsort"
|
2019-11-29 18:01:41 +00:00
|
|
|
toposort "github.com/philopon/go-toposort"
|
2020-04-09 15:07:57 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-09-21 21:29:50 +00:00
|
|
|
)
|
|
|
|
|
2019-11-11 10:02:32 +00:00
|
|
|
type PackagesAssertions []PackageAssert
|
|
|
|
|
2019-11-15 23:38:07 +00:00
|
|
|
type PackageHash struct {
|
|
|
|
BuildHash string
|
|
|
|
PackageHash string
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:03:50 +00:00
|
|
|
// PackageAssert represent a package assertion.
|
|
|
|
// It is composed of a Package and a Value which is indicating the absence or not
|
|
|
|
// of the associated package state.
|
2019-06-04 19:25:17 +00:00
|
|
|
type PackageAssert struct {
|
2019-11-22 23:29:24 +00:00
|
|
|
Package *pkg.DefaultPackage
|
2019-06-04 19:25:17 +00:00
|
|
|
Value bool
|
2019-11-15 23:38:07 +00:00
|
|
|
Hash PackageHash
|
2019-06-04 19:25:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:03:50 +00:00
|
|
|
// DecodeModel decodes a model from the SAT solver to package assertions (PackageAssert)
|
2019-11-15 23:38:07 +00:00
|
|
|
func DecodeModel(model map[string]bool, db pkg.PackageDatabase) (PackagesAssertions, error) {
|
2019-11-11 10:02:32 +00:00
|
|
|
ass := make(PackagesAssertions, 0)
|
2018-09-21 21:29:50 +00:00
|
|
|
for k, v := range model {
|
2019-11-15 23:38:07 +00:00
|
|
|
a, err := pkg.DecodePackage(k, db)
|
2019-06-04 19:57:13 +00:00
|
|
|
if err != nil {
|
2019-06-04 19:25:17 +00:00
|
|
|
return nil, err
|
2019-06-04 19:57:13 +00:00
|
|
|
|
2018-09-21 21:29:50 +00:00
|
|
|
}
|
2019-11-22 23:29:24 +00:00
|
|
|
ass = append(ass, PackageAssert{Package: a.(*pkg.DefaultPackage), Value: v})
|
2018-09-21 21:29:50 +00:00
|
|
|
}
|
2019-12-13 22:05:09 +00:00
|
|
|
return ass, nil
|
2018-09-21 21:29:50 +00:00
|
|
|
}
|
2019-10-31 11:34:28 +00:00
|
|
|
|
|
|
|
func (a *PackageAssert) Explain() {
|
|
|
|
fmt.Println(a.ToString())
|
|
|
|
a.Package.Explain()
|
|
|
|
}
|
|
|
|
|
2019-11-09 13:59:20 +00:00
|
|
|
func (a *PackageAssert) String() string {
|
|
|
|
return a.ToString()
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:34:28 +00:00
|
|
|
func (a *PackageAssert) ToString() string {
|
|
|
|
var msg string
|
2019-11-29 18:01:47 +00:00
|
|
|
if a.Value {
|
2019-10-31 11:34:28 +00:00
|
|
|
msg = "installed"
|
|
|
|
} else {
|
|
|
|
msg = "not installed"
|
|
|
|
}
|
2019-11-29 18:01:47 +00:00
|
|
|
return fmt.Sprintf("%s/%s %s %s", a.Package.GetCategory(), a.Package.GetName(), a.Package.GetVersion(), msg)
|
2019-10-31 11:34:28 +00:00
|
|
|
}
|
2019-11-11 10:02:32 +00:00
|
|
|
|
2019-11-12 16:27:05 +00:00
|
|
|
func (assertions PackagesAssertions) EnsureOrder() PackagesAssertions {
|
2019-11-11 10:02:32 +00:00
|
|
|
|
|
|
|
orderedAssertions := PackagesAssertions{}
|
|
|
|
unorderedAssertions := PackagesAssertions{}
|
|
|
|
fingerprints := []string{}
|
|
|
|
|
|
|
|
tmpMap := map[string]PackageAssert{}
|
|
|
|
|
|
|
|
for _, a := range assertions {
|
2019-11-11 23:13:03 +00:00
|
|
|
tmpMap[a.Package.GetFingerPrint()] = a
|
2019-11-12 16:27:05 +00:00
|
|
|
fingerprints = append(fingerprints, a.Package.GetFingerPrint())
|
|
|
|
unorderedAssertions = append(unorderedAssertions, a) // Build a list of the ones that must be ordered
|
|
|
|
|
2019-11-29 18:01:41 +00:00
|
|
|
if a.Value {
|
2019-11-11 10:02:32 +00:00
|
|
|
unorderedAssertions = append(unorderedAssertions, a) // Build a list of the ones that must be ordered
|
|
|
|
} else {
|
|
|
|
orderedAssertions = append(orderedAssertions, a) // Keep last the ones which are not meant to be installed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:27:05 +00:00
|
|
|
sort.Sort(unorderedAssertions)
|
|
|
|
|
2019-11-11 10:02:32 +00:00
|
|
|
// Build a topological graph
|
|
|
|
graph := toposort.NewGraph(len(unorderedAssertions))
|
|
|
|
graph.AddNodes(fingerprints...)
|
|
|
|
for _, a := range unorderedAssertions {
|
|
|
|
for _, req := range a.Package.GetRequires() {
|
|
|
|
graph.AddEdge(a.Package.GetFingerPrint(), req.GetFingerPrint())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result, ok := graph.Toposort()
|
|
|
|
if !ok {
|
2019-11-12 16:27:05 +00:00
|
|
|
panic("Cycle found")
|
2019-11-11 10:02:32 +00:00
|
|
|
}
|
|
|
|
for _, res := range result {
|
|
|
|
a, ok := tmpMap[res]
|
|
|
|
if !ok {
|
2019-11-12 16:27:05 +00:00
|
|
|
panic("fail")
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
orderedAssertions = append(orderedAssertions, a)
|
|
|
|
// orderedAssertions = append(PackagesAssertions{a}, orderedAssertions...) // push upfront
|
|
|
|
}
|
|
|
|
//helpers.ReverseAny(orderedAssertions)
|
|
|
|
return orderedAssertions
|
|
|
|
}
|
|
|
|
|
2019-12-14 08:12:49 +00:00
|
|
|
func (assertions PackagesAssertions) SearchByName(f string) *PackageAssert {
|
|
|
|
for _, a := range assertions {
|
|
|
|
if a.Value {
|
|
|
|
if a.Package.GetPackageName() == f {
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-13 22:45:02 +00:00
|
|
|
func (assertions PackagesAssertions) Search(f string) *PackageAssert {
|
|
|
|
for _, a := range assertions {
|
|
|
|
if a.Value {
|
2019-12-14 08:12:49 +00:00
|
|
|
if a.Package.GetFingerPrint() == f {
|
2019-12-13 22:45:02 +00:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-14 14:00:16 +00:00
|
|
|
|
2020-04-09 15:07:57 +00:00
|
|
|
func (assertions PackagesAssertions) Order(definitiondb pkg.PackageDatabase, fingerprint string) (PackagesAssertions, error) {
|
2019-11-12 16:27:05 +00:00
|
|
|
|
|
|
|
orderedAssertions := PackagesAssertions{}
|
|
|
|
unorderedAssertions := PackagesAssertions{}
|
2019-12-13 16:19:09 +00:00
|
|
|
|
2019-12-13 22:05:09 +00:00
|
|
|
tmpMap := map[string]PackageAssert{}
|
2019-11-12 16:27:05 +00:00
|
|
|
graph := topsort.NewGraph()
|
|
|
|
for _, a := range assertions {
|
|
|
|
graph.AddNode(a.Package.GetFingerPrint())
|
|
|
|
tmpMap[a.Package.GetFingerPrint()] = a
|
|
|
|
unorderedAssertions = append(unorderedAssertions, a) // Build a list of the ones that must be ordered
|
2019-11-11 10:02:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:27:05 +00:00
|
|
|
sort.Sort(unorderedAssertions)
|
|
|
|
// Build a topological graph
|
|
|
|
for _, a := range unorderedAssertions {
|
2019-12-17 18:32:31 +00:00
|
|
|
currentPkg := a.Package
|
2020-11-03 16:20:52 +00:00
|
|
|
added := map[string]interface{}{}
|
2020-05-04 10:21:49 +00:00
|
|
|
REQUIRES:
|
2019-12-17 18:32:31 +00:00
|
|
|
for _, requiredDef := range currentPkg.GetRequires() {
|
|
|
|
if def, err := definitiondb.FindPackage(requiredDef); err == nil { // Provides: Get a chance of being override here
|
|
|
|
requiredDef = def.(*pkg.DefaultPackage)
|
|
|
|
}
|
|
|
|
|
2019-12-13 22:45:02 +00:00
|
|
|
// We cannot search for fingerprint, as we could have selector in versions.
|
2019-12-14 08:12:49 +00:00
|
|
|
// We know that the assertions are unique for packages, so look for a package with such name in the assertions
|
|
|
|
req := assertions.SearchByName(requiredDef.GetPackageName())
|
2019-12-13 22:45:02 +00:00
|
|
|
if req != nil {
|
|
|
|
requiredDef = req.Package
|
2019-12-13 22:05:09 +00:00
|
|
|
}
|
2020-05-04 15:34:29 +00:00
|
|
|
if _, ok := added[requiredDef.GetFingerPrint()]; ok {
|
2020-05-04 10:21:49 +00:00
|
|
|
continue REQUIRES
|
|
|
|
}
|
2019-11-29 18:01:52 +00:00
|
|
|
// Expand also here, as we need to order them (or instead the solver should give back the dep correctly?)
|
2019-12-17 18:32:31 +00:00
|
|
|
graph.AddEdge(currentPkg.GetFingerPrint(), requiredDef.GetFingerPrint())
|
2020-11-03 16:20:52 +00:00
|
|
|
added[requiredDef.GetFingerPrint()] = true
|
2019-11-12 16:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result, err := graph.TopSort(fingerprint)
|
|
|
|
if err != nil {
|
2020-04-09 15:07:57 +00:00
|
|
|
return nil, errors.Wrap(err, "fail on sorting "+fingerprint)
|
2019-11-12 16:27:05 +00:00
|
|
|
}
|
|
|
|
for _, res := range result {
|
2019-12-13 22:05:09 +00:00
|
|
|
a, ok := tmpMap[res]
|
|
|
|
if !ok {
|
2020-10-30 18:12:12 +00:00
|
|
|
//return nil, errors.New("fail looking for " + res)
|
|
|
|
// Since now we don't return the entire world as part of assertions
|
|
|
|
// if we don't find any reference must be because fingerprint we are analyzing (which is the one we are ordering against)
|
|
|
|
// is not part of the assertions, thus we can omit it from the result
|
|
|
|
continue
|
2019-11-12 16:27:05 +00:00
|
|
|
}
|
2019-12-13 22:05:09 +00:00
|
|
|
orderedAssertions = append(orderedAssertions, a)
|
2019-11-12 16:27:05 +00:00
|
|
|
// orderedAssertions = append(PackagesAssertions{a}, orderedAssertions...) // push upfront
|
|
|
|
}
|
|
|
|
//helpers.ReverseAny(orderedAssertions)
|
2020-04-09 15:07:57 +00:00
|
|
|
return orderedAssertions, nil
|
2019-11-11 10:02:32 +00:00
|
|
|
}
|
2019-11-11 13:58:12 +00:00
|
|
|
|
|
|
|
func (assertions PackagesAssertions) Explain() string {
|
|
|
|
var fingerprint string
|
2019-11-12 16:27:05 +00:00
|
|
|
for _, assertion := range assertions { // Always order them
|
2019-11-11 13:58:12 +00:00
|
|
|
fingerprint += assertion.ToString() + "\n"
|
|
|
|
}
|
|
|
|
return fingerprint
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:27:05 +00:00
|
|
|
func (a PackagesAssertions) Len() int { return len(a) }
|
|
|
|
func (a PackagesAssertions) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a PackagesAssertions) Less(i, j int) bool {
|
|
|
|
|
|
|
|
iRunes := []rune(a[i].Package.GetName())
|
|
|
|
jRunes := []rune(a[j].Package.GetName())
|
|
|
|
|
|
|
|
max := len(iRunes)
|
|
|
|
if max > len(jRunes) {
|
|
|
|
max = len(jRunes)
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := 0; idx < max; idx++ {
|
|
|
|
ir := iRunes[idx]
|
|
|
|
jr := jRunes[idx]
|
|
|
|
|
|
|
|
lir := unicode.ToLower(ir)
|
|
|
|
ljr := unicode.ToLower(jr)
|
|
|
|
|
|
|
|
if lir != ljr {
|
|
|
|
return lir < ljr
|
|
|
|
}
|
|
|
|
|
|
|
|
// the lowercase runes are the same, so compare the original
|
|
|
|
if ir != jr {
|
|
|
|
return ir < jr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:10:38 +00:00
|
|
|
func (a PackagesAssertions) TrueLen() int {
|
|
|
|
count := 0
|
|
|
|
for _, ass := range a {
|
|
|
|
if ass.Value {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashFrom computes the assertion hash From a given package. It drops it from the assertions
|
|
|
|
// and checks it's not the only one. if it's unique it marks it specially - so the hash
|
|
|
|
// which is generated is unique for the selected package
|
|
|
|
func (assertions PackagesAssertions) HashFrom(p pkg.Package) string {
|
2021-05-18 12:39:28 +00:00
|
|
|
return assertions.SaltedHashFrom(p, map[string]string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (assertions PackagesAssertions) AssertionHash() string {
|
|
|
|
return assertions.SaltedAssertionHash(map[string]string{})
|
|
|
|
}
|
2020-05-18 17:10:38 +00:00
|
|
|
|
2021-05-18 12:39:28 +00:00
|
|
|
func (assertions PackagesAssertions) SaltedHashFrom(p pkg.Package, salts map[string]string) string {
|
2020-05-18 17:10:38 +00:00
|
|
|
var assertionhash string
|
|
|
|
|
|
|
|
// When we don't have any solution to hash for, we need to generate an UUID by ourselves
|
|
|
|
latestsolution := assertions.Drop(p)
|
|
|
|
if latestsolution.TrueLen() == 0 {
|
2021-05-18 12:39:28 +00:00
|
|
|
// Preserve the hash if supplied of marked packages
|
|
|
|
marked := p.Mark()
|
|
|
|
if markedHash, exists := salts[p.GetFingerPrint()]; exists {
|
|
|
|
salts[marked.GetFingerPrint()] = markedHash
|
|
|
|
}
|
|
|
|
assertionhash = assertions.Mark(p).SaltedAssertionHash(salts)
|
2020-05-18 17:10:38 +00:00
|
|
|
} else {
|
2021-05-18 12:39:28 +00:00
|
|
|
assertionhash = latestsolution.SaltedAssertionHash(salts)
|
2020-05-18 17:10:38 +00:00
|
|
|
}
|
|
|
|
return assertionhash
|
|
|
|
}
|
|
|
|
|
2021-05-18 12:39:28 +00:00
|
|
|
func (assertions PackagesAssertions) SaltedAssertionHash(salts map[string]string) string {
|
2019-11-11 13:58:12 +00:00
|
|
|
var fingerprint string
|
2019-11-12 16:27:05 +00:00
|
|
|
for _, assertion := range assertions { // Note: Always order them first!
|
2019-11-29 18:01:41 +00:00
|
|
|
if assertion.Value { // Tke into account only dependencies installed (get fingerprint of subgraph)
|
2021-05-18 12:39:28 +00:00
|
|
|
salt, exists := salts[assertion.Package.GetFingerPrint()]
|
|
|
|
if exists {
|
|
|
|
fingerprint += assertion.ToString() + salt + "\n"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fingerprint += assertion.ToString() + "\n"
|
|
|
|
}
|
2019-11-11 13:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hash := sha256.Sum256([]byte(fingerprint))
|
|
|
|
return fmt.Sprintf("%x", hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (assertions PackagesAssertions) Drop(p pkg.Package) PackagesAssertions {
|
|
|
|
ass := PackagesAssertions{}
|
|
|
|
|
|
|
|
for _, a := range assertions {
|
2019-11-15 17:04:46 +00:00
|
|
|
if !a.Package.Matches(p) {
|
2019-11-11 13:58:12 +00:00
|
|
|
ass = append(ass, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ass
|
|
|
|
}
|
2019-12-14 14:00:16 +00:00
|
|
|
|
|
|
|
// Cut returns an assertion list of installed (filter by Value) "cutted" until the package is found (included)
|
|
|
|
func (assertions PackagesAssertions) Cut(p pkg.Package) PackagesAssertions {
|
|
|
|
ass := PackagesAssertions{}
|
|
|
|
|
|
|
|
for _, a := range assertions {
|
|
|
|
if a.Value {
|
|
|
|
ass = append(ass, a)
|
|
|
|
if a.Package.Matches(p) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ass
|
|
|
|
}
|
2020-05-18 17:10:38 +00:00
|
|
|
|
|
|
|
// Mark returns a new assertion with the package marked
|
|
|
|
func (assertions PackagesAssertions) Mark(p pkg.Package) PackagesAssertions {
|
|
|
|
ass := PackagesAssertions{}
|
|
|
|
|
|
|
|
for _, a := range assertions {
|
|
|
|
if a.Package.Matches(p) {
|
2021-05-18 12:39:28 +00:00
|
|
|
marked := a.Package.Mark()
|
2020-05-18 17:10:38 +00:00
|
|
|
a = PackageAssert{Package: marked.(*pkg.DefaultPackage), Value: a.Value, Hash: a.Hash}
|
|
|
|
}
|
|
|
|
ass = append(ass, a)
|
|
|
|
}
|
|
|
|
return ass
|
|
|
|
}
|