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