source: projects/self-build-setup/trunk/self-build-rpm.sh @ 435

Revision 435, 6.9 KB checked in by kazutaka, 14 years ago (diff)

fix typo

  • Property svn:executable set to *
Line 
1#!/bin/bash
2#
3# Helper script for self-build rpm package.
4# arguments 1:package name, 2:spec file, 3 to n-1: source url
5#           n:build log size
6#
7# NOTE: To avoid scriptlet failure during rpm transaction,
8#       This script should always return 0 even if there is an error.
9
10TEXTDOMAIN=self-build-setup
11TEXTDOMAINDIR=/usr/share/locale
12
13_NAME=$1
14_SPEC=$2
15_ARCH=$(rpm --eval %_arch)
16_QUEDIR=/var/cache/self-build
17_BASE_SIZE=0
18
19#
20# bar() : function to draw text-base progress bar
21#
22
23BAR_W=$((`tput cols`-20))
24COUNT=0
25LINES=0
26
27bar(){
28    while read line; do
29        let $((COUNT+=${#line}))
30        let $((LINES+=1))
31        if [ $(($LINES % 10)) = 0 ]; then
32            if [ $_BASE_SIZE -gt 0 ]; then
33                PERCENT=$((100*COUNT/_BASE_SIZE))
34                [ $PERCENT -gt 100 ] && PERCENT=100
35                BAR_LEFT=$((BAR_W*PERCENT/100))
36                printf "     %3d%%[" $PERCENT
37                for i in $(seq 1 $BAR_LEFT); do
38                    printf "="
39                done
40                printf ">"
41                for i in $(seq 1 $((BAR_W-BAR_LEFT))); do
42                    printf " "
43                done
44                printf "]\r"
45            else
46                printf "      --%%["
47                for i in $(seq 1 $(($LINES / 10))); do
48                    printf "="
49                done
50                printf ">]\r"
51                if [ $(($LINES / 10)) -eq $BAR_W ]; then
52                    LINES=0
53                    printf "\n"
54                fi
55            fi
56        fi
57    done
58    printf "\n"
59}
60
61#
62# percentage() : function to print progress percentage (for zenity)
63#
64
65percentage(){
66    while read line; do
67        let $((COUNT+=${#line}))
68        let $((LINES+=1))
69        if [ $(($LINES % 10)) = 0 ]; then
70            PERCENT=$((100*COUNT/_BASE_SIZE))
71            [ $PERCENT -gt 100 ] && PERCENT=100
72            echo $PERCENT"%"
73        fi
74    done
75}
76
77#
78# main
79#
80
81. /etc/self-build.conf
82
83
84echo -n "     "
85echo -n $"Start creating rpm packages."
86echo "($_NAME)"
87
88# create temporary rpm directory
89if ! _TOPDIR=`mktemp -d $TEMP_DIR/$_NAME.XXXXXX`; then
90    echo -n "     "
91    echo $"*ERROR: can't crete temporary directory."
92    exit 0
93fi
94mkdir $_TOPDIR/rpm
95mkdir $_TOPDIR/rpm/BUILD
96mkdir $_TOPDIR/rpm/RPMS
97mkdir $_TOPDIR/rpm/RPMS/$_ARCH
98mkdir $_TOPDIR/rpm/RPMS/noarch
99mkdir $_TOPDIR/rpm/SOURCES
100
101# copy patches and other files.
102cp -a /usr/share/$_NAME/* $_TOPDIR/rpm/SOURCES
103
104# check last argument and set build log size
105eval LAST=\$\{$(expr $#)\}
106if echo $LAST | grep -E '^[0-9]+$' > /dev/null 2>&1; then
107    _BASE_SIZE=$LAST
108fi
109
110# get source
111echo -n "     "
112echo $"Downloading source files..."
113shift 2
114for u in $@; do
115    # if not valid url (http: or ftp:) then skip
116    if ! echo $u | grep "tp:"; then
117        continue
118    fi
119
120    # if you already downloaded source files,
121    # then copy these files from cache directory
122    _SRC=$_TOPDIR/rpm/SOURCES/$(basename $u)
123    _CACHEDSRC=$_QUEDIR/$(basename $u)
124    [ -s $_CACHEDSRC ] && cp -af $_CACHEDSRC $_SRC
125    [ -s $_SRC ] && continue
126
127    if [ ! $DL_SHOW_PROGRESS = "yes" ]; then
128        # run quiet, no GUI
129        wget -q -P $_TOPDIR/rpm/SOURCES $u
130        RESULT=$?
131    elif [ -z `pgrep -n synaptic` ]; then
132        # run verbose, without GUI (because no synaptic running)
133        wget -P $_TOPDIR/rpm/SOURCES $u
134        RESULT=$?
135    else
136        # run verbose with GUI (zenity)
137        DLSTAT=0
138        DLSTAT=(`wget -P $_TOPDIR/rpm/SOURCES $u 2>&1 | \
139             sed -u 's/.* \+\([0-9]\+%\).*$/\1/' 2>/dev/null | \
140             zenity --auto-close --progress --auto-kill \
141                     --title $"Download files" \
142                     --text $"Downloading $(basename $u)..."; echo ${PIPESTATUS[0]}`)
143        RESULT=$?
144        # need to use string cpmparison for DLSTAT because PIPESTATUS[0] returns
145        # null value when wget is killed with zenity auto-kill function.
146        if [ "$DLSTAT" == "0" ] && [ $RESULT -eq 0 ]; then
147            RESULT=0
148        else
149            RESULT=1
150        fi
151    fi
152    if [ $RESULT -ne 0 -a -e $_SRC ]; then
153        rm -f $_SRC
154    else
155        # if cache directory has no source files, then copy these files to cache directory
156        [ -s $_CACHEDSRC ] || cp -af $_SRC $_CACHEDSRC
157    fi
158done
159for f in $(echo $@ | xargs -r -n1 basename|sort -u|grep "tp:"); do
160    if [ ! -s $_TOPDIR/rpm/SOURCES/$f ]; then
161        echo -n "     "
162        echo $"*ERROR: can't download source file."": $f"
163        rm -rf $_TOPDIR
164        exit 0
165    fi
166done
167echo -n "     "
168echo $"Source files download successfully."
169
170# build rpm package
171echo -n "     "
172echo $"Building rpm packages..."
173echo -n "      "
174echo $"(see $TEMP_DIR/$_NAME.log for detail.)"
175# now rpmbuild done by non-root user $BUILD_USER
176rm -f $TEMP_DIR/$_NAME.log
177chown -R $BUILD_USER:$BUILD_USER $_TOPDIR
178
179if [ ! $BUILD_SHOW_PROGRESS = "yes" ]; then
180    # run quiet, no GUI
181    su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" >$TEMP_DIR/$_NAME.log 2>&1
182    RESULT=$?
183elif [ -z `pgrep -n synaptic` ]; then
184    # run verbose, without GUI (because no synaptic running)
185    su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | tee $TEMP_DIR/$_NAME.log | bar
186    RESULT=$?
187else
188    # run verbose with GUI (zenity)
189    BUILDSTAT=0
190    if [ $_BASE_SIZE -gt 0 ]; then
191        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | \
192                tee $TEMP_DIR/$_NAME.log | percentage | \
193                zenity --auto-close --progress --auto-kill \
194                        --title $"Build of rpm package" \
195                        --text $"Building $(basename $_SPEC .spec) rpm packages..."; \
196                echo ${PIPESTATUS[0]}`)
197        RESULT=$?
198    else
199        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | \
200                tee $TEMP_DIR/$_NAME.log | \
201                zenity --auto-close --progress --pulsate --auto-kill \
202                        --title $"Build of rpm package" \
203                        --text $"Building $(basename $_SPEC .spec) rpm packages..."; \
204                echo ${PIPESTATUS[0]}`)
205        RESULT=$?
206    fi   
207    # need to use string cpmparison for BUILDSTAT because PIPESTATUS[0] returns
208    # null value when wget is killed with zenity auto-kill function.
209    if [ "$BUILDSTAT" == "0" ] && [ $RESULT -eq 0 ]; then
210        RESULT=0
211    else
212        RESULT=1
213    fi
214fi
215
216if [ $RESULT -eq 0 ] ; then
217    echo -n "     "
218    echo $"rpm packages are successfully built."
219    # move created rpm packages to que directory
220    if ls $_TOPDIR/rpm/RPMS/$_ARCH | grep -e "\.rpm$" > /dev/null ; then
221        mv -f $_TOPDIR/rpm/RPMS/$_ARCH/*.rpm $_QUEDIR
222    fi
223    if ls $_TOPDIR/rpm/RPMS/noarch | grep -e "\.rpm$" > /dev/null ; then
224        mv -f $_TOPDIR/rpm/RPMS/noarch/*.rpm $_QUEDIR
225    fi
226    rm -rf $_TOPDIR
227else
228    echo -n "     "
229    echo $"*ERROR: can't build rpm packages."
230    rm -rf $_TOPDIR
231    exit 0
232fi
233exit 0
Note: See TracBrowser for help on using the repository browser.