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