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