apt-download 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. set -e
  3. LOADED_APT_DOWNLOAD="yes"
  4. function check-and-download() {
  5. remote_file=$1
  6. local_file=$2
  7. wget -q --spider ${remote_file}
  8. if [ $? -eq 0 ]; then
  9. echo "downloading ${remote_file}"
  10. wget -q -N -O ${local_file} ${remote_file}
  11. return
  12. fi
  13. return 0
  14. }
  15. function apt-download-binary() {
  16. base_url=$1
  17. dist=$2
  18. repo=$3
  19. arch=$4
  20. dest_base_dir=$5
  21. dest_dir="${dest_base_dir}/dists/${dist}"
  22. [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
  23. check-and-download "${base_url}/dists/${dist}/Contents-${arch}.gz" "${dest_dir}/Contents-${arch}.gz"
  24. check-and-download "${base_url}/dists/${dist}/InRelease" "${dest_dir}/InRelease" || true
  25. check-and-download "${base_url}/dists/${dist}/Release" "${dest_dir}/Release"
  26. check-and-download "${base_url}/dists/${dist}/Release.gpg" "${dest_dir}/Release.gpg" || true
  27. dest_dir="${dest_base_dir}/dists/${dist}/${repo}/binary-${arch}"
  28. [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
  29. pkgidxes=('Packages' 'Packages.gz' 'Packages.bz2' 'Packages.xz')
  30. pkgidx_content=""
  31. for pkgidx in ${pkgidxes[@]}; do
  32. pkgidx_file="${dest_dir}/${pkgidx}"
  33. pkglist_url="${base_url}/dists/${dist}/${repo}/binary-${arch}/${pkgidx}"
  34. check-and-download "${pkglist_url}" ${pkgidx_file} || true
  35. if [ -z "${pkgidx_content}" -a -f ${pkgidx_file} ]; then
  36. echo "getting packages index content"
  37. case $pkgidx in
  38. "*.bz2")
  39. pkgidx_content=`bunzip2 -c ${pkgidx_file}`
  40. ;;
  41. "*.gz")
  42. pkgidx_content=`gunzip -c ${pkgidx_file}`
  43. ;;
  44. *)
  45. pkgidx_content=`cat ${pkgidx_file}`
  46. ;;
  47. esac
  48. fi
  49. done
  50. if [ -z "${pkgidx_content}" ]; then
  51. echo "index is empty, failed"
  52. return 1
  53. fi
  54. (echo -e "${pkgidx_content}" | grep '^Filename' | cut -d' ' -f 2) | while read pkg_filename; do
  55. dest_filename="${dest_base_dir}/${pkg_filename}"
  56. dest_dir=`dirname ${dest_filename}`
  57. [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
  58. pkg_url="${base_url}/${pkg_filename}"
  59. if [ -f ${dest_filename} ]; then
  60. echo "Skipping ${pkg_filename}"
  61. else
  62. echo "downloading ${pkg_url}"
  63. # touch ${dest_filename}
  64. wget -q -O ${dest_filename} ${pkg_url} || true
  65. fi
  66. done
  67. }