updates_available 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/sh -e
  2. #
  3. # updates_available: calculate and cache the number of updates available
  4. #
  5. # Copyright (C) 2008 Canonical Ltd.
  6. # Copyright (C) 2011-2014 Dustin Kirkland
  7. #
  8. # Authors: Dustin Kirkland <kirkland@byobu.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. ___print_updates() {
  22. local u= s=
  23. read u s < "$1"
  24. if [ -n "$u" ]; then
  25. if [ "$u" -gt 0 ]; then
  26. color b r W; printf "%d" "$u"; color -; color r W
  27. if [ -n "$s" ] && [ "$s" -gt 0 ]; then
  28. printf "$ICON_SECURITY"
  29. else
  30. printf "$ICON_UPDATES"
  31. fi
  32. color --
  33. elif [ "$u" = "0" ] && [ -e "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/updates_available" ]; then
  34. # Clear out byobu's status cache
  35. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/updates_available"*
  36. fi
  37. fi
  38. }
  39. ___update_cache() {
  40. local mycache=$1 flock="$1.lock"
  41. # Now we actually have to do hard computational work to calculate updates.
  42. # Let's try to be "nice" about it:
  43. renice 10 $$ >/dev/null 2>&1 || true
  44. ionice -c3 -p $$ >/dev/null 2>&1 || true
  45. # These are very computationally intensive processes.
  46. # Background this work, have it write to the cache files,
  47. # and let the next cache check pick up the results.
  48. # Ensure that no more than one of these run at a given time
  49. if [ -x /usr/lib/update-notifier/apt-check ]; then
  50. # If apt-check binary exists, use it
  51. flock -xn "$flock" sh -c "(/usr/lib/update-notifier/apt-check 2>&1 | awk '-F;' 'END { print \$1, \$2 }' >\"${mycache}-x\" 2>/dev/null ; mv \"${mycache}-x\" \"$mycache\")" &
  52. elif eval $BYOBU_TEST apt-get >/dev/null; then
  53. # If apt-get exists, use it
  54. flock -xn "$flock" apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst >$mycache 2>/dev/null &
  55. elif eval $BYOBU_TEST pkcon >/dev/null; then
  56. # use packagekit to show list of packages
  57. LC_ALL=C flock -xn "$flock" pkcon get-updates -p | awk '/^Results:$/ { start=NR }; /^Security/ { security++ }; END { if (!/There are no updates available at this time./) { print NR-start, security }}' > "$mycache" 2>/dev/null &
  58. elif eval $BYOBU_TEST zypper >/dev/null; then
  59. # If zypper exists, use it
  60. flock -xn "$flock" zypper --no-refresh lu --best-effort | grep -c 'v |' >$mycache 2>/dev/null &
  61. elif eval $BYOBU_TEST yum >/dev/null; then
  62. # If yum exists, use it
  63. # TODO: We need a better way of counting updates available from a RH expert
  64. flock -xn "$flock" yum list updates -q | grep -vc "Updated Packages" >$mycache 2>/dev/null &
  65. elif eval $BYOBU_TEST pacman >/dev/null; then
  66. # If pacman (Archlinux) exists, use it
  67. LC_ALL=C flock -xn "$flock" pacman -Sup | grep -vc "^\(::\| \)" >$mycache 2>/dev/null &
  68. elif eval $BYOBU_TEST opkg >/dev/null; then
  69. # If opkg (OpenWrt) exists, use it, also background if flock exists
  70. if eval $BYOBU_TEST flock >/dev/null; then
  71. flock -xn "$flock" opkg list-upgradable | wc -l >$mycache 2>/dev/null &
  72. else
  73. opkg list-upgradable | wc -l >$mycache &
  74. fi
  75. elif eval $BYOBU_TEST brew >/dev/null; then
  76. # If homebrew (Mac OSX) exists, use it, also background if flock exists
  77. if eval $BYOBU_TEST flock >/dev/null; then
  78. flock -xn "$flock" brew outdated | wc -l >$mycache 2>/dev/null &
  79. else
  80. brew outdated | wc -l >$mycache &
  81. fi
  82. fi
  83. }
  84. ___update_needed() {
  85. # Checks if we need to update the cache.
  86. # TODO: add more distro
  87. local mycache=$1
  88. # The cache doesn't exist: create it
  89. [ ! -e "$mycache" ] && return 0
  90. if eval $BYOBU_TEST apt-get >/dev/null; then
  91. # Debian/ubuntu
  92. d0=$(($(stat -c %Y $mycache 2>/dev/null)-5))
  93. d1=$(stat -c %Y /var/lib/apt)
  94. d2=$(stat -c %Y /var/lib/apt/lists)
  95. d3=$(stat -c %Y /var/log/dpkg.log)
  96. now=$(date +%s)
  97. delta=$(($now-$d0))
  98. if [ $d0 -lt 0 ] || [ $d0 -lt $d1 ] || [ $d0 -lt $d2 ] || [ $d0 -lt $d3 ] || [ 3605 -lt $delta ] ; then
  99. return 0
  100. else
  101. return 1
  102. fi
  103. elif [ -e "/var/lib/PackageKit/transactions.db" ]; then
  104. [ "/var/lib/PackageKit/transactions.db" -nt "$mycache" ]
  105. return $?
  106. elif eval $BYOBU_TEST pacman >/dev/null; then
  107. # Archlinux
  108. local db
  109. for db in /var/lib/pacman/sync/*.db; do
  110. [ "$db" -nt "$mycache" ] && return 0
  111. done
  112. return 1
  113. elif eval $BYOBU_TEST opkg >/dev/null; then
  114. # OpenWrt
  115. [ ! -e /var/lock/opkg.lock ] || return 1
  116. if [ -d /var/opkg-lists ]; then
  117. [ /var/opkg-lists -nt "$mycache" ]
  118. return $?
  119. else
  120. local u s
  121. read u s < "$mycache"
  122. [ "$u" -gt 0 ]
  123. return $?
  124. fi
  125. elif eval $BYOBU_TEST brew >/dev/null; then
  126. # Mac OSX
  127. # check if any new versions have been installed since
  128. # we last cached. this may not recognize formulae
  129. # installed with HEAD
  130. for f in $(brew --prefix)/Cellar/*; do
  131. [ "$f" -nt "$mycache" ] && return 0
  132. done
  133. # nothing new has been installed, so check wether the
  134. # formulae database was updated
  135. [ "$(brew --prefix)/Library/Formula" -nt "$mycache" ]
  136. return $?
  137. fi
  138. return 1
  139. }
  140. __updates_available_detail() {
  141. if eval $BYOBU_TEST apt-get >/dev/null; then
  142. local detail=`apt-get -s -o Debug::NoLocking=true upgrade`
  143. if [ "$1" = "--detail" ]; then
  144. printf "$detail"
  145. else
  146. local short=`printf "%s" "$detail" | grep -c ^Inst`
  147. printf "$short"
  148. fi
  149. fi
  150. }
  151. __updates_available() {
  152. local mycache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/updates-available"
  153. # If mycache is present, use it
  154. [ -r $mycache ] && ___print_updates "$mycache"
  155. # If we really need to do so (mycache doesn't exist, or the package database has changed),
  156. # background an update now
  157. ___update_needed "$mycache" && ___update_cache "$mycache"
  158. }
  159. # vi: syntax=sh ts=4 noexpandtab