/[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 9 - (show annotations)
Tue Dec 7 23:36:41 2004 UTC (19 years, 3 months ago) by dpavlin
File size: 5108 byte(s)
fixed multi-word search (so that "apache server" [with quotes] will work same for
apt-cache search and apt-iselect as opposed to just apache server [without
quotes])

1 #!/bin/sh
2
3 # apt-iselect: interactive apt-cache search tool
4 #
5 # Dobrica Pavlinusic <dpavlin@rot13.org>
6 # https://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 details
26 # 2003-11-13 fix for RedHat 9.0 mktemp bug suggested by Dobes Vandermeer
27 # 2004-07-27 allow multi-selection of packages to install
28 # 2004-12-04 add quick install options
29 # 2004-12-06 fixed multi-word search
30 #
31 # I know it's ugly, but it's still faster than aptitude :-)
32 #
33 # It will automatically use sudo if installed or require user to enter root
34 # password when needed. It will also install iselect deb package
35 # if it's not already there.
36 #
37 # WARNING: due to iselect limitation, maximum number of results is
38 # 1020 and maximum length of all results is 1048576 bytes
39
40 if [ ! -z "`which sudo`" ] ; then
41 use_sudo=1
42 else
43 use_sudo=0
44 fi
45
46 do_sudo() {
47 msg=$1 ; shift
48 if [ "$use_sudo" = 1 ] ; then
49 sudo -p "$msg, please enter your password: " $*
50 else
51 if [ "`id -u`" != 0 ] ; then
52 echo "$msg, please enter root password..."
53 fi
54 su -c "$*"
55 fi
56 }
57
58 if [ -z "`which iselect`" ] ; then
59 echo "You really need iselect for apt-iselect, installing..."
60 do_sudo "Installing iselect" apt-get install iselect
61 fi
62
63 if [ -z "$*" ] ; then
64 echo "Usage: $0 [search pattern for apt-cache search]"
65 exit 1
66 fi
67
68 res=`mktemp /tmp/tmp-res.XXXXXX` || ( echo "Can't create temp file!" ; exit 1 )
69 res2=`mktemp /tmp/tmp-res2.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
70 sel=`mktemp /tmp/tmp-sel.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
71 pkg=`mktemp /tmp/tmp-pkg.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 )
72
73 rmtemp() {
74 rm -f $res
75 rm -f $res2
76 test -f $sel && rm -f $sel
77 rm -f $pkg
78 }
79
80 # need to update apt cache?
81 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
82
83 trap 'rmtemp; exit 1' INT QUIT TERM SEGV
84
85 apt_cache_search() {
86 search_words="$*"
87
88 echo "Searching apt-cache for \"$search_words\"..."
89
90 apt-cache search "$@" | head -1020 > $res
91 nr=`wc -l $res | sed 's/^ *//' | cut -d" " -f1`
92 if [ $nr = 0 ] ; then
93 echo "No results for \"$search_words\"" > $res2
94 nr="no"
95 else
96 echo "$nr results for \"$search_words\", enter new search {s:search=%[Search for]S}" > $res2
97 echo >> $res2
98 cat $res | sed 's/^/{s}/' >> $res2
99 fi
100 if [ $nr -gt 0 ] ; then
101 echo >> $res2
102 echo "{s:_quick_install_}Install all selected" >> $res2
103 fi
104 echo >> $res2
105 echo "Enter new apt-cache search {s:search=%[Search for]S}" >> $res2
106 }
107
108 apt_cache_search "$@"
109
110 pkg_nr=3
111 loop=1
112
113 while [ $loop = 1 ] ; do
114
115 loop=0;
116 iselect -m -d '{,}' -P -p $pkg_nr -n "apt-iselect: $nr results for \"$search_words\"" < $res2 > $sel
117
118 if [ ! -s $sel ] ; then
119 exit 0;
120 elif tmp=`grep search= <$sel 2>/dev/null` ; then
121 apt_cache_search `echo $tmp | grep search= | cut -d= -f2`
122 loop=1
123 elif tmp=`grep -i ':_quick_install_' $sel` ; then
124 debs=`grep -v ':_quick_install_' $sel | cut -d: -f2 | cut -d" " -f1 | xargs echo`
125 do_sudo "Installing '$debs'" apt-get install $debs
126 else
127 # not search, find packages info
128
129 echo '{s}Back to search results' > $pkg
130 echo >> $pkg
131 hl_regex=`echo $search_words | sed -e 's/ /|/' -e 's,^,s#(,' -e 's,$,)#{b}\\\1{/b}#g,'`
132
133 cat "$sel" | while read tmp ; do
134
135 pkg_nr=`echo $tmp | cut -d: -f1`
136 pkg_full=`echo $tmp | cut -d: -f2`
137 pkg_name=`echo $pkg_full | cut -d" " -f1`
138 pkg_list="$pkg_list $pkg_name"
139
140 apt-cache show "$pkg_name" | cut -c-128 | sed 's/^\(Package: \)/{s}\1/' | sed -r "$hl_regex" | sed '1,3 s,{/*b},,g' >> $pkg
141
142 done
143 echo '{s}Back to search results' >> $pkg
144
145 tmp=`iselect -d '{,}' -n "Packages: $pkg_list" -m < $pkg`
146 if echo $tmp | grep -i back >/dev/null ; then
147 loop=1
148 elif echo $tmp | grep '^Package: ' >/dev/null ; then
149 deb=`echo $tmp | sed -e 's,{b/*},,g' -e 's,Package: *,,g'`
150 do_sudo "Installing '$deb'" apt-get install $deb
151 elif [ -z "$tmp" ] ; then
152 loop=1
153 else
154 echo -n "Can't parse output '$tmp' from iselect! Press enter to continue..."
155 read tmp
156 loop=1
157 fi
158 fi
159 done
160
161 rmtemp

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26