2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-03-28 16:52:20 +02:00
|
|
|
"path/filepath"
|
2018-05-31 21:31:45 +02:00
|
|
|
"strings"
|
2018-08-18 07:41:16 +02:00
|
|
|
"sync"
|
2018-08-18 05:06:27 +02:00
|
|
|
|
2019-04-23 17:53:20 +02:00
|
|
|
alpm "github.com/Jguer/go-alpm"
|
2017-08-02 19:24:03 +02:00
|
|
|
)
|
|
|
|
|
2018-03-28 16:52:20 +02:00
|
|
|
// Decide what download method to use:
|
|
|
|
// Use the config option when the destination does not already exits
|
2018-11-30 10:41:30 +01:00
|
|
|
// If .git exists in the destination use git
|
2018-03-28 16:52:20 +02:00
|
|
|
// Otherwise use a tarrball
|
|
|
|
func shouldUseGit(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return config.GitClone
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(filepath.Join(path, ".git"))
|
2018-05-31 21:31:45 +02:00
|
|
|
return err == nil || os.IsExist(err)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
func downloadFile(path string, url string) (err error) {
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Writer the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-10 02:57:17 +02:00
|
|
|
func gitHasDiff(path string, name string) (bool, error) {
|
2018-07-19 19:37:28 +02:00
|
|
|
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
|
2018-05-31 21:31:45 +02:00
|
|
|
if err != nil {
|
2018-06-10 02:57:17 +02:00
|
|
|
return false, fmt.Errorf("%s%s", stderr, err)
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 19:37:28 +02:00
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
head := lines[0]
|
|
|
|
upstream := lines[1]
|
2018-06-10 02:57:17 +02:00
|
|
|
|
|
|
|
return head != upstream, nil
|
2018-05-31 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
func gitDownload(url string, path string, name string) (bool, error) {
|
2018-03-28 16:52:20 +02:00
|
|
|
_, err := os.Stat(filepath.Join(path, name, ".git"))
|
|
|
|
if os.IsNotExist(err) {
|
2018-08-03 00:07:36 +02:00
|
|
|
cmd := passToGit(path, "clone", "--no-progress", url, name)
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-29 21:19:31 +02:00
|
|
|
return false, fmt.Errorf("error cloning %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
return true, nil
|
2018-03-28 16:52:20 +02:00
|
|
|
} else if err != nil {
|
2018-06-10 04:41:25 +02:00
|
|
|
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 00:07:36 +02:00
|
|
|
cmd := passToGit(filepath.Join(path, name), "fetch")
|
|
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
|
|
_, stderr, err := capture(cmd)
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return false, fmt.Errorf("error fetching %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 04:41:25 +02:00
|
|
|
return false, nil
|
2018-06-10 01:42:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 15:14:16 +02:00
|
|
|
func gitMerge(path string, name string) error {
|
2018-08-02 17:11:37 +02:00
|
|
|
_, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return fmt.Errorf("error resetting %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:11:37 +02:00
|
|
|
_, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
|
2018-03-28 16:52:20 +02:00
|
|
|
if err != nil {
|
2018-08-02 17:11:37 +02:00
|
|
|
return fmt.Errorf("error merging %s: %s", name, stderr)
|
2018-03-28 16:52:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// DownloadAndUnpack downloads url tgz and extracts to path.
|
2018-08-18 07:41:16 +02:00
|
|
|
func downloadAndUnpack(url string, path string) error {
|
|
|
|
err := os.MkdirAll(path, 0755)
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 19:33:48 +02:00
|
|
|
fileName := filepath.Base(url)
|
2017-08-02 19:24:03 +02:00
|
|
|
|
2018-04-17 02:32:31 +02:00
|
|
|
tarLocation := filepath.Join(path, fileName)
|
2017-08-02 19:24:03 +02:00
|
|
|
defer os.Remove(tarLocation)
|
|
|
|
|
|
|
|
err = downloadFile(tarLocation, url)
|
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
_, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return fmt.Errorf("%s", stderr)
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
return nil
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-21 09:41:25 +01:00
|
|
|
func getPkgbuilds(pkgs []string) error {
|
2018-06-01 06:04:15 +02:00
|
|
|
missing := false
|
2017-08-02 19:24:03 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-02-17 18:44:30 +01:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 06:04:15 +02:00
|
|
|
pkgs = removeInvalidTargets(pkgs)
|
|
|
|
aur, repo, err := packageSlices(pkgs)
|
2018-08-18 05:06:27 +02:00
|
|
|
|
2019-04-23 18:03:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
for n := range aur {
|
2019-02-04 17:56:02 +01:00
|
|
|
_, pkg := splitDBFromName(aur[n])
|
2018-08-18 05:06:27 +02:00
|
|
|
aur[n] = pkg
|
|
|
|
}
|
|
|
|
|
2018-08-18 01:16:04 +02:00
|
|
|
info, err := aurInfoPrint(aur)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-01 06:04:15 +02:00
|
|
|
|
|
|
|
if len(repo) > 0 {
|
|
|
|
missing, err = getPkgbuildsfromABS(repo, wd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(aur) > 0 {
|
2018-08-30 16:30:52 +02:00
|
|
|
allBases := getBases(info)
|
|
|
|
bases := make([]Base, 0)
|
|
|
|
|
|
|
|
for _, base := range allBases {
|
|
|
|
name := base.Pkgbase()
|
|
|
|
_, err = os.Stat(filepath.Join(wd, name))
|
2019-03-07 21:02:44 +01:00
|
|
|
switch {
|
|
|
|
case err != nil && !os.IsNotExist(err):
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 16:30:52 +02:00
|
|
|
continue
|
2019-03-07 21:02:44 +01:00
|
|
|
case os.IsNotExist(err), cmdArgs.existsArg("f", "force"), shouldUseGit(filepath.Join(wd, name)):
|
2018-08-30 16:30:52 +02:00
|
|
|
if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 16:30:52 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-03-07 21:02:44 +01:00
|
|
|
default:
|
2018-08-30 16:30:52 +02:00
|
|
|
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bases = append(bases, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
|
2018-06-01 06:04:15 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-08-18 01:16:04 +02:00
|
|
|
|
|
|
|
missing = missing || len(aur) != len(info)
|
2018-06-01 06:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if missing {
|
|
|
|
err = fmt.Errorf("")
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:44:30 +01:00
|
|
|
return err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPkgbuild downloads pkgbuild from the ABS.
|
2018-08-18 05:06:27 +02:00
|
|
|
func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
|
2018-08-18 07:41:16 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var mux sync.Mutex
|
|
|
|
var errs MultiError
|
|
|
|
names := make(map[string]string)
|
|
|
|
missing := make([]string, 0)
|
|
|
|
downloaded := 0
|
|
|
|
|
2019-02-04 17:56:02 +01:00
|
|
|
dbList, err := alpmHandle.SyncDBs()
|
2017-08-02 19:24:03 +02:00
|
|
|
if err != nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
return false, err
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
for _, pkgN := range pkgs {
|
2018-08-18 05:06:27 +02:00
|
|
|
var pkg *alpm.Package
|
|
|
|
var err error
|
|
|
|
var url string
|
2019-02-04 17:56:02 +01:00
|
|
|
pkgDB, name := splitDBFromName(pkgN)
|
2018-06-01 06:04:15 +02:00
|
|
|
|
2019-02-04 17:56:02 +01:00
|
|
|
if pkgDB != "" {
|
|
|
|
if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
|
2019-02-26 12:41:35 +01:00
|
|
|
pkg = db.Pkg(name)
|
2018-06-01 06:04:15 +02:00
|
|
|
}
|
2018-08-18 05:06:27 +02:00
|
|
|
} else {
|
2019-02-04 17:56:02 +01:00
|
|
|
dbList.ForEach(func(db alpm.DB) error {
|
2019-02-14 21:44:39 +01:00
|
|
|
if pkg = db.Pkg(name); pkg != nil {
|
2018-08-18 05:06:27 +02:00
|
|
|
return fmt.Errorf("")
|
2018-05-14 19:33:48 +02:00
|
|
|
}
|
2018-08-18 05:06:27 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-05-14 19:33:48 +02:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
if pkg == nil {
|
2018-08-18 07:41:16 +02:00
|
|
|
missing = append(missing, name)
|
2018-08-18 05:06:27 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
name = pkg.Base()
|
|
|
|
if name == "" {
|
|
|
|
name = pkg.Name()
|
|
|
|
}
|
2018-05-14 19:33:48 +02:00
|
|
|
|
2018-08-18 05:06:27 +02:00
|
|
|
switch pkg.DB().Name() {
|
|
|
|
case "core", "extra", "testing":
|
|
|
|
url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
|
|
|
|
case "community", "multilib", "community-testing", "multilib-testing":
|
|
|
|
url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
|
|
|
|
default:
|
2018-08-18 07:41:16 +02:00
|
|
|
missing = append(missing, name)
|
2018-08-18 05:06:27 +02:00
|
|
|
continue
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-08-30 16:30:52 +02:00
|
|
|
_, err = os.Stat(filepath.Join(path, name))
|
2019-03-07 21:02:44 +01:00
|
|
|
switch {
|
|
|
|
case err != nil && !os.IsNotExist(err):
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-18 07:41:16 +02:00
|
|
|
continue
|
2019-03-07 21:02:44 +01:00
|
|
|
case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
|
2018-08-30 16:30:52 +02:00
|
|
|
if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
|
2018-08-30 16:30:52 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-03-07 21:02:44 +01:00
|
|
|
default:
|
2018-08-30 16:30:52 +02:00
|
|
|
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
|
|
|
|
continue
|
2018-08-18 07:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
names[name] = url
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missing) != 0 {
|
|
|
|
fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
download := func(pkg string, url string) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := downloadAndUnpack(url, cacheHome); err != nil {
|
|
|
|
errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
|
|
|
|
return
|
2018-08-18 05:06:27 +02:00
|
|
|
}
|
2018-02-17 18:44:30 +01:00
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
_, stderr, err := capture(exec.Command("mv", filepath.Join(cacheHome, "packages", pkg, "trunk"), filepath.Join(path, pkg)))
|
|
|
|
mux.Lock()
|
|
|
|
downloaded++
|
2018-08-18 05:06:27 +02:00
|
|
|
if err != nil {
|
2019-07-19 11:51:42 +02:00
|
|
|
errs.Add(fmt.Errorf("%s Failed to move %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(stderr))))
|
2018-08-18 05:06:27 +02:00
|
|
|
} else {
|
2018-08-18 07:41:16 +02:00
|
|
|
fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
|
2018-08-18 05:06:27 +02:00
|
|
|
}
|
2018-08-18 07:41:16 +02:00
|
|
|
mux.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:28:36 +02:00
|
|
|
count := 0
|
2018-08-18 07:41:16 +02:00
|
|
|
for name, url := range names {
|
|
|
|
wg.Add(1)
|
|
|
|
go download(name, url)
|
2018-09-04 19:28:36 +02:00
|
|
|
count++
|
|
|
|
if count%25 == 0 {
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2018-05-14 19:33:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 07:41:16 +02:00
|
|
|
wg.Wait()
|
|
|
|
errs.Add(os.RemoveAll(filepath.Join(cacheHome, "packages")))
|
|
|
|
return len(missing) != 0, errs.Return()
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|