Debian Codegraph @

build_tools/download_externals

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 if [[ ( ${BASH_VERSINFO[0]} == 4 && ${BASH_VERSINFO[1]} > 1 ) || ${BASH_VERSINFO[0]} > 4 ]] ; then
4 shopt -s compat41
5 fi
6 set -e
7
8
9 ASTTOPDIR=${ASTTOPDIR:-.}
10 export make=`sed -n -r -e "s/^MAKE\s*=\s*//gp" ${ASTTOPDIR}/makeopts`
11
12 getvar() {
13 $make --quiet --no-print-directory -f- <<EOF
14 include ${ASTTOPDIR}/makeopts
15 all:
16 @echo "\$($1)"
17 EOF
18 }
19
20 XMLSTARLET=`getvar XMLSTARLET`
21 ASTMODDIR=`getvar ASTMODDIR`
22 cache_dir=`getvar EXTERNALS_CACHE_DIR`
23 DOWNLOAD_TO_STDOUT=`getvar DOWNLOAD_TO_STDOUT`
24 HOST_CPU=`getvar HOST_CPU`
25 INSTALL=`getvar INSTALL`
26
27 module_name=${1%%-*}
28 variant=${1##*-}
29
30 if [[ "${variant}" = "${module_name}" ]] ; then
31 unset variant
32 fi
33
34 if [[ -z ${module_name} ]] ; then
35 echo "You must supply a module name."
36 exit 64
37 fi
38
39 tmpdir=$(mktemp -d)
40 if [[ -z "${tmpdir}" ]] ; then
41 echo "${module_name}: Unable to create temporary directory."
42 exit 1
43 fi
44 trap "rm -rf ${tmpdir}" EXIT
45
46 if [[ -z "${ASTMODDIR}" ]] ; then
47 echo "${module_name}: Unable to parse ${ASTTOPDIR}/makeopts."
48 exit 1
49 fi
50
51 if [[ "${XMLSTARLET}" = ":" ]] ; then
52 echo "${module_name}: The externals downloader requires xmlstarlet to be installed."
53 exit 1
54 fi
55
56 if [[ -z ${cache_dir} ]] ; then
57 cache_dir=${tmpdir}
58 fi
59
60 version=$(${ASTTOPDIR}/build_tools/make_version ${ASTTOPDIR})
61 if [[ ! ${version} =~ ^(GIT-)?(certified[/-])?([^.-]+)[.-].* ]] ; then
62 echo "${module_name}: Couldn't parse version ${version}"
63 exit 1
64 fi
65 major_version=${BASH_REMATCH[3]}
66
67 if [[ "${major_version}" == "master" ]] ; then
68 echo "${module_name}: External module downloading is not available in the 'master' git branch. Please disable in menuselect and download manually."
69 exit 1
70 fi
71
72 major_version=${major_version}.0
73
74 if [[ "${HOST_CPU}" = "x86_64" ]] ; then
75 host_bits=64
76 elif [[ "${HOST_CPU}" = "i386" ]] ; then
77 host_bits=32
78 elif [[ "${HOST_CPU}" = "i686" ]] ; then
79 host_bits=32
80 else
81 echo "${module_name}: External module downloading is not available for the ${HOST_CPU} platform. Only x86 based platforms are currently supported. Please disable this module in menuselect."
82 exit 1
83 fi
84
85 if [[ -z "${variant}" ]] ; then
86 variants=$(${XMLSTARLET} sel -t -m "/menu/category/member[@name = '${module_name}']/member_data/downloader/variants/variant" -v "@tag" -n ${ASTTOPDIR}/menuselect-tree || :)
87 member_name=${module_name}
88 for tag in ${variants} ; do
89 condition=$(${XMLSTARLET} sel -t -v "/menu/category/member[@name = '${module_name}']/member_data/downloader/variants/variant[@tag = '${tag}']/@condition" ${ASTTOPDIR}/menuselect-tree || :)
90 variant=$(eval "if $condition ; then echo $tag ; fi")
91 if [[ -n "${variant}" ]] ; then
92 break
93 fi
94 done
95 else
96 member_name=${module_name}${variant:+-${variant}}
97 fi
98
99 full_name=${module_name}${variant:+-${variant}}
100 variant_manifest=manifest${variant:+-${variant}}.xml
101
102 # Override the remote base for all packages
103 # useful for testing
104 remote_url=${REMOTE_BASE:+${REMOTE_BASE}/asterisk-${major_version}/x86-${host_bits}}
105
106 if [[ -z "${remote_url}" ]] ; then
107 remote_url=$(${XMLSTARLET} sel -t -v "/menu/category/member[@name = '${member_name}']/member_data/downloader/@remote_url" ${ASTTOPDIR}/menuselect-tree || :)
108 if [[ -n "${remote_url}" ]] ; then
109 remote_url="${remote_url}/asterisk-${major_version}/x86-${host_bits}"
110 else
111 directory_name=$(${XMLSTARLET} sel -t -v "/menu/category/member[@name = '${member_name}']/member_data/downloader/@directory_name" ${ASTTOPDIR}/menuselect-tree || :)
112 remote_url="https://downloads.digium.com/pub/telephony/${directory_name:-${module_name}}/asterisk-${major_version}/x86-${host_bits}"
113 fi
114 fi
115
116 version_convert() {
117 local v=${1##*_}
118 if [[ ${v} =~ ([0-9]+)[.]([0-9]+)[.]([0-9]+) ]] ; then
119 v=$(( ${BASH_REMATCH[1]}<<18 | ${BASH_REMATCH[2]}<<9 | ${BASH_REMATCH[3]} ))
120 fi
121 echo ${v}
122 }
123
124 ${DOWNLOAD_TO_STDOUT} ${remote_url}/${variant_manifest} > ${tmpdir}/${variant_manifest} || {
125 echo "${full_name}: Unable to fetch ${remote_url}/${variant_manifest}"
126 exit 1
127 }
128
129 rpv=$(${XMLSTARLET} sel -t -v "/package/@version" ${tmpdir}/${variant_manifest})
130 rpvi=$(version_convert ${rpv})
131 echo "${full_name}: Remote package version ${rpv} (${rpvi})"
132
133 module_dir=${full_name}-${rpv}-x86_${host_bits}
134 tarball=${module_dir}.tar.gz
135
136 export need_install=0
137
138 if [[ -f ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml ]] ; then
139 package_arch=$(${XMLSTARLET} sel -t -v "/package/@arch" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml)
140 ipv=$(${XMLSTARLET} sel -t -v "/package/@version" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml)
141 package_variant=$(${XMLSTARLET} sel -t -v "/package/@variant" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml || :)
142 ipvi=$(version_convert ${ipv})
143 ip_major=${ipv%_*}
144 echo "${full_name}: Installed package version ${ipv} (${ipvi})"
145 if [[ "${ip_major}" != "${major_version}" || "${package_arch}" != "x86_${host_bits}" || "${package_variant}" != "${variant}" ]] ; then
146 echo "${full_name}: The installed package is not for this version of Asterisk. Reinstalling."
147 need_install=1
148 elif [[ ${rpvi} > ${ipvi} ]] ; then
149 echo "${full_name}: A newer package is available"
150 need_install=1
151 else
152 sums=$(${XMLSTARLET} sel -t -m "//file" -v "@md5sum" -n ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml)
153 for sum in ${sums} ; do
154 install_path=$(${XMLSTARLET} sel -t -v "//file[@md5sum = '${sum}']/@install_path" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml )
155 executable=$(${XMLSTARLET} sel -t -v "//file[@md5sum = '${sum}']/@executable" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml || : )
156 f=${DESTDIR}$(eval echo ${install_path})
157 if [[ ! -f ${f} ]] ; then
158 echo Not found: ${f}
159 need_install=1
160 break
161 else
162 if [[ "$executable" = "yes" ]] ; then
163 # There are easier ways of doing this (objcopy --dump-section) but not in older binutils
164 length_offset=$(objdump -h $f | sed -n -r -e "s/^\s+[0-9]+\s+.ast_manifest\s+([0-9a-fA-F]+)\s+[0-9a-fA-F]+\s+[0-9a-fA-F]+\s+([0-9a-fA-F]+)\s+.*$/0x\1 0x\2/p")
165 tags=$($(eval 'printf "dd if=$f bs=1 count=%d skip=%d\n" $length_offset') 2>/dev/null)
166 if [[ -n "${tags}" && "${tags}" != "${module_name},${variant},${rpv}" ]] ; then
167 echo Tag mismatch: ${f} File: "${tags}" Manifest: "${module_name},${variant},${rpv}"
168 need_install=1
169 break
170 fi
171 fi
172
173 cs=$(${MD5} ${f} | cut -b1-32)
174 if [[ "${cs}" != "${sum}" ]] ; then
175 echo "Checksum mismatch: ${f}"
176 need_install=1
177 break
178 fi
179 fi
180 done
181 fi
182 else
183 need_install=1
184 fi
185
186 if [[ ${need_install} == 1 ]] ; then
187 if [[ ( -n "${ipvi}" ) && ${ipvi} > ${rpvi} ]] ; then
188 echo "${full_name}: Installed package is newer than that available for download."
189 exit 0
190 fi
191 else
192 echo "${full_name} is up to date."
193 exit 0;
194 fi
195
196 need_download=1
197 if [[ -f ${cache_dir}/${full_name}-${major_version}.manifest.xml ]] ; then
198 cpv=$(${XMLSTARLET} sel -t -v "/package/@version" ${cache_dir}/${full_name}-${major_version}.manifest.xml)
199 cpvi=$(version_convert ${cpv})
200 echo "${full_name}: Cached package version ${cpv} (${cpvi})"
201 if [[ ${cpvi} == ${rpvi} && ( -f ${cache_dir}/${tarball} ) ]] ; then
202 echo "${full_name}: Cached version is available."
203 need_download=0
204 fi
205 fi
206
207 if [[ ${need_download} = 1 ]] ; then
208 echo "${full_name}: Downloading ${remote_url}/${tarball} to ${cache_dir}/${tarball}"
209 ${DOWNLOAD_TO_STDOUT} ${remote_url}/${tarball} > ${cache_dir}/${tarball} || {
210 echo "${full_name}: Unable to fetch ${remote_url}/${tarball}"
211 exit 1
212 }
213 cp ${tmpdir}/${variant_manifest} ${cache_dir}/${full_name}-${major_version}.manifest.xml
214 fi
215
216 tar -xzf ${cache_dir}/${tarball} -C ${cache_dir}
217 trap "rm -rf ${cache_dir}/${module_dir} ; rm -rf ${tmpdir}" EXIT
218
219 echo "${full_name}: Installing."
220
221 if [[ $EUID == 0 ]] ; then
222 install_params="-g 0 -o 0"
223 fi
224
225 names=$(${XMLSTARLET} sel -t -m "//file" -v "@name" -n ${cache_dir}/${module_dir}/manifest.xml)
226 for name in ${names} ; do
227 source_path=${cache_dir}/${module_dir}/${name}
228 install_path=$(${XMLSTARLET} sel -t -v "//file[@name = '${name}']/@install_path" ${cache_dir}/${module_dir}/manifest.xml)
229 install_path=${DESTDIR}$(eval echo ${install_path})
230 executable=$(${XMLSTARLET} sel -t -v "//file[@name = '${name}']/@executable" ${cache_dir}/${module_dir}/manifest.xml || :)
231 if [[ "${executable}" = "yes" ]] ; then
232 mode=0755
233 else
234 mode=0644
235 fi
236
237 ${INSTALL} -Dp ${install_params} -m ${mode} ${source_path} ${install_path}
238
239 done
240 ${INSTALL} -Dp ${install_params} --mode=0644 ${cache_dir}/${module_dir}/manifest.xml ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml
241
242 echo "${full_name}: Installed."