source: projects/self-build-setup/tags/0.9.7/self-build-rpm.sh @ 475

Revision 475, 7.0 KB checked in by kazutaka, 14 years ago (diff)

実際よりかなり小さいログファイルサイズが与えられた時に、ビルドが中断して失敗する問題を解消する為に、パーセンテージが 100 を超えたら(=Zenitiy が auto close した後は) echo しないように修正。

  • 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 -le 100 ] && echo $PERCENT"%"
72        fi
73    done
74}
75
76#
77# main
78#
79
80. /etc/self-build.conf
81
82
83echo -n "     "
84echo -n $"Start creating rpm packages."
85echo "($_NAME)"
86
87# create temporary rpm directory
88if ! _TOPDIR=`mktemp -d $TEMP_DIR/$_NAME.XXXXXX`; then
89    echo -n "     "
90    echo $"*ERROR: can't crete temporary directory."
91    exit 0
92fi
93mkdir $_TOPDIR/rpm
94mkdir $_TOPDIR/rpm/BUILD
95mkdir $_TOPDIR/rpm/RPMS
96mkdir $_TOPDIR/rpm/RPMS/$_ARCH
97mkdir $_TOPDIR/rpm/RPMS/noarch
98mkdir $_TOPDIR/rpm/SOURCES
99
100# copy patches and other files.
101cp -a /usr/share/$_NAME/* $_TOPDIR/rpm/SOURCES
102
103# check last argument and set build log size
104eval LAST=\$\{$(expr $#)\}
105if echo $LAST | grep -E '^[0-9]+$' > /dev/null 2>&1; then
106    _BASE_SIZE=$LAST
107fi
108
109# get source
110echo -n "     "
111echo $"Downloading source files..."
112shift 2
113for u in $@; do
114    # if not valid url (http: or ftp:) then skip
115    if ! echo $u | grep "tp:"; then
116        continue
117    fi
118
119    # if you already downloaded source files,
120    # then copy these files from cache directory
121    _SRC=$_TOPDIR/rpm/SOURCES/$(basename $u)
122    _CACHEDSRC=$_QUEDIR/$(basename $u)
123    if [ "$CACHE_DOWNLOADED_FILES" = "yes" ]; then
124        [ -s $_CACHEDSRC ] && cp -af $_CACHEDSRC $_SRC
125    fi
126    [ -s $_SRC ] && continue
127
128    if [ ! $DL_SHOW_PROGRESS = "yes" ]; then
129        # run quiet, no GUI
130        wget -q -P $_TOPDIR/rpm/SOURCES $u
131        RESULT=$?
132    elif [ -z `pgrep -n synaptic` ]; then
133        # run verbose, without GUI (because no synaptic running)
134        wget -P $_TOPDIR/rpm/SOURCES $u
135        RESULT=$?
136    else
137        # run verbose with GUI (zenity)
138        DLSTAT=0
139        DLSTAT=(`wget -P $_TOPDIR/rpm/SOURCES $u 2>&1 | \
140             sed -u 's/.* \+\([0-9]\+%\).*$/\1/' 2>/dev/null | \
141             zenity --auto-close --progress --auto-kill \
142                     --title $"Download files" \
143                     --text $"Downloading $(basename $u)..."; echo ${PIPESTATUS[0]}`)
144        RESULT=$?
145        # need to use string cpmparison for DLSTAT because PIPESTATUS[0] returns
146        # null value when wget is killed with zenity auto-kill function.
147        if [ "$DLSTAT" == "0" ] && [ $RESULT -eq 0 ]; then
148            RESULT=0
149        else
150            RESULT=1
151        fi
152    fi
153    if [ $RESULT -ne 0 -a -e $_SRC ]; then
154        rm -f $_SRC
155    else
156        # if cache directory has no source files, then copy these files to cache directory
157        if [ "$CACHE_DOWNLOADED_FILES" = "yes" ]; then
158            [ -s $_CACHEDSRC ] || cp -af $_SRC $_CACHEDSRC
159        fi
160    fi
161done
162for f in $(echo $@ | xargs -r -n1 basename|sort -u|grep "tp:"); do
163    if [ ! -s $_TOPDIR/rpm/SOURCES/$f ]; then
164        echo -n "     "
165        echo $"*ERROR: can't download source file."": $f"
166        rm -rf $_TOPDIR
167        exit 0
168    fi
169done
170echo -n "     "
171echo $"Source files download successfully."
172
173# build rpm package
174echo -n "     "
175echo $"Building rpm packages..."
176echo -n "      "
177echo $"(see $TEMP_DIR/$_NAME.log for detail.)"
178# now rpmbuild done by non-root user $BUILD_USER
179rm -f $TEMP_DIR/$_NAME.log
180chown -R $BUILD_USER:$BUILD_USER $_TOPDIR
181
182if [ ! $BUILD_SHOW_PROGRESS = "yes" ]; then
183    # run quiet, no GUI
184    su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" >$TEMP_DIR/$_NAME.log 2>&1
185    RESULT=$?
186elif [ -z `pgrep -n synaptic` ]; then
187    # run verbose, without GUI (because no synaptic running)
188    su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | tee $TEMP_DIR/$_NAME.log | bar
189    RESULT=$?
190else
191    # run verbose with GUI (zenity)
192    BUILDSTAT=0
193    if [ $_BASE_SIZE -gt 0 ]; then
194        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | \
195                tee $TEMP_DIR/$_NAME.log | percentage | \
196                zenity --auto-close --progress --auto-kill \
197                        --title $"Build of rpm package" \
198                        --text $"Building $(basename $_SPEC .spec) rpm packages..."; \
199                echo ${PIPESTATUS[0]}`)
200        RESULT=$?
201    else
202        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" /usr/share/$_NAME/$_SPEC" 2>&1 | \
203                tee $TEMP_DIR/$_NAME.log | \
204                zenity --auto-close --progress --pulsate --auto-kill \
205                        --title $"Build of rpm package" \
206                        --text $"Building $(basename $_SPEC .spec) rpm packages..."; \
207                echo ${PIPESTATUS[0]}`)
208        RESULT=$?
209    fi   
210    # need to use string cpmparison for BUILDSTAT because PIPESTATUS[0] returns
211    # null value when wget is killed with zenity auto-kill function.
212    if [ "$BUILDSTAT" == "0" ] && [ $RESULT -eq 0 ]; then
213        RESULT=0
214    else
215        RESULT=1
216    fi
217fi
218
219if [ $RESULT -eq 0 ] ; then
220    echo -n "     "
221    echo $"rpm packages are successfully built."
222    # move created rpm packages to que directory
223    if ls $_TOPDIR/rpm/RPMS/$_ARCH | grep -e "\.rpm$" > /dev/null ; then
224        mv -f $_TOPDIR/rpm/RPMS/$_ARCH/*.rpm $_QUEDIR
225    fi
226    if ls $_TOPDIR/rpm/RPMS/noarch | grep -e "\.rpm$" > /dev/null ; then
227        mv -f $_TOPDIR/rpm/RPMS/noarch/*.rpm $_QUEDIR
228    fi
229    rm -rf $_TOPDIR
230else
231    echo -n "     "
232    echo $"*ERROR: can't build rpm packages."
233    rm -rf $_TOPDIR
234    exit 0
235fi
236exit 0
Note: See TracBrowser for help on using the repository browser.