]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- back to using temporary file (--nobuild works as not as I think/expect).
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # $Revision$, $Date$
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.8 (C) 1999 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         --opts          - additional options for rpm
89         -q, --quiet     - be quiet,
90         -r, --cvstag    - build package using resources from specified CVS
91                           tag,
92         -v, --verbose   - be verbose,
93
94 "
95 }
96
97 parse_spec()
98 {
99     if [ -n "$DEBUG" ]; then 
100         set -x;
101         set -v; 
102     fi
103
104     sed -e "s#%prep#%dump#I" $SPECFILE | grep -v -i "^Icon\:" > $SPECFILE.__
105
106     SOURCES="`rpm -bp --nobuild $SPECFILE.__ 2>&1 | awk '/ SOURCEURL[0-9]+/ {print $3}'`"
107     PATCHES="`rpm -bp --nobuild $SPECFILE.__ 2>&1 | awk '/ PATCHURL[0-9]+/ {print $3}'`"
108     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
109     PACKAGE_NAME="`rpm -bp --nobuild $SPECFILE.__ 2>&1 | awk '/ name/ {print $3}'`"
110     PACKAGE_VERSION="`rpm -bp --nobuild $SPECFILE.__ 2>&1 | awk '/ PACKAGE_VERSION/ {print $3}'`"
111     PACKAGE_RELEASE="`rpm -bp --nobuild $SPECFILE.__ 2>&1 | awk '/ PACKAGE_RELEASE/ {print $3}'`"
112
113     rm -f $SPECFILE.__
114
115     if [ -n "$BE_VERBOSE" ]; then
116         echo "- Sources :  `nourl $SOURCES`" 
117         if [ -n "$PATCHES" ]; then
118                 echo "- Patches :  `nourl $PATCHES`"
119         else
120                 echo "- Patches :  *no patches needed*"
121         fi
122         if [ -n "$ICONS" ]; then
123             echo "- Icon    :  `nourl $ICONS`"
124         else
125             echo "- Icon    :  *no package icon*"
126         fi
127         echo "- Name    : $PACKAGE_NAME"
128         echo "- Version : $PACKAGE_VERSION"
129         echo "- Release : $PACKAGE_RELEASE"
130     fi
131 }
132
133 Exit_error()
134 {
135     if [ -n "$DEBUG" ]; then 
136         set -x;
137         set -v; 
138     fi
139
140     cd $__PWD
141
142     case "$@" in
143     "err_no_spec_in_cmdl" )
144         echo "ERROR: spec file name not specified.";
145         exit 2 ;;
146     "err_no_spec_in_repo" )
147         echo "Error: spec file not stored in CVS repo.";
148         exit 3 ;;
149     "err_no_source_in_repo" )
150         echo "Error: some source, patch or icon files not stored in CVS repo.";
151         exit 4 ;;
152     "err_build_fail" )
153         echo "Error: package build failed.";
154         exit 5 ;;
155     esac
156 }
157
158 init_builder()
159 {
160     if [ -n "$DEBUG" ]; then 
161         set -x;
162         set -v; 
163     fi
164
165     SOURCE_DIR="`rpm --eval "%{_sourcedir}"`"
166     SPECS_DIR="`rpm --eval "%{_specdir}"`"
167
168     __PWD=`pwd`
169 }
170
171 get_spec()
172 {
173     if [ -n "$DEBUG" ]; then 
174         set -x;
175         set -v; 
176     fi
177
178     cd $SPECS_DIR
179
180     OPTIONS="up "
181
182     if [ -n "$CVSROOT" ]; then
183         OPTIONS="-d $CVSROOT $OPTIONS"
184     fi
185     if [ -n "$CVSTAG" ]; then
186         OPTIONS="$OPTIONS -r $CVSTAG"
187     else
188         OPTIONS="$OPTIONS -A"
189     fi
190
191     cvs $OPTIONS $SPECFILE
192     if [ "$?" -ne "0" ]; then
193         Exit_error err_no_spec_in_repo;
194     fi
195         if [ ! -f "$SPECFILE" ]; then
196         Exit_error err_no_spec_in_repo;
197         fi
198     
199     if [ "$CHMOD" = "yes" ]; then
200         chmod 444 $SPECFILE
201     fi
202     unset OPTIONS
203 }
204
205 get_all_files()
206 {
207     if [ -n "$DEBUG" ]; then 
208         set -x;
209         set -v; 
210     fi
211
212     if [ -n "$SOURCES$PATCHES$ICONS" ]; then
213         cd $SOURCE_DIR
214
215         OPTIONS="up "
216         if [ -n "$CVSROOT" ]; then
217             OPTIONS="-d $CVSROOT $OPTIONS"
218         fi
219         if [ -n "$CVSTAG" ]; then
220             OPTIONS="$OPTIONS -r $CVSTAG"
221         else
222             OPTIONS="$OPTIONS -A"
223         fi
224         for i in $SOURCES $PATCHES $ICONS; do
225             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ] 
226               then
227                 if 
228                         echo $i | grep -vE '(http|ftp|https|cvs)://' |\
229                         grep -qE '\.(gz|bz2)$'
230                 then
231                         echo "Warning: no URL given for $i"
232                 fi
233                 
234                 if      [ -z "$NOCVS" ]||\
235                         [ `echo $i | grep -vE '(ftp|http|https)://'` ]
236                 then
237                         cvs $OPTIONS `nourl $i`
238                 fi
239                 
240                 if      [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ]&&\
241                         [ `echo $i | grep -E 'ftp://|http://|https://'` ]
242                 then
243                         ${GETURI} "$i"
244                 fi
245
246                 if [ ! -f "`nourl $i`" ]; then
247                         Exit_error err_no_source_in_repo;
248                 fi
249             fi
250         done
251         
252         if [ "$CHMOD" = "yes" ]; then
253             chmod 444 `nourl $SOURCES $PATCHES $ICONS`
254         fi
255         unset OPTIONS
256     fi
257 }
258
259 build_package()
260 {
261     if [ -n "$DEBUG" ]; then 
262         set -x;
263         set -v; 
264     fi
265
266     cd $SPECS_DIR
267     case "$COMMAND" in
268         build )
269             BUILD_SWITCH="-ba" ;;
270         build-binary )
271             BUILD_SWITCH="-bb" ;;
272         build-source )
273             BUILD_SWITCH="-bs --nodeps" ;;
274     esac
275     nice -n ${DEF_NICE_LEVEL} rpm $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $SPECFILE 
276
277     if [ "$?" -ne "0" ]; then
278         Exit_error err_build_fail;
279     fi
280     unset BUILD_SWITCH
281 }
282
283 nourl()
284 {
285         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\)://.*/##g'
286 }
287 #---------------------------------------------
288 # main()
289
290 if [ "$#" = 0 ]; then
291     usage;
292     exit 1
293 fi
294
295 while test $# -gt 0 ; do
296     case "${1}" in
297         -D | --debug )
298             DEBUG="yes"; shift ;;
299         -V | --version )
300             COMMAND="version"; shift ;;
301         -a | --as_anon )
302             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
303         -b | -ba | --build )
304             COMMAND="build"; shift ;;
305         -bb | --build-binary )
306             COMMAND="build-binary"; shift ;;
307         -bs | --build-source )
308             COMMAND="build-source"; shift ;;
309         -c | --clean )
310             CLEAN="--clean --rmspec --rmsource"; shift ;;
311         -d | --cvsroot )
312             shift; CVSROOT="${1}"; shift ;;
313         -g | --get )
314             COMMAND="get"; shift ;;
315         -h | --help )
316             COMMAND="usage"; shift ;;
317         -l | --logtofile )
318             shift; LOGFILE="${1}"; shift ;;
319         -ni| --nice )
320             shift; DEF_NICE_LEVEL=${1}; shift ;;
321         -m | --mr-proper )
322             COMMAND="mr-proper"; shift ;;
323         -nc | --no-cvs )
324             NOCVS="yes"; shift ;;
325         -nu | --no-urls )
326             NOURLS="yes"; shift ;;
327         --opts )
328             shift; RPMOPTS="${1}"; shift ;;
329         -q | --quiet )
330             QUIET="--quiet"; shift ;;
331         -r | --cvstag )
332             shift; CVSTAG="${1}"; shift ;;
333         -v | --verbose )
334             BE_VERBOSE="1"; shift ;;
335         * )
336             SPECFILE="${1}"; shift ;;
337     esac
338 done
339
340 if [ -n "$DEBUG" ]; then 
341         set -x;
342         set -v; 
343 fi
344
345 case "$COMMAND" in
346     "build" | "build-binary" | "build-source" )
347         init_builder;
348         if [ -n "$SPECFILE" ]; then
349             get_spec;
350             parse_spec;
351             get_all_files;
352             build_package;
353         else
354             Exit_error err_no_spec_in_cmdl;
355         fi
356         ;;
357     "get" )
358         init_builder;
359         if [ -n "$SPECFILE" ]; then
360             get_spec;
361             parse_spec;
362             get_all_files;
363         else
364             Exit_error err_no_spec_in_cmdl;
365         fi
366         ;;
367     "mr-proper" )
368         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
369         ;;
370     "usage" )
371         usage;;
372     "version" )
373         echo "$VERSION";;
374 esac
375
376 cd $__PWD
This page took 0.052105 seconds and 4 git commands to generate.