/
generate_buildrequires.sh
212 lines (189 loc) · 5.35 KB
/
generate_buildrequires.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
#
# In the current package's specfile, updates a block delimited
# by "# BEGIN" / "# END" lines to contain BuildRequires: lines
# for each rubygem rpm (or rpm matching a given pattern) which
# has been built by the project.
#
# This gives us project-build-time dependency checking without the
# performance impact that specifying BuildRequires lines within
# each gem would cause. For more information, see:
#
# http://en.opensuse.org/openSUSE:Packaging_Ruby#Compensating_for_lack_of_BuildRequires
#
# Usage:
# ------
#
# 1. Ensure you have an "all-rubygems-good" package or similar
# in your project. If in doubt, copy the one from
# devel:languages:ruby:extensions.
#
# 2. cd to a working copy
#
# If you're feeling lazy, you are probably fine skipping the next two
# steps.
#
# 3. Run this script with the -l option and make sure you understand
# any differences between each repository/arch combination in the
# numbers of matching gems found.
#
# 4. If you don't, run with -l REPO ARCH to compare individual lists
# of matching gems.
#
# 5. If you want a BuildRequires: list of matching gems from *all*
# repo/arch combinations, run again with no arguments.
#
# OR
#
# If you want a BuildRequires: list of matching gems from a specific
# repo/arch combinations, run again with REPO ARCH as arguments.
#
# 6. osc diff to review the changes to the spec file, then osc commit.
me=`basename $0`
DEFAULT_PATTERN="rubygem-"
main () {
parse_opts "$@"
if [ -z "$project" ]; then
project=$( osc info | sed -ne '/^Project name: / { s///; p }' )
if [ -z "$project" ]; then
echo "Couldn't establish build service project name." >&2
echo "Are you inside a package working directory?" >&2
exit 1
fi
fi
echo "Project: $project"
case "$project" in
home:*:branches:*)
cat <<EOF >&2
WARNING: you are running this in a branch.
You probably need to specify the parent project via -P,
otherwise you may not get the dependencies you want.
EOF
;;
esac
specfile=$( ls -1 *.spec )
if ! [ -f "$specfile" ]; then
echo "Couldn't find spec file." >&2
echo "Are you inside a package working directory?" >&2
exit 1
fi
if [ -n "$list" ]; then
if [ -n "$repo" ]; then
get_buildrequires_lines "$repo" "$arch"
else
list_matches
fi
else
if [ -n "$repo" ]; then
get_buildrequires_lines "$repo" "$arch" | update_spec
else
find_all_matches | update_spec
fi
fi
}
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
case "$1" in
[0-9])
exit_code="$1"
shift
;;
*)
exit_code=1
;;
esac
if [ -n "$1" ]; then
echo "$*" >&2
echo
fi
cat <<EOF >&2
Usage: $me [options] [REPOSITORY ARCH]
Options:
-h, --help Show this help and exit
-l, --list List matching rpms for the given repository / arch.
If no repository specified, show counts of matching
rpms per repository / arch.
-P, --project=PROJ Retrieve rpm lists from PROJ, not the current project.
-p, --pattern=PAT Set the pattern to match rpms by [$DEFAULT_PATTERN]
EOF
exit "$exit_code"
}
parse_opts () {
list=
project=
pattern="$DEFAULT_PATTERN"
while [ $# != 0 ]; do
case "$1" in
-h|--help)
usage 0
;;
-l|--list)
list=y
shift
;;
-p|--pattern)
pattern="$2"
shift 2
;;
-P|--project)
project="$2"
shift 2
;;
-*)
usage "Unrecognised option: $1"
;;
*)
break
;;
esac
done
if [ $# = 1 ]; then
usage "Insufficient arguments."
fi
if [ $# -gt 2 ]; then
usage "Too many arguments."
fi
repo="$1"
arch="$2"
}
get_buildrequires_lines () {
repo="$1" arch="$2"
osc api "/build/$project/$repo/$arch/_repository" | \
grep "binary .*filename=\"$pattern" | \
sed -e 's,.* <binary filename=",,; s,\.rpm".*,,; s,^,BuildRequires: ,' | \
grep -v debuginfo
}
list_matches () {
echo
echo "Matching rpms per repository/arch:"
echo
osc repos | while read repo arch; do
count=$( get_buildrequires_lines "$repo" "$arch" | wc -l )
printf "%-17s %-8s %d\n" "$repo" "$arch" "$count"
done
echo
}
find_all_matches () {
osc repos "$project" | while read repo arch; do
echo "Obtaining BuildRequires from $repo $arch ..." >&2
get_buildrequires_lines "$repo" "$arch"
done | sort -u
}
edit_spec () {
sed -n -e '1,/BEGIN/p' $specfile
echo "# Automatically generated by $0"
echo "# on `date`"
echo "# See http://en.opensuse.org/openSUSE:Packaging_Ruby#Compensating_for_lack_of_BuildRequires"
cat
sed -n -e '/END/,$p' $specfile
}
update_spec () {
if edit_spec > $specfile.new; then
mv $specfile.new $specfile
echo "Updated spec: $specfile"
else
echo "Failed to generate new spec file contents; aborting." >&2
exit 1
fi
}
main "$@"