]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- s/^Utilities/Applications
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # Exit codes:
4 #       0 - succesful
5 #       1 - help dispayed
6 #       2 - no spec file name in cmdl parameters
7 #       3 - spec file not stored in repo
8 #       4 - some source, apatch or icon files not stored in repo
9 #       5 - build package no succed
10
11 VERSION="\
12 Build package utility from PLD CVS repository
13 V 0.8 (C) 1999 Tomasz K³oczko".
14
15 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
16  
17 COMMAND="build"
18
19 SPECFILE=""
20 BE_VERBOSE=""
21 QUIET=""
22 CLEAN=""
23 DEBUG=""
24 NOURLS=""
25 NOCVS=""
26 ALLWAYS_CVSUP="yes"
27 CVSROOT=${CVSROOT:-""}
28 LOGFILE=""
29 CHMOD="yes"
30 RPMOPTS=""
31
32 PATCHES=""
33 SOURCES=""
34 ICONS=""
35 PACKAGE_RELEASE=""
36 PACKAGE_VERSION=""
37 PACKAGE_NAME=""
38
39 DEF_NICE_LEVEL=0
40
41 if [ -f ~/etc/builderrc ]; then
42   . ~/etc/builderrc
43 elif [ -f ~/.builderrc ]; then
44   . ~/.builderrc
45 fi
46
47 #---------------------------------------------
48 # functions
49
50 usage()
51 {
52     if [ -n "$DEBUG" ]; then set -xv; fi
53     echo "\
54 Usage: builder [-D] [--debug] [-V] [--version] [-a] [--as_anon] [-b] [-ba]
55         [--build] [-bb] [--build-binary] [-bs] [--build-source]
56         [-d <cvsroot>] [--cvsroot <cvsroot>] [-g] [--get] [-h] [--help]
57         [-l <logfile>] [-m] [--mr-proper] [--logtofile <logfile>] [-q] [--quiet]
58         [-r <cvstag>] [--cvstag <cvstag>] [-u] [--no-urls] [-v] [--verbose]
59         [--opts <rpm opts>] <package>.spec
60
61         -D, --debug     - enable script debugging mode,
62         -V, --version   - output builder version
63         -a, --as_anon   - get files via pserver as cvs@anoncvs.pld.org.pl,
64         -b, -ba,
65         --build         - get all files from CVS repo or HTTP/FTP and build
66                           package from <package>.spec,
67         -bb,
68         --build-binary  - get all files from CVS repo or HTTP/FTP and build
69                           binary only package from <package>.spec,
70         -bs,
71         --build-source  - get all files from CVS repo or HTTP/FTP and only
72                           pack them into src.rpm,
73         -c, --clean     - clean all temporarily created files (in BUILD,
74                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
75                           SOURCES, SPECS and \$RPM_BUILD_ROOT),
76         -d, --cvsroot   - setup \$CVSROOT,
77         -g, --get       - get <package>.spec and all related files from
78                           CVS repo or HTTP/FTP,
79         -h, --help      - this message,
80         -l, --logtofile - log all to file,
81         -m, --mr-proper - only remove all files related to spec file and
82                           all work resources,
83         -nc, --no-cvs   - don't download from CVS, if source URL is given,
84         -nu, --no-urls  - don't try to download from FTP/HTTP location,
85         --opts          - additional options for rpm
86         -q, --quiet     - be quiet,
87         -r, --cvstag    - build package using resources from specified CVS
88                           tag,
89         -v, --verbose   - be verbose,
90
91 "
92 }
93
94 parse_spec()
95 {
96     if [ -n "$DEBUG" ]; then 
97         set -x;
98         set -v; 
99     fi
100
101     sed -e "s#%prep#%dump#I" $SPECFILE | grep -v -i "^Icon\:" > $SPECFILE.__
102
103     SOURCES="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ SOURCEURL[0-9]+/ {print $3}'`"
104     PATCHES="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ PATCHURL[0-9]+/ {print $3}'`"
105     ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
106     PACKAGE_NAME="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ name/ {print $3}'`"
107     PACKAGE_VERSION="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ PACKAGE_VERSION/ {print $3}'`"
108     PACKAGE_RELEASE="`rpm -bp --test $SPECFILE.__ 2>&1 | awk '/ PACKAGE_RELEASE/ {print $3}'`"
109
110     rm -f $SPECFILE.__
111
112     if [ -n "$BE_VERBOSE" ]; then
113         echo "- Sources :  `nourl $SOURCES`" 
114         if [ -n "$PATCHES" ]; then
115                 echo "- Patches :  `nourl $PATCHES`"
116         else
117                 echo "- Patches :  *no patches needed*"
118         fi
119         if [ -n "$ICONS" ]; then
120             echo "- Icon    :  `nourl $ICONS`"
121         else
122             echo "- Icon    :  *no package icon*"
123         fi
124         echo "- Name    : $PACKAGE_NAME"
125         echo "- Version : $PACKAGE_VERSION"
126         echo "- Release : $PACKAGE_RELEASE"
127     fi
128 }
129
130 Exit_error()
131 {
132     if [ -n "$DEBUG" ]; then 
133         set -x;
134         set -v; 
135     fi
136
137     cd $__PWD
138
139     case "$@" in
140     "err_no_spec_in_cmdl" )
141         echo "ERROR: spec file name not specified.";
142         exit 2 ;;
143     "err_no_spec_in_repo" )
144         echo "Error: spec file not stored in CVS repo.";
145         exit 3 ;;
146     "err_no_source_in_repo" )
147         echo "Error: some source, patch or icon files not stored in CVS repo.";
148         exit 4 ;;
149     "err_build_fail" )
150         echo "Error: package build failed.";
151         exit 5 ;;
152     esac
153 }
154
155 init_builder()
156 {
157     if [ -n "$DEBUG" ]; then 
158         set -x;
159         set -v; 
160     fi
161
162     SOURCE_DIR="`rpm --eval "%{_sourcedir}"`"
163     SPECS_DIR="`rpm --eval "%{_specdir}"`"
164
165     __PWD=`pwd`
166 }
167
168 get_spec()
169 {
170     if [ -n "$DEBUG" ]; then 
171         set -x;
172         set -v; 
173     fi
174
175     cd $SPECS_DIR
176
177     OPTIONS="up "
178
179     if [ -n "$CVSROOT" ]; then
180         OPTIONS="-d $CVSROOT $OPTIONS"
181     fi
182     if [ -n "$CVSTAG" ]; then
183         OPTIONS="$OPTIONS -r $CVSTAG"
184     else
185         OPTIONS="$OPTIONS -A"
186     fi
187
188     cvs $OPTIONS $SPECFILE
189     if [ "$?" -ne "0" ]; then
190         Exit_error err_no_spec_in_repo;
191     fi
192         if [ ! -f "$SPECFILE" ]; then
193         Exit_error err_no_spec_in_repo;
194         fi
195     
196     if [ "$CHMOD" = "yes" ]; then
197         chmod 444 $SPECFILE
198     fi
199     unset OPTIONS
200 }
201
202 get_all_files()
203 {
204     if [ -n "$DEBUG" ]; then 
205         set -x;
206         set -v; 
207     fi
208
209     if [ -n "$SOURCES$PATCHES$ICONS" ]; then
210         cd $SOURCE_DIR
211
212         OPTIONS="up "
213         if [ -n "$CVSROOT" ]; then
214             OPTIONS="-d $CVSROOT $OPTIONS"
215         fi
216         if [ -n "$CVSTAG" ]; then
217             OPTIONS="$OPTIONS -r $CVSTAG"
218         else
219             OPTIONS="$OPTIONS -A"
220         fi
221         for i in $SOURCES $PATCHES $ICONS; do
222             if [ ! -f `nourl $i` ] || [ $ALLWAYS_CVSUP = "yes" ] 
223               then
224                 if 
225                         echo $i | grep -vE '(http|ftp|https|cvs)://' |\
226                         grep -qE '\.(gz|bz2)$'
227                 then
228                         echo "Warning: no URL given for $i"
229                 fi
230                 
231                 if      [ -z "$NOCVS" ]||\
232                         [ `echo $i | grep -vE '(ftp|http|https)://'` ]
233                 then
234                         cvs $OPTIONS `nourl $i`
235                 fi
236                 
237                 if      [ -z "$NOURLS" ]&&[ ! -f "`nourl $i`" ]&&\
238                         [ `echo $i | grep -E 'ftp://|http://|https://'` ]
239                 then
240                         wget -c -nd -t0 "$i"
241                 fi
242
243                 if [ ! -f "`nourl $i`" ]; then
244                         Exit_error err_no_source_in_repo;
245                 fi
246             fi
247         done
248         
249         if [ "$CHMOD" = "yes" ]; then
250             chmod 444 `nourl $SOURCES $PATCHES $ICONS`
251         fi
252         unset OPTIONS
253     fi
254 }
255
256 build_package()
257 {
258     if [ -n "$DEBUG" ]; then 
259         set -x;
260         set -v; 
261     fi
262
263     cd $SPECS_DIR
264     case "$COMMAND" in
265         build )
266             BUILD_SWITCH="-ba" ;;
267         build-binary )
268             BUILD_SWITCH="-bb" ;;
269         build-source )
270             BUILD_SWITCH="-bs --nodeps" ;;
271     esac
272     nice -n ${DEF_NICE_LEVEL} rpm $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $SPECFILE 
273
274     if [ "$?" -ne "0" ]; then
275         Exit_error err_build_fail;
276     fi
277     unset BUILD_SWITCH
278 }
279
280 nourl()
281 {
282         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\)://.*/##g'
283 }
284 #---------------------------------------------
285 # main()
286
287 if [ "$#" = 0 ]; then
288     usage;
289     exit 1
290 fi
291
292 while test $# -gt 0 ; do
293     case "${1}" in
294         -D | --debug )
295             DEBUG="yes"; shift ;;
296         -V | --version )
297             COMMAND="version"; shift ;;
298         -a | --as_anon )
299             CVSROOT=":pserver:cvs@anoncvs.pld.org.pl:/cvsroot"; shift ;;
300         -b | -ba | --build )
301             COMMAND="build"; shift ;;
302         -bb | --build-binary )
303             COMMAND="build-binary"; shift ;;
304         -bs | --build-source )
305             COMMAND="build-source"; shift ;;
306         -c | --clean )
307             CLEAN="--clean --rmspec --rmsource"; shift ;;
308         -d | --cvsroot )
309             shift; CVSROOT="${1}"; shift ;;
310         -g | --get )
311             COMMAND="get"; shift ;;
312         -h | --help )
313             COMMAND="usage"; shift ;;
314         -l | --logtofile )
315             shift; LOGFILE="${1}"; shift ;;
316         -ni| --nice )
317             shift; DEF_NICE_LEVEL=${1}; shift ;;
318         -m | --mr-proper )
319             COMMAND="mr-proper"; shift ;;
320         -nc | --no-cvs )
321             NOCVS="yes"; shift ;;
322         -nu | --no-urls )
323             NOURLS="yes"; shift ;;
324         --opts )
325             shift; RPMOPTS="${1}"; shift ;;
326         -q | --quiet )
327             QUIET="--quiet"; shift ;;
328         -r | --cvstag )
329             shift; CVSTAG="${1}"; shift ;;
330         -v | --verbose )
331             BE_VERBOSE="1"; shift ;;
332         * )
333             SPECFILE="${1}"; shift ;;
334     esac
335 done
336
337 if [ -n "$DEBUG" ]; then 
338         set -x;
339         set -v; 
340 fi
341
342 case "$COMMAND" in
343     "build" | "build-binary" | "build-source" )
344         init_builder;
345         if [ -n "$SPECFILE" ]; then
346             get_spec;
347             parse_spec;
348             get_all_files;
349             build_package;
350         else
351             Exit_error err_no_spec_in_cmdl;
352         fi
353         ;;
354     "get" )
355         init_builder;
356         if [ -n "$SPECFILE" ]; then
357             get_spec;
358             parse_spec;
359             get_all_files;
360         else
361             Exit_error err_no_spec_in_cmdl;
362         fi
363         ;;
364     "mr-proper" )
365         rpm --clean --rmsource --rmspec --force --nodeps $SPECFILE
366         ;;
367     "usage" )
368         usage;;
369     "version" )
370         echo "$VERSION";;
371 esac
372
373 cd $__PWD
This page took 0.113979 seconds and 3 git commands to generate.