download.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 uer 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. if err != nil && !os.IsNotExist(err) {
  136. fmt.Println(bold(red(smallArrow)), err)
  137. continue
  138. } else if os.IsNotExist(err) || cmdArgs.existsArg("f", "force") || shouldUseGit(filepath.Join(wd, name)) {
  139. if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
  140. fmt.Println(bold(red(smallArrow)), err)
  141. continue
  142. }
  143. } else {
  144. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  145. continue
  146. }
  147. bases = append(bases, base)
  148. }
  149. if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
  150. return err
  151. }
  152. missing = missing || len(aur) != len(info)
  153. }
  154. if missing {
  155. err = fmt.Errorf("")
  156. }
  157. return err
  158. }
  159. // GetPkgbuild downloads pkgbuild from the ABS.
  160. func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
  161. var wg sync.WaitGroup
  162. var mux sync.Mutex
  163. var errs MultiError
  164. names := make(map[string]string)
  165. missing := make([]string, 0)
  166. downloaded := 0
  167. dbList, err := alpmHandle.SyncDbs()
  168. if err != nil {
  169. return false, err
  170. }
  171. for _, pkgN := range pkgs {
  172. var pkg *alpm.Package
  173. var err error
  174. var url string
  175. pkgDb, name := splitDbFromName(pkgN)
  176. if pkgDb != "" {
  177. if db, err := alpmHandle.SyncDbByName(pkgDb); err == nil {
  178. pkg, err = db.PkgByName(name)
  179. }
  180. } else {
  181. dbList.ForEach(func(db alpm.Db) error {
  182. if pkg, err = db.PkgByName(name); err == nil {
  183. return fmt.Errorf("")
  184. }
  185. return nil
  186. })
  187. }
  188. if pkg == nil {
  189. missing = append(missing, name)
  190. continue
  191. }
  192. name = pkg.Base()
  193. if name == "" {
  194. name = pkg.Name()
  195. }
  196. switch pkg.DB().Name() {
  197. case "core", "extra", "testing":
  198. url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
  199. case "community", "multilib", "community-testing", "multilib-testing":
  200. url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
  201. default:
  202. missing = append(missing, name)
  203. continue
  204. }
  205. _, err = os.Stat(filepath.Join(path, name))
  206. if err != nil && !os.IsNotExist(err) {
  207. fmt.Println(bold(red(smallArrow)), err)
  208. continue
  209. } else if os.IsNotExist(err) || cmdArgs.existsArg("f", "force") {
  210. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  211. fmt.Println(bold(red(smallArrow)), err)
  212. continue
  213. }
  214. } else {
  215. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  216. continue
  217. }
  218. names[name] = url
  219. }
  220. if len(missing) != 0 {
  221. fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
  222. }
  223. download := func(pkg string, url string) {
  224. defer wg.Done()
  225. if err := downloadAndUnpack(url, cacheHome); err != nil {
  226. errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
  227. return
  228. }
  229. _, stderr, err := capture(exec.Command("mv", filepath.Join(cacheHome, "packages", pkg, "trunk"), filepath.Join(path, pkg)))
  230. mux.Lock()
  231. downloaded++
  232. if err != nil {
  233. errs.Add(fmt.Errorf("%s Failed to move %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(string(stderr)))))
  234. } else {
  235. fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
  236. }
  237. mux.Unlock()
  238. }
  239. for name, url := range names {
  240. wg.Add(1)
  241. go download(name, url)
  242. }
  243. wg.Wait()
  244. errs.Add(os.RemoveAll(filepath.Join(cacheHome, "packages")))
  245. return len(missing) != 0, errs.Return()
  246. }