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