|
@@ -0,0 +1,219 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+
|
|
3
|
+TRIGGER_COPR_CONFIG=${TRIGGER_COPR_CONFIG:-}
|
|
4
|
+TRIGGER_COPR_PKGNAME=${TRIGGER_COPR_PKGNAME:-}
|
|
5
|
+
|
|
6
|
+warn() {
|
|
7
|
+ echo "$1" >&2
|
|
8
|
+}
|
|
9
|
+
|
|
10
|
+die() {
|
|
11
|
+ warn "fatal: $1"
|
|
12
|
+ exit 3
|
|
13
|
+}
|
|
14
|
+
|
|
15
|
+usage() {
|
|
16
|
+ warn "usage: $0 [options] MODE"
|
|
17
|
+ warn ""
|
|
18
|
+ warn "Options:"
|
|
19
|
+ warn ""
|
|
20
|
+ warn " -b BRN build from branch BRN (default: last tag)"
|
|
21
|
+ warn " -c COPR_PROJECT COPR project name"
|
|
22
|
+ warn " -C CONF config file"
|
|
23
|
+ warn " -n dry mode, don't do anything (just show"
|
|
24
|
+ warn " what would be done)"
|
|
25
|
+ warn " -r REL use REL as Release number in SPEC file"
|
|
26
|
+ warn " -v VER use VER as Version number in SPEC file"
|
|
27
|
+ warn " -u URL use URL as base (to get last tag and"
|
|
28
|
+ warn " compose Source0 in SPEC file)"
|
|
29
|
+ warn ""
|
|
30
|
+ warn "If -b is not used, build is launched from last tag available"
|
|
31
|
+ warn "in the GitHub repo. In that case, Release is '1' and Version"
|
|
32
|
+ warn "is deduced from the tag by removing the initial 'v'."
|
|
33
|
+ warn ""
|
|
34
|
+ warn "If -b is used, the project repo is temporarily cloned, and"
|
|
35
|
+ warn "both Version and Release are found by consulting git-describe"
|
|
36
|
+ warn "on the specified branch."
|
|
37
|
+ warn ""
|
|
38
|
+ warn "If MODE is 'scratch' (default), Release is pre-fixed by string"
|
|
39
|
+ warn "'0.scratch.' and build is triggered in scratch COPR project."
|
|
40
|
+ exit 2
|
|
41
|
+}
|
|
42
|
+
|
|
43
|
+last_version() {
|
|
44
|
+ git ls-remote --tag "$UrlBase" \
|
|
45
|
+ | grep '/tags/' \
|
|
46
|
+ | cut -d/ -f3 \
|
|
47
|
+ | grep -v '[^0-9a-z.]' \
|
|
48
|
+ | sort -V \
|
|
49
|
+ | tail -1 \
|
|
50
|
+ | sed "s/^v//"
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+mkspec() {
|
|
54
|
+ local self_version
|
|
55
|
+ local cl_date
|
|
56
|
+ self_version="$(basename "$0") $(git describe --tags)"
|
|
57
|
+ cl_date=$(LC_ALL=C date +"%a %b %d %Y")
|
|
58
|
+ sed -e "
|
|
59
|
+ s|__APP_VERSION__|$Version|
|
|
60
|
+ s|__APP_RELEASE__|$Release|
|
|
61
|
+ s|__APP_URLBASE__|$UrlBase|
|
|
62
|
+ s|__APP_BUILDSCRIPT_VERSION__|$self_version|
|
|
63
|
+ s|__APP_DATE__|$cl_date|
|
|
64
|
+ " <"$PkgName.spec.in"
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+git_guess() {
|
|
68
|
+ #
|
|
69
|
+ # Print git-guessed $1
|
|
70
|
+ #
|
|
71
|
+ local what=$1 # what we want (ver|rel)
|
|
72
|
+ local describe # full git-describe output
|
|
73
|
+ local xtra= # extra part (-N-gHASH)
|
|
74
|
+ local num=0 # num. of commits since tag (N)
|
|
75
|
+ local sha= # '.g'+sha1 of this commit (gHASH)
|
|
76
|
+ describe=$(git describe --tags --always HEAD)
|
|
77
|
+ case $describe in
|
|
78
|
+ *-*) # v1.2.3-21-g654cba
|
|
79
|
+ tag=${describe%%-*} # tag=v1.2.3
|
|
80
|
+ xtra=${describe#$tag-} # xtra=-21-g654cba
|
|
81
|
+ num=${xtra%%-*} # num=21
|
|
82
|
+ sha=.${xtra#$num-} # sha=.g654cba
|
|
83
|
+ ;;
|
|
84
|
+ *)
|
|
85
|
+ tag=$describe
|
|
86
|
+ ;;
|
|
87
|
+ esac
|
|
88
|
+ case $what in
|
|
89
|
+ ver) echo "${tag#v}" ;;
|
|
90
|
+ rel) echo "$((num + 1))$sha" ;;
|
|
91
|
+ esac
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+choose_relpfx() {
|
|
95
|
+ #
|
|
96
|
+ # Choose COPR project based on $Mode
|
|
97
|
+ #
|
|
98
|
+ test "$Mode" == scratch && echo 0.scratch.
|
|
99
|
+ return 0
|
|
100
|
+}
|
|
101
|
+
|
|
102
|
+read_conffile() {
|
|
103
|
+ #
|
|
104
|
+ # Read item T
|
|
105
|
+ #
|
|
106
|
+ local what=$1
|
|
107
|
+ local fieldn
|
|
108
|
+ case $what in
|
|
109
|
+ urlbase) fieldn=2 ;;
|
|
110
|
+ copr) fieldn=3 ;;
|
|
111
|
+ esac
|
|
112
|
+ grep -v '^[[:blank:]]*#' "$ConfFile" \
|
|
113
|
+ | grep "^ *$Mode\>" \
|
|
114
|
+ | sed 's/ */ /g' \
|
|
115
|
+ | cut -d' ' -f$fieldn
|
|
116
|
+}
|
|
117
|
+
|
|
118
|
+choose_conffile() {
|
|
119
|
+ #
|
|
120
|
+ # Find config file and echo its name
|
|
121
|
+ #
|
|
122
|
+ find . -name '*.trigger' \
|
|
123
|
+ | cut -d/ -f2 \
|
|
124
|
+ | grep .
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+do_action() {
|
|
128
|
+ local url
|
|
129
|
+ case $Action in
|
|
130
|
+ build)
|
|
131
|
+ copr build "$CoprProject" "$Tmp/$PkgName.spec"
|
|
132
|
+ ;;
|
|
133
|
+ demo)
|
|
134
|
+ warn "demo mode active, we would build:"
|
|
135
|
+ warn " at $CoprProject"
|
|
136
|
+ test -n "$Branch" && warn " using branch $Branch"
|
|
137
|
+ test -n "$Branch" || warn " using last tag"
|
|
138
|
+ warn " from $UrlBase"
|
|
139
|
+ warn " yielding $PkgName-$Version-$Release.*.rpm"
|
|
140
|
+ warn ""
|
|
141
|
+ warn "===== BEGIN $PkgName.spec ====="
|
|
142
|
+ cat "$Tmp/$PkgName.spec"
|
|
143
|
+ warn "===== END $PkgName.spec ====="
|
|
144
|
+ return 1
|
|
145
|
+ ;;
|
|
146
|
+ mkspec)
|
|
147
|
+ cat "$Tmp/$PkgName.spec"
|
|
148
|
+ ;;
|
|
149
|
+ rpmstuff)
|
|
150
|
+ url=$(
|
|
151
|
+ grep -o 'Source.*:.*http.*' "$Tmp/$PkgName.spec" \
|
|
152
|
+ | sed "
|
|
153
|
+ s/.*http/http/
|
|
154
|
+ s/ *$//
|
|
155
|
+ s/%{version}/$Version/
|
|
156
|
+ "
|
|
157
|
+ )
|
|
158
|
+ wget --quiet "$url"
|
|
159
|
+ cat "$Tmp/$PkgName.spec" > "$PkgName.spec"
|
|
160
|
+ ;;
|
|
161
|
+ esac
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+main() {
|
|
165
|
+ local Version # Version in SPEC file
|
|
166
|
+ local Release # Release in SPEC file
|
|
167
|
+ local CoprProject # COPR project
|
|
168
|
+ local UrlBase # GitHub URL base
|
|
169
|
+ local Tmp # our temp
|
|
170
|
+ local Branch # branch to use, if empty, tags are considered
|
|
171
|
+ local Mode=scratch # implies COPR project and release prefix
|
|
172
|
+ local Action=build # what to do
|
|
173
|
+ local ConfFile # config file to use
|
|
174
|
+ local PkgName # package name
|
|
175
|
+ local es=0 # exit status
|
|
176
|
+ which copr >/dev/null \
|
|
177
|
+ || die "copr not found, try 'sudo install copr-cli'"
|
|
178
|
+ Tmp=$(mktemp -d)
|
|
179
|
+ while true; do case $1 in
|
|
180
|
+ -C) ConfFile=$2; shift 2 || usage ;;
|
|
181
|
+ -a) Action=$2; shift 2 || usage ;;
|
|
182
|
+ -b) Branch=$2; shift 2 || usage ;;
|
|
183
|
+ -c) CoprProject=$2; shift 2 || usage ;;
|
|
184
|
+ -u) UrlBase=$2; shift 2 || usage ;;
|
|
185
|
+ -r) Release=$2; shift 2 || usage ;;
|
|
186
|
+ -v) Version=${2#v}; shift 2 || usage ;;
|
|
187
|
+ -n) Action=demo; shift ;;
|
|
188
|
+ -*) usage ;;
|
|
189
|
+ *) break ;;
|
|
190
|
+ esac done
|
|
191
|
+ Mode=${1:-$Mode}
|
|
192
|
+ case $Action in
|
|
193
|
+ build|demo|mkspec|rpmstuff) : ;;
|
|
194
|
+ *) usage ;;
|
|
195
|
+ esac
|
|
196
|
+ test -n "$ConfFile" || ConfFile=$TRIGGER_COPR_CONFIG
|
|
197
|
+ test -n "$ConfFile" || ConfFile=$(choose_conffile)
|
|
198
|
+ test -n "$ConfFile" || die "could not find config file"
|
|
199
|
+ test -r "$ConfFile" || die "could not read config file"
|
|
200
|
+ test -n "$PkgName" || PkgName=$TRIGGER_COPR_PKGNAME
|
|
201
|
+ test -n "$PkgName" || PkgName=${ConfFile%.trigger}
|
|
202
|
+ test -n "$CoprProject" || CoprProject=$(read_conffile copr)
|
|
203
|
+ test -n "$UrlBase" || UrlBase=$(read_conffile urlbase)
|
|
204
|
+ if test -n "$Branch"; then
|
|
205
|
+ die "not implemented"
|
|
206
|
+ test -n "$Version" || Version=$(git_guess ver)
|
|
207
|
+ test -n "$Release" || Release=$(git_guess rel)
|
|
208
|
+ else
|
|
209
|
+ test -n "$Version" || Version=$(last_version)
|
|
210
|
+ test -n "$Release" || Release=1
|
|
211
|
+ fi
|
|
212
|
+ Release=$(choose_relpfx)$Release
|
|
213
|
+ mkspec >"$Tmp/$PkgName.spec"
|
|
214
|
+ do_action
|
|
215
|
+ rm -rf "$Tmp"
|
|
216
|
+ return $es
|
|
217
|
+}
|
|
218
|
+
|
|
219
|
+main "$@"
|