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