download.go 6.1 KB

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