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"
12 CACHE_HISTORY=".cache_history"
14 # Make sure the cache dir exists
16 if [ -f $CACHE_DIR ]; then rm -rf $CACHE_DIR; fi
17 if [ ! -d $CACHE_DIR/ ]; then mkdir $CACHE_DIR/; fi
18 if [ ! -f $CACHE_DIR/$CACHE_HISTORY ]; then touch $CACHE_DIR/$CACHE_HISTORY; fi
19 echo "Cache Dir: $CACHE_DIR"
22 function cache_git_tar {
23 echo "cache_git_tar git ls-remote"
27 # $2 = filename to write to
29 if [ -f $CACHE_DIR/$2 ]; then
30 echo "Removing stale $2"
34 echo "Cache download location: $CACHE_DIR/$2"
35 until curl -C- -L -o $CACHE_DIR/$2 $1 || (( count++ >= 20 )); do
36 echo -n '' #do nothing, we just want to loop
38 sed -i "/$2/d" $CACHE_DIR/$CACHE_HISTORY
39 echo "$(md5sum $CACHE_DIR/$2) $2" >> $CACHE_DIR/$CACHE_HISTORY
44 function populate_cache {
53 # check if the cache file exists
54 # and if it has an md5 compare that
55 echo "Checking cache file exists: ${filename}"
56 if [ ! -f $CACHE_DIR/${filename} ]; then
57 echo "Cache file: ${CACHE_DIR}/${filename} missing...will download..."
58 curl_file $1 $filename
60 echo "Cache file exists...comparing MD5 checksum"
61 if [ -z $remote_md5 ]; then
62 remote_md5="$(curl -sf -L ${1}.md5 | awk {'print $1'})"
64 if [ -z "$remote_md5" ]; then
65 echo "Got empty MD5 from remote for $filename, skipping MD5 check"
66 curl_file $1 $filename
68 my_md5=$(grep ${filename} $CACHE_HISTORY | awk {'print $1'})
69 if [ -z "$my_md5" ]; then
70 echo "${filename} missing in $CACHE_HISTORY file. Caculating md5..."
71 my_md5=$(md5sum ${CACHE_DIR}/${filename} | awk {'print $1'})
73 if [ "$remote_md5" != "$my_md5" ]; then
74 echo "MD5 mismatch, local cache file MD5 is ${my_md5}"
75 echo " remote cache file MD5 is ${remote_md5}"
76 echo "Downloading $filename"
77 curl_file $1 $filename
79 echo "Will use cache for ${filename}"
85 # $1 = filename to get from cache
87 function get_cached_file {
88 if [ ! -f $CACHE_DIR/$1 ]; then
89 echo "Cache file: ${CACHE_DIR}/$1 is not in cache."
91 echo "Cache file: Using cached file ${CACHE_DIR}/$1."
93 if [ -n $2 ]; then dest=$2; fi
94 cp -f $CACHE_DIR/$1 $dest