forked from delphix/linux-pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-packages.sh
executable file
·186 lines (171 loc) · 4.86 KB
/
query-packages.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
#
# Copyright 2020 Delphix
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
TOP="$PWD"
source "$TOP/lib/common.sh"
#
# Disable "logmust" output as that is unnecessary here, and actually
# trips up some of our testing (e.g. verify-query-packages.sh).
#
export LOGGING=false
ALL_OUTPUT_FIELDS=(name git-url dependencies can-update)
function usage() {
local output_fields="${ALL_OUTPUT_FIELDS[*]}"
(
[[ $# != 0 ]] && (
echo "$(basename "$0"): $*"
echo
)
echo "Usage:"
echo " $(basename "$0") single [-o FIELDS] PACKAGE"
echo " $(basename "$0") list [-o FIELDS] LIST_FILE"
echo ""
echo " Either display information about a single package"
echo " or a package list located under the package-lists"
echo " directory. You can also pass one of the following"
echo " special values to the list command:"
echo " all: displays all known packages"
echo " appliance: displays all packages used by appliance"
echo " linux-kernel: displays all linux kernel packages"
echo ""
echo " -o Comma delimited output fields."
echo " Possible values: ${output_fields// /, }."
echo " By default, only print package name."
echo " -h Show this help."
) >&2
exit 2
}
function print_package() {
local pkgname="$1"
local outarray=()
local deps_array=()
#
# Run this in a sub-shell so that functions and variables sourced from
# load_package_config() are reset each time we load a new package.
#
(
local field
local dep
load_package_config "$pkgname" >/dev/null
for field in "${ACTIVE_OUTPUT_FIELDS[@]}"; do
case "$field" in
name) outarray+=("$pkgname") ;;
git-url) outarray+=("${DEFAULT_PACKAGE_GIT_URL:-none}") ;;
dependencies)
for dep in $PACKAGE_DEPENDENCIES; do
check_package_exists "$dep"
deps_array+=("$dep")
done
if [[ ${#deps_array[@]} -eq 0 ]]; then
outarray+=(none)
else
# comma-separated list of dependencies
outarray+=("$(
IFS=,
echo "${deps_array[*]}"
)")
fi
;;
can-update)
type -t "update_upstream" >/dev/null &&
outarray+=(true) || outarray+=(false)
;;
esac
done
IFS=$'\t'
echo "${outarray[*]}"
) || die "Failed to print info for package '$pkgname'"
}
function query_list() {
local list="$1"
local dups
# Package list is returned in _RET_LIST by the functions below.
# Note that we should always redirect stdout to /dev/null when
# calling lib/common.sh functions as they may generate unwanted
# debug output.
if [[ $list == all ]]; then
list_all_packages >/dev/null
elif [[ $list == appliance ]]; then
# concatenate kernel and userland packages
read_package_list "$TOP/package-lists/build/kernel-modules.pkgs" >/dev/null
local kernel_list=("${_RET_LIST[@]}")
list_linux_kernel_packages >/dev/null
kernel_list+=("${_RET_LIST[@]}")
read_package_list "$TOP/package-lists/build/main.pkgs" >/dev/null
_RET_LIST+=("${kernel_list[@]}")
# check that there are no duplicates
dups=$(printf '%s\n' "${_RET_LIST[@]}" | sort | uniq -d)
[[ -z $dups ]] || die "Some apliance packages appear in both" \
"build/kernel.pkgs and build/userland.pkgs:\\n${dups}"
elif [[ $list == linux-kernel ]]; then
list_linux_kernel_packages >/dev/null
else
read_package_list "$TOP/package-lists/${list}" >/dev/null
fi
}
function do_list() {
local list="$1"
local pkgname
query_list "$list"
for pkgname in "${_RET_LIST[@]}"; do
print_package "$pkgname"
done
}
function do_single() {
local pkgname="$1"
[[ -n $pkgname ]] || usage "missing argument for pkgname"
check_package_exists "$pkgname" >/dev/null
print_package "$pkgname"
}
case "$1" in
list) target=do_list ;;
single) target=do_single ;;
*) usage >&2 ;;
esac
shift
opt_o=""
while getopts ':ho:' c; do
case "$c" in
o)
[[ -z $opt_o ]] || usage "-o appears more than once"
opt_o="$OPTARG"
;;
h) usage ;;
*) usage "illegal option -- $OPTARG" ;;
esac
done
shift $((OPTIND - 1))
#
# Parse list of fields to output
#
if [[ -n $opt_o ]]; then
ACTIVE_OUTPUT_FIELDS=()
for field in $(echo "$opt_o" | tr ',' ' '); do
for f in "${ALL_OUTPUT_FIELDS[@]}"; do
[[ $f == "$field" ]] && break
done
if [[ $f == "$field" ]]; then
ACTIVE_OUTPUT_FIELDS+=("$field")
else
usage "invalid output field '$field'"
fi
done
else
# By default, only print package name
ACTIVE_OUTPUT_FIELDS=(name)
fi
$target "$@"