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