yay/pkg/download/unified.go

252 lines
5.6 KiB
Go
Raw Normal View History

2020-12-15 01:29:05 +01:00
package download
import (
2021-08-12 18:56:23 +02:00
"context"
2021-01-30 12:52:50 +01:00
"net/http"
"os"
"path/filepath"
2020-12-15 01:29:05 +01:00
"sync"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/aur"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/multierror"
"github.com/Jguer/yay/v12/pkg/settings/exe"
"github.com/Jguer/yay/v12/pkg/settings/parser"
"github.com/Jguer/yay/v12/pkg/text"
2020-12-15 01:29:05 +01:00
)
2021-08-11 22:05:47 +02:00
type httpRequestDoer interface {
Get(string) (*http.Response, error)
}
type DBSearcher interface {
SyncPackage(string) db.IPackage
SatisfierFromDB(string, string) db.IPackage
}
2021-08-12 18:56:23 +02:00
func downloadGitRepo(ctx context.Context, cmdBuilder exe.GitCmdBuilder,
pkgURL, pkgName, dest string, force bool, gitArgs ...string,
) (bool, error) {
finalDir := filepath.Join(dest, pkgName)
newClone := true
2021-08-11 20:13:28 +02:00
switch _, err := os.Stat(filepath.Join(finalDir, ".git")); {
case os.IsNotExist(err) || (err == nil && force):
if _, errD := os.Stat(finalDir); force && errD == nil {
if errR := os.RemoveAll(finalDir); errR != nil {
return false, ErrGetPKGBUILDRepo{inner: errR, pkgName: pkgName, errOut: ""}
}
}
gitArgs = append(gitArgs, pkgURL, pkgName)
cloneArgs := make([]string, 0, len(gitArgs)+4)
cloneArgs = append(cloneArgs, "clone", "--no-progress")
cloneArgs = append(cloneArgs, gitArgs...)
2021-08-12 18:56:23 +02:00
cmd := cmdBuilder.BuildGitCmd(ctx, dest, cloneArgs...)
2021-08-12 18:56:23 +02:00
_, stderr, errCapture := cmdBuilder.Capture(cmd)
if errCapture != nil {
return false, ErrGetPKGBUILDRepo{inner: errCapture, pkgName: pkgName, errOut: stderr}
}
2021-08-11 20:13:28 +02:00
case err != nil:
return false, ErrGetPKGBUILDRepo{
inner: err,
pkgName: pkgName,
errOut: gotext.Get("error reading %s", filepath.Join(dest, pkgName, ".git")),
}
2021-08-11 20:13:28 +02:00
default:
cmd := cmdBuilder.BuildGitCmd(ctx, filepath.Join(dest, pkgName), "pull", "--rebase", "--autostash")
2021-08-12 18:56:23 +02:00
_, stderr, errCmd := cmdBuilder.Capture(cmd)
if errCmd != nil {
return false, ErrGetPKGBUILDRepo{inner: errCmd, pkgName: pkgName, errOut: stderr}
}
2021-08-11 20:13:28 +02:00
newClone = false
}
return newClone, nil
}
2021-02-17 10:33:56 +01:00
func getURLName(pkg db.IPackage) string {
2020-12-15 01:29:05 +01:00
name := pkg.Base()
if name == "" {
name = pkg.Name()
}
2020-12-15 01:29:05 +01:00
return name
}
func PKGBUILDs(dbExecutor DBSearcher, aurClient aur.QueryClient, httpClient *http.Client, targets []string,
aurURL string, mode parser.TargetMode,
) (map[string][]byte, error) {
2020-12-15 01:29:05 +01:00
pkgbuilds := make(map[string][]byte, len(targets))
var (
mux sync.Mutex
errs multierror.MultiError
wg sync.WaitGroup
)
2020-12-15 01:29:05 +01:00
sem := make(chan uint8, MaxConcurrentFetch)
for _, target := range targets {
// Probably replaceable by something in query.
dbName, name, isAUR, toSkip := getPackageUsableName(dbExecutor, aurClient, target, mode)
if toSkip {
2020-12-15 01:29:05 +01:00
continue
}
sem <- 1
2020-12-15 01:29:05 +01:00
wg.Add(1)
go func(target, dbName, pkgName string, aur bool) {
var (
err error
pkgbuild []byte
)
2020-12-15 01:29:05 +01:00
if aur {
pkgbuild, err = AURPKGBUILD(httpClient, pkgName, aurURL)
2020-12-15 01:29:05 +01:00
} else {
pkgbuild, err = ABSPKGBUILD(httpClient, dbName, pkgName)
2020-12-15 01:29:05 +01:00
}
if err == nil {
mux.Lock()
pkgbuilds[target] = pkgbuild
mux.Unlock()
} else {
errs.Add(err)
}
<-sem
wg.Done()
}(target, dbName, name, isAUR)
2020-12-15 01:29:05 +01:00
}
wg.Wait()
2020-12-15 01:29:05 +01:00
return pkgbuilds, errs.Return()
}
func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher, aurClient aur.QueryClient,
cmdBuilder exe.GitCmdBuilder,
targets []string, mode parser.TargetMode, aurURL, dest string, force bool,
) (map[string]bool, error) {
cloned := make(map[string]bool, len(targets))
var (
mux sync.Mutex
errs multierror.MultiError
wg sync.WaitGroup
)
sem := make(chan uint8, MaxConcurrentFetch)
for _, target := range targets {
// Probably replaceable by something in query.
dbName, name, isAUR, toSkip := getPackageUsableName(dbExecutor, aurClient, target, mode)
if toSkip {
continue
}
sem <- 1
wg.Add(1)
go func(target, dbName, pkgName string, aur bool) {
2021-08-11 20:13:28 +02:00
var (
err error
newClone bool
)
if aur {
2021-08-12 18:56:23 +02:00
newClone, err = AURPKGBUILDRepo(ctx, cmdBuilder, aurURL, pkgName, dest, force)
} else {
2021-08-12 18:56:23 +02:00
newClone, err = ABSPKGBUILDRepo(ctx, cmdBuilder, dbName, pkgName, dest, force)
}
progress := 0
2021-08-11 20:13:28 +02:00
if err != nil {
errs.Add(err)
} else {
mux.Lock()
cloned[target] = newClone
progress = len(cloned)
mux.Unlock()
}
if aur {
text.OperationInfoln(
gotext.Get("(%d/%d) Downloaded PKGBUILD: %s",
progress, len(targets), text.Cyan(pkgName)))
} else {
text.OperationInfoln(
gotext.Get("(%d/%d) Downloaded PKGBUILD from ABS: %s",
progress, len(targets), text.Cyan(pkgName)))
}
<-sem
wg.Done()
}(target, dbName, name, isAUR)
}
wg.Wait()
return cloned, errs.Return()
}
2021-08-11 20:13:28 +02:00
// TODO: replace with dep.ResolveTargets.
func getPackageUsableName(dbExecutor DBSearcher, aurClient aur.QueryClient,
target string, mode parser.TargetMode,
) (dbname, pkgname string, isAUR, toSkip bool) {
dbName, name := text.SplitDBFromName(target)
2021-08-09 13:26:32 +02:00
if dbName != "aur" && mode.AtLeastRepo() {
var pkg db.IPackage
if dbName != "" {
pkg = dbExecutor.SatisfierFromDB(name, dbName)
} else {
pkg = dbExecutor.SyncPackage(name)
}
if pkg != nil {
name = getURLName(pkg)
dbName = pkg.DB().Name()
return dbName, name, false, false
}
// If the package is not found in the database and it was expected to be
if pkg == nil && dbName != "" {
return dbName, name, true, true
}
}
if mode == parser.ModeRepo {
return dbName, name, true, true
}
pkgs, err := aurClient.Get(context.Background(), &aur.Query{
By: aur.Name,
Contains: false,
Needles: []string{name},
})
if err != nil {
text.Warnln(err)
return dbName, name, true, true
}
if len(pkgs) == 0 {
return dbName, name, true, true
}
return "aur", name, true, false
}