]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- added raw branching support
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # $Id$
4 # Exit codes:
5 #       0 - succesful
6 #       1 - help dispayed
7 #       2 - no spec file name in cmdl parameters
8 #       3 - spec file not stored in repo
9 #       4 - some source, apatch or icon files not stored in repo
10 #       5 - build package no succed
11
12 VERSION="\
13 Build package utility from PLD CVS repository
14 V 0.9 (C) 1999-2001 Tomasz K³oczko".
15
16 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
17  
18 COMMAND="build"
19
20 SPECFILE=""
21 BE_VERBOSE=""
22 QUIET=""
23 CLEAN=""
24 DEBUG=""
25 NOURLS=""
26 NOCVS=""
27 ALLWAYS_CVSUP="yes"
28 if [ -s CVS/Root ]; then
29     CVSROOT=$(cat CVS/Root)
30 else
31     CVSROOT=${CVSROOT:-""}
32 fi
33 LOGFILE=""
34 CHMOD=${CHMOD:-"yes"}
35 CHMOD_MODE=${CHMOD_MODE:-444}
36 RPMOPTS=""
37 BCOND=""
38
39 PATCHES=""
40 SOURCES=""
41 ICONS=""
42 PACKAGE_RELEASE=""
43 PACKAGE_VERSION=""
44 PACKAGE_NAME=""
45 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
46
47 DEF_NICE_LEVEL=0
48
49 FAIL_IF_NO_SOURCES="yes"
50
51 GETURI="wget -c -nd -t$WGET_RETRIES"
52
53 if [ -f ~/etc/builderrc ]; then
54   . ~/etc/builderrc
55 elif [ -f ~/.builderrc ]; then
56   . ~/.builderrc
57 fi
58
59 #---------------------------------------------
60 # functions
61
62 usage()
63 {
64     if [ -n "$DEBUG" ]; then set -xv; fi
65     echo "\
66 Usage: builder [-D] [--debug] [-V] [--version] [-a] [--as_anon] [-b] [-ba]
67         [--build] [-bb] [--build-binary] [-bs] [--build-source]
68         [-B <branch>] [--branch <branch>] [-d <cvsroot>] [--cvsroot <cvsroot>] 
69         [-g] [--get] [-h] [--help] [-l <logfile>] [-m] [--mr-proper] 
70         [--logtofile <logfile>] [-q] [--quiet] [-r <cvstag>] [--cvstag <cvstag>] 
71         [-u] [--no-urls] [-v] [--verbose] [--opts <rpm opts>] 
72         [--with/--without pkg] <package>.spec
73
74         -D, --debug     - enable script debugging mode,
75         -V, --version   - output builder version
76         -a, --as_anon   - get files via pserver as cvs@anoncvs.pld.org.pl,
77         -b, -ba,
78         --build         - get all files from CVS repo or HTTP/FTP and build
79                           package from <package>.spec,
80         -bb,
81         --build-binary  - get all files from CVS repo or HTTP/FTP and build
82                           binary only package from <package>.spec,
83         -bs,
84         --build-source  - get all files from CVS repo or HTTP/FTP and only
85                           pack them into src.rpm,
86         -B, --branch    - add branch
87         -c, --clean     - clean all temporarily created files (in BUILD,
88                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
89                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
90         -d, --cvsroot   - setup \$CVSROOT,
91         -g, --get       - get <package>.spec and all related files from
92                           CVS repo or HTTP/FTP,
93         -h, --help      - this message,
94         -l, --logtofile - log all to file,
95         -m, --mr-proper - only remove all files related to spec file and
96                           all work resources,
97         -nc, --no-cvs   - don't download from CVS, if source URL is given,
98         -nu, --no-urls  - don't try to download from FTP/HTTP location,
99         -ns, --no-srcs  - don't downland Sources
100         --opts          - additional options for rpm
101         -q, --quiet     - be quiet,
102         -r, --cvstag    - build package using resources from specified CVS
103                           tag,
104         -T, --tag       - add cvs tags for files,
105         -v, --verbose   - be verbose,
106
107 "
108 }
109
110 parse_spec()
111 {
112     if [ -n "$DEBUG" ]; then 
113         set -x;
114         set -v;
115     fi
116
117     cd $SPECS_DIR
118
119     if [ "$NOSRCS" != "yes" ]; then
120         SOURCES="`rpm -bp --nobuild --define 'prep %dump' $SPECFILE 2>&1 | awk '/SOURCEURL[0-9]+/ {print $3}'`"
121     fi
122     if (rpm -bp --nobuild --define 'prep %dump' $SPECFILE 2>&1 | grep -qEi ":.*nosource.*1"); then
123         FAIL_IF_NO_SOURCES="no"
124     fi
125
126
127     PATCHES="`rpm -bp --nobuild --define 'prep %dump' $SPECFILE 2>&1 | awk '/PATCHURL[0-9]+/ {print $3}'`"
128     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
129     PACKAGE_NAME="`rpm -q --qf '%{NAME}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
130     PACKAGE_VERSION="`rpm -q --qf '%{VERSION}\n' --specfile ${SPECFILE} 2> /dev/null| head -1`"
131     PACKAGE_RELEASE="`rpm -q --qf '%{RELEASE}\n' --specfile ${SPECFILE} 2> /dev/null | head -1`"
132
133     if [ -n "$BE_VERBOSE" ]; then
134         echo "- Sources :  `nourl $SOURCES`" 
135         if [ -n "$PATCHES" ]; then
136                 echo "- Patches :  `nourl $PATCHES`"
137         else
138                 echo "- Patches :  *no patches needed*"
139         fi
140         if [ -n "$ICONS" ]; then
141             echo "- Icon    :  `nourl $ICONS`"
142         else
143             echo "- Icon    :  *no package icon*"
144         fi
145         echo "- Name    : $PACKAGE_NAME"
146         echo "- Version : $PACKAGE_VERSION"
147         echo "- Release : $PACKAGE_RELEASE"
148     fi
149 }
150
151 Exit_error()
152 {
153     if [ -n "$DEBUG" ]; then 
154         set -x;
155         set -v; 
156     fi
157
158     cd $__PWD
159
160     case "$1" in
161     "err_no_spec_in_cmdl" )
162         echo "ERROR: spec file name not specified.";
163         exit 2 ;;
164     "err_no_spec_in_repo" )
165         echo "Error: spec file not stored in CVS repo.";
166         exit 3 ;;
167     "err_no_source_in_repo" )
168         echo "Error: some source, patch or icon files not stored in CVS repo. ($2)";
169         exit 4 ;;
170     "err_build_fail" )
171         echo "Error: package build failed.";
172         exit 5 ;;
173     esac
174 }
175
176 init_builder()
177 {
178     if [ -n "$DEBUG" ]; then 
179         set -x;
180         set -v; 
181     fi
182
183     SOURCE_DIR="`rpm --eval '%{_sourcedir}'`"
184     SPECS_DIR="`rpm --eval '%{_specdir}'`"
185
186     __PWD=`pwd`
187 }
188
189 get_spec()
190 {
191     if [ -n "$DEBUG" ]; then 
192         set -x;
193         set -v; 
194     fi
195
196     cd $SPECS_DIR
197
198     OPTIONS="up "
199
200     if [ -n "$CVSROOT" ]; then
201         OPTIONS="-d $CVSROOT $OPTIONS"
202     fi
203     if [ -n "$CVSTAG" ]; then
204         OPTIONS="$OPTIONS -r $CVSTAG"
205     else
206         OPTIONS="$OPTIONS -A"
207     fi
208
209     cvs $OPTIONS $SPECFILE
210     if [ "$?" -ne "0" ]; then
211         Exit_error err_no_spec_in_repo;
212     fi
213         if [ ! -f "$SPECFILE" ]; then
214         Exit_error err_no_spec_in_repo;
215         fi
216     
217     if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
218         chmod $CHMOD_MODE $SPECFILE
219     fi
220     unset OPTIONS
221 }
222
223 get_files()
224 {
225     GET_FILES="$@"
226
227     if [ -n "$DEBUG" ]; then 
228         set -x;
229         set -v; 
230     fi
231
232     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
233         cd $SOURCE_DIR
234
235         OPTIONS="up "
236         if [ -n "$CVSROOT" ]; then
237             OPTIONS="-d $CVSROOT $OPTIONS"
238         fi
239         if [ -n "$CVSTAG" ]; then
240             OPTIONS="$OPTIONS -r $CVSTAG"
241         else
242             OPTIONS="$OPTIONS -A"
243         fi
244         for i in $GET_FILES; do
245             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ] 
246               then
247                 if 
248                         echo $i | grep -vE '(http|ftp|https|cvs)://' |\
249                         grep -qE '\.(gz|bz2)$'
250                 then
251                         echo "Warning: no URL given for $i"
252                 fi
253                 
254                 if      [ -z "$NOCVS" ]||\
255                         [ `echo $i | grep -vE '(ftp|http|https)://'` ]
256                 then
257                         cvs $OPTIONS `nourl $i`
258                 fi
259                 
260                 if      [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ]&&\
261                         [ `echo $i | grep -E 'ftp://|http://|https://'` ]
262                 then
263                         ${GETURI} "$i"
264                 fi
265
266                 if [ ! -f "`nourl $i`" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
267                         Exit_error err_no_source_in_repo $i;
268                 fi
269             fi
270         done
271         
272         if [ "$CHMOD" = "yes" ]; then
273             CHMOD_FILES="`nourl $GET_FILES`"
274             if [ -n "$CHMOD_FILES" ]; then
275                     chmod $CHMOD_MODE $CHMOD_FILES
276             fi
277         fi
278         unset OPTIONS
279     fi
280 }
281
282 tag_files()
283 {
284     TAG_FILES="$@"
285
286     if [ -n "$DEBUG" ]; then 
287         set -x;
288         set -v; 
289     fi
290
291     if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
292         echo $PACKAGE_VERSION
293         echo $PACKAGE_RELEASE
294         TAG=$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g"`
295         echo "CVS tag: $TAG"
296
297         OPTIONS="tag -F"
298         if [ -n "$CVSROOT" ]; then
299             OPTIONS="-d $CVSROOT $OPTIONS"
300         fi
301
302         cd $SOURCE_DIR
303         for i in $TAG_FILES; do
304             if [ -f `nourl $i` ]; then
305                 cvs $OPTIONS $TAG `nourl $i`
306                 cvs $OPTIONS STABLE `nourl $i`
307             else
308                 Exit_error err_no_source_in_repo $i
309             fi
310         done
311
312         cd $SPECS_DIR
313         cvs $OPTIONS $TAG $SPECFILE
314         cvs $OPTIONS STABLE $SPECFILE
315
316         unset OPTIONS
317     fi
318 }
319
320 branch_files()
321 {
322         TAG=$1
323         echo "CVS tag: $TAG"
324         shift;
325
326         TAG_FILES="$@"
327
328         if [ -n "$DEBUG" ]; then
329                 set -x;
330                 set -v;
331         fi
332
333         if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
334                 
335                 OPTIONS="tag -b"
336                 if [ -n "$CVSROOT" ]; then
337                         OPTIONS="-d $CVSROOT $OPTIONS"
338                 fi
339                 cd $SOURCE_DIR
340                 for i in $TAG_FILES; do
341                         if [ -f `nourl $i` ]; then
342                                 cvs $OPTIONS $TAG `nourl $i`
343                         else
344                                 Exit_error err_no_source_in_repo $i
345                         fi
346                 done
347                 cd $SPECS_DIR
348                 cvs $OPTIONS $TAG $SPECFILE
349
350                 unset OPTIONS
351         fi
352 }
353
354 build_package()
355 {
356     if [ -n "$DEBUG" ]; then 
357         set -x;
358         set -v; 
359     fi
360
361     cd $SPECS_DIR
362     case "$COMMAND" in
363         build )
364             BUILD_SWITCH="-ba" ;;
365         build-binary )
366             BUILD_SWITCH="-bb" ;;
367         build-source )
368             BUILD_SWITCH="-bs --nodeps" ;;
369     esac
370     nice -n ${DEF_NICE_LEVEL} rpm $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $SPECFILE 
371
372     if [ "$?" -ne "0" ]; then
373         Exit_error err_build_fail;
374     fi
375     unset BUILD_SWITCH
376 }
377
378 nourl()
379 {
380         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\)://.*/##g'
381 }
382 #---------------------------------------------
383 # main()
384
385 if [ "$#" = 0 ]; then
386     usage;
387     exit 1
388 fi
389
390 while test $# -gt 0 ; do
391     case "${1}" in
392         -D | --debug )
393             DEBUG="yes"; shift ;;
394         -V | --version )
395             COMMAND="version"; shift ;;
396         -a | --as_anon )
397             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
398         -b | -ba | --build )
399             COMMAND="build"; shift ;;
400         -bb | --build-binary )
401             COMMAND="build-binary"; shift ;;
402         -bs | --build-source )
403             COMMAND="build-source"; shift ;;
404         -B | --branch )
405             COMMAND="branch"; shift; TAG="${1}"; shift;;
406         -c | --clean )
407             CLEAN="--clean --rmspec --rmsource"; shift ;;
408         -d | --cvsroot )
409             shift; CVSROOT="${1}"; shift ;;
410         -g | --get )
411             COMMAND="get"; shift ;;
412         -h | --help )
413             COMMAND="usage"; shift ;;
414         -l | --logtofile )
415             shift; LOGFILE="${1}"; shift ;;
416         -ni| --nice )
417             shift; DEF_NICE_LEVEL=${1}; shift ;;
418         -m | --mr-proper )
419             COMMAND="mr-proper"; shift ;;
420         -nc | --no-cvs )
421             NOCVS="yes"; shift ;;
422         -nu | --no-urls )
423             NOURLS="yes"; shift ;;
424         -ns | --no-srcs )
425             NOSRCS="yes"; shift ;;
426         --opts )
427             shift; RPMOPTS="${1}"; shift ;;
428         --with | --without )
429             BCOND="$BCOND $1 $2" ; shift 2 ;;
430         -q | --quiet )
431             QUIET="--quiet"; shift ;;
432         -r | --cvstag )
433             shift; CVSTAG="${1}"; shift ;;
434         -T | --tag )
435             COMMAND="tag"; shift;;
436         -v | --verbose )
437             BE_VERBOSE="1"; shift ;;
438         * )
439             SPECFILE="${1}"; shift ;;
440     esac
441 done
442
443 if [ -n "$DEBUG" ]; then 
444         set -x;
445         set -v; 
446 fi
447
448 case "$COMMAND" in
449     "build" | "build-binary" | "build-source" )
450         init_builder;
451         if [ -n "$SPECFILE" ]; then
452             get_spec;
453             parse_spec;
454             if [ -n "$ICONS" ]; then
455                 get_files $ICONS;
456                 parse_spec;
457             fi
458             get_files "$SOURCES $PATCHES";
459             build_package;
460         else
461             Exit_error err_no_spec_in_cmdl;
462         fi
463         ;;
464     "branch" )
465         init_builder;
466         if [ -n "$SPECFILE" ]; then
467                 get_spec;
468                 parse_spec;
469                 if [ -n "$ICONS" ]; then
470                         get_files $ICONS
471                         parse_spec;
472                 fi
473                 get_files $SOURCES $PATCHES;
474                 branch_files $TAG "$SOURCES $PATCHES $ICONS";
475         else
476                 Exit_error err_no_spec_in_cmdl;
477         fi
478         ;;
479     "get" )
480         init_builder;
481         if [ -n "$SPECFILE" ]; then
482             get_spec;
483             parse_spec;
484             if [ -n "$ICONS" ]; then
485                     get_files $ICONS
486                     parse_spec;
487             fi
488             get_files $SOURCES $PATCHES
489         else
490             Exit_error err_no_spec_in_cmdl;
491         fi
492         ;;
493     "tag" )
494         init_builder;
495         if [ -n "$SPECFILE" ]; then
496             get_spec;
497             parse_spec;
498             if [ -n "$ICONS" ]; then
499                     get_files $ICONS
500                     parse_spec;
501             fi
502             get_files $SOURCES $PATCHES;
503             tag_files "$SOURCES $PATCHES $ICONS";
504         else
505             Exit_error err_no_spec_in_cmdl;
506         fi
507         ;;
508     "mr-proper" )
509         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
510         ;;
511     "usage" )
512         usage;;
513     "version" )
514         echo "$VERSION";;
515 esac
516
517 cd $__PWD
518
519 # $Log$
520 # Revision 1.84  2001/11/07 22:08:48  ankry
521 # - make builder's chmod configurable
522 #
523 # Revision 1.83  2001/10/10 08:41:32  misiek
524 # - allow more bconds than one
525 #
526 # Revision 1.82  2001/09/18 10:55:37  ankry
527 # - added support for limitting number of wget retries when fetching a file
528 #   from ftp/http server via environment variable MAX_WGET_RETRIES.
529 #   Defaults to infinite (0).
530 #
531 # Revision 1.81  2001/07/06 16:52:30  misiek
532 # - by default use CVSroot from CVS/Root and if it doesn't exist use from CVSROOT variable
533 #
534 # Revision 1.80  2001/06/22 18:52:39  misiek
535 # - added support for --with/--without options
536 #
537 # Revision 1.79  2001/05/28 14:44:16  baggins
538 # - if file is not in repo TELL which fucking file it is!
539 #
540 # Revision 1.78  2001/05/13 19:04:44  misiek
541 # fixes for ksh93
542 #
543 # Revision 1.77  2001/05/13 10:51:30  misiek
544 # don't fail if no sources found (hack to allow build nosrc packages)
545 #
546 # Revision 1.76  2001/04/19 23:24:06  misiek
547 # fix chmod again
548 #
549 # Revision 1.75  2001/04/19 23:14:25  misiek
550 # redirect errors from query to /dev/null
551 #
552 # Revision 1.74  2001/04/02 15:39:29  misiek
553 # fix problems with get_files when no files passed
554 #
555 # Revision 1.73  2001/03/30 14:06:10  wiget
556 # massive typo by kloczek
557 #
558 # Revision 1.72  2001/03/26 22:16:22  kloczek
559 # - fixed grabbing name, version and release in parse_spec(),
560 # - added -T option (tag) (temporary it tags also additional STABLE tag - must
561 #   be added -Ts for separate tagging as STABLE).
562 #
563 # Revision 1.71  2001/03/05 14:12:27  misiek
564 # fix chmod
565 #
566 # Revision 1.70  2001/03/03 19:55:42  misiek
567 # workaround for problems with rpm when icons isn't cvs up'ed
568 #
569 #
This page took 0.095346 seconds and 3 git commands to generate.