2 ##############################################################################
3 # Copyright (c) 2016 Red Hat Inc.
4 # Dan Radez <dradez@redhat.com>
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
11 CACHE_DIR="$(pwd)/cache"
13 # Make sure the cache dir exists
15 if [ ! -d $CACHE_DIR/ ]; then mkdir $CACHE_DIR/; fi
16 if [ ! -f $CACHE_DIR/.cache ]; then touch $CACHE_DIR/.cache; fi
17 echo "Cache Dir: $CACHE_DIR"
20 function cache_git_tar {
21 echo "cache_git_tar git ls-remote"
25 # $2 = filename to write to
27 if [ -f $CACHE_DIR/$2 ]; then
28 echo "Removing stale $2"
32 echo "Cache download location: $CACHE_DIR/$2"
33 until curl -C- -L -o $CACHE_DIR/$2 $1 || (( count++ >= 20 )); do
34 echo -n '' #do nothing, we just want to loop
36 sed -i "/$2/d" $CACHE_DIR/.cache
37 echo "$(md5sum $CACHE_DIR/$2) $2" >> $CACHE_DIR/.cache
41 function populate_cache {
48 # check if the cache file exists
49 # and if it has an md5 compare that
50 echo "Checking cache file exists: ${filename}"
51 if [ ! -f $CACHE_DIR/${filename} ]; then
52 echo "Cache file: ${CACHE_DIR}/${filename} missing...will download..."
53 curl_file $1 $filename
55 echo "Cache file exists...comparing MD5 checksum"
56 remote_md5="$(curl -sf -L ${1}.md5 | awk {'print $1'})"
57 if [ -z "$remote_md5" ]; then
58 echo "Got empty MD5 from remote for $filename, skipping MD5 check"
59 curl_file $1 $filename
61 my_md5=$(grep ${filename} $CACHE_DIR/.cache | awk {'print $1'})
62 if [ "$remote_md5" != "$my_md5" ]; then
63 echo "MD5 mismatch: Remote MD5 is ${remote_md5}, Cache file MD5 is ${my_md5}"
64 echo "Downloading $filename"
65 curl_file $1 $filename
67 echo "Will use cache for ${filename}"
73 # $1 = filename to get from cache
74 function get_cached_file {