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