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