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