]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
redirect errors from query to /dev/null
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # $Id$
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.9 (C) 1999-2001 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         -ns, --no-srcs  - don't downland Sources
89         --opts          - additional options for rpm
90         -q, --quiet     - be quiet,
91         -r, --cvstag    - build package using resources from specified CVS
92                           tag,
93         -T, --tag       - add cvs tags for files,
94         -v, --verbose   - be verbose,
95
96 "
97 }
98
99 parse_spec()
100 {
101     if [ -n "$DEBUG" ]; then 
102         set -x;
103         set -v;
104     fi
105
106     cd $SPECS_DIR
107
108     if [ "$NOSRCS" != "yes" ]; then
109         SOURCES="`rpm -bp --nobuild --define "prep %dump" $SPECFILE 2>&1 | awk '/SOURCEURL[0-9]+/ {print $3}'`"
110     fi
111
112     PATCHES="`rpm -bp --nobuild --define "prep %dump" $SPECFILE 2>&1 | awk '/PATCHURL[0-9]+/ {print $3}'`"
113     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
114     PACKAGE_NAME="`rpm -q --qf '%{NAME}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
115     PACKAGE_VERSION="`rpm -q --qf '%{VERSION}\n' --specfile ${SPECFILE} 2> /dev/null| head -1`"
116     PACKAGE_RELEASE="`rpm -q --qf '%{RELEASE}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
117
118     if [ -n "$BE_VERBOSE" ]; then
119         echo "- Sources :  `nourl $SOURCES`" 
120         if [ -n "$PATCHES" ]; then
121                 echo "- Patches :  `nourl $PATCHES`"
122         else
123                 echo "- Patches :  *no patches needed*"
124         fi
125         if [ -n "$ICONS" ]; then
126             echo "- Icon    :  `nourl $ICONS`"
127         else
128             echo "- Icon    :  *no package icon*"
129         fi
130         echo "- Name    : $PACKAGE_NAME"
131         echo "- Version : $PACKAGE_VERSION"
132         echo "- Release : $PACKAGE_RELEASE"
133     fi
134 }
135
136 Exit_error()
137 {
138     if [ -n "$DEBUG" ]; then 
139         set -x;
140         set -v; 
141     fi
142
143     cd $__PWD
144
145     case "$@" in
146     "err_no_spec_in_cmdl" )
147         echo "ERROR: spec file name not specified.";
148         exit 2 ;;
149     "err_no_spec_in_repo" )
150         echo "Error: spec file not stored in CVS repo.";
151         exit 3 ;;
152     "err_no_source_in_repo" )
153         echo "Error: some source, patch or icon files not stored in CVS repo.";
154         exit 4 ;;
155     "err_build_fail" )
156         echo "Error: package build failed.";
157         exit 5 ;;
158     esac
159 }
160
161 init_builder()
162 {
163     if [ -n "$DEBUG" ]; then 
164         set -x;
165         set -v; 
166     fi
167
168     SOURCE_DIR="`rpm --eval "%{_sourcedir}"`"
169     SPECS_DIR="`rpm --eval "%{_specdir}"`"
170
171     __PWD=`pwd`
172 }
173
174 get_spec()
175 {
176     if [ -n "$DEBUG" ]; then 
177         set -x;
178         set -v; 
179     fi
180
181     cd $SPECS_DIR
182
183     OPTIONS="up "
184
185     if [ -n "$CVSROOT" ]; then
186         OPTIONS="-d $CVSROOT $OPTIONS"
187     fi
188     if [ -n "$CVSTAG" ]; then
189         OPTIONS="$OPTIONS -r $CVSTAG"
190     else
191         OPTIONS="$OPTIONS -A"
192     fi
193
194     cvs $OPTIONS $SPECFILE
195     if [ "$?" -ne "0" ]; then
196         Exit_error err_no_spec_in_repo;
197     fi
198         if [ ! -f "$SPECFILE" ]; then
199         Exit_error err_no_spec_in_repo;
200         fi
201     
202     if [ "$CHMOD" = "yes" ]; then
203         chmod 444 $SPECFILE
204     fi
205     unset OPTIONS
206 }
207
208 get_files()
209 {
210     GET_FILES="$@"
211
212     if [ -n "$DEBUG" ]; then 
213         set -x;
214         set -v; 
215     fi
216
217     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
218         cd $SOURCE_DIR
219
220         OPTIONS="up "
221         if [ -n "$CVSROOT" ]; then
222             OPTIONS="-d $CVSROOT $OPTIONS"
223         fi
224         if [ -n "$CVSTAG" ]; then
225             OPTIONS="$OPTIONS -r $CVSTAG"
226         else
227             OPTIONS="$OPTIONS -A"
228         fi
229         for i in $GET_FILES; do
230             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ] 
231               then
232                 if 
233                         echo $i | grep -vE '(http|ftp|https|cvs)://' |\
234                         grep -qE '\.(gz|bz2)$'
235                 then
236                         echo "Warning: no URL given for $i"
237                 fi
238                 
239                 if      [ -z "$NOCVS" ]||\
240                         [ `echo $i | grep -vE '(ftp|http|https)://'` ]
241                 then
242                         cvs $OPTIONS `nourl $i`
243                 fi
244                 
245                 if      [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ]&&\
246                         [ `echo $i | grep -E 'ftp://|http://|https://'` ]
247                 then
248                         ${GETURI} "$i"
249                 fi
250
251                 if [ ! -f "`nourl $i`" ]; then
252                         Exit_error err_no_source_in_repo;
253                 fi
254             fi
255         done
256         
257         if [ "$CHMOD" = "yes" ]; then
258             chmod 444 `nourl $GET_FILES`
259         fi
260         unset OPTIONS
261     fi
262 }
263
264 tag_files()
265 {
266     TAG_FILES="$@"
267
268     if [ -n "$DEBUG" ]; then 
269         set -x;
270         set -v; 
271     fi
272
273     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
274         echo $PACKAGE_VERSION
275         echo $PACKAGE_RELEASE
276         TAG=$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g"`
277         echo "CVS tag: $TAG"
278
279         OPTIONS="tag -F"
280         if [ -n "$CVSROOT" ]; then
281             OPTIONS="-d $CVSROOT $OPTIONS"
282         fi
283
284         cd $SOURCE_DIR
285         for i in $TAG_FILES; do
286             if [ -f `nourl $i` ]; then
287                 cvs $OPTIONS $TAG `nourl $i`
288                 cvs $OPTIONS STABLE `nourl $i`
289             else
290                 Exit_error err_no_source_in_repo
291             fi
292         done
293
294         cd $SPECS_DIR
295         cvs $OPTIONS $TAG $SPECFILE
296         cvs $OPTIONS STABLE $SPECFILE
297
298         unset OPTIONS
299     fi
300 }
301
302 build_package()
303 {
304     if [ -n "$DEBUG" ]; then 
305         set -x;
306         set -v; 
307     fi
308
309     cd $SPECS_DIR
310     case "$COMMAND" in
311         build )
312             BUILD_SWITCH="-ba" ;;
313         build-binary )
314             BUILD_SWITCH="-bb" ;;
315         build-source )
316             BUILD_SWITCH="-bs --nodeps" ;;
317     esac
318     nice -n ${DEF_NICE_LEVEL} rpm $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $SPECFILE 
319
320     if [ "$?" -ne "0" ]; then
321         Exit_error err_build_fail;
322     fi
323     unset BUILD_SWITCH
324 }
325
326 nourl()
327 {
328         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\)://.*/##g'
329 }
330 #---------------------------------------------
331 # main()
332
333 if [ "$#" = 0 ]; then
334     usage;
335     exit 1
336 fi
337
338 while test $# -gt 0 ; do
339     case "${1}" in
340         -D | --debug )
341             DEBUG="yes"; shift ;;
342         -V | --version )
343             COMMAND="version"; shift ;;
344         -a | --as_anon )
345             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
346         -b | -ba | --build )
347             COMMAND="build"; shift ;;
348         -bb | --build-binary )
349             COMMAND="build-binary"; shift ;;
350         -bs | --build-source )
351             COMMAND="build-source"; shift ;;
352         -c | --clean )
353             CLEAN="--clean --rmspec --rmsource"; shift ;;
354         -d | --cvsroot )
355             shift; CVSROOT="${1}"; shift ;;
356         -g | --get )
357             COMMAND="get"; shift ;;
358         -h | --help )
359             COMMAND="usage"; shift ;;
360         -l | --logtofile )
361             shift; LOGFILE="${1}"; shift ;;
362         -ni| --nice )
363             shift; DEF_NICE_LEVEL=${1}; shift ;;
364         -m | --mr-proper )
365             COMMAND="mr-proper"; shift ;;
366         -nc | --no-cvs )
367             NOCVS="yes"; shift ;;
368         -nu | --no-urls )
369             NOURLS="yes"; shift ;;
370         -ns | --no-srcs )
371             NOSRCS="yes"; shift ;;
372         --opts )
373             shift; RPMOPTS="${1}"; shift ;;
374         -q | --quiet )
375             QUIET="--quiet"; shift ;;
376         -r | --cvstag )
377             shift; CVSTAG="${1}"; shift ;;
378         -T | --tag )
379             COMMAND="tag"; shift;;
380         -v | --verbose )
381             BE_VERBOSE="1"; shift ;;
382         * )
383             SPECFILE="${1}"; shift ;;
384     esac
385 done
386
387 if [ -n "$DEBUG" ]; then 
388         set -x;
389         set -v; 
390 fi
391
392 case "$COMMAND" in
393     "build" | "build-binary" | "build-source" )
394         init_builder;
395         if [ -n "$SPECFILE" ]; then
396             get_spec;
397             parse_spec;
398             if [ -n "$ICONS" ]; then
399                 get_files $ICONS;
400                 parse_spec;
401             fi
402             get_files "$SOURCES $PATCHES";
403             build_package;
404         else
405             Exit_error err_no_spec_in_cmdl;
406         fi
407         ;;
408     "get" )
409         init_builder;
410         if [ -n "$SPECFILE" ]; then
411             get_spec;
412             parse_spec;
413             if [ -n "$ICONS" ]; then
414                     get_files $ICONS
415                     parse_spec;
416             fi
417             get_files $SOURCES $PATCHES
418         else
419             Exit_error err_no_spec_in_cmdl;
420         fi
421         ;;
422     "tag" )
423         init_builder;
424         if [ -n "$SPECFILE" ]; then
425             get_spec;
426             parse_spec;
427             if [ -n "$ICONS" ]; then
428                     get_files $ICONS
429                     parse_spec;
430             fi
431             get_files $SOURCES $PATCHES;
432             tag_files "$SOURCES $PATCHES $ICONS";
433         else
434             Exit_error err_no_spec_in_cmdl;
435         fi
436         ;;
437     "mr-proper" )
438         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
439         ;;
440     "usage" )
441         usage;;
442     "version" )
443         echo "$VERSION";;
444 esac
445
446 cd $__PWD
447
448 # $Log$
449 # Revision 1.74  2001/04/02 15:39:29  misiek
450 # fix problems with get_files when no files passed
451 #
452 # Revision 1.73  2001/03/30 14:06:10  wiget
453 # massive typo by kloczek
454 #
455 # Revision 1.72  2001/03/26 22:16:22  kloczek
456 # - fixed grabbing name, version and release in parse_spec(),
457 # - added -T option (tag) (temporary it tags also additional STABLE tag - must
458 #   be added -Ts for separate tagging as STABLE).
459 #
460 # Revision 1.71  2001/03/05 14:12:27  misiek
461 # fix chmod
462 #
463 # Revision 1.70  2001/03/03 19:55:42  misiek
464 # workaround for problems with rpm when icons isn't cvs up'ed
465 #
466 #
This page took 0.070639 seconds and 4 git commands to generate.