OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
5.9 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: bin/sde-download2
  6. # Copyright (C) 2007 - 2008 The OpenSDE Project
  7. #
  8. # More information can be found in the files COPYING and README.
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; version 2 of the License. A copy of the
  13. # GNU General Public License can be found in the file COPYING.
  14. # --- SDE-COPYRIGHT-NOTE-END ---
  15. #Description: Download sources
  16. [ -n "$SDEROOT" ] ||
  17. export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
  18. . $SDEROOT/lib/libsde.in
  19. download_usage() {
  20. local progname=${0##*/}
  21. cat <<EOT >&2
  22. Usage: $progname [DISCRIMINATOR] [OPTIONS] [ITEMS...]
  23. Accepted discriminators:
  24. * config (cfg)
  25. * package (pkg) - default
  26. * repository (repo)
  27. For more information read \`sde help download\`.
  28. EOT
  29. }
  30. # detect discriminator
  31. discriminator=package
  32. if [ $# -gt 0 ]; then
  33. case "$1" in
  34. -*) ;;
  35. config|cfg)
  36. discriminator=config; shift ;;
  37. package|pkg)
  38. discriminator=package; shift ;;
  39. repository|repo)
  40. discriminator=repository; shift ;;
  41. *)
  42. echo_error "$1: invalid discriminator"
  43. download_usage
  44. exit -1 ;;
  45. esac
  46. fi
  47. shortopts='nqvc:m:e'
  48. longopts='dry-run,quiet,verbose,cfg,timeout:,cfg,check:,mirror:,alt-dir:,mode:,extenders'
  49. options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
  50. if [ $? -ne 0 ]; then
  51. download_usage
  52. exit -1
  53. fi
  54. # load new arguments list
  55. eval set -- "$options"
  56. dryrun=
  57. verbose=1
  58. timeout=normal
  59. check=always
  60. mirror=
  61. altdir=
  62. mode=hardlink
  63. extenders=
  64. config=
  65. # load global settings
  66. eval $( $SDEROOT/bin/sde-config-ini --file=$SDESETTINGS download )
  67. eval $( $SDEROOT/bin/sde-config-ini --file=$SDESETTINGS download-$sdever )
  68. while [ $# -gt 0 ]; do
  69. case "$1" in
  70. --) shift; break ;;
  71. -n|--dry-run)
  72. dryrun=yes ;;
  73. -e|--extenders)
  74. extenders=yes ;;
  75. -v|--verbose)
  76. let verbose++ ;;
  77. -q|--quiet)
  78. let verbose-- ;;
  79. --timeout)
  80. shift
  81. case "$1" in
  82. normal|long|no)
  83. timeout="$1" ;;
  84. *)
  85. echo_abort 1 "$1: invalid timeout value: [normal*|long|no]"
  86. ;;
  87. esac ;;
  88. --check)
  89. shift
  90. case "$1" in
  91. always|download|never)
  92. check="$1" ;;
  93. *)
  94. echo_abort 1 "$1: invalid ckeck value: [always*|download|never]"
  95. ;;
  96. esac ;;
  97. --mirror)
  98. shift
  99. case "$1" in
  100. none|auto|http://*|https://*|ftp://*|file://*)
  101. mirror="$1" ;;
  102. *)
  103. echo_abort 1 "$1: invalid mirror value"
  104. ;;
  105. esac ;;
  106. --alt-dir)
  107. shift
  108. if [ "$1" = "none" -o -z "$1" ]; then
  109. altdir=
  110. $SDEROOT/bin/sde-config-ini --file=$SDESETTINGS --delete "download.altdir"
  111. elif [ -d "$1/" ]; then
  112. altdir="$( cd "$1"; pwd -P )"
  113. $SDEROOT/bin/sde-config-ini --file=$SDESETTINGS "download.altdir=$altdir"
  114. else
  115. echo_abort 1 "$1: directory not found."
  116. fi ;;
  117. -m|--mode)
  118. shift
  119. case "$1" in
  120. hardlink|copy|move)
  121. mode="$1" ;;
  122. *)
  123. echo_abort 1 "$1: invalid altdir mode [hardlink*|copy|move]"
  124. ;;
  125. esac ;;
  126. -c|--cfg)
  127. shift
  128. config=${1:-default}
  129. ;;
  130. esac
  131. shift
  132. done
  133. # verbose messages
  134. if [ $verbose -gt 1 -a -d "$altdir" ]; then
  135. echo_info "Using '$altdir' to look for files locally."
  136. fi
  137. # constants to pass to sde-download-get
  138. downloadgetopt=
  139. if [ $verbose -gt 2 ]; then
  140. downloadgetopt='-v'
  141. elif [ $verbose -lt 0 ]; then
  142. downloadgetopt='-q'
  143. fi
  144. if [ -n "$dryrun" ]; then
  145. downloadgetopt="$downloadgetopt --dry-run"
  146. fi
  147. case "$discriminator" in
  148. package)
  149. $SDEROOT/bin/sde-list-download ${config:+--cfg $config} ${extenders:+--extenders} -pkl -- "$@"
  150. ;;
  151. repository)
  152. $SDEROOT/bin/sde-list-download ${config:+--cfg $config} ${extenders:+--extenders} -rpkl -- "$@"
  153. ;;
  154. config)
  155. [ -z "$config" ] || set -- "$config" "$@"
  156. [ $# -gt 0 ] || set -- default
  157. for config; do
  158. $SDEROOT/bin/sde-list-download --cfg "$config" -pkl
  159. done
  160. ;;
  161. esac | while read pkg cksum file location; do
  162. # temporal download location
  163. tmpfile="$SDEROOT/tmp/down.$( echo $file | tr '/' '-' )"
  164. mkdir -p "$SDEROOT/tmp"
  165. [ $? -eq 0 ] || exit -1
  166. if [ -s "$SDEROOT/$file" ]; then
  167. :
  168. elif [ -e "$tmpfile.lock" ]; then
  169. echo_warning "$file: skipping."
  170. else
  171. check_this=yes
  172. # look on $altdir, to save time, bandwidth and diskspace
  173. #
  174. if [ -n "$altdir" ]; then
  175. altfile=$(find $altdir/ -name ${file##*/} | head -n 1)
  176. if [ -r "$altfile" ]; then
  177. # a file was found, what should I do with it?
  178. [ $verbose -le 0 ] || echo_info "$pkg:${file##*/} found in $altfile ($mode)."
  179. [ "$check" = "always" ] || check_this=no
  180. case "$mode" in
  181. hardlink) cp -lf "$altfile" "$tmpfile" ;;
  182. copy) cp -f "$altfile" "$tmpfile" ;;
  183. move) mv -f "$altfile" "$tmpfile" ;;
  184. esac
  185. fi
  186. fi
  187. # try to download the file
  188. #
  189. if [ ! -s "$tmpfile" ]; then
  190. [ "$check" != "never" ] || check_this=no
  191. # verbose message
  192. if [ $verbose -le 0 ]; then
  193. :
  194. elif [ $verbose -le 1 ]; then
  195. echo_info "Downloading $pkg:$file"
  196. else
  197. echo_info "Downloading $pkg:$file ($cksum) $location"
  198. fi
  199. "$SDEROOT"/bin/sde-download-get $downloadgetopt -- "$tmpfile" $location
  200. if [ $? -ne 0 ]; then
  201. echo_warning "$file: failed to download"
  202. rm -f "$tmpfile"
  203. fi
  204. fi
  205. # validate the file against the known checksum
  206. #
  207. if [ -s "$tmpfile" -a "$check_this" = "yes" -a "$cksum" != "X" ]; then
  208. cksum_real=$( sh $SDEROOT/lib/sde-download/cksum.sh $tmpfile | cut -d' ' -f2 )
  209. if [ "$cksum" = "0" ]; then
  210. echo_warning "$pkg:${file##*/} is missing a checksum value, ignoring. ($cksum_real)"
  211. elif [ "$cksum" != "$cksum_real" ]; then
  212. echo_error "$pkg:${file##*/} has a wrong checksum, failing. ($cksum != $cksum_real)"
  213. mv -f "$tmpfile" "$tmpfile.cksum-err"
  214. elif [ $verbose -gt 0 ]; then
  215. echo_info "$pkg:${file##*/} is correct."
  216. fi
  217. fi
  218. # and finally accept the file
  219. #
  220. if [ -s "$tmpfile" ]; then
  221. mkdir -p "${file%/*}"
  222. mv -f "${tmpfile}" "$file"
  223. fi
  224. fi
  225. done