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"
|
2021-08-02 18:00:50 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-12-15 01:29:05 +01:00
|
|
|
"sync"
|
|
|
|
|
2021-08-02 18:00:50 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
"github.com/Jguer/aur"
|
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2021-08-02 20:42:36 +02:00
|
|
|
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,
|
2023-01-03 22:43:56 +01:00
|
|
|
pkgURL, pkgName, dest string, force bool, gitArgs ...string,
|
|
|
|
) (bool, error) {
|
2021-08-02 18:00:50 +02:00
|
|
|
finalDir := filepath.Join(dest, pkgName)
|
2021-08-07 19:11:52 +02:00
|
|
|
newClone := true
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
switch _, err := os.Stat(filepath.Join(finalDir, ".git")); {
|
|
|
|
case os.IsNotExist(err) || (err == nil && force):
|
2021-08-02 18:00:50 +02:00
|
|
|
if _, errD := os.Stat(finalDir); force && errD == nil {
|
|
|
|
if errR := os.RemoveAll(finalDir); errR != nil {
|
2021-08-07 19:11:52 +02:00
|
|
|
return false, ErrGetPKGBUILDRepo{inner: errR, pkgName: pkgName, errOut: ""}
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 20:42:36 +02:00
|
|
|
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-02 18:00:50 +02:00
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
_, stderr, errCapture := cmdBuilder.Capture(cmd)
|
2021-08-02 18:00:50 +02:00
|
|
|
if errCapture != nil {
|
2021-08-07 19:11:52 +02:00
|
|
|
return false, ErrGetPKGBUILDRepo{inner: errCapture, pkgName: pkgName, errOut: stderr}
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
case err != nil:
|
2021-08-07 19:11:52 +02:00
|
|
|
return false, ErrGetPKGBUILDRepo{
|
2021-08-02 18:00:50 +02:00
|
|
|
inner: err,
|
|
|
|
pkgName: pkgName,
|
|
|
|
errOut: gotext.Get("error reading %s", filepath.Join(dest, pkgName, ".git")),
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
default:
|
2021-12-12 19:05:48 +01:00
|
|
|
cmd := cmdBuilder.BuildGitCmd(ctx, filepath.Join(dest, pkgName), "pull", "--rebase", "--autostash")
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
_, stderr, errCmd := cmdBuilder.Capture(cmd)
|
2021-08-02 18:00:50 +02:00
|
|
|
if errCmd != nil {
|
2021-08-07 19:11:52 +02:00
|
|
|
return false, ErrGetPKGBUILDRepo{inner: errCmd, pkgName: pkgName, errOut: stderr}
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-07 19:11:52 +02:00
|
|
|
newClone = false
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-07 19:11:52 +02:00
|
|
|
return newClone, nil
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2021-07-25 12:10:51 +02:00
|
|
|
|
2020-12-15 01:29:05 +01:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
func PKGBUILDs(dbExecutor DBSearcher, aurClient aur.QueryClient, httpClient *http.Client, targets []string,
|
2023-01-03 22:43:56 +01:00
|
|
|
aurURL string, mode parser.TargetMode,
|
|
|
|
) (map[string][]byte, error) {
|
2020-12-15 01:29:05 +01:00
|
|
|
pkgbuilds := make(map[string][]byte, len(targets))
|
2021-08-02 18:00:50 +02:00
|
|
|
|
|
|
|
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 {
|
2021-08-02 20:48:21 +02:00
|
|
|
// Probably replaceable by something in query.
|
2023-02-21 00:58:13 +01:00
|
|
|
dbName, name, isAUR, toSkip := getPackageUsableName(dbExecutor, aurClient, target, mode)
|
2021-08-02 20:48:21 +02:00
|
|
|
if toSkip {
|
2020-12-15 01:29:05 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sem <- 1
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2020-12-15 01:29:05 +01:00
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(target, dbName, pkgName string, aur bool) {
|
2021-08-02 18:00:50 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
pkgbuild []byte
|
|
|
|
)
|
2020-12-15 01:29:05 +01:00
|
|
|
|
|
|
|
if aur {
|
2021-08-11 18:40:59 +02:00
|
|
|
pkgbuild, err = AURPKGBUILD(httpClient, pkgName, aurURL)
|
2020-12-15 01:29:05 +01:00
|
|
|
} else {
|
2021-08-02 18:05:36 +02:00
|
|
|
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()
|
2023-02-21 00:58:13 +01:00
|
|
|
}(target, dbName, name, isAUR)
|
2020-12-15 01:29:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2021-07-25 12:10:51 +02:00
|
|
|
|
2020-12-15 01:29:05 +01:00
|
|
|
return pkgbuilds, errs.Return()
|
|
|
|
}
|
2021-08-02 18:00:50 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher, aurClient aur.QueryClient,
|
2021-08-02 18:00:50 +02:00
|
|
|
cmdBuilder exe.GitCmdBuilder,
|
2023-01-03 22:43:56 +01:00
|
|
|
targets []string, mode parser.TargetMode, aurURL, dest string, force bool,
|
|
|
|
) (map[string]bool, error) {
|
2021-08-02 18:00:50 +02:00
|
|
|
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 {
|
2021-08-02 20:48:21 +02:00
|
|
|
// Probably replaceable by something in query.
|
2023-02-21 00:58:13 +01:00
|
|
|
dbName, name, isAUR, toSkip := getPackageUsableName(dbExecutor, aurClient, target, mode)
|
2021-08-02 20:48:21 +02:00
|
|
|
if toSkip {
|
2021-08-02 18:00:50 +02:00
|
|
|
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
|
|
|
|
)
|
2021-08-02 18:00:50 +02:00
|
|
|
|
|
|
|
if aur {
|
2021-08-12 18:56:23 +02:00
|
|
|
newClone, err = AURPKGBUILDRepo(ctx, cmdBuilder, aurURL, pkgName, dest, force)
|
2021-08-02 18:00:50 +02:00
|
|
|
} else {
|
2021-08-12 18:56:23 +02:00
|
|
|
newClone, err = ABSPKGBUILDRepo(ctx, cmdBuilder, dbName, pkgName, dest, force)
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 18:40:59 +02:00
|
|
|
progress := 0
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-07 19:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
errs.Add(err)
|
|
|
|
} else {
|
2021-08-02 18:00:50 +02:00
|
|
|
mux.Lock()
|
2021-08-07 19:11:52 +02:00
|
|
|
cloned[target] = newClone
|
2021-08-11 18:40:59 +02:00
|
|
|
progress = len(cloned)
|
2021-08-02 18:00:50 +02:00
|
|
|
mux.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if aur {
|
|
|
|
text.OperationInfoln(
|
|
|
|
gotext.Get("(%d/%d) Downloaded PKGBUILD: %s",
|
2021-08-11 18:40:59 +02:00
|
|
|
progress, len(targets), text.Cyan(pkgName)))
|
2021-08-02 18:00:50 +02:00
|
|
|
} else {
|
|
|
|
text.OperationInfoln(
|
|
|
|
gotext.Get("(%d/%d) Downloaded PKGBUILD from ABS: %s",
|
2021-08-11 18:40:59 +02:00
|
|
|
progress, len(targets), text.Cyan(pkgName)))
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
2021-08-02 18:05:36 +02:00
|
|
|
|
2021-08-02 18:00:50 +02:00
|
|
|
<-sem
|
2021-08-02 18:05:36 +02:00
|
|
|
|
2021-08-02 18:00:50 +02:00
|
|
|
wg.Done()
|
2023-02-21 00:58:13 +01:00
|
|
|
}(target, dbName, name, isAUR)
|
2021-08-02 18:00:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return cloned, errs.Return()
|
|
|
|
}
|
2021-08-02 20:48:21 +02:00
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
// TODO: replace with dep.ResolveTargets.
|
2023-02-21 00:58:13 +01:00
|
|
|
func getPackageUsableName(dbExecutor DBSearcher, aurClient aur.QueryClient,
|
|
|
|
target string, mode parser.TargetMode,
|
|
|
|
) (dbname, pkgname string, isAUR, toSkip bool) {
|
2021-08-02 20:48:21 +02:00
|
|
|
dbName, name := text.SplitDBFromName(target)
|
2021-08-09 13:26:32 +02:00
|
|
|
if dbName != "aur" && mode.AtLeastRepo() {
|
2021-08-11 18:40:59 +02:00
|
|
|
var pkg db.IPackage
|
2021-08-02 20:48:21 +02:00
|
|
|
if dbName != "" {
|
|
|
|
pkg = dbExecutor.SatisfierFromDB(name, dbName)
|
|
|
|
} else {
|
|
|
|
pkg = dbExecutor.SyncPackage(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pkg != nil {
|
|
|
|
name = getURLName(pkg)
|
|
|
|
dbName = pkg.DB().Name()
|
2023-02-21 00:58:13 +01:00
|
|
|
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
|
2021-08-02 20:48:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
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
|
2021-08-02 20:48:21 +02:00
|
|
|
}
|
2021-08-02 21:56:19 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
return "aur", name, true, false
|
2021-08-02 20:48:21 +02:00
|
|
|
}
|