mirror of
https://github.com/mudler/luet.git
synced 2025-09-13 13:50:26 +00:00
Enhance Uninstall and resolve conflict sets
Compute a minimum conflict set over the uninstall. Adds also tests for specific cases covered by Uninstall now
This commit is contained in:
@@ -15,4 +15,6 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// Database is a merely simple in-memory db.
|
||||
// FIXME: Use a proper structure or delegate to third-party
|
||||
var Database map[string]string = map[string]string{}
|
||||
|
@@ -23,10 +23,13 @@ import (
|
||||
"hash/crc32"
|
||||
|
||||
"github.com/crillab/gophersat/bf"
|
||||
version "github.com/hashicorp/go-version"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
// Package is a package interface (TBD)
|
||||
// FIXME: Currently some of the methods are returning DefaultPackages due to JSON serialization of the package
|
||||
type Package interface {
|
||||
Encode() (string, error)
|
||||
SetState(state State) Package
|
||||
@@ -39,8 +42,14 @@ type Package interface {
|
||||
|
||||
GetRequires() []*DefaultPackage
|
||||
GetConflicts() []*DefaultPackage
|
||||
Expand([]Package) ([]Package, error)
|
||||
|
||||
GetName() string
|
||||
GetVersion() string
|
||||
RequiresContains(Package) bool
|
||||
}
|
||||
|
||||
// DefaultPackage represent a standard package definition
|
||||
type DefaultPackage struct {
|
||||
Name string
|
||||
Version string
|
||||
@@ -51,16 +60,21 @@ type DefaultPackage struct {
|
||||
IsSet bool
|
||||
}
|
||||
|
||||
type PackageUse []string
|
||||
// State represent the package state
|
||||
type State string
|
||||
|
||||
// NewPackage returns a new package
|
||||
func NewPackage(name, version string, requires []*DefaultPackage, conflicts []*DefaultPackage) *DefaultPackage {
|
||||
return &DefaultPackage{Name: name, Version: version, PackageRequires: requires, PackageConflicts: conflicts}
|
||||
}
|
||||
|
||||
// GetFingerPrint returns a UUID of the package.
|
||||
// FIXME: this needs to be unique, now just name is generalized
|
||||
func (p *DefaultPackage) GetFingerPrint() string {
|
||||
return p.Name
|
||||
}
|
||||
|
||||
// AddUse adds a use to a package
|
||||
func (p *DefaultPackage) AddUse(use string) {
|
||||
for _, v := range p.UseFlags {
|
||||
if v == use {
|
||||
@@ -70,6 +84,7 @@ func (p *DefaultPackage) AddUse(use string) {
|
||||
p.UseFlags = append(p.UseFlags, use)
|
||||
}
|
||||
|
||||
// RemoveUse removes a use to a package
|
||||
func (p *DefaultPackage) RemoveUse(use string) {
|
||||
|
||||
for i := len(p.UseFlags) - 1; i >= 0; i-- {
|
||||
@@ -80,6 +95,8 @@ func (p *DefaultPackage) RemoveUse(use string) {
|
||||
|
||||
}
|
||||
|
||||
// Encode encodes the package to string.
|
||||
// It returns an ID which can be used to retrieve the package later on.
|
||||
func (p *DefaultPackage) Encode() (string, error) {
|
||||
res, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
@@ -103,7 +120,12 @@ func (p *DefaultPackage) IsFlagged(b bool) Package {
|
||||
func (p *DefaultPackage) Flagged() bool {
|
||||
return p.IsSet
|
||||
}
|
||||
|
||||
func (p *DefaultPackage) GetName() string {
|
||||
return p.Name
|
||||
}
|
||||
func (p *DefaultPackage) GetVersion() string {
|
||||
return p.Version
|
||||
}
|
||||
func (p *DefaultPackage) SetState(state State) Package {
|
||||
p.State = state
|
||||
return p
|
||||
@@ -128,6 +150,30 @@ func (p *DefaultPackage) Clone() Package {
|
||||
return new
|
||||
}
|
||||
|
||||
func (p *DefaultPackage) Expand(world []Package) ([]Package, error) {
|
||||
|
||||
var versionsInWorld []Package
|
||||
for _, w := range world {
|
||||
if w.GetName() == p.GetName() {
|
||||
|
||||
v, err := version.NewVersion(w.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
constraints, err := version.NewConstraint(p.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if constraints.Check(v) {
|
||||
versionsInWorld = append(versionsInWorld, w)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return versionsInWorld, nil
|
||||
}
|
||||
|
||||
func DecodePackage(ID string) (Package, error) {
|
||||
|
||||
pa, ok := Database[ID]
|
||||
@@ -158,6 +204,20 @@ func NormalizeFlagged(p Package) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *DefaultPackage) RequiresContains(s Package) bool {
|
||||
for _, re := range p.GetRequires() {
|
||||
if re.GetFingerPrint() == s.GetFingerPrint() {
|
||||
return true
|
||||
}
|
||||
|
||||
if re.RequiresContains(s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *DefaultPackage) BuildFormula() ([]bf.Formula, error) {
|
||||
encodedA, err := p.IsFlagged(true).Encode()
|
||||
if err != nil {
|
||||
|
28
pkg/package/package_suite_test.go
Normal file
28
pkg/package/package_suite_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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 pkg_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestSolver(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Package Suite")
|
||||
}
|
39
pkg/package/package_test.go
Normal file
39
pkg/package/package_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// 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 pkg_test
|
||||
|
||||
import (
|
||||
. "github.com/mudler/luet/pkg/package"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Package", func() {
|
||||
Context("Simple package", func() {
|
||||
a := NewPackage("A", ">=1.0", []*DefaultPackage{}, []*DefaultPackage{})
|
||||
a1 := NewPackage("A", "1.0", []*DefaultPackage{}, []*DefaultPackage{})
|
||||
a11 := NewPackage("A", "1.1", []*DefaultPackage{}, []*DefaultPackage{})
|
||||
a01 := NewPackage("A", "0.1", []*DefaultPackage{}, []*DefaultPackage{})
|
||||
It("Expands correctly", func() {
|
||||
lst, err := a.Expand([]Package{a1, a11, a01})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(lst).To(ContainElement(a11))
|
||||
Expect(lst).To(ContainElement(a1))
|
||||
Expect(lst).ToNot(ContainElement(a01))
|
||||
Expect(len(lst)).To(Equal(2))
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user