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