-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmtps-tag
executable file
·209 lines (186 loc) · 4.92 KB
/
mtps-tag
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/bash -efu
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2019, 2023
# Author: Andrei Stepanov <[email protected]>
shopt -s extglob
# Source `mtps-setup' from $PATH
if command -v "mtps-setup" >/dev/null; then source "mtps-setup"; fi
# If previous fails source `mtps-setup` from this script dir
if [ -z "${YUMDNFCMD:-}" ]; then source "$(dirname "${BASH_SOURCE[0]}")/mtps-setup" || ( echo "Cannot source mtps-setup" >&2; exit 91 ); fi
tagged2tasks() {
local xml="$1" && shift
local tmpfile="$(mktemp)"
echo "$xml" > "$tmpfile"
local cmd="cat //member[name='task_id']//value/int/text()"
local tasks="$(echo "$cmd" | xmllint --shell "$tmpfile" | sed -n -e '/^[[:digit:]]/p')"
debug "Tag tasks:\n${tasks}\n---"
echo "$tasks"
}
tagged2nvrs() {
local xml="$1" && shift
local tmpfile="$(mktemp)"
echo "$xml" > "$tmpfile"
local cmd="cat //member[name='nvr']//value/string/text()"
local nvrs="$(echo "$cmd" | xmllint --shell "$tmpfile" | sed -n -e '/^[[:alnum:]]/p')"
debug "Tag tasks:\n${nvrs}\n---"
echo "$nvrs"
}
tag_info2tag_id() {
local xml="$1" && shift
local task=
tag_id="$(echo "$xml" | xmllint --xpath 'string(//member[name="id"]/value/int/text())' -)"
debug "Tag name: $tag_id"
echo "$tag_id"
}
send_query() {
local query="$1" && shift
local answer
answer="$(curl --silent -k --data "$query" "$BREWHUB")"
debug "Brew answer:\n${answer}"
echo "$answer"
}
# Use:
#
# 'koji list-api' to get a list of available XMLRPC.
# 'brew --debug-xmlrpc' to get XML
#
brew_get_tag() {
# tag may be either
# a string (the tag name) or an int (the tag ID)
local tag="$1" && shift
local re='^[0-9]+$'
[[ $tag =~ $re ]] && tag="<int>$tag</int>"
local query="$(cat << EOF
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>getTag</methodName>
<params>
<param>
<value>
<string>$tag</string>
</value>
</param>
</params>
</methodCall>
EOF
)"
debug "Brew query for getTag: ${query:0:200}...(truncated)"
send_query "$query"
}
brew_list_tagged() {
# tag: tag name or ID number
local tag="$1" && shift
local re='^[0-9]+$'
[[ $tag =~ $re ]] && tag="<int>$tag</int>"
local query="$(cat << EOF
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>listTagged</methodName>
<params>
<param>
<value>
<string>$tag</string>
</value>
</param>
</params>
</methodCall>
EOF
)"
debug "Brew query for listTagged: ${query:0:200}...(truncated)"
send_query "$query"
}
msg_usage() {
cat << EOF
Inspect Brew tag.
Usage:
$PROG <options>
Options:
--tname tag name
-i, --id print tag id
-t, --tasks list tasks
-n, --nvrs list nvrs
-h, --help display this help and exit
-v, --verbose turn on debug
EOF
}
# Entry
DEBUG="${DEBUG:-}"
PROG="${PROG:-${0##*/}}"
tag_id="${tag_id:-}"
GET_TASKS="${GET_TASKS:-}"
GET_NVRS="${GET_NVRS:-}"
GET_TAG_ID="${GET_TAG_ID:-}"
BREWHUB="${BREWHUB:-https://brewhub.engineering.redhat.com/brewhub}"
# http://wiki.bash-hackers.org/howto/getopts_tutorial
opt_str="$@"
opt=$(getopt -n "$0" --options "hvtni" --longoptions "help,verbose,tasks,nvrs,id,tname:" -- "$@")
eval set -- "$opt"
while [[ $# -gt 0 ]]; do
case "$1" in
--tname)
tag_id="$2"
shift 2
;;
-t|--tasks)
GET_TASKS="yes"
shift
;;
-n|--nvrs)
GET_NVRS="yes"
shift
;;
-i|--id)
GET_TAG_ID="yes"
shift
;;
-v|--verbose)
DEBUG="yes"
shift
;;
-h|--help)
msg_usage
exit 0
;;
--)
shift
;;
*)
msg_usage
exit 1
esac
done
# Test correct invocation
if [ -z "$tag_id" ]; then
echo "Use: $PROG -h for help."
exit
fi
debug "Tag: $tag_id"
if [[ -n "$GET_NVRS" || -n "$GET_TASKS" ]]; then
list_tagged="$(brew_list_tagged "$tag_id")"
if [ -n "$GET_NVRS" ]; then
nvrs="$(tagged2nvrs "$list_tagged")"
echo "$nvrs"
elif [ -n "$GET_TASKS" ]; then
tasks_id="$(tagged2tasks "$list_tagged")"
echo "$tasks_id"
fi
exit 0
fi
if [ -n "$GET_TAG_ID" ]; then
tag="$(brew_get_tag "$tag_id")"
tag_id="$(tag_info2tag_id "$tag")"
echo "$tag_id"
exit 0
fi
echo "Use: $PROG -h for help."
exit 1