2019-07-27 11:30:44 +02: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 gentoo
|
|
|
|
|
|
|
|
// NOTE: Look here as an example of the builder definition executor
|
|
|
|
// https://gist.github.com/adnaan/6ca68c7985c6f851def3
|
|
|
|
|
|
|
|
import (
|
2019-11-03 12:03:26 +01:00
|
|
|
"context"
|
2019-07-27 11:30:44 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-11-02 18:02:55 +01:00
|
|
|
"strconv"
|
2019-07-27 11:30:44 +02:00
|
|
|
"strings"
|
2019-11-01 12:03:48 +01:00
|
|
|
"sync"
|
2019-11-03 12:03:26 +01:00
|
|
|
"time"
|
2019-07-27 11:30:44 +02:00
|
|
|
|
2019-11-01 16:26:03 +01:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
2019-07-27 11:30:44 +02:00
|
|
|
tree "github.com/mudler/luet/pkg/tree"
|
|
|
|
|
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
|
|
|
)
|
|
|
|
|
2019-11-01 12:41:31 +01:00
|
|
|
func NewGentooBuilder(e EbuildParser, concurrency int) tree.Parser {
|
|
|
|
return &GentooBuilder{EbuildParser: e, Concurrency: concurrency}
|
2019-07-27 11:30:44 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 12:41:31 +01:00
|
|
|
type GentooBuilder struct {
|
|
|
|
EbuildParser EbuildParser
|
|
|
|
Concurrency int
|
|
|
|
}
|
2019-07-27 11:30:44 +02:00
|
|
|
|
2019-10-28 17:12:29 +01:00
|
|
|
type GentooTree struct {
|
2019-10-30 17:52:57 +01:00
|
|
|
*tree.DefaultTree
|
2019-10-28 17:12:29 +01:00
|
|
|
}
|
2019-07-27 11:30:44 +02:00
|
|
|
|
|
|
|
type EbuildParser interface {
|
2019-10-30 17:52:57 +01:00
|
|
|
ScanEbuild(string, pkg.Tree) ([]pkg.Package, error)
|
2019-07-27 11:30:44 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 17:12:29 +01:00
|
|
|
func (gt *GentooTree) Prelude() string {
|
|
|
|
return "/usr/portage/"
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:03:48 +01:00
|
|
|
func (gb *GentooBuilder) scanEbuild(path string, t pkg.Tree) error {
|
2019-11-02 18:02:55 +01:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
Error(r)
|
|
|
|
}
|
|
|
|
}()
|
2019-11-01 12:03:48 +01:00
|
|
|
pkgs, err := gb.EbuildParser.ScanEbuild(path, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, p := range pkgs {
|
|
|
|
_, err := t.GetPackageSet().FindPackage(p)
|
|
|
|
if err != nil {
|
|
|
|
_, err := t.GetPackageSet().CreatePackage(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-03 12:03:26 +01:00
|
|
|
func (gb *GentooBuilder) worker(ctx context.Context, i int, wg *sync.WaitGroup, s <-chan string, t pkg.Tree) {
|
2019-11-01 12:03:48 +01:00
|
|
|
defer wg.Done()
|
2019-11-03 12:03:26 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
Info("Worker is done!")
|
|
|
|
return
|
|
|
|
case path, ok := <-s:
|
|
|
|
if !ok {
|
|
|
|
// Channel closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Info("#"+strconv.Itoa(i), "parsing", path)
|
|
|
|
err := gb.scanEbuild(path, t)
|
|
|
|
if err != nil {
|
|
|
|
Error(path, ":", err.Error())
|
|
|
|
}
|
2019-11-01 12:03:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 11:30:44 +02:00
|
|
|
func (gb *GentooBuilder) Generate(dir string) (pkg.Tree, error) {
|
2019-11-02 18:02:55 +01:00
|
|
|
// tmpfile, err := ioutil.TempFile("", "boltdb")
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2019-11-01 12:03:48 +01:00
|
|
|
var toScan = make(chan string)
|
2019-11-01 16:26:03 +01:00
|
|
|
Spinner(27)
|
|
|
|
defer SpinnerStop()
|
2019-11-02 18:02:55 +01:00
|
|
|
tree := &GentooTree{DefaultTree: &tree.DefaultTree{Packages: pkg.NewInMemoryDatabase(false)}}
|
2019-11-03 12:03:26 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2019-11-01 13:08:53 +01:00
|
|
|
|
2019-11-02 18:02:55 +01:00
|
|
|
//tree := &GentooTree{DefaultTree: &tree.DefaultTree{Packages: pkg.NewBoltDatabase(tmpfile.Name())}}
|
|
|
|
Debug("Concurrency", gb.Concurrency)
|
2019-11-01 12:03:48 +01:00
|
|
|
// the waitgroup will allow us to wait for all the goroutines to finish at the end
|
|
|
|
var wg = new(sync.WaitGroup)
|
2019-11-01 12:41:31 +01:00
|
|
|
for i := 0; i < gb.Concurrency; i++ {
|
2019-11-01 12:03:48 +01:00
|
|
|
wg.Add(1)
|
2019-11-03 12:03:26 +01:00
|
|
|
go gb.worker(ctx, i, wg, toScan, tree)
|
2019-11-01 12:03:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle cleaning after? Cleanup implemented in GetPackageSet().Clean()
|
2019-11-02 18:02:55 +01:00
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
2019-07-27 11:30:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if strings.Contains(info.Name(), "ebuild") {
|
2019-11-02 18:02:55 +01:00
|
|
|
Debug("Enqueueing", path)
|
2019-11-01 12:03:48 +01:00
|
|
|
toScan <- path
|
2019-07-27 11:30:44 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-11-01 11:32:17 +01:00
|
|
|
|
2019-11-02 10:26:28 +01:00
|
|
|
Debug("Waiting for goroutines to finish")
|
2019-11-03 12:03:26 +01:00
|
|
|
close(toScan)
|
|
|
|
|
2019-11-02 18:02:55 +01:00
|
|
|
wg.Wait() // FIXME: With BoltDB as backend goroutines timeouts and deadlocks
|
2019-11-03 12:03:26 +01:00
|
|
|
|
2019-11-02 18:02:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
2019-11-02 10:26:28 +01:00
|
|
|
Info("Scan finished")
|
2019-11-01 16:26:03 +01:00
|
|
|
Info("Resolving deps")
|
2019-11-01 16:30:46 +01:00
|
|
|
return tree, tree.ResolveDeps(gb.Concurrency)
|
2019-07-27 11:30:44 +02:00
|
|
|
}
|