Blame view

RIOT/dist/tools/dlcache/dlcache.sh 2.24 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  #!/bin/sh
  
  DLCACHE_DIR=${DLCACHE_DIR:-~/.dlcache}
  
  mkdir -p "$DLCACHE_DIR"
  
  _echo() {
      echo "$@" 1>&2
  }
  
  if [ "$(uname)" = Darwin ]; then
      _locked() {
          local lockfile="$1"
          shift
  
          while ! shlock -p $$ -f $lockfile; do
              sleep 0.2
          done
  
          $*
  
          rm $lockfile
      }
  else
      _locked() {
          local lockfile="$1"
          shift
  
          (
          flock -w 600 9 || exit 1
          $*
          ) 9>"$lockfile"
      }
  fi
  
  if [ "$(uname)" = Darwin ]; then
      MD5="md5 -r"
  else
      MD5=md5sum
  fi
  
  calcmd5() {
      local file="$1"
      local md5="$2"
  
      local file_md5=$(${MD5} "$file" | cut -d\  -f1)
  
      test "$md5" = "$file_md5"
  }
  
  downloader() {
      if [ -n "$(command -v wget)" ]; then
          wget -nv "$1" -O $2
      elif [ -n "$(command -v curl)" ]; then
          curl -L $1 -o $2
      else
          _echo "$0: neither wget nor curl available!"
          return 1
      fi
  }
  
  download() {
      local url="$1"
      local _md5="$2"
      local basename_url=$(basename ${url})
      local target="${3:-${basename_url}}"
  
      [ -f "$target" ] && {
          # if our target file exists, check it's md5.
          calcmd5 "$target" "$_md5" && {
              _echo "$0: target exists, md5 matches."
              exit 0
          }
      }
  
      local filename="$(basename $url)"
      [ -f "$DLCACHE_DIR/$filename" ] && {
          # if the file exists in cache, check it's md5 and possibly remove it.
          if calcmd5 "$DLCACHE_DIR/$filename" "$_md5"; then
              _echo "$0: getting \"$url\" from cache"
          else
              _echo "$0: \"$DLCACHE_DIR/$filename\" has wrong checksum, re-downloading"
              rm "$DLCACHE_DIR/$filename"
          fi
      }
  
      [ ! -f "$DLCACHE_DIR/$filename" ] && {
          _echo "$0: downloading \"$url\"..."
          downloader "$url" "$DLCACHE_DIR/$filename" || {
              _echo "$0: error downloading $url to $DLCACHE_DIR/$filename!"
              exit 1
          }
          _echo "$0: done downloading \"$url\""
      }
  
      calcmd5 "$DLCACHE_DIR/$filename" "$_md5" || {
          _echo "$0: checksum mismatch!"
          exit 1
      }
  
      if [ "$target" = "-" ]; then
          cat "$DLCACHE_DIR/$filename"
      else
          cp "$DLCACHE_DIR/$filename" "$target"
      fi
  }
  
  _locked "$DLCACHE_DIR/$(basename $1).locked" download "$@"