This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
forked from felixge/depmake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.sh
168 lines (130 loc) · 3.59 KB
/
lib.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# Stop on errors or when encountering unset variables being references
set -eu
# Conventions:
#
# * private methods start with '_dm_' ('dm' stands for depmake)
# * public functions start with 'dm_'
###################
# Private methods #
###################
_dm_packet_file() {
local packet=${1}
echo ${DM_PACKETS_DIR}/${packet}.sh
}
_dm_build_dir() {
local packet=${1}
echo ${DM_BUILD_DIR}/${packet}
}
dm_sha1() {
openssl sha1 ${1} | cut -d ' ' -f 2
}
_dm_verify_sha1() {
local path=${1}
local expected_sha1=${2}
if [[ ! -f "${path}" ]]; then
echo "Error: cannot verify sha1 for non-existing file: ${path}"
return 1
fi
local actual_sha1=`dm_sha1 ${path}`
if [ "${actual_sha1}" != "${expected_sha1}" ]; then
echo "Error: expected sha1: ${expected_sha1}, but got: ${actual_sha1} for ${path}"
return 1
fi
}
_dm_verify_fingerprint() {
local packet=${1}
local deps=${@:2}
expected_fingerprint=`_dm_calculate_fingerprint ${packet} ${deps}`
actual_fingerprint=`_dm_read_fingerprint ${packet}`
if [ "${actual_fingerprint}" != "${expected_fingerprint}" ]; then
return 1
fi
}
_dm_calculate_fingerprint() {
local packet=${1}
local deps=${@:2}
local packet_file=`_dm_packet_file ${packet}`
local packet_fingerprint="${packet}-`dm_sha1 ${packet_file}`"
local dep_fingerprints=""
for dep in ${deps[@]}
do
local dep_fingerprint=`_dm_read_fingerprint ${dep}`
dep_fingerprints="${dep_fingerprints}:${dep_fingerprint}"
done
local packet_custom_fingerprint=""
# a packet may provide a custom fingerprint function, this checks if such
# a function exists
if type fingerprint >/dev/null 2>&1; then
packet_custom_fingerprint=":`fingerprint`"
fi
echo "${packet_fingerprint}${packet_custom_fingerprint}${dep_fingerprints}"
}
_dm_read_fingerprint() {
local packet=${1}
cat `_dm_fingerprint_path ${packet}`
}
_dm_fingerprint_path() {
local packet=${1}
echo "`_dm_build_dir ${packet}`/build.fingerprint"
}
_dm_write_fingerprint() {
local packet=${1}
local deps=${@:2}
local fingerprint_path=`_dm_fingerprint_path ${packet}`
local fingerprint=`_dm_calculate_fingerprint ${packet} ${deps}`
echo ${fingerprint} > ${fingerprint_path}
}
##################
# Public methods #
##################
dm_build() {
local packet=${1}
local deps=${@:2}
local packet_file=`_dm_packet_file ${packet}`
local build_dir=`_dm_build_dir ${packet}`
source ${packet_file}
echo "========== Building ${packet} =========="
if _dm_verify_fingerprint ${packet} ${deps}; then
echo "Not rebuilding, fingerprint is current."
else
mkdir -p ${build_dir}
cd ${build_dir}
time build ${DM_STACK_DIR}
_dm_write_fingerprint ${packet} ${deps}
fi
}
dm_download_file() {
local filename=${1}
local url=${2}
local sha1=${3}
if _dm_verify_sha1 ${filename} ${sha1}; then
echo "Sha1 for ${filename} matches, no need to re-download"
return 0
fi
echo "Downloading ${filename} from ${url} ..."
curl --fail -L -o ${filename} ${url}
_dm_verify_sha1 ${filename} ${sha1}
}
dm_download_git() {
local repo=${1}
local ref=${2}
local clone_dir=${3}
code=`cd ${clone_dir} && git reset ${ref} -q --hard; echo $?`
if [ ${code} -eq 0 ]; then
return 0
fi
code=`cd ${clone_dir} && git fetch && git reset ${ref} -q --hard; echo $?`
if [ ${code} -eq 0 ]; then
return 0
fi
rm -rf ${clone_dir}
git clone ${repo} ${clone_dir}
( cd ${clone_dir} && git reset ${ref} -q --hard )
}
dm_download_svn() {
local repo=${1}
local ref=${2}
local checkout_dir=${3}
svn checkout -r ${ref} ${repo} ${checkout_dir}
}