download.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. // TODO: yay-next passes args through the header, use that to unify ABS and AUR
  53. func gitDownloadABS(url string, path string, name string) (bool, error) {
  54. _, err := os.Stat(filepath.Join(path, name))
  55. if os.IsNotExist(err) {
  56. cmd := passToGit(path, "clone", "--no-progress", "--single-branch",
  57. "-b", "packages/"+name, url, name)
  58. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  59. _, stderr, err := capture(cmd)
  60. if err != nil {
  61. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  62. }
  63. return true, nil
  64. } else if err != nil {
  65. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  66. }
  67. cmd := passToGit(filepath.Join(path, name), "fetch")
  68. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  69. _, stderr, err := capture(cmd)
  70. if err != nil {
  71. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  72. }
  73. return true, nil
  74. }
  75. func gitDownload(url string, path string, name string) (bool, error) {
  76. _, err := os.Stat(filepath.Join(path, name, ".git"))
  77. if os.IsNotExist(err) {
  78. cmd := passToGit(path, "clone", "--no-progress", url, name)
  79. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  80. _, stderr, err := capture(cmd)
  81. if err != nil {
  82. return false, fmt.Errorf("error cloning %s: %s", name, stderr)
  83. }
  84. return true, nil
  85. } else if err != nil {
  86. return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  87. }
  88. cmd := passToGit(filepath.Join(path, name), "fetch")
  89. cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
  90. _, stderr, err := capture(cmd)
  91. if err != nil {
  92. return false, fmt.Errorf("error fetching %s: %s", name, stderr)
  93. }
  94. return false, nil
  95. }
  96. func gitMerge(path string, name string) error {
  97. _, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
  98. if err != nil {
  99. return fmt.Errorf("error resetting %s: %s", name, stderr)
  100. }
  101. _, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
  102. if err != nil {
  103. return fmt.Errorf("error merging %s: %s", name, stderr)
  104. }
  105. return nil
  106. }
  107. // DownloadAndUnpack downloads url tgz and extracts to path.
  108. func downloadAndUnpack(url string, path string) error {
  109. err := os.MkdirAll(path, 0755)
  110. if err != nil {
  111. return err
  112. }
  113. fileName := filepath.Base(url)
  114. tarLocation := filepath.Join(path, fileName)
  115. defer os.Remove(tarLocation)
  116. err = downloadFile(tarLocation, url)
  117. if err != nil {
  118. return err
  119. }
  120. _, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
  121. if err != nil {
  122. return fmt.Errorf("%s", stderr)
  123. }
  124. return nil
  125. }
  126. func getPkgbuilds(pkgs []string) error {
  127. missing := false
  128. wd, err := os.Getwd()
  129. if err != nil {
  130. return err
  131. }
  132. pkgs = removeInvalidTargets(pkgs)
  133. aur, repo, err := packageSlices(pkgs)
  134. if err != nil {
  135. return err
  136. }
  137. for n := range aur {
  138. _, pkg := splitDBFromName(aur[n])
  139. aur[n] = pkg
  140. }
  141. info, err := aurInfoPrint(aur)
  142. if err != nil {
  143. return err
  144. }
  145. if len(repo) > 0 {
  146. missing, err = getPkgbuildsfromABS(repo, wd)
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. if len(aur) > 0 {
  152. allBases := getBases(info)
  153. bases := make([]Base, 0)
  154. for _, base := range allBases {
  155. name := base.Pkgbase()
  156. _, err = os.Stat(filepath.Join(wd, name))
  157. switch {
  158. case err != nil && !os.IsNotExist(err):
  159. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  160. continue
  161. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"), shouldUseGit(filepath.Join(wd, name)):
  162. if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
  163. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  164. continue
  165. }
  166. default:
  167. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  168. continue
  169. }
  170. bases = append(bases, base)
  171. }
  172. if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
  173. return err
  174. }
  175. missing = missing || len(aur) != len(info)
  176. }
  177. if missing {
  178. err = fmt.Errorf("")
  179. }
  180. return err
  181. }
  182. // GetPkgbuild downloads pkgbuild from the ABS.
  183. func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
  184. var wg sync.WaitGroup
  185. var mux sync.Mutex
  186. var errs MultiError
  187. names := make(map[string]string)
  188. missing := make([]string, 0)
  189. downloaded := 0
  190. dbList, err := alpmHandle.SyncDBs()
  191. if err != nil {
  192. return false, err
  193. }
  194. for _, pkgN := range pkgs {
  195. var pkg *alpm.Package
  196. var err error
  197. var url string
  198. pkgDB, name := splitDBFromName(pkgN)
  199. if pkgDB != "" {
  200. if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
  201. pkg = db.Pkg(name)
  202. }
  203. } else {
  204. dbList.ForEach(func(db alpm.DB) error {
  205. if pkg = db.Pkg(name); pkg != nil {
  206. return fmt.Errorf("")
  207. }
  208. return nil
  209. })
  210. }
  211. if pkg == nil {
  212. missing = append(missing, name)
  213. continue
  214. }
  215. name = pkg.Base()
  216. if name == "" {
  217. name = pkg.Name()
  218. }
  219. // TODO: Check existence with ls-remote
  220. // https://git.archlinux.org/svntogit/packages.git
  221. switch pkg.DB().Name() {
  222. case "core", "extra", "testing":
  223. url = "https://git.archlinux.org/svntogit/packages.git"
  224. case "community", "multilib", "community-testing", "multilib-testing":
  225. url = "https://git.archlinux.org/svntogit/community.git"
  226. default:
  227. missing = append(missing, name)
  228. continue
  229. }
  230. _, err = os.Stat(filepath.Join(path, name))
  231. switch {
  232. case err != nil && !os.IsNotExist(err):
  233. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  234. continue
  235. case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
  236. if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
  237. fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
  238. continue
  239. }
  240. default:
  241. fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
  242. continue
  243. }
  244. names[name] = url
  245. }
  246. if len(missing) != 0 {
  247. fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
  248. }
  249. download := func(pkg string, url string) {
  250. defer wg.Done()
  251. if _, err := gitDownloadABS(url, config.BuildDir, pkg); err != nil {
  252. errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
  253. return
  254. }
  255. _, stderr, err := capture(exec.Command("ln", "-s", filepath.Join(config.BuildDir, pkg, "trunk"), filepath.Join(path, pkg)))
  256. mux.Lock()
  257. downloaded++
  258. if err != nil {
  259. errs.Add(fmt.Errorf("%s Failed to link %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(stderr))))
  260. } else {
  261. fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
  262. }
  263. mux.Unlock()
  264. }
  265. count := 0
  266. for name, url := range names {
  267. wg.Add(1)
  268. go download(name, url)
  269. count++
  270. if count%25 == 0 {
  271. wg.Wait()
  272. }
  273. }
  274. wg.Wait()
  275. errs.Add(os.RemoveAll(filepath.Join(config.BuildDir, "packages")))
  276. return len(missing) != 0, errs.Return()
  277. }