distro-manifest-generator: add support for JSON output format
[AGL/meta-agl.git] / scripts / distro-manifest-generator.sh
1 #!/usr/bin/env bash
2
3 ################################################################################
4 #
5 # The MIT License (MIT)
6 #
7 # Copyright (c) 2018 Stéphane Desneux <sdx@iot.bzh>
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 # SOFTWARE.
26 #
27 ################################################################################
28
29 mode=deploy
30 manifest=
31 verbose=0
32 format=bash
33 sourcefile=
34
35 function info() { echo "$@" >&2; }
36 function error() { echo "$BASH_SOURCE: $@" >&2; }
37 function out() { echo -n "$@"; }
38 function out_object() {
39         # expected stdin stream is:
40         # --------------
41         # key
42         # value
43         # key
44         # value
45         # ...
46         # --------------
47         local sep=""
48         local k
49         case $format in
50                 bash)
51                         while read x; do
52                                 [[ -z "$k" ]] && { k="$x"; continue; }
53                                 out "$sep${k}="
54                                 out_value "$x"
55                                 sep=$'\n'
56                                 k=
57                         done
58                         out "$sep"
59                         ;;
60                 json)
61                         out "{"
62                         while read x; do
63                                 [[ -z "$k" ]] && { k="$x"; continue; }
64                                 out "$sep\"${k}\":"
65                                 out_value "$x"
66                                 sep=","
67                                 k=
68                         done
69                         out "}"
70                         ;;
71         esac
72 }
73
74 function out_array() {
75         # expected stdin stream is:
76         # --------------
77         # value
78         # value
79         # ...
80         # --------------
81         local sep=""
82         case $format in
83                 bash)
84                         while read x; do
85                                 out "$sep"
86                                 out_value "$x"
87                                 sep=" "
88                         done
89                         ;;
90                 json)
91                         out "["
92                         while read x; do
93                                 out $sep
94                                 out_value "$x"
95                                 sep=","
96                         done
97                         out "]"
98                         ;;
99         esac
100 }
101
102 function out_value() {
103         # string
104         # number
105         # object
106         # array
107         # 'true'
108         # 'false'
109         # 'null'
110
111         x=$1
112
113         # litterals
114         if [[ "$x" =~ ^(true|false|null)$ ]]; then
115                 out "$x"
116         # number
117         elif [[ "$x" =~ ^[+-]?[0-9]+(\.[0-9]+)?$ ]]; then
118                 out "$x"
119         # object
120         elif [[ "$x" =~ ^\{.*\}$ ]]; then
121                 out "$x"
122         # array
123         elif [[ "$x" =~ ^\[.*\]$ ]]; then
124                 out "$x"
125         # string
126         else
127                 out "\"$(sed 's/\("\)/\\\1/g' <<<$x)\""
128         fi
129 }
130
131 function out_comment() {
132         case $format in
133                 bash)
134                         [[ "$verbose" == 1 ]] && echo "# $@" || true
135                         ;;
136                 json)
137                         ;;
138         esac
139 }
140
141 function _getgitmanifest() {
142         # this function takes the setup.manifest generated by setup script and uses DIST_METADIR
143         # to analyze git repos
144
145         local manifest=$1 mode=$2
146         [[ -f $manifest ]] && source $manifest || { error "Invalid setup manifest '$manifest'"; return 1; }
147         [[ ! -d "$DIST_METADIR" ]] && {
148                 error "Invalid meta directory. Check variable DIST_METADIR in manifest file '$manifest'."
149                 error "$BASH_SOURCE: Also, check directory '$DIST_METADIR'."
150                 return 2
151         }
152         local GIT=$(which git) REALPATH=$(which realpath)
153         [[ ! -x $GIT ]] && { error "$BASH_SOURCE: Unable to find git command in $PATH."; return 3; }
154         [[ ! -x $REALPATH ]] && { error "$BASH_SOURCE: Unable to find realpath command in $PATH."; return 4; }
155
156         local gitrepo gitrev metagitdir sep=""
157         DIST_LAYERS=""
158         for metagitdir in $(ls -d $DIST_METADIR/*/.git); do
159                 gitrepo=$($REALPATH -Ls $metagitdir/.. --relative-to=$DIST_METADIR)
160                 pushd $DIST_METADIR/$gitrepo &>/dev/null && {
161                         gitrev=$( { $GIT describe --long --dirty --always 2>/dev/null || echo "unknown_revision"; } | tr ' \t' '__' )
162                         popd &>/dev/null
163                 } || {
164                         gitrev=unknown
165                 }
166                 DIST_LAYERS="${DIST_LAYERS}${sep}${gitrepo}:${gitrev}"
167                 sep=" "
168         done
169
170         # layers checksum
171         DIST_LAYERS_MD5=$(echo $DIST_LAYERS|md5sum -|awk '{print $1;}')
172
173         # in json, transform layers in an object, features in array
174         [[ "$format" == "json" ]] && {
175                 DIST_FEATURES=$(for x in $DIST_FEATURES; do
176                         echo $x
177                 done | out_array)
178                 DIST_LAYERS=$(for x in $DIST_LAYERS; do
179                         echo ${x%%:*}
180                         echo ${x#*:}
181                 done | out_object)
182         }
183
184
185         # compute build hash
186         DIST_BUILD_HASH="F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
187         DIST_BUILD_ID="${DIST_DISTRO_NAME}-${DIST_MACHINE}-F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
188
189         # compute setup manifest path and build TS
190         DIST_SETUP_MANIFEST="$($REALPATH $manifest)"
191
192         # Manifest build timestamp
193         DIST_BUILD_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
194
195         # what to retain from setup manifest?
196         # to generate the full list: cat setup.manifest  | grep = | cut -f1 -d"=" | awk '{printf("%s ",$1);}'
197         declare -A SETUP_VARS
198         SETUP_VARS[deploy]="DIST_MACHINE DIST_FEATURES DIST_FEATURES_MD5 DIST_BUILD_HOST DIST_BUILD_OS DIST_SETUP_TS"
199         SETUP_VARS[target]="DIST_MACHINE DIST_FEATURES"
200         SETUP_VARS[sdk]="DIST_MACHINE DIST_FEATURES"
201
202         # extra vars not coming from setup.manifest but generated here
203         declare -A EXTRA_VARS
204         EXTRA_VARS[deploy]="DIST_SETUP_MANIFEST DIST_BUILD_TS DIST_LAYERS DIST_LAYERS_MD5 DIST_BUILD_HASH DIST_BUILD_ID"
205         EXTRA_VARS[target]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID"
206         EXTRA_VARS[sdk]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID"
207
208         # BITBAKE_VARS may be defined from external file to source (--source arg)
209         # this is used to dump extra vars from inside bitbake recipe
210
211         { for x in ${SETUP_VARS[$mode]} ${EXTRA_VARS[$mode]} ${BITBAKE_VARS[$mode]}; do
212                 k=$x
213                 [[ "$format" == "json" ]] && {
214                         k=${k#DIST_} # remove prefix
215                         k=${k,,*} # to lower case
216                 }
217                 echo $k
218                 echo ${!x}
219         done } | out_object
220 }
221
222 function getmanifest() {
223         local rc=0
224         out_comment "DISTRO BUILD MANIFEST"
225         out_comment
226
227         # add layers manifest
228         out_comment "----- this fragment has been generated by $BASH_SOURCE"
229         _getgitmanifest $1 $2 || rc=$?
230         out_comment "------------ end of $BASH_SOURCE fragment --------"
231
232         return $rc
233 }
234
235 function __usage() {
236         cat <<EOF >&2
237 Usage: $BASH_SOURCE [-v|--verbose] [-f|--format <fmt>] [-m|--mode <mode>] [-s|--source <file>] <setup_manifest_file>
238    Options:
239       -v|--verbose: generate comments in the output file
240       -s|--source: extra file to source (get extra variables generated from bitbake recipe)
241       -f|--format: specify output format: 'bash' or 'json'
242       -m|--mode: specify the destination for the generated manifest
243          'deploy' : for the tmp/deploy/images/* directories
244          'target' : for the manifest to be installed inside a target image
245          'sdk'    : for the manifest to be installed inside the SDK
246
247    <setup_manifest_file> is the input manifest generated from setup script
248 EOF
249 }
250
251 set -e
252
253 tmp=$(getopt -o h,v,m:,f:,s: --long help,verbose,mode:,format:,source: -n "$BASH_SOURCE" -- "$@") || {
254         error "Invalid arguments."
255         __usage
256         exit 1
257 }
258 eval set -- $tmp
259
260 while true; do
261         case "$1" in
262                 -h|--help) __usage; exit 0;;
263                 -v|--verbose) verbose=1; shift ;;
264                 -f|--format) format=$2; shift 2;;
265                 -m|--mode) mode=$2; shift 2;;
266                 -s|--source) sourcefile=$2; shift 2;;
267                 --) shift; break;;
268                 *) fatal "Internal error";;
269         esac
270 done
271
272 manifest=$1
273 shift
274 [[ ! -f "$manifest" ]] && { __usage; exit 1; }
275
276 case $mode in
277         deploy|target|sdk) ;;
278         *) error "Invalid mode specified. Allowed modes are: 'deploy', 'target', 'sdk'"; __usage; exit 42;;
279 esac
280
281 case $format in
282         bash|json) ;;
283         *) error "Invalid format specified. Allowed formats are 'json' or 'bash'"; __usage; exit 43;;
284 esac
285
286 info "Generating manifest: mode=$mode format=$format manifest=$manifest"
287 [[ -f "$sourcefile" ]] && {
288         info "Sourcing file $sourcefile"
289         . $sourcefile
290         # this may define extra vars: to be taken into account BITBAKE_VARS must be defined
291 }
292
293 [[ "$format" == "json" ]] && {
294         # if jq is present, use it to format json output
295         jq=$(which jq || true)
296         [[ -n "$jq" ]] && {
297                 getmanifest $manifest $mode | $jq ""
298                 exit ${PIPESTATUS[0]}
299         }
300 }
301
302 getmanifest $manifest $mode
303