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

Revision 7448, 7.0 KB checked in by munepi, 9 years ago (diff)

bump vertion to 1.0.2

  • 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 -lt 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
92_SOURCEDIR=$_TOPDIR/rpm/SOURCES/$_NAME
93
94mkdir -p \
95    $_TOPDIR/rpm/BUILD \
96    $_TOPDIR/rpm/RPMS/{i386,i586,i686,x86_64,ppc,noarch} \
97    $_TOPDIR/rpm/SRPMS \
98    $_SOURCEDIR
99
100# copy patches and other files.
101cp -a /usr/share/$_NAME/* $_SOURCEDIR
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 (https?: or ftp:) then skip
115    if ! echo $u | egrep "^(https?|ftp):"; then
116        continue
117    fi
118
119    # if you already downloaded source files,
120    # then copy these files from cache directory
121    _SRC=$_SOURCEDIR/$(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 ${WGET_OPTS} -q -O $_SOURCEDIR/$(basename $u) $u
131        RESULT=$?
132    elif [ -z `pgrep -n synaptic` ]; then
133        # run verbose, without GUI (because no synaptic running)
134        wget ${WGET_OPTS} -O $_SOURCEDIR/$(basename $u) $u
135        RESULT=$?
136    else
137        # run verbose with GUI (zenity)
138        DLSTAT=0
139        DLSTAT=(`wget ${WGET_OPTS} -O $_SOURCEDIR/$(basename $u) $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|egrep "^(https?|ftp):"); do
163    if [ ! -s $_SOURCEDIR/$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\" --define=\"_sourcedir $_SOURCEDIR\" /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\" --define=\"_sourcedir $_SOURCEDIR\" /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\" --define=\"_sourcedir $_SOURCEDIR\" /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 | sed -e 's/-vl$//') rpm packages..."; \
199                echo ${PIPESTATUS[0]}`)
200        RESULT=$?
201    else
202        BUILDSTAT=(`su $BUILD_USER -c "rpmbuild -bb --define=\"_topdir $_TOPDIR/rpm\" --define=\"_sourcedir $_SOURCEDIR\" /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 | sed -e 's/-vl$//') 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 rpmbuild 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    find $_TOPDIR/rpm/RPMS -type f -name "*.rpm" -exec mv {} $_QUEDIR \;
224    rm -rf $_TOPDIR
225else
226    echo -n "     "
227    echo $"*ERROR: can't build rpm packages."
228    rm -rf $_TOPDIR
229    exit 0
230fi
231exit 0
Note: See TracBrowser for help on using the repository browser.