~ubuntu-branches/ubuntu/utopic/byobu/utopic

« back to all changes in this revision

Viewing changes to usr/lib/byobu/updates_available

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2010-01-11 22:54:36 UTC
  • mto: This revision was merged to the branch mainline in revision 49.
  • Revision ID: james.westby@ubuntu.com-20100111225436-csnrc41k0bewth1e
Tags: upstream-2.47
ImportĀ upstreamĀ versionĀ 2.47

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
#
 
3
#    updates_available: calculate and cache the number of updates available
 
4
#    Copyright (C) 2008 Canonical Ltd.
 
5
#
 
6
#    Authors: Dustin Kirkland <kirkland@canonical.com>
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, version 3 of the License.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
PKG="byobu"
 
21
color 2>/dev/null || color() { true; }
 
22
 
 
23
if [ "$1" = "--detail" -o "$1" = "--short" ]; then
 
24
        if which apt-get >/dev/null; then
 
25
                detail=`apt-get -s -o Debug::NoLocking=true upgrade`
 
26
                if [ "$1" = "--detail" ]; then
 
27
                        printf "$detail"
 
28
                else
 
29
                        short=`printf "$detail" | grep ^Inst | wc -l`
 
30
                        printf "$short"
 
31
                fi
 
32
        fi
 
33
        exit 0
 
34
fi
 
35
 
 
36
print_updates() {
 
37
        u=$1
 
38
        s=$2
 
39
        if [ -n "$u" ]; then
 
40
                if [ "$u" -gt 0 ]; then
 
41
                        printf "$(color b r W)%d$(color -)$(color r W)!" "$u"
 
42
                        if [ -n "$s" ]; then
 
43
                                if [ "$s" -gt 0 ]; then
 
44
                                        printf "!"
 
45
                                fi
 
46
                        fi
 
47
                        printf "$(color -) "
 
48
                fi
 
49
        fi
 
50
}
 
51
 
 
52
update_cache() {
 
53
        mycache=$1
 
54
        # Now we actually have to do hard computational work to calculate updates.
 
55
        # Let's try to be "nice" about it:
 
56
        renice 10 $$ >/dev/null 2>&1 || true
 
57
        ionice -c3 -p $$ >/dev/null 2>&1 || true
 
58
        # These are very computationally intensive processes.
 
59
        # Background this work, have it write to the cache files,
 
60
        # and let the next cache check pick up the results.
 
61
        if [ -x /usr/lib/update-notifier/apt-check ]; then
 
62
                # If apt-check binary exists, use it
 
63
                /usr/lib/update-notifier/apt-check 2>&1 | tail -n 1 | sed "s/;/ /" > $mycache &
 
64
        elif which apt-get >/dev/null; then
 
65
                # If apt-get exists, use it
 
66
                apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst > $mycache &
 
67
        elif which zypper >/dev/null; then
 
68
                # If zypper exists, use it
 
69
                zypper --no-refresh lu --best-effort | grep 'v |' | wc -l > $mycache &
 
70
        elif which yum >/dev/null; then
 
71
                # If yum exists, use it
 
72
                # TODO: We need a better way of counting updates available from a RH expert
 
73
                yum list updates -q | egrep -v "Updated Packages" | wc -l > $mycache &
 
74
        fi
 
75
}
 
76
 
 
77
PKG="byobu"
 
78
 
 
79
# The following is somewhat Ubuntu and Debian specific (apt).
 
80
# I would welcome contributions from other distros to make this
 
81
# more distro-agnostic.
 
82
 
 
83
[ -d "/var/run/screen/S-$USER" ] && DIR="/var/run/screen/S-$USER" || DIR="$HOME/.byobu"
 
84
mycache="$DIR/$PKG.updates-available"
 
85
 
 
86
# If mycache is present, use it
 
87
[ -r $mycache ] && print_updates `grep "^[0-9]" $mycache | sed "s/ .*$//"`
 
88
 
 
89
# Update the cache if necessary
 
90
if [ ! -e "$mycache" ] || [ "/var/lib/apt" -nt "$mycache" ] || [ "/var/lib/apt/lists" -nt "$mycache" ]; then
 
91
        # If apt is newer than mycache, background an update now
 
92
        update_cache "$mycache"
 
93
fi