0c2da399fc3514531ef77102212255efc6cd7431
[apex.git] / build / cache.sh
1 #!/bin/sh
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 ##############################################################################
10
11 CACHE_DIR="$(pwd)/.cache"
12 CACHE_HISTORY=".cache_history"
13
14 # Make sure the cache dir exists
15 function cache_dir {
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"
20 }
21
22 function cache_git_tar {
23     echo "cache_git_tar git ls-remote"
24 }
25
26 # $1 = download url
27 # $2 = filename to write to
28 function curl_file {
29     if [ -f $CACHE_DIR/$2 ]; then
30     echo "Removing stale $2"
31         rm -f $CACHE_DIR/$2
32     fi
33     echo "Downloading $1"
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
37     done
38     sed -i "/$2/d" $CACHE_DIR/$CACHE_HISTORY
39     echo "$(md5sum $CACHE_DIR/$2) $2" >> $CACHE_DIR/$CACHE_HISTORY
40 }
41
42 # $1 =  download url
43 # $2 =  remote md5
44 function populate_cache {
45     local my_md5
46     cache_dir
47
48     # get the file name
49     filename="${1##*/}"
50     # copy passed in md5
51     remote_md5=$2
52
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
59     else
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'})"
63         fi
64         if [ -z "$remote_md5" ]; then
65             echo "Got empty MD5 from remote for $filename, skipping MD5 check"
66             curl_file $1 $filename
67         else
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'})
72             fi
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
78             else
79               echo "Will use cache for ${filename}"
80             fi
81         fi
82     fi
83 }
84
85 # $1 = filename to get from cache
86 # $2 = destintation
87 function get_cached_file {
88     if [ ! -f $CACHE_DIR/$1 ]; then
89         echo "Cache file: ${CACHE_DIR}/$1 is not in cache."
90     else
91         echo "Cache file: Using cached file ${CACHE_DIR}/$1."
92         dest='.'
93         if [ -n $2 ]; then dest=$2; fi
94         cp -f $CACHE_DIR/$1 $dest
95     fi
96 }