source: projects/self-build-setup/tags/0.9.9/self-build-rpm.sh @ 548

Revision 548, 6.9 KB checked in by kazutaka, 12 years ago (diff)

discard tput's stderr to supress error message while running via synaptic package manager.

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