]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- s/-U/-u/
[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] [-u|--try-upgrade] 
78         [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>] [-g|--get] 
79         [-h|--help] [{-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] [-nu|--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         -u, --try-upgrade - check version, and try to upgrade package
135         --define        - define a macro
136
137 "
138 }
139
140 parse_spec()
141 {
142     if [ -n "$DEBUG" ]; then
143             set -x;
144             set -v;
145     fi
146
147     cd $SPECS_DIR
148
149     if [ "$NOSRCS" != "yes" ]; then
150             SOURCES="`$RPMBUILD -bp  $BCOND --define 'prep %dump' $SPECFILE 2>&1 | awk '/SOURCEURL[0-9]+/ {print $3}'`"
151     fi
152     if ($RPMBUILD -bp  $BCOND --define 'prep %dump' $SPECFILE 2>&1 | grep -qEi ":.*nosource.*1"); then
153             FAIL_IF_NO_SOURCES="no"
154     fi
155
156     PATCHES="`$RPMBUILD -bp  $BCOND --define 'prep %dump' $SPECFILE 2>&1 | awk '/PATCHURL[0-9]+/ {print $3}'`"
157     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
158     PACKAGE_NAME="`$RPM -q --qf '%{NAME}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
159     PACKAGE_VERSION="`$RPM -q --qf '%{VERSION}\n' --specfile ${SPECFILE} 2> /dev/null| head -1`"
160     PACKAGE_RELEASE="`$RPM -q --qf '%{RELEASE}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
161
162     if [ -n "$BE_VERBOSE" ]; then
163         echo "- Sources :  `nourl $SOURCES`" 
164         if [ -n "$PATCHES" ]; then
165                 echo "- Patches :  `nourl $PATCHES`"
166         else
167                 echo "- Patches :  *no patches needed*"
168         fi
169         if [ -n "$ICONS" ]; then
170                 echo "- Icon    :  `nourl $ICONS`"
171         else
172                 echo "- Icon    :  *no package icon*"
173         fi
174         echo "- Name    : $PACKAGE_NAME"
175         echo "- Version : $PACKAGE_VERSION"
176         echo "- Release : $PACKAGE_RELEASE"
177     fi
178 }
179
180 Exit_error()
181 {
182     if [ -n "$DEBUG" ]; then 
183         set -x;
184         set -v; 
185     fi
186
187     cd $__PWD
188
189     case "$1" in
190     "err_no_spec_in_cmdl" )
191         echo "ERROR: spec file name not specified.";
192         exit 2 ;;
193     "err_no_spec_in_repo" )
194         echo "Error: spec file not stored in CVS repo.";
195         exit 3 ;;
196     "err_no_source_in_repo" )
197         echo "Error: some source, patch or icon files not stored in CVS repo. ($2)";
198         exit 4 ;;
199     "err_build_fail" )
200         echo "Error: package build failed.";
201         exit 5 ;;
202     esac
203 }
204
205 init_builder()
206 {
207     if [ -n "$DEBUG" ]; then 
208         set -x;
209         set -v; 
210     fi
211
212     SOURCE_DIR="`$RPM --eval '%{_sourcedir}'`"
213     SPECS_DIR="`$RPM --eval '%{_specdir}'`"
214
215     __PWD=`pwd`
216 }
217
218 get_spec()
219 {
220     if [ -n "$DEBUG" ]; then 
221         set -x;
222         set -v; 
223     fi
224
225     cd $SPECS_DIR
226
227     OPTIONS="up "
228
229     if [ -n "$CVSROOT" ]; then
230         OPTIONS="-d $CVSROOT $OPTIONS"
231     fi
232     if [ -n "$CVSTAG" ]; then
233         OPTIONS="$OPTIONS -r $CVSTAG"
234     else
235         OPTIONS="$OPTIONS -A"
236     fi
237
238     cvs $OPTIONS $SPECFILE
239     if [ "$?" -ne "0" ]; then
240         Exit_error err_no_spec_in_repo;
241     fi
242         if [ ! -f "$SPECFILE" ]; then
243         Exit_error err_no_spec_in_repo;
244         fi
245     
246     if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
247         chmod $CHMOD_MODE $SPECFILE
248     fi
249     unset OPTIONS
250     grep -E -m 1 "^#.*Revision:.*Date" $SPECFILE
251 }
252
253 find_mirror(){
254
255     cd "$SPECS_DIR"
256     url="$1"    
257     if [ ! -f "mirrors" ] ; then 
258             cvs update mirrors >&2 
259     fi
260
261     IFS="|"
262     while read origin mirror name rest; do
263         ol=`echo -n "$origin"|wc -c`    
264         prefix="`echo -n "$url" | head -c $ol`"
265         if [ "$prefix" = "$origin" ] ; then
266                 suffix="`echo "$url"|cut -b $ol-`"
267                 echo -n "$mirror$suffix"
268                 return 0
269         fi
270     done < mirrors
271     echo "$url"
272 }
273
274 get_files()
275 {
276     GET_FILES="$@"
277
278     if [ -n "$DEBUG" ]; then 
279         set -x;
280         set -v; 
281     fi
282
283     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
284         cd $SOURCE_DIR
285
286         OPTIONS="up "
287         if [ -n "$CVSROOT" ]; then
288             OPTIONS="-d $CVSROOT $OPTIONS"
289         fi
290         if [ -n "$CVSTAG" ]; then
291             OPTIONS="$OPTIONS -r $CVSTAG"
292         else
293             OPTIONS="$OPTIONS -A"
294         fi
295         for i in $GET_FILES; do
296             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ]; then
297                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
298                         echo "Warning: no URL given for $i"
299                 fi
300                 
301                 if [ -z "$NOCVS" ]|| [ `echo $i | grep -vE '(ftp|http|https)://'` ]; then
302                         cvs $OPTIONS `nourl $i`
303                 fi
304                 
305                 if [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ] && [ `echo $i | grep -E 'ftp://|http://|https://'` ]; then
306                         if [ -z "$NOMIRRORS" ] ; then 
307                                 i="`find_mirror "$i"`"
308                         fi
309                         ${GETURI} "$i"
310                 fi
311
312                 if [ ! -f "`nourl $i`" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
313                         Exit_error err_no_source_in_repo $i;
314                 fi
315             fi
316         done
317         
318         if [ "$CHMOD" = "yes" ]; then
319             CHMOD_FILES="`nourl $GET_FILES`"
320             if [ -n "$CHMOD_FILES" ]; then
321                     chmod $CHMOD_MODE $CHMOD_FILES
322             fi
323         fi
324         unset OPTIONS
325     fi
326 }
327
328 tag_files()
329 {
330     TAG_FILES="$@"
331
332     if [ -n "$DEBUG" ]; then 
333         set -x;
334         set -v; 
335     fi
336
337     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
338         echo $PACKAGE_VERSION
339         echo $PACKAGE_RELEASE
340         TAG=$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g"`
341         echo "CVS tag: $TAG"
342
343         OPTIONS="tag -F"
344         if [ -n "$CVSROOT" ]; then
345             OPTIONS="-d $CVSROOT $OPTIONS"
346         fi
347
348         cd $SOURCE_DIR
349         for i in $TAG_FILES; do
350             if [ -f `nourl $i` ]; then
351                 cvs $OPTIONS $TAG `nourl $i`
352                 cvs $OPTIONS STABLE `nourl $i`
353             else
354                 Exit_error err_no_source_in_repo $i
355             fi
356         done
357
358         cd $SPECS_DIR
359         cvs $OPTIONS $TAG $SPECFILE
360         cvs $OPTIONS STABLE $SPECFILE
361
362         unset OPTIONS
363     fi
364 }
365
366 branch_files()
367 {
368         TAG=$1
369         echo "CVS branch tag: $TAG"
370         shift;
371
372         TAG_FILES="$@"
373
374         if [ -n "$DEBUG" ]; then
375                 set -x;
376                 set -v;
377         fi
378
379         if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
380                 
381                 OPTIONS="tag -b"
382                 if [ -n "$CVSROOT" ]; then
383                         OPTIONS="-d $CVSROOT $OPTIONS"
384                 fi
385                 cd $SOURCE_DIR
386                 for i in $TAG_FILES; do
387                         if [ -f `nourl $i` ]; then
388                                 cvs $OPTIONS $TAG `nourl $i`
389                         else
390                                 Exit_error err_no_source_in_repo $i
391                         fi
392                 done
393                 cd $SPECS_DIR
394                 cvs $OPTIONS $TAG $SPECFILE
395
396                 unset OPTIONS
397         fi
398 }
399
400 build_package()
401 {
402     if [ -n "$DEBUG" ]; then 
403         set -x;
404         set -v; 
405     fi
406
407     cd $SPECS_DIR
408
409     if [ -n "$TRY_UPGRADE" ]; then 
410     
411         TNOTIFY=`./pldnotify.awk $SPECFILE`
412         
413         TNEWVER=`echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }'`
414         
415         if [ -n "$TNEWVER" ]; then
416         
417             TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
418             
419             echo "New version found, updating spec file to version " $TNEWVER
420             
421             cp -f $SPECFILE $SPECFILE.bak
422             
423             chmod +w $SPECFILE
424         
425             eval "perl -pi -e 's/Version:\t"$TOLDVER"/Version:\t"$TNEWVER"/gs' $SPECFILE"
426             eval "perl -pi -e 's/Release:\t[1-9]{0,4}/Release:\t1/' $SPECFILE"
427             
428             parse_spec;
429             
430             get_files "$SOURCES $PATCHES";
431             
432             unset TOLDVER TNEWVER TNOTIFY
433         fi
434         
435     fi
436
437
438     cd $SPECS_DIR
439
440     case "$COMMAND" in
441         build )
442             BUILD_SWITCH="-ba" ;;
443         build-binary )
444             BUILD_SWITCH="-bb" ;;
445         build-source )
446             BUILD_SWITCH="-bs --nodeps" ;;
447     esac
448     if [ -n "$LOGFILE" ]; then
449         LOG=`eval echo $LOGFILE`
450         eval nice -n ${DEF_NICE_LEVEL} time $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $SPECFILE 2>&1 | tee $LOG
451     else
452         eval nice -n ${DEF_NICE_LEVEL} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $SPECFILE
453     fi
454
455     if [ "$?" -ne "0" ]; then
456     
457         if [ -n "$TRY_UPGRADE" ]; then 
458             echo "\n!!! Package with new version cannot be build automagically\n"
459             mv -f $SPECFILE.bak $SPECFILE
460         fi
461         
462         Exit_error err_build_fail;
463     fi
464     
465     unset BUILD_SWITCH
466 }
467
468 nourl()
469 {
470         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
471 }
472 #---------------------------------------------
473 # main()
474
475 if [ "$#" = 0 ]; then
476     usage;
477     exit 1
478 fi
479
480 while test $# -gt 0 ; do
481     case "${1}" in
482         -D | --debug )
483             DEBUG="yes"; shift ;;
484         -V | --version )
485             COMMAND="version"; shift ;;
486         -a | --as_anon )
487             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
488         -b | -ba | --build )
489             COMMAND="build"; shift ;;
490         -bb | --build-binary )
491             COMMAND="build-binary"; shift ;;
492         -bs | --build-source )
493             COMMAND="build-source"; shift ;;
494         -B | --branch )
495             COMMAND="branch"; shift; TAG="${1}"; shift;;
496         -c | --clean )
497             CLEAN="--clean --rmspec --rmsource"; shift ;;
498         -d | --cvsroot )
499             shift; CVSROOT="${1}"; shift ;;
500         -g | --get )
501             COMMAND="get"; shift ;;
502         -h | --help )
503             COMMAND="usage"; shift ;;
504         -l | --logtofile )
505             shift; LOGFILE="${1}"; shift ;;
506         -ni| --nice )
507             shift; DEF_NICE_LEVEL=${1}; shift ;;
508         -m | --mr-proper )
509             COMMAND="mr-proper"; shift ;;
510         -nc | --no-cvs )
511             NOCVS="yes"; shift ;;
512         -nm | --no-mirrors )
513             NOMIRRORS="yes"; shift ;;
514         -nu | --no-urls )
515             NOURLS="yes"; shift ;;
516         -ns | --no-srcs )
517             NOSRCS="yes"; shift ;;
518         -ns0 | --no-source0 )
519             NOSOURCE0="yes"; shift ;;
520         --opts )
521             shift; RPMOPTS="${1}"; shift ;;
522         --with | --without )
523             BCOND="$BCOND $1 $2" ; shift 2 ;;
524         -q | --quiet )
525             QUIET="--quiet"; shift ;;
526         -r | --cvstag )
527             shift; CVSTAG="${1}"; shift ;;
528         -Tvs | --tag-version-stable )
529             COMMAND="tag";
530             TAG="STABLE"
531             TAG_VERSION="yes"
532             shift;;
533         -Tvd | --tag-version-devel )
534             COMMAND="tag";
535             TAG="DEVEL"
536             TAG_VERSION="yes"
537             shift;;
538         -Ts | --tag-stable )
539             COMMAND="tag";
540             TAG="STABLE"
541             TAG_VERSION="no"
542             shift;;
543         -Td | --tag-devel )
544             COMMAND="tag";
545             TAG="DEVEL"
546             TAG_VERSION="no"
547             shift;;
548         -Tv | --tag-version )
549             COMMAND="tag";
550             TAG_VERSION="yes"
551             shift;;
552         -T | --tag )
553             COMMAND="tag";
554             shift
555             TAG="$1"
556             TAG_VERSION="no"
557             shift;;
558         -v | --verbose )
559             BE_VERBOSE="1"; shift ;;
560         -u | --try-upgrade )
561             TRY_UPGRADE="1"; shift ;;
562         --define)
563             shift
564             MACRO="${1}"
565             VALUE="${2}"
566             shift 2
567             RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
568             ;;
569         * )
570             SPECFILE="${1}"; shift ;;
571     esac
572 done
573
574 if [ -n "$DEBUG" ]; then 
575         set -x;
576         set -v; 
577 fi
578
579 case "$COMMAND" in
580     "build" | "build-binary" | "build-source" )
581         init_builder;
582         if [ -n "$SPECFILE" ]; then
583             get_spec;
584             parse_spec;
585             if [ -n "$ICONS" ]; then
586                 get_files $ICONS;
587                 parse_spec;
588             fi
589             get_files "$SOURCES $PATCHES";
590             build_package;
591         else
592             Exit_error err_no_spec_in_cmdl;
593         fi
594         ;;
595     "branch" )
596         init_builder;
597         if [ -n "$SPECFILE" ]; then
598                 get_spec;
599                 parse_spec;
600                 if [ -n "$ICONS" ]; then
601                         get_files $ICONS
602                         parse_spec;
603                 fi
604                 get_files $SOURCES $PATCHES;
605                 branch_files $TAG "$SOURCES $PATCHES $ICONS";
606         else
607                 Exit_error err_no_spec_in_cmdl;
608         fi
609         ;;
610     "get" )
611         init_builder;
612         if [ -n "$SPECFILE" ]; then
613             get_spec;
614             parse_spec;
615             if [ -n "$ICONS" ]; then
616                 get_files $ICONS
617                 parse_spec;
618             fi
619             if [ -n "$NOSOURCE0" ] ; then
620                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
621             fi
622             get_files $SOURCES $PATCHES
623         else
624             Exit_error err_no_spec_in_cmdl;
625         fi
626         ;;
627     "tag" )
628         init_builder;
629         if [ -n "$SPECFILE" ]; then
630             get_spec;
631             parse_spec;
632             if [ -n "$ICONS" ]; then
633                 get_files $ICONS
634                 parse_spec;
635             fi
636             get_files $SOURCES $PATCHES;
637             tag_files "$SOURCES $PATCHES $ICONS";
638         else
639             Exit_error err_no_spec_in_cmdl;
640         fi
641         ;;
642     "mr-proper" )
643         $RPM --clean --rmsource --rmspec --force --nodeps $SPECFILE
644         ;;
645     "usage" )
646         usage;;
647     "version" )
648         echo "$VERSION";;
649 esac
650
651 cd $__PWD
This page took 0.093486 seconds and 3 git commands to generate.