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