]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
27e005a167bb61898c550bb8fc3b80ebcab0a8df
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # $Revision$, $Date$
4 # Exit codes:
5 #       0 - succesful
6 #       1 - help dispayed
7 #       2 - no spec file name in cmdl parameters
8 #       3 - spec file not stored in repo
9 #       4 - some source, apatch or icon files not stored in repo
10 #       5 - build package no succed
11
12 VERSION="\
13 Build package utility from PLD CVS repository
14 V 0.8 (C) 1999 Tomasz K³oczko".
15
16 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
17  
18 COMMAND="build"
19
20 SPECFILE=""
21 BE_VERBOSE=""
22 QUIET=""
23 CLEAN=""
24 DEBUG=""
25 NOURLS=""
26 NOCVS=""
27 ALLWAYS_CVSUP="yes"
28 CVSROOT=${CVSROOT:-""}
29 LOGFILE=""
30 CHMOD="yes"
31 RPMOPTS=""
32
33 PATCHES=""
34 SOURCES=""
35 ICONS=""
36 PACKAGE_RELEASE=""
37 PACKAGE_VERSION=""
38 PACKAGE_NAME=""
39
40 DEF_NICE_LEVEL=0
41
42 GETURI="wget -c -nd -t0"
43
44 if [ -f ~/etc/builderrc ]; then
45   . ~/etc/builderrc
46 elif [ -f ~/.builderrc ]; then
47   . ~/.builderrc
48 fi
49
50 #---------------------------------------------
51 # functions
52
53 usage()
54 {
55     if [ -n "$DEBUG" ]; then set -xv; fi
56     echo "\
57 Usage: builder [-D] [--debug] [-V] [--version] [-a] [--as_anon] [-b] [-ba]
58         [--build] [-bb] [--build-binary] [-bs] [--build-source]
59         [-d <cvsroot>] [--cvsroot <cvsroot>] [-g] [--get] [-h] [--help]
60         [-l <logfile>] [-m] [--mr-proper] [--logtofile <logfile>] [-q] [--quiet]
61         [-r <cvstag>] [--cvstag <cvstag>] [-u] [--no-urls] [-v] [--verbose]
62         [--opts <rpm opts>] <package>.spec
63
64         -D, --debug     - enable script debugging mode,
65         -V, --version   - output builder version
66         -a, --as_anon   - get files via pserver as cvs@anoncvs.pld.org.pl,
67         -b, -ba,
68         --build         - get all files from CVS repo or HTTP/FTP and build
69                           package from <package>.spec,
70         -bb,
71         --build-binary  - get all files from CVS repo or HTTP/FTP and build
72                           binary only package from <package>.spec,
73         -bs,
74         --build-source  - get all files from CVS repo or HTTP/FTP and only
75                           pack them into src.rpm,
76         -c, --clean     - clean all temporarily created files (in BUILD,
77                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
78                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
79         -d, --cvsroot   - setup \$CVSROOT,
80         -g, --get       - get <package>.spec and all related files from
81                           CVS repo or HTTP/FTP,
82         -h, --help      - this message,
83         -l, --logtofile - log all to file,
84         -m, --mr-proper - only remove all files related to spec file and
85                           all work resources,
86         -nc, --no-cvs   - don't download from CVS, if source URL is given,
87         -nu, --no-urls  - don't try to download from FTP/HTTP location,
88         --opts          - additional options for rpm
89         -q, --quiet     - be quiet,
90         -r, --cvstag    - build package using resources from specified CVS
91                           tag,
92         -v, --verbose   - be verbose,
93
94 "
95 }
96
97 parse_spec()
98 {
99     if [ -n "$DEBUG" ]; then 
100         set -x;
101         set -v; 
102     fi
103
104     SOURCES="`rpm -bp --nobuild --define "__spec_prep_pre %{dump}" $SPECFILE 2>&1 | awk '/ SOURCEURL[0-9]+/ {print $3}'`"
105     PATCHES="`rpm -bp --nobuild --define "__spec_prep_pre %{dump}" $SPECFILE 2>&1 | awk '/ PATCHURL[0-9]+/ {print $3}'`"
106     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
107     PACKAGE_NAME="`rpm -bp --nobuild --define "__spec_prep_pre %{dump}" $SPECFILE 2>&1 | awk '/ name/ {print $3}'`"
108     PACKAGE_VERSION="`rpm -bp --nobuild --define "__spec_prep_pre %{dump}" $SPECFILE 2>&1 | awk '/ PACKAGE_VERSION/ {print $3}'`"
109     PACKAGE_RELEASE="`rpm -bp --nobuild --define "__spec_prep_pre %{dump}" $SPECFILE 2>&1 | awk '/ PACKAGE_RELEASE/ {print $3}'`"
110
111     if [ -n "$BE_VERBOSE" ]; then
112         echo "- Sources :  `nourl $SOURCES`" 
113         if [ -n "$PATCHES" ]; then
114                 echo "- Patches :  `nourl $PATCHES`"
115         else
116                 echo "- Patches :  *no patches needed*"
117         fi
118         if [ -n "$ICONS" ]; then
119             echo "- Icon    :  `nourl $ICONS`"
120         else
121             echo "- Icon    :  *no package icon*"
122         fi
123         echo "- Name    : $PACKAGE_NAME"
124         echo "- Version : $PACKAGE_VERSION"
125         echo "- Release : $PACKAGE_RELEASE"
126     fi
127 }
128
129 Exit_error()
130 {
131     if [ -n "$DEBUG" ]; then 
132         set -x;
133         set -v; 
134     fi
135
136     cd $__PWD
137
138     case "$@" in
139     "err_no_spec_in_cmdl" )
140         echo "ERROR: spec file name not specified.";
141         exit 2 ;;
142     "err_no_spec_in_repo" )
143         echo "Error: spec file not stored in CVS repo.";
144         exit 3 ;;
145     "err_no_source_in_repo" )
146         echo "Error: some source, patch or icon files not stored in CVS repo.";
147         exit 4 ;;
148     "err_build_fail" )
149         echo "Error: package build failed.";
150         exit 5 ;;
151     esac
152 }
153
154 init_builder()
155 {
156     if [ -n "$DEBUG" ]; then 
157         set -x;
158         set -v; 
159     fi
160
161     SOURCE_DIR="`rpm --eval "%{_sourcedir}"`"
162     SPECS_DIR="`rpm --eval "%{_specdir}"`"
163
164     __PWD=`pwd`
165 }
166
167 get_spec()
168 {
169     if [ -n "$DEBUG" ]; then 
170         set -x;
171         set -v; 
172     fi
173
174     cd $SPECS_DIR
175
176     OPTIONS="up "
177
178     if [ -n "$CVSROOT" ]; then
179         OPTIONS="-d $CVSROOT $OPTIONS"
180     fi
181     if [ -n "$CVSTAG" ]; then
182         OPTIONS="$OPTIONS -r $CVSTAG"
183     else
184         OPTIONS="$OPTIONS -A"
185     fi
186
187     cvs $OPTIONS $SPECFILE
188     if [ "$?" -ne "0" ]; then
189         Exit_error err_no_spec_in_repo;
190     fi
191         if [ ! -f "$SPECFILE" ]; then
192         Exit_error err_no_spec_in_repo;
193         fi
194     
195     if [ "$CHMOD" = "yes" ]; then
196         chmod 444 $SPECFILE
197     fi
198     unset OPTIONS
199 }
200
201 get_all_files()
202 {
203     if [ -n "$DEBUG" ]; then 
204         set -x;
205         set -v; 
206     fi
207
208     if [ -n "$SOURCES$PATCHES$ICONS" ]; then
209         cd $SOURCE_DIR
210
211         OPTIONS="up "
212         if [ -n "$CVSROOT" ]; then
213             OPTIONS="-d $CVSROOT $OPTIONS"
214         fi
215         if [ -n "$CVSTAG" ]; then
216             OPTIONS="$OPTIONS -r $CVSTAG"
217         else
218             OPTIONS="$OPTIONS -A"
219         fi
220         for i in $SOURCES $PATCHES $ICONS; do
221             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ] 
222               then
223                 if 
224                         echo $i | grep -vE '(http|ftp|https|cvs)://' |\
225                         grep -qE '\.(gz|bz2)$'
226                 then
227                         echo "Warning: no URL given for $i"
228                 fi
229                 
230                 if      [ -z "$NOCVS" ]||\
231                         [ `echo $i | grep -vE '(ftp|http|https)://'` ]
232                 then
233                         cvs $OPTIONS `nourl $i`
234                 fi
235                 
236                 if      [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ]&&\
237                         [ `echo $i | grep -E 'ftp://|http://|https://'` ]
238                 then
239                         ${GETURI} "$i"
240                 fi
241
242                 if [ ! -f "`nourl $i`" ]; then
243                         Exit_error err_no_source_in_repo;
244                 fi
245             fi
246         done
247         
248         if [ "$CHMOD" = "yes" ]; then
249             chmod 444 `nourl $SOURCES $PATCHES $ICONS`
250         fi
251         unset OPTIONS
252     fi
253 }
254
255 build_package()
256 {
257     if [ -n "$DEBUG" ]; then 
258         set -x;
259         set -v; 
260     fi
261
262     cd $SPECS_DIR
263     case "$COMMAND" in
264         build )
265             BUILD_SWITCH="-ba" ;;
266         build-binary )
267             BUILD_SWITCH="-bb" ;;
268         build-source )
269             BUILD_SWITCH="-bs --nodeps" ;;
270     esac
271     nice -n ${DEF_NICE_LEVEL} rpm $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $SPECFILE 
272
273     if [ "$?" -ne "0" ]; then
274         Exit_error err_build_fail;
275     fi
276     unset BUILD_SWITCH
277 }
278
279 nourl()
280 {
281         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\)://.*/##g'
282 }
283 #---------------------------------------------
284 # main()
285
286 if [ "$#" = 0 ]; then
287     usage;
288     exit 1
289 fi
290
291 while test $# -gt 0 ; do
292     case "${1}" in
293         -D | --debug )
294             DEBUG="yes"; shift ;;
295         -V | --version )
296             COMMAND="version"; shift ;;
297         -a | --as_anon )
298             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
299         -b | -ba | --build )
300             COMMAND="build"; shift ;;
301         -bb | --build-binary )
302             COMMAND="build-binary"; shift ;;
303         -bs | --build-source )
304             COMMAND="build-source"; shift ;;
305         -c | --clean )
306             CLEAN="--clean --rmspec --rmsource"; shift ;;
307         -d | --cvsroot )
308             shift; CVSROOT="${1}"; shift ;;
309         -g | --get )
310             COMMAND="get"; shift ;;
311         -h | --help )
312             COMMAND="usage"; shift ;;
313         -l | --logtofile )
314             shift; LOGFILE="${1}"; shift ;;
315         -ni| --nice )
316             shift; DEF_NICE_LEVEL=${1}; shift ;;
317         -m | --mr-proper )
318             COMMAND="mr-proper"; shift ;;
319         -nc | --no-cvs )
320             NOCVS="yes"; shift ;;
321         -nu | --no-urls )
322             NOURLS="yes"; shift ;;
323         --opts )
324             shift; RPMOPTS="${1}"; shift ;;
325         -q | --quiet )
326             QUIET="--quiet"; shift ;;
327         -r | --cvstag )
328             shift; CVSTAG="${1}"; shift ;;
329         -v | --verbose )
330             BE_VERBOSE="1"; shift ;;
331         * )
332             SPECFILE="${1}"; shift ;;
333     esac
334 done
335
336 if [ -n "$DEBUG" ]; then 
337         set -x;
338         set -v; 
339 fi
340
341 case "$COMMAND" in
342     "build" | "build-binary" | "build-source" )
343         init_builder;
344         if [ -n "$SPECFILE" ]; then
345             get_spec;
346             parse_spec;
347             get_all_files;
348             build_package;
349         else
350             Exit_error err_no_spec_in_cmdl;
351         fi
352         ;;
353     "get" )
354         init_builder;
355         if [ -n "$SPECFILE" ]; then
356             get_spec;
357             parse_spec;
358             get_all_files;
359         else
360             Exit_error err_no_spec_in_cmdl;
361         fi
362         ;;
363     "mr-proper" )
364         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
365         ;;
366     "usage" )
367         usage;;
368     "version" )
369         echo "$VERSION";;
370 esac
371
372 cd $__PWD
This page took 0.057611 seconds and 3 git commands to generate.