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-06-05 17:13:09 +00:00
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
2019-11-11 10:02:32 +00:00
|
|
|
toposort "github.com/philopon/go-toposort"
|
2018-09-21 21:29:50 +00:00
|
|
|
)
|
|
|
|
|
2019-11-11 10:02:32 +00:00
|
|
|
type PackagesAssertions []PackageAssert
|
|
|
|
|
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 {
|
|
|
|
Package pkg.Package
|
|
|
|
Value bool
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:03:50 +00:00
|
|
|
// DecodeModel decodes a model from the SAT solver to package assertions (PackageAssert)
|
2019-11-11 10:02:32 +00:00
|
|
|
func DecodeModel(model map[string]bool) (PackagesAssertions, error) {
|
|
|
|
ass := make(PackagesAssertions, 0)
|
2018-09-21 21:29:50 +00:00
|
|
|
for k, v := range model {
|
2019-06-04 19:57:13 +00:00
|
|
|
a, err := pkg.DecodePackage(k)
|
|
|
|
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-06-04 19:57:13 +00:00
|
|
|
ass = append(ass, PackageAssert{Package: a, Value: v})
|
2018-09-21 21:29:50 +00:00
|
|
|
}
|
2019-06-04 19:25:17 +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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *PackageAssert) ToString() string {
|
|
|
|
var msg string
|
|
|
|
if a.Package.Flagged() {
|
|
|
|
msg = "installed"
|
|
|
|
} else {
|
|
|
|
msg = "not installed"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s %s %s: %t", a.Package.GetCategory(), a.Package.GetName(), a.Package.GetVersion(), msg, a.Value)
|
|
|
|
}
|
2019-11-11 10:02:32 +00:00
|
|
|
|
|
|
|
func (assertions PackagesAssertions) Order() PackagesAssertions {
|
|
|
|
|
|
|
|
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-11 10:02:32 +00:00
|
|
|
if a.Package.Flagged() {
|
|
|
|
unorderedAssertions = append(unorderedAssertions, a) // Build a list of the ones that must be ordered
|
|
|
|
fingerprints = append(fingerprints, a.Package.GetFingerPrint())
|
|
|
|
} else {
|
|
|
|
orderedAssertions = append(orderedAssertions, a) // Keep last the ones which are not meant to be installed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
panic("cycle detected")
|
|
|
|
}
|
|
|
|
for _, res := range result {
|
|
|
|
a, ok := tmpMap[res]
|
|
|
|
if !ok {
|
2019-11-11 23:13:03 +00:00
|
|
|
continue
|
2019-11-11 10:02:32 +00:00
|
|
|
}
|
2019-11-11 23:13:03 +00:00
|
|
|
orderedAssertions = append(PackagesAssertions{a}, orderedAssertions...) // push upfront
|
2019-11-11 10:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return orderedAssertions
|
|
|
|
}
|
2019-11-11 13:58:12 +00:00
|
|
|
|
|
|
|
func (assertions PackagesAssertions) Explain() string {
|
|
|
|
var fingerprint string
|
|
|
|
for _, assertion := range assertions.Order() { // Always order them
|
|
|
|
fingerprint += assertion.ToString() + "\n"
|
|
|
|
}
|
|
|
|
return fingerprint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (assertions PackagesAssertions) AssertionHash() string {
|
|
|
|
var fingerprint string
|
|
|
|
for _, assertion := range assertions.Order() { // Always order them
|
|
|
|
if assertion.Value && assertion.Package.Flagged() { // Tke into account only dependencies installed (get fingerprint of subgraph)
|
|
|
|
fingerprint += assertion.ToString() + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash := sha256.Sum256([]byte(fingerprint))
|
|
|
|
return fmt.Sprintf("%x", hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (assertions PackagesAssertions) Drop(p pkg.Package) PackagesAssertions {
|
|
|
|
ass := PackagesAssertions{}
|
|
|
|
|
|
|
|
for _, a := range assertions {
|
|
|
|
if a.Package.GetFingerPrint() != p.GetFingerPrint() {
|
|
|
|
ass = append(ass, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ass
|
|
|
|
}
|