]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- GROUP_BCONDS option added (defaults to "no"). Lets you choose if you prefer
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # $Id$
4 # Exit codes:
5 #       0 - succesful
6 #       1 - help displayed
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 #       6 - spec file with errors
12 #       7 - wrong source in /etc/poldek.conf
13
14 # Notes (todo):
15 #       - builder -u fetches current version first
16 #       - tries to get new version from distfiles without new md5
17 #       - after fetching new version doesn't update md5
18 #       - doesn't get sources for specs with %include /usr/lib/rpm/macros.python
19 #         when there's no rpm-pythonprov (rpm's fault, but it's ugly anyway)
20 #       - as above with %include /usr/lib/rpm/macros.perl and no rpm-perlprov
21 #       - when Icon: field is present, -5 and -a5 doesn't work
22
23 VERSION="\
24 Build package utility from PLD CVS repository
25 V 0.11 (C) 1999-2003 Free Penguins".
26 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
27
28 COMMAND="build"
29
30 SPECFILE=""
31 BE_VERBOSE=""
32 QUIET=""
33 CLEAN=""
34 DEBUG=""
35 NOURLS=""
36 NOCVS=""
37 NOCVSSPEC=""
38 NODIST=""
39 UPDATE=""
40 UPDATE5=""
41 ADD5=""
42 ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
43 if [ -s CVS/Root ]; then
44     CVSROOT=$(cat CVS/Root)
45 else
46     CVSROOT=${CVSROOT:-""}
47 fi
48 # Example: LOGFILE='../log.$PACKAGE_NAME'
49 # Yes, you can use variable name! Note _single_ quotes!
50 LOGFILE=''
51
52 LOGDIR=""
53 LOGDIROK=""
54 LOGDIRFAIL=""
55 LASTLOG_FILE=""
56 CHMOD="no"
57 CHMOD_MODE="0444"
58 RPMOPTS=""
59 BCOND=""
60 GROUP_BCONDS="no"
61
62 PATCHES=""
63 SOURCES=""
64 ICONS=""
65 PACKAGE_RELEASE=""
66 PACKAGE_VERSION=""
67 PACKAGE_NAME=""
68 PROTOCOL="ftp"
69 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
70 CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
71
72 CVSTAG=""
73 RES_FILE=""
74
75 CVS_SERVER="cvs.pld-linux.org"
76 DISTFILES_SERVER="://distfiles.pld-linux.org"
77
78 DEF_NICE_LEVEL=0
79
80 FAIL_IF_NO_SOURCES="yes"
81
82 wget --help 2>&1 | grep -q ' \-\-inet ' && WGET_OPTS="$WGET_OPTS --inet"
83 wget --help 2>&1 | grep -q ' \-\-retry\-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
84
85 GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
86 GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
87 GETLOCAL="cp -a"
88
89 if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
90     RPM="rpm"
91     RPMBUILD="rpm"
92 else
93     RPM="rpm"
94     RPMBUILD="rpmbuild"
95 fi
96
97 POLDEK_INDEX_DIR="`$RPM --eval %_rpmdir`/"
98 POLDEK_SOURCE="cvs"
99
100 # Example grep cvs /etc/poldek.conf:
101 # source = cvs /home/users/yoshi/rpm/RPMS/
102 if [ "$UPDATE_POLDEK_INDEXES" == "yes" ]; then
103         POLDEK_SOURCE_VALIDITY="`grep ${POLDEK_SOURCE} /etc/poldek.conf|grep -v ^#`"
104         if [ "${POLDEK_SOURCE_VALIDITY}" == "" ]; then 
105                 echo "Using improper source '${POLDEK_SOURCE}' in /etc/poldek.conf"
106                 echo "Fix it and try to contiune"
107                 exit 7
108         fi
109 fi
110
111 if [ -f ~/etc/builderrc ]; then
112     . ~/etc/builderrc
113 elif [ -f ~/.builderrc ]; then
114     . ~/.builderrc
115 fi
116
117 #---------------------------------------------
118 # functions
119
120 usage()
121 {
122     if [ -n "$DEBUG" ]; then set -xv; fi
123     echo "\
124 Usage: builder [-D|--debug] [-V|--version] [-a|--as_anon] [-b|-ba|--build]
125
126         [-bb|--build-binary] [-bs|--build-source] [-u|--try-upgrade]
127         [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>] [-g|--get]
128         [-h|--help] [--http] [{-l,--logtofile} <logfile>] [-m|--mr-proper]
129         [-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T--tag <cvstag>]
130         [-Tvs|--tag-version-stable] [-Tvn|--tag-version-nest]
131         [-Ts|--tag-stable] [-Tn|--tag-nest] [-Tv|--tag-version] 
132         [{-Tp|--tag-prefix} <prefix>]
133         [-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>]
134         [--with/--without <feature>] [--define <macro> <value>] <package>[.spec]
135
136         -5, --update-md5
137                         - update md5 comments in spec, implies -nd -ncs
138         -a5, --add-md5  - add md5 comments to URL sources, implies -nc -nd -ncs
139         -D, --debug     - enable script debugging mode,
140         -V, --version   - output builder version
141         -a, --as_anon   - get files via pserver as cvs@$CVS_SERVER,
142         -b, -ba,
143         --build         - get all files from CVS repo or HTTP/FTP and build
144                           package from <package>.spec,
145         -bb, --build-binary
146                         - get all files from CVS repo or HTTP/FTP and build
147                           binary only package from <package>.spec,
148         -bs,
149         --build-source  - get all files from CVS repo or HTTP/FTP and only
150                           pack them into src.rpm,
151         -B, --branch    - add branch
152         -c, --clean     - clean all temporarily created files (in BUILD,
153                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
154         -d <cvsroot>, --cvsroot <cvsroot>
155                         - setup \$CVSROOT,
156         --define <macro> <value>
157                         - define a macro <macro> with value <value>,
158         --nodeps        - rpm won't check any dependences
159         -g, --get       - get <package>.spec and all related files from
160                           CVS repo or HTTP/FTP,
161         -h, --help      - this message,
162         --http          - use http instead of ftp,
163         -l <logfile>, --logtofile <logfile>
164                         - log all to file,
165         -m, --mr-proper - only remove all files related to spec file and
166                           all work resources,
167         -nc, --no-cvs   - don't download sources from CVS, if source URL is
168                           given,
169         -ncs, --no-cvs-specs
170                         - don't check specs in CVS
171         -nd, --no-distfiles
172                         - don't download from distfiles
173         -nm, --no-mirrors - don't download from mirror, if source URL is given,
174         -nu, --no-urls  - don't try to download from FTP/HTTP location,
175         -ns, --no-srcs  - don't download Sources
176         -ns0, --no-source0
177                         - don't download Source0
178         --opts <rpm opts>
179                         - additional options for rpm
180         -q, --quiet     - be quiet,
181         --date yyyy-mm-dd
182                         - build package using resources from specified CVS
183                           date,
184         -r <cvstag>, --cvstag <cvstag>
185                         - build package using resources from specified CVS
186                           tag,
187         -R, --fetch-build-requires
188                         - fetch what is BuildRequired,
189         -RB, --remove-build-requires
190                         - remove all you fetched with -R or --fetch-build-requires
191                           remember, this option requires confirmation,
192         -FRB, --force-remove-build-requires
193                         - remove all you fetched with -R or --fetch-build-requires
194                           remember, this option works without confirmation,
195         -T <cvstag> , --tag <cvstag>
196                         - add cvs tag <cvstag> for files,
197         -Tvs, --tag-version-stable
198                         - add cvs tags STABLE and NAME-VERSION-RELESE for files,
199         -Tvn, --tag-version-nest
200                         - add cvs tags NEST and NAME-VERSION-RELESE for files,
201         -Ts, --tag-stable
202                         - add cvs tag STABLE for files,
203         -Tn, --tag-nest
204                         - add cvs tag NEST for files,
205         -Tv, --tag-version
206                         - add cvs tag NAME-VERSION-RELESE for files,
207         -Tp, --tag-prefix <prefix>
208                         - add <prefix> to NAME-VERSION-RELEASE tags,
209         -v, --verbose   - be verbose,
210         -u, --try-upgrade
211                         - check version, and try to upgrade package
212         -un, --try-upgrade-with-float-version
213                         - as above, but allow float version
214         -U, --update
215                         - refetch sources, don't use distfiles, and update md5 
216                           comments
217         -Upi, --update-poldek-indexes 
218                         - refresh or make poldek package index files.
219         --with/--without <feature>
220                         - conditional build package depending on
221                           %_with_<feature>/%_without_<feature> macro
222                           switch. 
223                           You may now use --with feat1 feat2 feat3 --without feat4
224                           feat5 --with feat6 constructions.
225 "
226 }
227
228 cache_rpm_dump () {
229    rpm_dump_cache=`
230         case "$RPMBUILD" in
231         rpm )
232                 rpm -bp --nodeps --define 'prep %dump' $BCOND $SPECFILE 2>&1 
233                 ;;
234         rpmbuild )
235                 rpmbuild --nodigest --nosignature --define 'prep %dump' $BCOND $SPECFILE 2>&1 
236                 ;;
237         esac`
238 }
239
240 rpm_dump () {
241     if [ -z "$rpm_dump_cache" ] ; then
242         echo "internal error: cache_rpm_dump not called!" 1>&2
243     fi
244     echo "$rpm_dump_cache"
245 }
246
247 parse_spec()
248 {
249     if [ -n "$DEBUG" ]; then
250         set -x;
251         set -v;
252     fi
253
254     cd $SPECS_DIR
255
256     cache_rpm_dump
257
258     if [ "$NOSRCS" != "yes" ]; then
259         SOURCES="`rpm_dump | awk '/SOURCEURL[0-9]+/ {print $3}'`"
260     fi
261     if (rpm_dump | grep -qEi ":.*nosource.*1"); then
262         FAIL_IF_NO_SOURCES="no"
263     fi
264
265     PATCHES="`rpm_dump | awk '/PATCHURL[0-9]+/ {print $3}'`"
266     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
267     PACKAGE_NAME="`$RPM -q --qf '%{NAME}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
268     PACKAGE_VERSION="`$RPM -q --qf '%{VERSION}\n' --specfile ${SPECFILE} 2> /dev/null| head -1`"
269     PACKAGE_RELEASE="`$RPM -q --qf '%{RELEASE}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
270
271     if [ -n "$BE_VERBOSE" ]; then
272         echo "- Sources :  `nourl $SOURCES`"
273         if [ -n "$PATCHES" ]; then
274             echo "- Patches :  `nourl $PATCHES`"
275         else
276             echo "- Patches :  *no patches needed*"
277         fi
278         if [ -n "$ICONS" ]; then
279             echo "- Icon    :  `nourl $ICONS`"
280         else
281             echo "- Icon    :  *no package icon*"
282         fi
283         echo "- Name    : $PACKAGE_NAME"
284         echo "- Version : $PACKAGE_VERSION"
285         echo "- Release : $PACKAGE_RELEASE"
286     fi
287 }
288
289 Exit_error()
290 {
291     if [ -n "$DEBUG" ]; then
292         set -x;
293         set -v;
294     fi
295
296     cd $__PWD
297
298     case "$1" in
299     "err_no_spec_in_cmdl" )
300         echo "ERROR: spec file name not specified.";
301         exit 2 ;;
302     "err_no_spec_in_repo" )
303         echo "Error: spec file not stored in CVS repo.";
304         exit 3 ;;
305     "err_no_source_in_repo" )
306         echo "Error: some source, patch or icon files not stored in CVS repo. ($2)";
307         exit 4 ;;
308     "err_build_fail" )
309         echo "Error: package build failed. (${2:-no more info})";
310         exit 5 ;;
311     esac
312 }
313
314 init_builder()
315 {
316     if [ -n "$DEBUG" ]; then
317         set -x;
318         set -v;
319     fi
320
321     SOURCE_DIR="`$RPM --eval '%{_sourcedir}'`"
322     SPECS_DIR="`$RPM --eval '%{_specdir}'`"
323
324     __PWD=`pwd`
325 }
326
327 get_spec()
328 {
329     if [ -n "$DEBUG" ]; then
330         set -x;
331         set -v;
332     fi
333
334     if [ "$NOCVSSPEC" != "yes" ]; then
335         cd $SPECS_DIR
336
337         OPTIONS="up "
338
339         if [ -n "$CVSROOT" ]; then
340             OPTIONS="-d $CVSROOT $OPTIONS"
341         else
342             if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
343                 echo "warning: No cvs access defined - using local .spec file"
344                 NOCVSSPEC="yes"
345             fi
346         fi
347
348         if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
349             OPTIONS="$OPTIONS -A"
350         else
351             if [ -n "$CVSDATE" ]; then
352                 OPTIONS="$OPTIONS -D $CVSDATE"
353             fi
354             if [ -n "$CVSTAG" ]; then
355                 OPTIONS="$OPTIONS -r $CVSTAG"
356             fi
357         fi
358
359         result=1
360         retries_counter=0
361         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
362             retries_counter=$(( $retries_counter + 1 ))
363             output=$(LC_ALL=C cvs $OPTIONS $SPECFILE 2>&1)
364             result=$?
365             [ -n "$output" ] && echo "$output"
366             if [ "$result" -ne "0" ]; then
367                 if (echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)") && [ "$retries_counter" -le "$CVS_RETRIES" ]; then
368                     echo "Trying again [$SPECFILE]... ($retries_counter)"
369                     sleep 2
370                     continue
371                 fi
372                 Exit_error err_no_spec_in_repo;
373             fi
374         done
375     fi
376     if [ ! -f "$SPECFILE" ]; then
377         Exit_error err_no_spec_in_repo;
378     fi
379
380     if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
381         chmod $CHMOD_MODE $SPECFILE
382     fi
383     unset OPTIONS
384     grep -E -m 1 "^#.*Revision:.*Date" $SPECFILE
385 }
386
387 find_mirror(){
388
389     cd "$SPECS_DIR"
390     url="$1"
391     if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
392         cvs update mirrors >&2
393     fi
394
395     IFS="|"
396     while read origin mirror name rest; do
397         ol=`echo -n "$origin"|wc -c`
398         prefix="`echo -n "$url" | head -c $ol`"
399         if [ "$prefix" = "$origin" ] ; then
400             suffix="`echo "$url"|cut -b $ol-`"
401             echo -n "$mirror$suffix"
402             return 0
403         fi
404     done < mirrors
405     echo "$url"
406 }
407
408 src_no ()
409 {
410     cd $SPECS_DIR
411     rpm_dump | \
412         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
413         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
414         head -1 | xargs
415 }
416
417 src_md5 ()
418 {
419     no=$(src_no "$1")
420     [ -z "$no" ] && return
421     cd $SPECS_DIR
422     spec_rev=$(grep $SPECFILE CVS/Entries | sed -e s:/$SPECFILE/:: -e s:/.*::)
423     if [ -z "$spec_rev" ]; then
424         spec_rev="$(head -1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')"
425     fi
426     spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
427     md5=$(grep -s -v '^#' additional-md5sums | \
428         grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
429         sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
430         grep -E '^[0-9a-f]{32}$')
431     if [ X"$md5" = X"" ] ; then
432         grep -i "#[     ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://' | xargs
433     else
434         if [ $(echo "$md5" | wc -l) != 1 ] ; then
435             echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
436         fi
437         echo "$md5" | tail -1
438     fi
439 }
440
441 distfiles_url ()
442 {
443     echo "$PROTOCOL$DISTFILES_SERVER/by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
444 }
445
446 good_md5 ()
447 {
448     md5=$(src_md5 "$1")
449     [ "$md5" = "" ] || \
450     [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
451 }
452
453 get_files()
454 {
455     GET_FILES="$@"
456
457     if [ -n "$DEBUG" ]; then
458         set -x;
459         set -v;
460     fi
461
462     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
463         cd $SOURCE_DIR
464
465         OPTIONS="up "
466         if [ -n "$CVSROOT" ]; then
467             OPTIONS="-d $CVSROOT $OPTIONS"
468         else
469             if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
470                 echo "warning: No cvs access defined for SOURCES"
471                 NOCVS="yes"
472             fi
473         fi
474         if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
475             OPTIONS="$OPTIONS -A"
476         else
477             if [ -n "$CVSDATE" ]; then
478                 OPTIONS="$OPTIONS -D $CVSDATE"
479             fi
480             if [ -n "$CVSTAG" ]; then
481                 OPTIONS="$OPTIONS -r $CVSTAG"
482             fi
483         fi
484         for i in $GET_FILES; do
485             if [ -n "$UPDATE5" ]; then
486                 if [ -n "$ADD5" ]; then
487                     [ `nourl $i` = "$i" ] && continue
488                     grep -qiE '^#[      ]*Source'$(src_no $i)'-md5[     ]*:' $SPECS_DIR/$SPECFILE && continue
489                 else
490                     grep -qiE '^#[      ]*Source'$(src_no $i)'-md5[     ]*:' $SPECS_DIR/$SPECFILE || continue
491                 fi
492             fi
493             FROM_DISTFILES=0
494             if [ ! -f `nourl $i` ] || [ $ALWAYS_CVSUP = "yes" ]; then
495                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
496                     echo "Warning: no URL given for $i"
497                 fi
498
499                 if [ -n "$(src_md5 "$i")" ] && [ -z "$NODIST" ]; then
500                     if good_md5 "$i"; then
501                         echo "$(nourl "$i") having proper md5sum already exists"
502                         continue
503                     fi
504                     target=$(nourl "$i")
505                     url=$(distfiles_url "$i")
506                     if [ `echo $url | grep -E '^(\.|/)'` ]; then
507                         ${GETLOCAL} $url $target
508                     else
509                         FROM_DISTFILES=1
510                         if [ -z "$NOMIRRORS" ]; then
511                             url="`find_mirror "$url"`"
512                         fi
513                         ${GETURI} -O "$target" "$url" || \
514                             if [ `echo $url | grep -E 'ftp://'` ]; then
515                                 ${GETURI2} -O "$target" "$url"
516                             fi
517                         if ! test -s "$target"; then
518                             rm -f "$target"
519                             FROM_DISTFILES=0
520                         fi
521                     fi
522                 elif [ -z "$(src_md5 "$i")" -a "$NOCVS" != "yes" ]; then
523                     # ( echo $i | grep -qvE '(ftp|http|https)://' ); -- if CVS should be used, but URLs preferred
524                     result=1
525                     retries_counter=0
526                     while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
527                         retries_counter=$(( $retries_counter + 1 ))
528                         output=$(LC_ALL=C cvs $OPTIONS `nourl $i` 2>&1)
529                         result=$?
530                         [ -n "$output" ] && echo "$output"
531                         if (echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)") && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
532                                 echo "Trying again [`nourl $i`]... ($retries_counter)"
533                                 sleep 2
534                                 continue
535                         else
536                                 break
537                         fi
538                     done
539                 fi
540
541                 if [ -z "$NOURLS" ] && [ ! -f "`nourl $i`" -o -n "$UPDATE" ] && [ `echo $i | grep -E 'ftp://|http://|https://'` ]; then
542                     if [ -z "$NOMIRRORS" ]; then
543                         im="`find_mirror "$i"`"
544                     else
545                         im="$i"
546                     fi
547                     ${GETURI} "$im" || \
548                         if [ `echo $im | grep -E 'ftp://'` ]; then ${GETURI2} "$im" ; fi
549                 fi
550
551
552             fi
553             srcno=$(src_no $i)
554             if [ ! -f "`nourl $i`" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
555                 Exit_error err_no_source_in_repo $i;
556             elif [ -n "$UPDATE5" ] && \
557                 ( ( [ -n "$ADD5" ] && echo $i | grep -q -E 'ftp://|http://|https://' && \
558                     [ -z "$(grep -E -i '^NoSource[      ]*:[    ]*'$i'([        ]|$)' $SPECS_DIR/$SPECFILE)" ] ) || \
559                 grep -q -i -E '^#[      ]*source'$(src_no $i)'-md5[     ]*:' $SPECS_DIR/$SPECFILE )
560             then
561                 echo "Updating source-$srcno md5."
562                 md5=$(md5sum `nourl $i` | cut -f1 -d' ')
563                 perl -i -ne '
564                                 print unless /^\s*#\s*Source'$srcno'-md5\s*:/i;
565                                 print "# Source'$srcno'-md5:\t'$md5'\n" 
566                                         if /^Source'$srcno'\s*:\s+/;
567                                 ' \
568                                 $SPECS_DIR/$SPECFILE
569             fi
570
571             if good_md5 "$i"; then
572                 :
573             elif [ "$FROM_DISTFILES" = 1 ]; then
574                 # wrong md5 from distfiles: remove the file and try again
575                 # but only once ...
576                 echo "MD5 sum mismatch. Trying full fetch."
577                 FROM_DISTFILES=2
578                 rm -f $target
579                 ${GETURI} -O "$target" "$url" || \
580                     if [ `echo $url | grep -E 'ftp://'` ]; then
581                         ${GETURI2} -O "$target" "$url"
582                     fi
583                 test -s "$target" || rm -f "$target"
584             fi
585
586             if good_md5 "$i"; then
587                 :
588             else
589                 echo "MD5 sum mismatch.  Use -U to refetch sources,"
590                 echo "or -5 to update md5 sums, if you're sure files are correct."
591                 Exit_error err_no_source_in_repo $i
592             fi
593         done
594
595         if [ "$CHMOD" = "yes" ]; then
596             CHMOD_FILES="`nourl $GET_FILES`"
597             if [ -n "$CHMOD_FILES" ]; then
598                 chmod $CHMOD_MODE $CHMOD_FILES
599             fi
600         fi
601         unset OPTIONS
602     fi
603 }
604
605 tag_files()
606 {
607     TAG_FILES="$@"
608
609     if [ -n "$DEBUG" ]; then
610         set -x;
611         set -v;
612     fi
613
614     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
615         echo "Version: $PACKAGE_VERSION"
616         echo "Release: $PACKAGE_RELEASE"
617         # Check whether first character of PACKAGE_NAME is legal for tag name
618         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
619             TAG_PREFIX=tag_
620         fi
621         TAGVER=$TAG_PREFIX$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g" -e "s/@/#/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g" -e "s/@/#/g"`
622         # Remove #kernel.version_release from TAGVER because tagging sources
623         # could occur with different kernel-headers than kernel-headers used at build time.
624         TAGVER=$(echo "$TAGVER" | sed -e 's/#.*//g')
625         if [ "$TAG_VERSION" = "yes" ]; then
626             echo "CVS tag: $TAGVER"
627         fi
628         if [ -n "$TAG" ]; then
629             echo "CVS tag: $TAG"
630         fi
631
632         OPTIONS="tag -F"
633         if [ -n "$CVSROOT" ]; then
634             OPTIONS="-d $CVSROOT $OPTIONS"
635         fi
636
637         cd $SOURCE_DIR
638         for i in $TAG_FILES; do
639             # don't tag non cvs files (ie. stored on distfiles)
640             [ "`nourl $i`" != "$i" ] && continue
641             if [ -f "`nourl $i`" ]; then
642                 if [ "$TAG_VERSION" = "yes" ]; then
643                     cvs $OPTIONS $TAGVER `nourl $i`
644                 fi
645                 if [ -n "$TAG" ]; then
646                     cvs $OPTIONS $TAG `nourl $i`
647                 fi
648             else
649                 Exit_error err_no_source_in_repo $i
650             fi
651         done
652
653         cd $SPECS_DIR
654         if [ "$TAG_VERSION" = "yes" ]; then
655             cvs $OPTIONS $TAGVER $SPECFILE
656         fi
657         if [ -n "$TAG" ]; then
658             cvs $OPTIONS $TAG $SPECFILE
659         fi
660
661         unset OPTIONS
662     fi
663 }
664
665 branch_files()
666 {
667     TAG=$1
668     echo "CVS branch tag: $TAG"
669     shift;
670
671     TAG_FILES="$@"
672
673     if [ -n "$DEBUG" ]; then
674         set -x;
675         set -v;
676     fi
677
678     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
679
680         OPTIONS="tag -b"
681         if [ -n "$CVSROOT" ]; then
682             OPTIONS="-d $CVSROOT $OPTIONS"
683         fi
684         cd $SOURCE_DIR
685         for i in $TAG_FILES; do
686             if [ -f `nourl $i` ]; then
687                 cvs $OPTIONS $TAG `nourl $i`
688             else
689                 Exit_error err_no_source_in_repo $i
690             fi
691         done
692         cd $SPECS_DIR
693         cvs $OPTIONS $TAG $SPECFILE
694
695         unset OPTIONS
696     fi
697 }
698
699
700
701 build_package()
702 {
703     if [ -n "$DEBUG" ]; then
704         set -x;
705         set -v;
706     fi
707
708     cd $SPECS_DIR
709
710     if [ -n "$TRY_UPGRADE" ]; then
711
712         if [ -n "$FLOAT_VERSION" ]; then
713             TNOTIFY=`./pldnotify.awk $SPECFILE -n`
714         else
715             TNOTIFY=`./pldnotify.awk $SPECFILE`
716         fi
717
718         TNEWVER=`echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }'`
719
720         if [ -n "$TNEWVER" ]; then
721
722             TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
723
724             echo "New version found, updating spec file to version " $TNEWVER
725
726             cp -f $SPECFILE $SPECFILE.bak
727
728             chmod +w $SPECFILE
729
730             eval "perl -pi -e 's/Version:\t"$TOLDVER"/Version:\t"$TNEWVER"/gs' $SPECFILE"
731             eval "perl -pi -e 's/Release:\t[1-9]{0,4}/Release:\t1/' $SPECFILE"
732
733             parse_spec;
734
735             get_files "$SOURCES $PATCHES";
736
737             unset TOLDVER TNEWVER TNOTIFY
738         fi
739
740     fi
741
742
743     cd $SPECS_DIR
744
745     case "$COMMAND" in
746         build )
747             BUILD_SWITCH="-ba" ;;
748         build-binary )
749             BUILD_SWITCH="-bb" ;;
750         build-source )
751             BUILD_SWITCH="-bs --nodeps" ;;
752     esac
753     if [ -n "$LOGFILE" ]; then
754         LOG=`eval echo $LOGFILE`
755         if [ -n "$LASTLOG_FILE" ]; then
756             echo "LASTLOG=$LOG" > $LASTLOG_FILE
757         fi
758         RES_FILE=~/tmp/$RPMBUILD-exit-status.$RANDOM
759         (time nice -n ${DEF_NICE_LEVEL} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
760         RETVAL=`cat $RES_FILE`
761         rm $RES_FILE
762         if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
763             if [ "$RETVAL" -eq "0" ]; then
764                 mv $LOG $LOGDIROK
765             else
766                 mv $LOG $LOGDIRFAIL
767             fi
768         fi
769     else
770         eval nice -n ${DEF_NICE_LEVEL} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $SPECFILE
771         RETVAL=$?
772     fi
773
774     if [ "$RETVAL" -ne "0" ]; then
775
776         if [ -n "$TRY_UPGRADE" ]; then
777             echo "\n!!! Package with new version cannot be build automagically\n"
778             mv -f $SPECFILE.bak $SPECFILE
779         fi
780
781         Exit_error err_build_fail;
782     fi
783
784     unset BUILD_SWITCH
785 }
786
787 nourl()
788 {
789     echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
790 }
791
792 set_bconds_values()
793 {
794 AVAIL_BCONDS_WITHOUT=""
795 AVAIL_BCONDS_WITH=""
796 for opt in `$RPMBUILD --bcond $SPECFILE |grep ^_without_`
797 do
798         AVAIL_BCOND_WITHOUT=`echo $opt|sed -e "s/^_without_//g"`
799         if [ "`echo $BCOND|grep -- "--without $AVAIL_BCOND_WITHOUT"`" != "" ];then
800                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
801         else
802                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
803         fi
804 done
805
806 for opt in `$RPMBUILD --bcond $SPECFILE |grep ^_with_`
807 do
808         AVAIL_BCOND_WITH=`echo $opt|sed -e "s/^_with_//g"`
809         if [ "`echo $BCOND|grep -- "--with $AVAIL_BCOND_WITH"`" != "" ];then
810                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
811         else
812                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
813         fi
814 done
815 }
816
817 display_bconds()
818 {
819 if [ "$AVAIL_BCONDS_WITH" != "" ] || [ "$AVAIL_BCONDS_WITHOUT" != "" ]; then
820         echo -ne "We are going to build $SPECFILE with the following conditional flags:\n"
821         if [ "$BCOND" != "" ]; then
822                 echo -ne "$BCOND"
823         else
824                 echo -ne "No --with || --without conditions passed to $0!"
825         fi
826         echo -ne "\n\nfrom available:\n\n"
827         echo -ne "--with   :\t$AVAIL_BCONDS_WITH\n--without:\t$AVAIL_BCONDS_WITHOUT\n\n"
828 fi
829 }
830
831 fetch_build_requires()
832 {
833 if [ "$FETCH_BUILD_REQUIRES" == "yes" ]; then
834             for package_item in `cat $SPECFILE|grep -B100000 ^%changelog|grep -v ^#|grep BuildRequires|grep -v ^-|sed -e "s/^.*BuildRequires://g"|awk '{print $1}'|sed -e s,\(.*\),,g -e s,%{,,g`
835             do
836                 package_item=`echo $package_item|sed -e s,rpmbuild,rpm-build,g`
837                 GO="yes"
838                 package=`basename "$package_item"|sed -e "s/}$//g"`
839                 COND_ARCH_TST="`cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g"`"
840                 mach=`uname -m`
841                 
842                 COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
843                 if [ "`echo $COND_TST|grep '^BuildRequires:'`" != "" ]; then
844                         if [ "$COND_ARCH_TST" != "" ] && [ "`echo $COND_ARCH_TST|sed -e "s/i.86/ix86/g"`" != "`echo $mach|sed -e "s/i.86/ix86/g"`" ]; then
845                                 GO="yes"
846                         fi
847                 # bcond:
848                 else
849                         COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
850                         GO=""
851                         # %{without}
852                         if [ "`echo $COND_TST|grep 'without_'`" != "" ]; then
853                                 COND_NAME=`echo $COND_NAME|sed -e s,^.*_without_,,g`
854                                 if [ "`echo $COND_TST|grep !`" != "" ]; then
855                                         COND_STATE="with"
856                                 else
857                                         COND_STATE="wout"
858                                 fi
859                                 if [ "`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`" != "" ]; then
860                                         COND_ARGV="wout"
861                                 else
862                                         COND_ARGV="with"
863                                 fi
864                         # %{with}
865                         elif [ "`echo $COND_TST|grep 'with_'`" != "" ]; then
866                                 COND_NAME=`echo $COND_NAME|sed -e s,^.*_with_,,g`
867                                 if [ "`echo $COND_TST|grep !`" != "" ]; then
868                                         COND_STATE="wout"
869                                 else
870                                         COND_STATE="with"
871                                 fi                                      
872                                 if [ "`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`" != "" ]; then
873                                         COND_ARGV="with"
874                                 else
875                                         COND_ARGV="wout"
876                                 fi      
877                         fi
878                         RESULT="${COND_STATE}-${COND_ARGV}"
879                         case "$RESULT" in 
880                                 "with-wout" | "wout-with" )
881                                         GO=""
882                                         ;;
883                                 "wout-wout" | "with-with" )
884                                         GO="yes"
885                                         ;;
886                                 * )
887                                         echo "Action '$RESULT' was not defined for package '$package_item'"
888                                         ;;
889                         esac
890
891                 fi
892                
893                 if [ "$GO" == "yes" ]; then
894                         if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
895                                 echo "Package $package is not installed. Attempting to install..."
896                                 poldek -i $package
897                                 case $? in
898                                 1)
899                                         echo "Unable to install $package package! Still trying to fetch rest..."
900                                         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package"
901                                         ;;
902                                 0)
903                                         INSTALLED_PACKAGES="$package $INSTALLED_PACKAGES"
904                                         ;;
905                                 esac
906                         else
907                                 echo "Package $package is already installed. BuildRequirement satisfied."
908                         fi
909                 fi
910             done
911             if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
912                     echo "Nie uda³o siê zainstalowaæ nastêpuj±cych pakietów i ich zale¿no¶ci:"
913                     for pkg in "$NOT_INSTALLED_PACKAGES"
914                     do
915                             echo $pkg
916                     done
917                     exit 1
918             fi
919 fi
920 }
921
922 #---------------------------------------------
923 # main()
924
925 if [ "$#" = 0 ]; then
926     usage;
927     exit 1
928 fi
929
930 while test $# -gt 0 ; do
931     case "${1}" in
932         -5 | --update-md5 )
933             COMMAND="get";
934             NODIST="yes"
935             NOCVSSPEC="yes"
936             UPDATE5="yes"
937             shift ;;
938         -a5 | --add-md5 )
939             COMMAND="get";
940             NODIST="yes"
941             NOCVS="yes"
942             NOCVSSPEC="yes"
943             UPDATE5="yes"
944             ADD5="yes"
945             shift ;;
946         -D | --debug )
947             DEBUG="yes"; shift ;;
948         -V | --version )
949             COMMAND="version"; shift ;;
950         -a | --as_anon )
951             CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
952         -b | -ba | --build )
953             COMMAND="build"; shift ;;
954         -bb | --build-binary )
955             COMMAND="build-binary"; shift ;;
956         -bs | --build-source )
957             COMMAND="build-source"; shift ;;
958         -B | --branch )
959             COMMAND="branch"; shift; TAG="${1}"; shift;;
960         -c | --clean )
961             CLEAN="--clean --rmspec --rmsource"; shift ;;
962         -d | --cvsroot )
963             shift; CVSROOT="${1}"; shift ;;
964         -g | --get )
965             COMMAND="get"; shift ;;
966         -h | --help )
967             COMMAND="usage"; shift ;;
968         --http )
969             PROTOCOL="http"; shift ;;
970         -l | --logtofile )
971             shift; LOGFILE="${1}"; shift ;;
972         -ni| --nice )
973             shift; DEF_NICE_LEVEL=${1}; shift ;;
974         -m | --mr-proper )
975             COMMAND="mr-proper"; shift ;;
976         -nc | --no-cvs )
977             NOCVS="yes"; shift ;;
978         -ncs | --no-cvs-specs )
979             NOCVSSPEC="yes"; shift ;;
980         -nd | --no-distfiles )
981             NODIST="yes"; shift ;;
982         -nm | --no-mirrors )
983             NOMIRRORS="yes"; shift ;;
984         -nu | --no-urls )
985             NOURLS="yes"; shift ;;
986         -ns | --no-srcs )
987             NOSRCS="yes"; shift ;;
988         -ns0 | --no-source0 )
989             NOSOURCE0="yes"; shift ;;
990         --opts )
991             shift; RPMOPTS="${1}"; shift ;;
992         --with | --without )
993             case $GROUP_BCONDS in
994                 "yes")
995                         COND=${1}
996                         shift
997                         while [ "`echo ${1}|grep ^-`" == "" ] && [ "`echo ${1}|grep spec`" == "" ]
998                         do
999                                 BCOND="$BCOND $COND $1"
1000                                 shift
1001                         done;;
1002                 "no")
1003                         BCOND="$BCOND $1 $2" ; shift 2 ;;
1004             esac
1005             ;;
1006         -q | --quiet )
1007             QUIET="--quiet"; shift ;;
1008         --date )
1009             CVSDATE="${2}"; shift 2 ;;
1010         -r | --cvstag )
1011             shift; CVSTAG="${1}"; shift ;;
1012         -R | --fetch-build-requires)
1013             FETCH_BUILD_REQUIRES="yes"
1014             #UPDATE_POLDEK_INDEXES="yes"
1015             NOT_INSTALLED_PACKAGES=
1016             shift ;;
1017         -RB | --remove-build-requires)
1018             REMOVE_BUILD_REQUIRES="nice"
1019             shift ;;
1020         -FRB | --force-remove-build-requires)
1021             REMOVE_BUILD_REQUIRES="force"
1022             shift ;;
1023         -Tvs | --tag-version-stable )
1024             COMMAND="tag";
1025             TAG="STABLE"
1026             TAG_VERSION="yes"
1027             shift;;
1028         -Tvn | --tag-version-nest )
1029             COMMAND="tag";
1030             TAG="NEST"
1031             TAG_VERSION="yes"
1032             shift;;
1033         -Ts | --tag-stable )
1034             COMMAND="tag";
1035             TAG="STABLE"
1036             TAG_VERSION="no"
1037             shift;;
1038         -Tn | --tag-nest )
1039             COMMAND="tag";
1040             TAG="NEST"
1041             TAG_VERSION="no"
1042             shift;;
1043         -Tv | --tag-version )
1044             COMMAND="tag";
1045             TAG=""
1046             TAG_VERSION="yes"
1047             shift;;
1048         -Tp | --tag-prefix )
1049             TAG_PREFIX="$2"
1050             shift 2;;
1051         -T | --tag )
1052             COMMAND="tag";
1053             shift
1054             TAG="$1"
1055             TAG_VERSION="no"
1056             shift;;
1057         -U | --update )
1058             COMMAND="get"
1059             UPDATE="yes"
1060             NOCVSSPEC="yes"
1061             NODIST="yes"
1062             UPDATE5="yes"
1063             shift ;;
1064         -Upi | --update-poldek-indexes )
1065             UPDATE_POLDEK_INDEXES="yes"
1066             shift ;;
1067         -u | --try-upgrade )
1068             TRY_UPGRADE="1"; shift ;;
1069         -un | --try-upgrade-with-float-version )
1070             TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
1071         -v | --verbose )
1072             BE_VERBOSE="1"; shift ;;
1073         --define)
1074             shift
1075             MACRO="${1}"
1076             VALUE="${2}"
1077             shift 2
1078             RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
1079             ;;
1080         --nodeps)
1081             shift
1082             RPMOPTS="${RPMOPTS} --nodeps"
1083             ;;
1084         * )
1085             SPECFILE="`basename ${1} .spec`.spec"; shift ;;
1086     esac
1087 done
1088
1089 if [ -n "$DEBUG" ]; then
1090     set -x;
1091     set -v;
1092 fi
1093
1094 case "$COMMAND" in
1095     "build" | "build-binary" | "build-source" )
1096         init_builder;
1097         if [ -n "$SPECFILE" ]; then
1098             get_spec;
1099             set_bconds_values;
1100             display_bconds;
1101             fetch_build_requires;
1102             parse_spec;
1103
1104             if [ -n "$FAIL_IF_CHANGED_BUT_NOT_BUMPED" ]; then
1105                 TAGVER=$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g" -e "s/@/#/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g" -e "s/@/#/g"`
1106                 CURTAGREL=$(cvs status $SPECFILE | grep "Working revision:" | awk '{ print $3 }')
1107                 TAGREL=$(cvs status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
1108
1109                 if [ -n "$TAGREL" -a "$TAGREL" != "$CURTAGREL" ]; then
1110                     Exit_error err_build_fail "not bumped ver-rel - was already used in rev $TAGREL"
1111                 fi
1112             fi
1113
1114             if [ -n "$ICONS" ]; then
1115                 get_files $ICONS;
1116                 parse_spec;
1117             fi
1118             if [ -n "$NOSOURCE0" ] ; then
1119                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
1120             fi
1121             get_files "$SOURCES $PATCHES";
1122             build_package;
1123             if [ $? -eq 0 ] && [ "$UPDATE_POLDEK_INDEXES" == "yes" ]; then
1124                     poldek --sn ${POLDEK_SOURCE} --mkidx="${POLDEK_INDEX_DIR}/packages.dir.gz"
1125                     poldek --sn ${POLDEK_SOURCE} --up
1126             fi
1127             case "$REMOVE_BUILD_REQUIRES" in
1128                     "force")
1129                         poldek --noask -e $INSTALLED_PACKAGES
1130                     ;;
1131                     "nice")
1132                         poldek --ask -e $INSTALLED_PACKAGES
1133                     ;;
1134                     *)
1135                             echo You may want to manually remove following BuildRequires fetched:
1136                             echo $INSTALLED_PACKAGES
1137                     ;;
1138             esac
1139         else
1140             Exit_error err_no_spec_in_cmdl;
1141         fi
1142         ;;
1143     "branch" )
1144         init_builder;
1145         if [ -n "$SPECFILE" ]; then
1146                 get_spec;
1147                 parse_spec;
1148                 if [ -n "$ICONS" ]; then
1149                         get_files $ICONS
1150                         parse_spec;
1151                 fi
1152                 get_files $SOURCES $PATCHES;
1153                 branch_files $TAG "$SOURCES $PATCHES $ICONS";
1154         else
1155                 Exit_error err_no_spec_in_cmdl;
1156         fi
1157         ;;
1158     "get" )
1159         init_builder;
1160         if [ -n "$SPECFILE" ]; then
1161             get_spec;
1162             parse_spec;
1163             if [ -n "$ICONS" ]; then
1164                 get_files $ICONS
1165                 parse_spec;
1166             fi
1167             if [ -n "$NOSOURCE0" ] ; then
1168                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
1169             fi
1170             get_files $SOURCES $PATCHES
1171         else
1172             Exit_error err_no_spec_in_cmdl;
1173         fi
1174         ;;
1175     "tag" )
1176         init_builder;
1177         if [ -n "$SPECFILE" ]; then
1178             get_spec;
1179             parse_spec;
1180             if [ -n "$ICONS" ]; then
1181                 get_files $ICONS
1182                 parse_spec;
1183             fi
1184             # don't fetch sources from remote locations
1185             new_SOURCES=""
1186             for file in $SOURCES; do
1187                 [ "`nourl $file`" != "$file" ] && continue
1188                 new_SOURCES="$new_SOURCES $file"
1189             done
1190             SOURCES="$new_SOURCES"
1191             get_files $SOURCES $PATCHES;
1192             tag_files "$SOURCES $PATCHES $ICONS";
1193         else
1194             Exit_error err_no_spec_in_cmdl;
1195         fi
1196         ;;
1197     "mr-proper" )
1198         $RPM --clean --rmsource --rmspec --force --nodeps $SPECFILE
1199         ;;
1200     "usage" )
1201         usage;;
1202     "version" )
1203         echo "$VERSION";;
1204 esac
1205
1206 cd $__PWD
This page took 0.146852 seconds and 4 git commands to generate.