Merge pull request #112 from mudler/package-sanitized

Package sanitized
This commit is contained in:
geaaru
2020-05-20 11:42:47 +02:00
committed by GitHub
6 changed files with 232 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ import (
//"sort"
. "github.com/mudler/luet/pkg/logger"
spectooling "github.com/mudler/luet/pkg/spectooling"
tree "github.com/mudler/luet/pkg/tree"
"github.com/spf13/cobra"
@@ -54,7 +55,7 @@ func NewTreeBumpCommand() *cobra.Command {
}
if toStdout {
data, err := pack.Yaml()
data, err := spectooling.NewDefaultPackageSanitized(&pack).Yaml()
if err != nil {
Fatal("Error on yaml conversion: " + err.Error())
}

View File

@@ -26,11 +26,12 @@ import (
"strconv"
"strings"
version "github.com/mudler/luet/pkg/versioner"
gentoo "github.com/Sabayon/pkgs-checker/pkg/gentoo"
"github.com/crillab/gophersat/bf"
"github.com/ghodss/yaml"
"github.com/jinzhu/copier"
version "github.com/mudler/luet/pkg/versioner"
"github.com/pkg/errors"
)

View File

@@ -0,0 +1,106 @@
// Copyright © 2019-2020 Ettore Di Giacinto <mudler@gentoo.org>,
// Daniele Rondina <geaaru@sabayonlinux.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 spectooling
import (
pkg "github.com/mudler/luet/pkg/package"
"gopkg.in/yaml.v2"
)
type DefaultPackageSanitized struct {
Name string `json:"name" yaml:"name"`
Version string `json:"version" yaml:"version"`
Category string `json:"category" yaml:"category"`
UseFlags []string `json:"use_flags,omitempty" yaml:"use_flags,omitempty"`
PackageRequires []*DefaultPackageSanitized `json:"requires,omitempty" yaml:"requires,omitempty"`
PackageConflicts []*DefaultPackageSanitized `json:"conflicts,omitempty" yaml:"conflicts,omitempty"`
IsSet bool `json:"set,omitempty" yaml:"set,omitempty"`
Provides []*DefaultPackageSanitized `json:"provides,omitempty" yaml:"provides,omitempty"`
// Path is set only internally when tree is loaded from disk
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Uri []string `json:"uri,omitempty" yaml:"uri,omitempty"`
License string `json:"license,omitempty" yaml:"license,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
}
func NewDefaultPackageSanitized(p pkg.Package) *DefaultPackageSanitized {
ans := &DefaultPackageSanitized{
Name: p.GetName(),
Version: p.GetVersion(),
Category: p.GetCategory(),
UseFlags: p.GetUses(),
IsSet: p.Flagged(),
Path: p.GetPath(),
Description: p.GetDescription(),
Uri: p.GetURI(),
License: p.GetLicense(),
Labels: p.GetLabels(),
}
if p.GetRequires() != nil && len(p.GetRequires()) > 0 {
ans.PackageRequires = []*DefaultPackageSanitized{}
for _, r := range p.GetRequires() {
// I avoid recursive call of NewDefaultPackageSanitized
ans.PackageRequires = append(ans.PackageRequires,
&DefaultPackageSanitized{
Name: r.Name,
Version: r.Version,
Category: r.Category,
},
)
}
}
if p.GetConflicts() != nil && len(p.GetConflicts()) > 0 {
ans.PackageConflicts = []*DefaultPackageSanitized{}
for _, c := range p.GetConflicts() {
// I avoid recursive call of NewDefaultPackageSanitized
ans.PackageConflicts = append(ans.PackageConflicts,
&DefaultPackageSanitized{
Name: c.Name,
Version: c.Version,
Category: c.Category,
},
)
}
}
if p.GetProvides() != nil && len(p.GetProvides()) > 0 {
ans.Provides = []*DefaultPackageSanitized{}
for _, prov := range p.GetProvides() {
// I avoid recursive call of NewDefaultPackageSanitized
ans.Provides = append(ans.Provides,
&DefaultPackageSanitized{
Name: prov.Name,
Version: prov.Version,
Category: prov.Category,
},
)
}
}
return ans
}
func (p *DefaultPackageSanitized) Yaml() ([]byte, error) {
return yaml.Marshal(p)
}

View File

@@ -0,0 +1,87 @@
// Copyright © 2019-2020 Ettore Di Giacinto <mudler@gentoo.org>,
// Daniele Rondina <geaaru@sabayonlinux.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 spectooling_test
import (
pkg "github.com/mudler/luet/pkg/package"
. "github.com/mudler/luet/pkg/spectooling"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Spec Tooling", func() {
Context("Conversion1", func() {
b := pkg.NewPackage("B", "1.0", []*pkg.DefaultPackage{}, []*pkg.DefaultPackage{})
c := pkg.NewPackage("C", "1.0", []*pkg.DefaultPackage{}, []*pkg.DefaultPackage{})
d := pkg.NewPackage("D", "1.0", []*pkg.DefaultPackage{}, []*pkg.DefaultPackage{})
p1 := pkg.NewPackage("A", "1.0", []*pkg.DefaultPackage{b, c}, []*pkg.DefaultPackage{d})
virtual := pkg.NewPackage("E", "1.0", []*pkg.DefaultPackage{}, []*pkg.DefaultPackage{})
virtual.SetCategory("virtual")
p1.Provides = []*pkg.DefaultPackage{virtual}
p1.AddLabel("label1", "value1")
p1.AddLabel("label2", "value2")
p1.SetDescription("Package1")
p1.SetCategory("cat1")
p1.SetLicense("GPL")
p1.AddURI("https://github.com/mudler/luet")
p1.AddUse("systemd")
It("Convert pkg1", func() {
res := NewDefaultPackageSanitized(p1)
expected_res := &DefaultPackageSanitized{
Name: "A",
Version: "1.0",
Category: "cat1",
PackageRequires: []*DefaultPackageSanitized{
&DefaultPackageSanitized{
Name: "B",
Version: "1.0",
},
&DefaultPackageSanitized{
Name: "C",
Version: "1.0",
},
},
PackageConflicts: []*DefaultPackageSanitized{
&DefaultPackageSanitized{
Name: "D",
Version: "1.0",
},
},
Provides: []*DefaultPackageSanitized{
&DefaultPackageSanitized{
Name: "E",
Category: "virtual",
Version: "1.0",
},
},
Labels: map[string]string{
"label1": "value1",
"label2": "value2",
},
Description: "Package1",
License: "GPL",
Uri: []string{"https://github.com/mudler/luet"},
UseFlags: []string{"systemd"},
}
Expect(res).To(Equal(expected_res))
})
})
})

View File

@@ -0,0 +1,33 @@
// Copyright © 2019-2020 Ettore Di Giacinto <mudler@gentoo.org>,
// Daniele Rondina <geaaru@sabayonlinux.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 spectooling_test
import (
"testing"
. "github.com/mudler/luet/cmd"
config "github.com/mudler/luet/pkg/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestSolver(t *testing.T) {
RegisterFailHandler(Fail)
LoadConfig(config.LuetCfg)
RunSpecs(t, "Spec Tooling Suite")
}

View File

@@ -26,6 +26,7 @@ import (
"path/filepath"
pkg "github.com/mudler/luet/pkg/package"
spectooling "github.com/mudler/luet/pkg/spectooling"
"github.com/pkg/errors"
)
@@ -42,7 +43,7 @@ type Recipe struct {
}
func WriteDefinitionFile(p pkg.Package, definitionFilePath string) error {
data, err := p.Yaml()
data, err := spectooling.NewDefaultPackageSanitized(p).Yaml()
if err != nil {
return err
}