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