download.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. alpm "github.com/jguer/go-alpm"
  12. )
  13. // Decide what download method to use:
  14. // Use the config option when the destination does not already exits
  15. // If .git exists in the destination use git
  16. // Otherwise use a tarrball
  17. func shouldUseGit(path string) bool {
  18. _, err := os.Stat(path)
  19. if os.IsNotExist(err) {
  20. return config.GitClone
  21. }
  22. _, err = os.Stat(filepath.Join(path, ".git"))
  23. return err == nil || os.IsExist(err)
  24. }
  25. func downloadFile(path string, url string) (err error) {
  26. // Create the file
  27. out, err := os.Create(path)
  28. if err != nil {
  29. return err
  30. }
  31. defer out.Close()
  32. // Get the data
  33. resp, err := http.Get(url)
  34. if err != nil {
  35. return err
  36. }
  37. defer resp.Body.Close()
  38. // Writer the body to file
  39. _, err = io.Copy(out, resp.Body)
  40. return err
  41. }
  42. func gitHasDiff(path string, name string) (bool, error) {
  43. stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
  44. if err != nil {
  45. return false, fmt.Errorf("%s%s", stderr, err)
  46. }
  47. lines := strings.Split(stdout, "\n")
  48. head := lines[0]
  49. upstream := lines[1]
  50. return head != upstream, nil
  51. }
  52. func gitDownload(url string, path string, name string) (bool, error) {
  53. _, err := os.Stat(filepath.Join(path, name, ".git"))
  54. if os.IsNotExist(err) {
  55. cmd := passToGit(path, "clone", "--no-progress", url, name)
  56. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  57. _, stderr, err := capture(cmd)
  58. if err != nil {
  59. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  60. }
  61. return true, nil
  62. } else if err != nil {
  63. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  64. }
  65. cmd := passToGit(filepath.Join(path, name), "fetch")
  66. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  67. _, stderr, err := capture(cmd)
  68. if err != nil {
  69. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  70. }
  71. return false, nil
  72. }
  73. func gitMerge(path string, name string) error {
  74. _, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
  75. if err != nil {
  76. return fmt.Errorf("error resetting %s: %s", name, stderr)
  77. }
  78. _, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
  79. if err != nil {
  80. return fmt.Errorf("error merging %s: %s", name, stderr)
  81. }
  82. return nil
  83. }
  84. func gitDiff(path string, name string) error {
  85. err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))
  86. return err
  87. }
  88. // DownloadAndUnpack downloads url tgz and extracts to path.
  89. func downloadAndUnpack(url string, path string) error {
  90. err := os.MkdirAll(path, 0755)
  91. if err != nil {
  92. return err
  93. }
  94. fileName := filepath.Base(url)
  95. tarLocation := filepath.Join(path, fileName)
  96. defer os.Remove(tarLocation)
  97. err = downloadFile(tarLocation, url)
  98. if err != nil {
  99. return err
  100. }
  101. _, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
  102. if err != nil {
  103. return fmt.Errorf("%s", stderr)
  104. }
  105. return nil
  106. }
  107. func getPkgbuilds(pkgs []string) error {
  108. missing := false
  109. wd, err := os.Getwd()
  110. if err != nil {
  111. return err
  112. }
  113. pkgs = removeInvalidTargets(pkgs)
  114. aur, repo, err := packageSlices(pkgs)
  115. for n := range aur {
  116. _, pkg := splitDBFromName(aur[n])
  117. aur[n] = pkg
  118. }
  119. info, err := aurInfoPrint(aur)
  120. if err != nil {
  121. return err
  122. }
  123. if len(repo) > 0 {
  124. missing, err = getPkgbuildsfromABS(repo, wd)
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. if len(aur) > 0 {
  130. allBases := getBases(info)
  131. bases := make([]Base, 0)
  132. for _, base := range allBases {
  133. name := base.Pkgbase()
  134. _, err = os.Stat(filepath.Join(wd, name))
  135. switch {
  136. case err != nil && !os.IsNotExist(err):
  137. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  138. continue
  139. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"), shouldUseGit(filepath.Join(wd, name)):
  140. if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
  141. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  142. continue
  143. }
  144. default:
  145. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  146. continue
  147. }
  148. bases = append(bases, base)
  149. }
  150. if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
  151. return err
  152. }
  153. missing = missing || len(aur) != len(info)
  154. }
  155. if missing {
  156. err = fmt.Errorf("")
  157. }
  158. return err
  159. }
  160. // GetPkgbuild downloads pkgbuild from the ABS.
  161. func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
  162. var wg sync.WaitGroup
  163. var mux sync.Mutex
  164. var errs MultiError
  165. names := make(map[string]string)
  166. missing := make([]string, 0)
  167. downloaded := 0
  168. dbList, err := alpmHandle.SyncDBs()
  169. if err != nil {
  170. return false, err
  171. }
  172. for _, pkgN := range pkgs {
  173. var pkg *alpm.Package
  174. var err error
  175. var url string
  176. pkgDB, name := splitDBFromName(pkgN)
  177. if pkgDB != "" {
  178. if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
  179. pkg = db.Pkg(name)
  180. }
  181. } else {
  182. dbList.ForEach(func(db alpm.DB) error {
  183. if pkg = db.Pkg(name); pkg != nil {
  184. return fmt.Errorf("")
  185. }
  186. return nil
  187. })
  188. }
  189. if pkg == nil {
  190. missing = append(missing, name)
  191. continue
  192. }
  193. name = pkg.Base()
  194. if name == "" {
  195. name = pkg.Name()
  196. }
  197. switch pkg.DB().Name() {
  198. case "core", "extra", "testing":
  199. url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
  200. case "community", "multilib", "community-testing", "multilib-testing":
  201. url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
  202. default:
  203. missing = append(missing, name)
  204. continue
  205. }
  206. _, err = os.Stat(filepath.Join(path, name))
  207. switch {
  208. case err != nil && !os.IsNotExist(err):
  209. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  210. continue
  211. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
  212. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  213. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  214. continue
  215. }
  216. default:
  217. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  218. continue
  219. }
  220. names[name] = url
  221. }
  222. if len(missing) != 0 {
  223. fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
  224. }
  225. download := func(pkg string, url string) {
  226. defer wg.Done()
  227. if err := downloadAndUnpack(url, cacheHome); err != nil {
  228. errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
  229. return
  230. }
  231. _, stderr, err := capture(exec.Command("mv", filepath.Join(cacheHome, "packages", pkg, "trunk"), filepath.Join(path, pkg)))
  232. mux.Lock()
  233. downloaded++
  234. if err != nil {
  235. errs.Add(fmt.Errorf("%s Failed to move %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(string(stderr)))))
  236. } else {
  237. fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
  238. }
  239. mux.Unlock()
  240. }
  241. count := 0
  242. for name, url := range names {
  243. wg.Add(1)
  244. go download(name, url)
  245. count++
  246. if count%25 == 0 {
  247. wg.Wait()
  248. }
  249. }
  250. wg.Wait()
  251. errs.Add(os.RemoveAll(filepath.Join(cacheHome, "packages")))
  252. return len(missing) != 0, errs.Return()
  253. }