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