]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- allow more bconds than one
[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 if [ -s CVS/Root ]; then
29     CVSROOT=$(cat CVS/Root)
30 else
31     CVSROOT=${CVSROOT:-""}
32 fi
33 LOGFILE=""
34 CHMOD="yes"
35 RPMOPTS=""
36 BCOND=""
37
38 PATCHES=""
39 SOURCES=""
40 ICONS=""
41 PACKAGE_RELEASE=""
42 PACKAGE_VERSION=""
43 PACKAGE_NAME=""
44 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
45
46 DEF_NICE_LEVEL=0
47
48 FAIL_IF_NO_SOURCES="yes"
49
50 GETURI="wget -c -nd -t$WGET_RETRIES"
51
52 if [ -f ~/etc/builderrc ]; then
53   . ~/etc/builderrc
54 elif [ -f ~/.builderrc ]; then
55   . ~/.builderrc
56 fi
57
58 #---------------------------------------------
59 # functions
60
61 usage()
62 {
63     if [ -n "$DEBUG" ]; then set -xv; fi
64     echo "\
65 Usage: builder [-D] [--debug] [-V] [--version] [-a] [--as_anon] [-b] [-ba]
66         [--build] [-bb] [--build-binary] [-bs] [--build-source]
67         [-d <cvsroot>] [--cvsroot <cvsroot>] [-g] [--get] [-h] [--help]
68         [-l <logfile>] [-m] [--mr-proper] [--logtofile <logfile>] [-q] [--quiet]
69         [-r <cvstag>] [--cvstag <cvstag>] [-u] [--no-urls] [-v] [--verbose]
70         [--opts <rpm opts>] [--with/--without pkg] <package>.spec
71
72         -D, --debug     - enable script debugging mode,
73         -V, --version   - output builder version
74         -a, --as_anon   - get files via pserver as cvs@anoncvs.pld.org.pl,
75         -b, -ba,
76         --build         - get all files from CVS repo or HTTP/FTP and build
77                           package from <package>.spec,
78         -bb,
79         --build-binary  - get all files from CVS repo or HTTP/FTP and build
80                           binary only package from <package>.spec,
81         -bs,
82         --build-source  - get all files from CVS repo or HTTP/FTP and only
83                           pack them into src.rpm,
84         -c, --clean     - clean all temporarily created files (in BUILD,
85                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
86                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
87         -d, --cvsroot   - setup \$CVSROOT,
88         -g, --get       - get <package>.spec and all related files from
89                           CVS repo or HTTP/FTP,
90         -h, --help      - this message,
91         -l, --logtofile - log all to file,
92         -m, --mr-proper - only remove all files related to spec file and
93                           all work resources,
94         -nc, --no-cvs   - don't download from CVS, if source URL is given,
95         -nu, --no-urls  - don't try to download from FTP/HTTP location,
96         -ns, --no-srcs  - don't downland Sources
97         --opts          - additional options for rpm
98         -q, --quiet     - be quiet,
99         -r, --cvstag    - build package using resources from specified CVS
100                           tag,
101         -T, --tag       - add cvs tags for files,
102         -v, --verbose   - be verbose,
103
104 "
105 }
106
107 parse_spec()
108 {
109     if [ -n "$DEBUG" ]; then 
110         set -x;
111         set -v;
112     fi
113
114     cd $SPECS_DIR
115
116     if [ "$NOSRCS" != "yes" ]; then
117         SOURCES="`rpm -bp --nobuild --define 'prep %dump' $SPECFILE 2>&1 | awk '/SOURCEURL[0-9]+/ {print $3}'`"
118     fi
119     if (rpm -bp --nobuild --define 'prep %dump' $SPECFILE 2>&1 | grep -qEi ":.*nosource.*1"); then
120         FAIL_IF_NO_SOURCES="no"
121     fi
122
123
124     PATCHES="`rpm -bp --nobuild --define 'prep %dump' $SPECFILE 2>&1 | awk '/PATCHURL[0-9]+/ {print $3}'`"
125     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
126     PACKAGE_NAME="`rpm -q --qf '%{NAME}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
127     PACKAGE_VERSION="`rpm -q --qf '%{VERSION}\n' --specfile ${SPECFILE} 2> /dev/null| head -1`"
128     PACKAGE_RELEASE="`rpm -q --qf '%{RELEASE}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
129
130     if [ -n "$BE_VERBOSE" ]; then
131         echo "- Sources :  `nourl $SOURCES`" 
132         if [ -n "$PATCHES" ]; then
133                 echo "- Patches :  `nourl $PATCHES`"
134         else
135                 echo "- Patches :  *no patches needed*"
136         fi
137         if [ -n "$ICONS" ]; then
138             echo "- Icon    :  `nourl $ICONS`"
139         else
140             echo "- Icon    :  *no package icon*"
141         fi
142         echo "- Name    : $PACKAGE_NAME"
143         echo "- Version : $PACKAGE_VERSION"
144         echo "- Release : $PACKAGE_RELEASE"
145     fi
146 }
147
148 Exit_error()
149 {
150     if [ -n "$DEBUG" ]; then 
151         set -x;
152         set -v; 
153     fi
154
155     cd $__PWD
156
157     case "$1" in
158     "err_no_spec_in_cmdl" )
159         echo "ERROR: spec file name not specified.";
160         exit 2 ;;
161     "err_no_spec_in_repo" )
162         echo "Error: spec file not stored in CVS repo.";
163         exit 3 ;;
164     "err_no_source_in_repo" )
165         echo "Error: some source, patch or icon files not stored in CVS repo. ($2)";
166         exit 4 ;;
167     "err_build_fail" )
168         echo "Error: package build failed.";
169         exit 5 ;;
170     esac
171 }
172
173 init_builder()
174 {
175     if [ -n "$DEBUG" ]; then 
176         set -x;
177         set -v; 
178     fi
179
180     SOURCE_DIR="`rpm --eval '%{_sourcedir}'`"
181     SPECS_DIR="`rpm --eval '%{_specdir}'`"
182
183     __PWD=`pwd`
184 }
185
186 get_spec()
187 {
188     if [ -n "$DEBUG" ]; then 
189         set -x;
190         set -v; 
191     fi
192
193     cd $SPECS_DIR
194
195     OPTIONS="up "
196
197     if [ -n "$CVSROOT" ]; then
198         OPTIONS="-d $CVSROOT $OPTIONS"
199     fi
200     if [ -n "$CVSTAG" ]; then
201         OPTIONS="$OPTIONS -r $CVSTAG"
202     else
203         OPTIONS="$OPTIONS -A"
204     fi
205
206     cvs $OPTIONS $SPECFILE
207     if [ "$?" -ne "0" ]; then
208         Exit_error err_no_spec_in_repo;
209     fi
210         if [ ! -f "$SPECFILE" ]; then
211         Exit_error err_no_spec_in_repo;
212         fi
213     
214     if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
215         chmod 444 $SPECFILE
216     fi
217     unset OPTIONS
218 }
219
220 get_files()
221 {
222     GET_FILES="$@"
223
224     if [ -n "$DEBUG" ]; then 
225         set -x;
226         set -v; 
227     fi
228
229     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
230         cd $SOURCE_DIR
231
232         OPTIONS="up "
233         if [ -n "$CVSROOT" ]; then
234             OPTIONS="-d $CVSROOT $OPTIONS"
235         fi
236         if [ -n "$CVSTAG" ]; then
237             OPTIONS="$OPTIONS -r $CVSTAG"
238         else
239             OPTIONS="$OPTIONS -A"
240         fi
241         for i in $GET_FILES; do
242             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ] 
243               then
244                 if 
245                         echo $i | grep -vE '(http|ftp|https|cvs)://' |\
246                         grep -qE '\.(gz|bz2)$'
247                 then
248                         echo "Warning: no URL given for $i"
249                 fi
250                 
251                 if      [ -z "$NOCVS" ]||\
252                         [ `echo $i | grep -vE '(ftp|http|https)://'` ]
253                 then
254                         cvs $OPTIONS `nourl $i`
255                 fi
256                 
257                 if      [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ]&&\
258                         [ `echo $i | grep -E 'ftp://|http://|https://'` ]
259                 then
260                         ${GETURI} "$i"
261                 fi
262
263                 if [ ! -f "`nourl $i`" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
264                         Exit_error err_no_source_in_repo $i;
265                 fi
266             fi
267         done
268         
269         if [ "$CHMOD" = "yes" ]; then
270             CHMOD_FILES="`nourl $GET_FILES`"
271             if [ -n "$CHMOD_FILES" ]; then
272                     chmod 444 $CHMOD_FILES
273             fi
274         fi
275         unset OPTIONS
276     fi
277 }
278
279 tag_files()
280 {
281     TAG_FILES="$@"
282
283     if [ -n "$DEBUG" ]; then 
284         set -x;
285         set -v; 
286     fi
287
288     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
289         echo $PACKAGE_VERSION
290         echo $PACKAGE_RELEASE
291         TAG=$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g"`
292         echo "CVS tag: $TAG"
293
294         OPTIONS="tag -F"
295         if [ -n "$CVSROOT" ]; then
296             OPTIONS="-d $CVSROOT $OPTIONS"
297         fi
298
299         cd $SOURCE_DIR
300         for i in $TAG_FILES; do
301             if [ -f `nourl $i` ]; then
302                 cvs $OPTIONS $TAG `nourl $i`
303                 cvs $OPTIONS STABLE `nourl $i`
304             else
305                 Exit_error err_no_source_in_repo $i
306             fi
307         done
308
309         cd $SPECS_DIR
310         cvs $OPTIONS $TAG $SPECFILE
311         cvs $OPTIONS STABLE $SPECFILE
312
313         unset OPTIONS
314     fi
315 }
316
317 build_package()
318 {
319     if [ -n "$DEBUG" ]; then 
320         set -x;
321         set -v; 
322     fi
323
324     cd $SPECS_DIR
325     case "$COMMAND" in
326         build )
327             BUILD_SWITCH="-ba" ;;
328         build-binary )
329             BUILD_SWITCH="-bb" ;;
330         build-source )
331             BUILD_SWITCH="-bs --nodeps" ;;
332     esac
333     nice -n ${DEF_NICE_LEVEL} rpm $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $SPECFILE 
334
335     if [ "$?" -ne "0" ]; then
336         Exit_error err_build_fail;
337     fi
338     unset BUILD_SWITCH
339 }
340
341 nourl()
342 {
343         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\)://.*/##g'
344 }
345 #---------------------------------------------
346 # main()
347
348 if [ "$#" = 0 ]; then
349     usage;
350     exit 1
351 fi
352
353 while test $# -gt 0 ; do
354     case "${1}" in
355         -D | --debug )
356             DEBUG="yes"; shift ;;
357         -V | --version )
358             COMMAND="version"; shift ;;
359         -a | --as_anon )
360             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
361         -b | -ba | --build )
362             COMMAND="build"; shift ;;
363         -bb | --build-binary )
364             COMMAND="build-binary"; shift ;;
365         -bs | --build-source )
366             COMMAND="build-source"; shift ;;
367         -c | --clean )
368             CLEAN="--clean --rmspec --rmsource"; shift ;;
369         -d | --cvsroot )
370             shift; CVSROOT="${1}"; shift ;;
371         -g | --get )
372             COMMAND="get"; shift ;;
373         -h | --help )
374             COMMAND="usage"; shift ;;
375         -l | --logtofile )
376             shift; LOGFILE="${1}"; shift ;;
377         -ni| --nice )
378             shift; DEF_NICE_LEVEL=${1}; shift ;;
379         -m | --mr-proper )
380             COMMAND="mr-proper"; shift ;;
381         -nc | --no-cvs )
382             NOCVS="yes"; shift ;;
383         -nu | --no-urls )
384             NOURLS="yes"; shift ;;
385         -ns | --no-srcs )
386             NOSRCS="yes"; shift ;;
387         --opts )
388             shift; RPMOPTS="${1}"; shift ;;
389         --with | --without )
390             BCOND="$BCOND $1 $2" ; shift 2 ;;
391         -q | --quiet )
392             QUIET="--quiet"; shift ;;
393         -r | --cvstag )
394             shift; CVSTAG="${1}"; shift ;;
395         -T | --tag )
396             COMMAND="tag"; shift;;
397         -v | --verbose )
398             BE_VERBOSE="1"; shift ;;
399         * )
400             SPECFILE="${1}"; shift ;;
401     esac
402 done
403
404 if [ -n "$DEBUG" ]; then 
405         set -x;
406         set -v; 
407 fi
408
409 case "$COMMAND" in
410     "build" | "build-binary" | "build-source" )
411         init_builder;
412         if [ -n "$SPECFILE" ]; then
413             get_spec;
414             parse_spec;
415             if [ -n "$ICONS" ]; then
416                 get_files $ICONS;
417                 parse_spec;
418             fi
419             get_files "$SOURCES $PATCHES";
420             build_package;
421         else
422             Exit_error err_no_spec_in_cmdl;
423         fi
424         ;;
425     "get" )
426         init_builder;
427         if [ -n "$SPECFILE" ]; then
428             get_spec;
429             parse_spec;
430             if [ -n "$ICONS" ]; then
431                     get_files $ICONS
432                     parse_spec;
433             fi
434             get_files $SOURCES $PATCHES
435         else
436             Exit_error err_no_spec_in_cmdl;
437         fi
438         ;;
439     "tag" )
440         init_builder;
441         if [ -n "$SPECFILE" ]; then
442             get_spec;
443             parse_spec;
444             if [ -n "$ICONS" ]; then
445                     get_files $ICONS
446                     parse_spec;
447             fi
448             get_files $SOURCES $PATCHES;
449             tag_files "$SOURCES $PATCHES $ICONS";
450         else
451             Exit_error err_no_spec_in_cmdl;
452         fi
453         ;;
454     "mr-proper" )
455         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
456         ;;
457     "usage" )
458         usage;;
459     "version" )
460         echo "$VERSION";;
461 esac
462
463 cd $__PWD
464
465 # $Log$
466 # Revision 1.82  2001/09/18 10:55:37  ankry
467 # - added support for limitting number of wget retries when fetching a file
468 #   from ftp/http server via environment variable MAX_WGET_RETRIES.
469 #   Defaults to infinite (0).
470 #
471 # Revision 1.81  2001/07/06 16:52:30  misiek
472 # - by default use CVSroot from CVS/Root and if it doesn't exist use from CVSROOT variable
473 #
474 # Revision 1.80  2001/06/22 18:52:39  misiek
475 # - added support for --with/--without options
476 #
477 # Revision 1.79  2001/05/28 14:44:16  baggins
478 # - if file is not in repo TELL which fucking file it is!
479 #
480 # Revision 1.78  2001/05/13 19:04:44  misiek
481 # fixes for ksh93
482 #
483 # Revision 1.77  2001/05/13 10:51:30  misiek
484 # don't fail if no sources found (hack to allow build nosrc packages)
485 #
486 # Revision 1.76  2001/04/19 23:24:06  misiek
487 # fix chmod again
488 #
489 # Revision 1.75  2001/04/19 23:14:25  misiek
490 # redirect errors from query to /dev/null
491 #
492 # Revision 1.74  2001/04/02 15:39:29  misiek
493 # fix problems with get_files when no files passed
494 #
495 # Revision 1.73  2001/03/30 14:06:10  wiget
496 # massive typo by kloczek
497 #
498 # Revision 1.72  2001/03/26 22:16:22  kloczek
499 # - fixed grabbing name, version and release in parse_spec(),
500 # - added -T option (tag) (temporary it tags also additional STABLE tag - must
501 #   be added -Ts for separate tagging as STABLE).
502 #
503 # Revision 1.71  2001/03/05 14:12:27  misiek
504 # fix chmod
505 #
506 # Revision 1.70  2001/03/03 19:55:42  misiek
507 # workaround for problems with rpm when icons isn't cvs up'ed
508 #
509 #
This page took 0.062645 seconds and 4 git commands to generate.