download.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. alpm "github.com/jguer/go-alpm"
  11. )
  12. // Decide what download method to use:
  13. // Use the config option when the destination does not already exits
  14. // If .git exists in the destination uer git
  15. // Otherwise use a tarrball
  16. func shouldUseGit(path string) bool {
  17. _, err := os.Stat(path)
  18. if os.IsNotExist(err) {
  19. return config.GitClone
  20. }
  21. _, err = os.Stat(filepath.Join(path, ".git"))
  22. return err == nil || os.IsExist(err)
  23. }
  24. func downloadFile(path string, url string) (err error) {
  25. // Create the file
  26. out, err := os.Create(path)
  27. if err != nil {
  28. return err
  29. }
  30. defer out.Close()
  31. // Get the data
  32. resp, err := http.Get(url)
  33. if err != nil {
  34. return err
  35. }
  36. defer resp.Body.Close()
  37. // Writer the body to file
  38. _, err = io.Copy(out, resp.Body)
  39. return err
  40. }
  41. func gitHasDiff(path string, name string) (bool, error) {
  42. stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
  43. if err != nil {
  44. return false, fmt.Errorf("%s%s", stderr, err)
  45. }
  46. lines := strings.Split(stdout, "\n")
  47. head := lines[0]
  48. upstream := lines[1]
  49. return head != upstream, nil
  50. }
  51. func gitDownload(url string, path string, name string) (bool, error) {
  52. _, err := os.Stat(filepath.Join(path, name, ".git"))
  53. if os.IsNotExist(err) {
  54. cmd := passToGit(path, "clone", "--no-progress", url, name)
  55. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  56. _, stderr, err := capture(cmd)
  57. if err != nil {
  58. return false, fmt.Errorf("error cloning %s: stderr", name, stderr)
  59. }
  60. return true, nil
  61. } else if err != nil {
  62. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  63. }
  64. cmd := passToGit(filepath.Join(path, name), "fetch")
  65. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  66. _, stderr, err := capture(cmd)
  67. if err != nil {
  68. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  69. }
  70. return false, nil
  71. }
  72. func gitMerge(path string, name string) error {
  73. _, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
  74. if err != nil {
  75. return fmt.Errorf("error resetting %s: %s", name, stderr)
  76. }
  77. _, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
  78. if err != nil {
  79. return fmt.Errorf("error merging %s: %s", name, stderr)
  80. }
  81. return nil
  82. }
  83. func gitDiff(path string, name string) error {
  84. err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))
  85. return err
  86. }
  87. // DownloadAndUnpack downloads url tgz and extracts to path.
  88. func downloadAndUnpack(url string, path string) (err error) {
  89. err = os.MkdirAll(path, 0755)
  90. if err != nil {
  91. return
  92. }
  93. fileName := filepath.Base(url)
  94. tarLocation := filepath.Join(path, fileName)
  95. defer os.Remove(tarLocation)
  96. err = downloadFile(tarLocation, url)
  97. if err != nil {
  98. return
  99. }
  100. err = exec.Command(config.TarBin, "-xf", tarLocation, "-C", path).Run()
  101. if err != nil {
  102. return
  103. }
  104. return
  105. }
  106. func getPkgbuilds(pkgs []string) error {
  107. missing := false
  108. wd, err := os.Getwd()
  109. if err != nil {
  110. return err
  111. }
  112. pkgs = removeInvalidTargets(pkgs)
  113. aur, repo, err := packageSlices(pkgs)
  114. for n := range aur {
  115. _, pkg := splitDbFromName(aur[n])
  116. aur[n] = pkg
  117. }
  118. info, err := aurInfoPrint(aur)
  119. if err != nil {
  120. return err
  121. }
  122. if len(repo) > 0 {
  123. missing, err = getPkgbuildsfromABS(repo, wd)
  124. if err != nil {
  125. return err
  126. }
  127. }
  128. if len(aur) > 0 {
  129. bases := getBases(info)
  130. toSkip := pkgbuildsToSkip(bases, nil)
  131. if _, err = downloadPkgbuilds(bases, toSkip, wd); err != nil {
  132. return err
  133. }
  134. missing = missing || len(aur) != len(info)
  135. }
  136. if missing {
  137. err = fmt.Errorf("")
  138. }
  139. return err
  140. }
  141. // GetPkgbuild downloads pkgbuild from the ABS.
  142. func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
  143. missing := false
  144. dbList, err := alpmHandle.SyncDbs()
  145. if err != nil {
  146. return missing, err
  147. }
  148. for n, pkgN := range pkgs {
  149. pkgDb, name := splitDbFromName(pkgN)
  150. var pkg *alpm.Package
  151. var err error
  152. var url string
  153. if pkgDb != "" {
  154. if db, err := alpmHandle.SyncDbByName(pkgDb); err == nil {
  155. pkg, err = db.PkgByName(name)
  156. }
  157. } else {
  158. dbList.ForEach(func(db alpm.Db) error {
  159. if pkg, err = db.PkgByName(name); err == nil {
  160. return fmt.Errorf("")
  161. }
  162. return nil
  163. })
  164. }
  165. if pkg == nil {
  166. fmt.Println(name, "could not find package in database")
  167. missing = true
  168. continue
  169. }
  170. name = pkg.Base()
  171. if name == "" {
  172. name = pkg.Name()
  173. }
  174. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  175. fmt.Println(err)
  176. continue
  177. }
  178. switch pkg.DB().Name() {
  179. case "core", "extra", "testing":
  180. url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
  181. case "community", "multilib", "community-testing", "multilib-testing":
  182. url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
  183. default:
  184. fmt.Println(name, "not in standard repositories")
  185. continue
  186. }
  187. if err = downloadAndUnpack(url, cacheHome); err != nil {
  188. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(err.Error())))
  189. }
  190. err = exec.Command("mv", filepath.Join(cacheHome, "packages", name, "trunk"), filepath.Join(path, name)).Run()
  191. if err != nil {
  192. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(err.Error())))
  193. } else {
  194. fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", n+1, len(pkgs), cyan(pkg.Name()))
  195. }
  196. }
  197. err = os.RemoveAll(filepath.Join(cacheHome, "packages"))
  198. return missing, err
  199. }