2020-03-14 10:13:48 +00:00
|
|
|
// Copyright © 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 cmd_tree
|
|
|
|
|
|
|
|
import (
|
2020-04-30 19:48:46 +00:00
|
|
|
"errors"
|
2020-03-14 10:13:48 +00:00
|
|
|
"fmt"
|
2020-03-14 16:38:57 +00:00
|
|
|
"os"
|
2020-04-30 19:48:46 +00:00
|
|
|
"regexp"
|
2020-03-14 10:13:48 +00:00
|
|
|
"sort"
|
2020-04-30 19:48:46 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-05-23 06:51:33 +00:00
|
|
|
helpers "github.com/mudler/luet/cmd/helpers"
|
2020-04-30 19:48:46 +00:00
|
|
|
. "github.com/mudler/luet/pkg/config"
|
2020-03-14 10:13:48 +00:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
|
|
|
"github.com/mudler/luet/pkg/solver"
|
|
|
|
tree "github.com/mudler/luet/pkg/tree"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
func validateWorker(i int,
|
|
|
|
wg *sync.WaitGroup,
|
|
|
|
c <-chan pkg.Package,
|
|
|
|
reciper tree.Builder,
|
|
|
|
withSolver bool,
|
|
|
|
regExcludes, regMatches []*regexp.Regexp,
|
|
|
|
excludes, matches []string,
|
|
|
|
errs chan error) {
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var depSolver solver.PackageSolver
|
2020-05-08 18:05:21 +00:00
|
|
|
var cacheDeps *pkg.InMemoryDatabase
|
2020-04-30 19:48:46 +00:00
|
|
|
brokenPkgs := 0
|
|
|
|
brokenDeps := 0
|
|
|
|
var errstr string
|
|
|
|
|
|
|
|
emptyInstallationDb := pkg.NewInMemoryDatabase(false)
|
|
|
|
if withSolver {
|
|
|
|
depSolver = solver.NewSolver(pkg.NewInMemoryDatabase(false),
|
|
|
|
reciper.GetDatabase(),
|
|
|
|
emptyInstallationDb)
|
2020-05-08 18:05:21 +00:00
|
|
|
|
|
|
|
// Use Singleton in memory cache for speedup dependencies
|
|
|
|
// analysis
|
|
|
|
cacheDeps = pkg.NewInMemoryDatabase(true).(*pkg.InMemoryDatabase)
|
2020-04-30 19:48:46 +00:00
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
for p := range c {
|
2020-05-08 18:05:21 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
found, err := reciper.GetDatabase().FindPackages(
|
|
|
|
&pkg.DefaultPackage{
|
|
|
|
Name: p.GetName(),
|
|
|
|
Category: p.GetCategory(),
|
|
|
|
Version: ">=0",
|
|
|
|
},
|
|
|
|
)
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if err != nil || len(found) < 1 {
|
|
|
|
if err != nil {
|
|
|
|
errstr = err.Error()
|
|
|
|
} else {
|
|
|
|
errstr = "No packages"
|
2020-03-14 10:13:48 +00:00
|
|
|
}
|
2020-04-30 19:48:46 +00:00
|
|
|
Error(fmt.Sprintf("%s/%s-%s: Broken. No versions could be found by database %s",
|
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
|
|
|
errstr,
|
|
|
|
))
|
|
|
|
|
|
|
|
errs <- errors.New(
|
|
|
|
fmt.Sprintf("%s/%s-%s: Broken. No versions could be found by database %s",
|
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
|
|
|
errstr,
|
|
|
|
))
|
|
|
|
|
|
|
|
brokenPkgs++
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgstr := fmt.Sprintf("%s/%s-%s", p.GetCategory(), p.GetName(),
|
|
|
|
p.GetVersion())
|
|
|
|
|
|
|
|
validpkg := true
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
matched := false
|
|
|
|
for _, rgx := range regMatches {
|
|
|
|
if rgx.MatchString(pkgstr) {
|
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if !matched {
|
|
|
|
continue
|
2020-03-14 10:13:48 +00:00
|
|
|
}
|
2020-04-30 19:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(excludes) > 0 {
|
|
|
|
excluded := false
|
|
|
|
for _, rgx := range regExcludes {
|
|
|
|
if rgx.MatchString(pkgstr) {
|
|
|
|
excluded = true
|
|
|
|
break
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if excluded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 18:05:21 +00:00
|
|
|
|
|
|
|
Info("Checking package "+
|
|
|
|
fmt.Sprintf("%s/%s-%s", p.GetCategory(), p.GetName(), p.GetVersion()),
|
|
|
|
"with", len(p.GetRequires()), "dependencies and", len(p.GetConflicts()), "conflicts.")
|
2020-04-30 19:48:46 +00:00
|
|
|
|
|
|
|
all := p.GetRequires()
|
|
|
|
all = append(all, p.GetConflicts()...)
|
2020-05-08 18:05:21 +00:00
|
|
|
for idx, r := range all {
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
var deps pkg.Packages
|
|
|
|
var err error
|
|
|
|
if r.IsSelector() {
|
|
|
|
deps, err = reciper.GetDatabase().FindPackages(
|
2020-03-29 10:06:14 +00:00
|
|
|
&pkg.DefaultPackage{
|
2020-04-30 19:48:46 +00:00
|
|
|
Name: r.GetName(),
|
|
|
|
Category: r.GetCategory(),
|
|
|
|
Version: r.GetVersion(),
|
2020-03-29 10:06:14 +00:00
|
|
|
},
|
|
|
|
)
|
2020-04-30 19:48:46 +00:00
|
|
|
} else {
|
|
|
|
deps = append(deps, r)
|
|
|
|
}
|
2020-03-29 10:06:14 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if err != nil || len(deps) < 1 {
|
|
|
|
if err != nil {
|
|
|
|
errstr = err.Error()
|
|
|
|
} else {
|
|
|
|
errstr = "No packages"
|
|
|
|
}
|
|
|
|
Error(fmt.Sprintf("%s/%s-%s: Broken Dep %s/%s-%s - %s",
|
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
|
|
|
r.GetCategory(), r.GetName(), r.GetVersion(),
|
|
|
|
errstr,
|
|
|
|
))
|
|
|
|
|
|
|
|
errs <- errors.New(
|
|
|
|
fmt.Sprintf("%s/%s-%s: Broken Dep %s/%s-%s - %s",
|
2020-03-29 10:06:14 +00:00
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
2020-04-30 19:48:46 +00:00
|
|
|
r.GetCategory(), r.GetName(), r.GetVersion(),
|
|
|
|
errstr))
|
2020-03-29 10:06:14 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
brokenDeps++
|
2020-03-29 10:06:14 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
validpkg = false
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
} else {
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
Debug("Find packages for dep",
|
|
|
|
fmt.Sprintf("%s/%s-%s", r.GetCategory(), r.GetName(), r.GetVersion()))
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if withSolver {
|
2020-05-08 18:05:21 +00:00
|
|
|
|
|
|
|
Info(fmt.Sprintf(" :soap: [%2d/%2d] %s/%s-%s: %s/%s-%s",
|
|
|
|
idx+1, len(all),
|
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
|
|
|
r.GetCategory(), r.GetName(), r.GetVersion(),
|
|
|
|
))
|
|
|
|
|
|
|
|
// Check if the solver is already been done for the deep
|
|
|
|
_, err := cacheDeps.Get(r.HashFingerprint())
|
|
|
|
if err == nil {
|
|
|
|
Debug(" :direct_hit: Cache Hit for dep",
|
|
|
|
fmt.Sprintf("%s/%s-%s", r.GetCategory(), r.GetName(), r.GetVersion()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
Spinner(32)
|
|
|
|
solution, err := depSolver.Install(pkg.Packages{r})
|
|
|
|
ass := solution.SearchByName(r.GetPackageName())
|
|
|
|
if err == nil {
|
|
|
|
_, err = solution.Order(reciper.GetDatabase(), ass.Package.GetFingerPrint())
|
2020-03-14 10:13:48 +00:00
|
|
|
}
|
2020-04-30 19:48:46 +00:00
|
|
|
SpinnerStop()
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if err != nil {
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
Error(fmt.Sprintf("%s/%s-%s: solver broken for dep %s/%s-%s - %s",
|
2020-03-14 10:13:48 +00:00
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
|
|
|
r.GetCategory(), r.GetName(), r.GetVersion(),
|
2020-04-30 19:48:46 +00:00
|
|
|
err.Error(),
|
2020-03-14 10:13:48 +00:00
|
|
|
))
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
errs <- errors.New(
|
|
|
|
fmt.Sprintf("%s/%s-%s: solver broken for Dep %s/%s-%s - %s",
|
2020-03-14 10:13:48 +00:00
|
|
|
p.GetCategory(), p.GetName(), p.GetVersion(),
|
|
|
|
r.GetCategory(), r.GetName(), r.GetVersion(),
|
2020-04-30 19:48:46 +00:00
|
|
|
err.Error()))
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-03-15 16:40:10 +00:00
|
|
|
brokenDeps++
|
2020-03-14 10:13:48 +00:00
|
|
|
validpkg = false
|
2020-04-30 19:48:46 +00:00
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-05-08 18:05:21 +00:00
|
|
|
// Register the key
|
|
|
|
cacheDeps.Set(r.HashFingerprint(), "1")
|
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
if !validpkg {
|
|
|
|
brokenPkgs++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
func NewTreeValidateCommand() *cobra.Command {
|
|
|
|
var excludes []string
|
|
|
|
var matches []string
|
|
|
|
var treePaths []string
|
|
|
|
|
|
|
|
var ans = &cobra.Command{
|
|
|
|
Use: "validate [OPTIONS]",
|
|
|
|
Short: "Validate a tree or a list of packages",
|
|
|
|
Args: cobra.OnlyValidArgs,
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(treePaths) < 1 {
|
|
|
|
Fatal("Mandatory tree param missing.")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
concurrency := LuetCfg.GetGeneral().Concurrency
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
withSolver, _ := cmd.Flags().GetBool("with-solver")
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
reciper := tree.NewInstallerRecipe(pkg.NewInMemoryDatabase(false))
|
|
|
|
for _, treePath := range treePaths {
|
|
|
|
err := reciper.Load(treePath)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Error on load tree ", err)
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
regExcludes, err := helpers.CreateRegexArray(excludes)
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err.Error())
|
|
|
|
}
|
|
|
|
regMatches, err := helpers.CreateRegexArray(matches)
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err.Error())
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
all := make(chan pkg.Package)
|
|
|
|
errs := make(chan error)
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
var wg = new(sync.WaitGroup)
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go validateWorker(i, wg, all,
|
|
|
|
reciper, withSolver, regExcludes, regMatches, excludes, matches,
|
|
|
|
errs)
|
|
|
|
}
|
|
|
|
for _, p := range reciper.GetDatabase().World() {
|
|
|
|
all <- p
|
2020-03-14 10:13:48 +00:00
|
|
|
}
|
2020-04-30 19:48:46 +00:00
|
|
|
close(all)
|
|
|
|
|
|
|
|
// Wait separately and once done close the channel
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(errs)
|
|
|
|
}()
|
2020-03-14 10:13:48 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
stringerrs := []string{}
|
|
|
|
for e := range errs {
|
|
|
|
stringerrs = append(stringerrs, e.Error())
|
|
|
|
}
|
|
|
|
sort.Strings(stringerrs)
|
|
|
|
for _, e := range stringerrs {
|
2020-03-14 10:13:48 +00:00
|
|
|
fmt.Println(e)
|
|
|
|
}
|
2020-03-14 16:38:57 +00:00
|
|
|
|
2020-04-30 19:48:46 +00:00
|
|
|
// fmt.Println("Broken packages:", brokenPkgs, "(", brokenDeps, "deps ).")
|
|
|
|
if len(stringerrs) != 0 {
|
|
|
|
Fatal("Errors: " + strconv.Itoa(len(stringerrs)))
|
|
|
|
// if brokenPkgs > 0 {
|
|
|
|
//os.Exit(1)
|
2020-03-14 16:38:57 +00:00
|
|
|
} else {
|
2020-04-30 19:48:46 +00:00
|
|
|
Info("All good! :white_check_mark:")
|
2020-03-14 16:38:57 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2020-03-14 10:13:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ans.Flags().BoolP("with-solver", "s", false,
|
|
|
|
"Enable check of requires also with solver.")
|
2020-03-15 18:16:22 +00:00
|
|
|
ans.Flags().StringSliceVarP(&treePaths, "tree", "t", []string{},
|
|
|
|
"Path of the tree to use.")
|
2020-03-14 10:13:48 +00:00
|
|
|
ans.Flags().StringSliceVarP(&excludes, "exclude", "e", []string{},
|
|
|
|
"Exclude matched packages from analysis. (Use string as regex).")
|
|
|
|
ans.Flags().StringSliceVarP(&matches, "matches", "m", []string{},
|
|
|
|
"Analyze only matched packages. (Use string as regex).")
|
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|