/[scripts]/trunk/apt-iselect
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/apt-iselect

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations)
Tue Jul 27 09:08:17 2004 UTC (18 years, 6 months ago) by dpavlin
File size: 4673 byte(s)
import current version to subversion

1 #!/bin/sh
2
3 # apt-iselect: interactive apt-cache search tool
4 #
5 # Dobrica Pavlinusic <dpavlin@rot13.org>
6 # http://www.rot13.org/~dpavlin/apt-iselect.html
7 #
8 # 2003-08-02 first version
9 # 2003-08-03 using mktemp to create temp files, auto-install iselect
10 # 2003-08-04 catch signals so temp files are not left behind, added
11 # enter new search at end of results, autoconfig
12 # 2003-08-06 changed delimiter to \s/ to prevent conflict with e-mails
13 # 2003-08-15 automatically run apt-get update if there is no package list
14 # (e.g. changing apt/sources.list without update), cut lines
15 # to 80 chars before sending to iselect (bug workaround)
16 # 2003-08-31 changed delimiters to {}, q on detail page brings back list of
17 # results, highlighted search word(s) in package details
18 # 2003-09-01 changed script to work with any POSIX complient shell, and not
19 # only bash (thanks to Ulrich Doehner for bug report and
20 # suggestions), added support to use su if sudo is not
21 # installed, fixed search with more than one word (thanks
22 # to Tobias Gruetzmacher who reported this bug)
23 # 2003-09-04 moved do_sudo before first call (fixes bug when trying to
24 # install iselect)
25 # 2003-10-09 allow multi-selection of packages to view and install
26 # 2003-11-13 fix for RedHat 9.0 mktemp bug suggested by Dobes Vandermeer
27 #
28 # I know it's ugly, but it's still faster than aptitude :-)
29 #
30 # It will automatically use sudo if installed or require user to enter root
31 # password when needed. It will also install iselect deb package
32 # if it's not already there.
33 #
34 # WARNING: due to iselect limitation, maximum number of results is
35 # 1020 and maximum length of all results is 1048576 bytes
36
37 if [ ! -z "`which sudo`" ] ; then
38 use_sudo=1
39 else
40 use_sudo=0
41 fi
42
43 do_sudo() {
44 msg=$1 ; shift
45 if [ "$use_sudo" = 1 ] ; then
46 sudo -p "$msg, please enter your password: " $*
47 else
48 if [ "`id -u`" != 0 ] ; then
49 echo "$msg, please enter root password..."
50 fi
51 su -c "$*"
52 fi
53 }
54
55 if [ -z "`which iselect`" ] ; then
56 echo "You really need iselect for apt-iselect, installing..."
57 do_sudo "Installing iselect" apt-get install iselect
58 fi
59
60 if [ -z "$*" ] ; then
61 echo "Usage: $0 [search pattern for apt-cache search]"
62 exit 1
63 fi
64
65 res=`mktemp /tmp/tmp-res.XXXXXX` || ( echo "Can't create temp file!" ; exit 1 )
66 res2=`mktemp /tmp/tmp-res2.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
67 sel=`mktemp /tmp/tmp-sel.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
68 pkg=`mktemp /tmp/tmp-pkg.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
69
70 rmtemp() {
71 rm -f $res
72 rm -f $res2
73 rm -f $pkg
74 }
75
76 # need to update apt cache?
77 LANG=C apt-cache stats 2>&1 | grep "^W: Couldn't stat source package list" >/dev/null && do_sudo "apt-get update needed" apt-get update
78
79 trap 'rmtemp; exit 1' INT QUIT TERM SEGV
80
81 apt_cache_search() {
82 search_words="$@"
83
84 echo "Searching apt-cache for \"$search_words\"..."
85
86 apt-cache search "$search_words" | head -1020 > $res
87 nr=`wc -l $res | sed 's/^ *//' | cut -d" " -f1`
88 if [ $nr = 0 ] ; then
89 echo "No results for \"$search_words\"" > $res2
90 nr="no"
91 else
92 echo "$nr results for \"$search_words\", enter new search {s:search=%[Search for]S}" > $res2
93 echo >> $res2
94 cat $res | sed 's/^/{s}/' >> $res2
95 fi
96 echo >> $res2
97 echo "Enter new apt-cache search {s:search=%[Search for]S}" >> $res2
98 }
99
100 apt_cache_search "$@"
101
102 pkg_nr=3
103 loop=1
104
105 while [ $loop = 1 ] ; do
106
107 loop=0;
108 iselect -m -d '{,}' -P -p $pkg_nr -n "apt-iselect: $nr results for \"$search_words\"" < $res2 > $sel
109
110 if [ ! -s $sel ] ; then
111 exit 0;
112 elif tmp=`grep search= <$sel 2>/dev/null` ; then
113 apt_cache_search `echo $tmp | grep search= | cut -d= -f2`
114 loop=1
115 else
116 # not search, find packages info
117
118 echo '{s}Back to search results' > $pkg
119 echo >> $pkg
120 hl_regex=`echo $search_words | sed -e 's/ /|/' -e 's,^,s#(,' -e 's,$,)#{b}\\\1{/b}#g,'`
121
122 cat "$sel" | while read tmp ; do
123
124 pkg_nr=`echo $tmp | cut -d: -f1`
125 pkg_full=`echo $tmp | cut -d: -f2`
126 pkg_name=`echo $pkg_full | cut -d" " -f1`
127 pkg_list="$pkg_list $pkg_name"
128
129 apt-cache show "$pkg_name" | cut -c-128 | sed 's/^\(Package: \)/{s}\1/' | sed -r "$hl_regex" | sed '1,3 s,{/*b},,g' >> $pkg
130
131 done
132 echo '{s}Back to search results' >> $pkg
133
134 tmp=`iselect -d '{,}' -n "Packages: $pkg_list" < $pkg`
135 if echo $tmp | grep -i back >/dev/null ; then
136 loop=1
137 elif echo $tmp | grep '^Package: ' >/dev/null ; then
138 deb=`echo $tmp | sed 's,{b/*},,g' | cut -d: -f2 | sed 's/^ *//'`
139 do_sudo "Installing '$deb'" apt-get install $deb
140 elif [ -z "$tmp" ] ; then
141 loop=1
142 else
143 echo -n "Can't parse output '$tmp' from iselect! Press enter to continue..."
144 read tmp
145 loop=1
146 fi
147 fi
148 done
149
150 rmtemp

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26