source: projects/self-build-setup/tags/0.9.6/self-build-rpm.sh @ 439

Revision 439, 7.0 KB checked in by munepi, 14 years ago (diff)

added CACHE_DOWNLOADED_FILES option to cache downloaded files

  • 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    if [ "$CACHE_DOWNLOADED_FILES" = "yes" ]; then
125        [ -s $_CACHEDSRC ] && cp -af $_CACHEDSRC $_SRC
126    fi
127    [ -s $_SRC ] && continue
128
129    if [ ! $DL_SHOW_PROGRESS = "yes" ]; then
130        # run quiet, no GUI
131        wget -q -P $_TOPDIR/rpm/SOURCES $u
132        RESULT=$?
133    elif [ -z `pgrep -n synaptic` ]; then
134        # run verbose, without GUI (because no synaptic running)
135        wget -P $_TOPDIR/rpm/SOURCES $u
136        RESULT=$?
137    else
138        # run verbose with GUI (zenity)
139        DLSTAT=0
140        DLSTAT=(`wget -P $_TOPDIR/rpm/SOURCES $u 2>&1 | \
141             sed -u 's/.* \+\([0-9]\+%\).*$/\1/' 2>/dev/null | \
142             zenity --auto-close --progress --auto-kill \
143                     --title $"Download files" \
144                     --text $"Downloading $(basename $u)..."; echo ${PIPESTATUS[0]}`)
145        RESULT=$?
146        # need to use string cpmparison for DLSTAT because PIPESTATUS[0] returns
147        # null value when wget is killed with zenity auto-kill function.
148        if [ "$DLSTAT" == "0" ] && [ $RESULT -eq 0 ]; then
149            RESULT=0
150        else
151            RESULT=1
152        fi
153    fi
154    if [ $RESULT -ne 0 -a -e $_SRC ]; then
155        rm -f $_SRC
156    else
157        # if cache directory has no source files, then copy these files to cache directory
158        if [ "$CACHE_DOWNLOADED_FILES" = "yes" ]; then
159            [ -s $_CACHEDSRC ] || cp -af $_SRC $_CACHEDSRC
160        fi
161    fi
162done
163for f in $(echo $@ | xargs -r -n1 basename|sort -u|grep "tp:"); do
164    if [ ! -s $_TOPDIR/rpm/SOURCES/$f ]; then
165        echo -n "     "
166        echo $"*ERROR: can't download source file."": $f"
167        rm -rf $_TOPDIR
168        exit 0
169    fi
170done
171echo -n "     "
172echo $"Source files download successfully."
173
174# build rpm package
175echo -n "     "
176echo $"Building rpm packages..."
177echo -n "      "
178echo $"(see $TEMP_DIR/$_NAME.log for detail.)"
179# now rpmbuild done by non-root user $BUILD_USER
180rm -f $TEMP_DIR/$_NAME.log
181chown -R $BUILD_USER:$BUILD_USER $_TOPDIR
182
183if [ ! $BUILD_SHOW_PROGRESS = "yes" ]; then
184    # run quiet, no GUI
185    su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" >$TEMP_DIR/$_NAME.log 2>&1
186    RESULT=$?
187elif [ -z `pgrep -n synaptic` ]; then
188    # run verbose, without GUI (because no synaptic running)
189    su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | tee $TEMP_DIR/$_NAME.log | bar
190    RESULT=$?
191else
192    # run verbose with GUI (zenity)
193    BUILDSTAT=0
194    if [ $_BASE_SIZE -gt 0 ]; then
195        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | \
196                tee $TEMP_DIR/$_NAME.log | percentage | \
197                zenity --auto-close --progress --auto-kill \
198                        --title $"Build of rpm package" \
199                        --text $"Building $(basename $_SPEC .spec) rpm packages..."; \
200                echo ${PIPESTATUS[0]}`)
201        RESULT=$?
202    else
203        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | \
204                tee $TEMP_DIR/$_NAME.log | \
205                zenity --auto-close --progress --pulsate --auto-kill \
206                        --title $"Build of rpm package" \
207                        --text $"Building $(basename $_SPEC .spec) rpm packages..."; \
208                echo ${PIPESTATUS[0]}`)
209        RESULT=$?
210    fi   
211    # need to use string cpmparison for BUILDSTAT because PIPESTATUS[0] returns
212    # null value when wget is killed with zenity auto-kill function.
213    if [ "$BUILDSTAT" == "0" ] && [ $RESULT -eq 0 ]; then
214        RESULT=0
215    else
216        RESULT=1
217    fi
218fi
219
220if [ $RESULT -eq 0 ] ; then
221    echo -n "     "
222    echo $"rpm packages are successfully built."
223    # move created rpm packages to que directory
224    if ls $_TOPDIR/rpm/RPMS/$_ARCH | grep -e "\.rpm$" > /dev/null ; then
225        mv -f $_TOPDIR/rpm/RPMS/$_ARCH/*.rpm $_QUEDIR
226    fi
227    if ls $_TOPDIR/rpm/RPMS/noarch | grep -e "\.rpm$" > /dev/null ; then
228        mv -f $_TOPDIR/rpm/RPMS/noarch/*.rpm $_QUEDIR
229    fi
230    rm -rf $_TOPDIR
231else
232    echo -n "     "
233    echo $"*ERROR: can't build rpm packages."
234    rm -rf $_TOPDIR
235    exit 0
236fi
237exit 0
Note: See TracBrowser for help on using the repository browser.