pkgsearch list all packages that contain your string and display installed packages. pksearch -h for options
1 #!/bin/bash
2 #
3 # pkgsearch
4 #
5 # Copyright (c) 2006 by Benoit Chesneau <benoit@bchesneau.info>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
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, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 # USA.
21 #
22 # AUTHORS :
23 # - Maintainer : Benoit Chesneau <benoitc@archlinuxfr.org>
24 # - Contributor : slubman <slubman@archlinuxfr.org>
25 # CHANGELOG :
26 # - version 0.6
27 # cosmethics changes
28 # internal rewrite
29 # - version 0.5 (2007-02-28 15:19) :
30 # search on multiple words (slubman)
31 # search in description is case insensitive (slubman)
32 # - version 0.4 (2007-02-28 13:14) :
33 # add possibility to search in repository (slubman)
34 # add possibility to exclude repository in search (slubman)
35 # - version 0.3 (2007-02-28) :
36 # add possibility to display only installed packages (slubman)
37 # add possibility to display only not installed packages (benoitc)
38 # - version 0.1
39 # initial release
40 #
41
42 PACMAN_SOURCES="/var/lib/pacman"
43 PWD=`pwd`
44
45 cd $PACMAN_SOURCES
46
47 myver="0.6"
48
49
50 NAME_ONLY=0
51 USE_COLOR="y"
52 NOT_INSTALLED=0
53 ONLY_INSTALLED=0
54 IN="all packages"
55 SKIP_REPO=()
56 IN_REPO=()
57 search_string=()
58
59 installed() {
60 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
61 echo -e "\033[1;35m$1\033[1;0m \033[1;35m(*)\033[1;0m" >&2
62 else
63 echo "$1 (*)" >&2
64 fi
65 let nb=nb+1
66 }
67
68 not_installed() {
69 echo "$1" >&2
70 let nb=nb+1
71 }
72
73 msg() {
74 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
75 echo -e "\033[1;32m==>\033[1;0m \033[1;1m$1\033[1;0m" >&2
76 else
77 echo "==> $1" >&2
78 fi
79 }
80
81 error() {
82 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
83 echo -e "\033[1;31m==> ERROR:\033[1;0m \033[1;1m$1\033[1;0m" >&2
84 else
85 echo "==> ERROR: $1" >&2
86 fi
87 }
88
89 usage() {
90 echo "pkgsearch $myver"
91 echo "usage : $0 [-n] <searchstring>"
92 echo
93 echo "pkgsearch list all packages that contain your"
94 echo "string and display installed packages"
95 echo
96 echo "Options"
97 echo " -n, --name Search on pkgname only"
98 echo " -m, --nocolor Disable colorized output messages"
99 echo " -i, --installed Display only installed packages"
100 echo " -sr, --skip-repo Exclude repo in search"
101 echo " -ir, --ni-repo Search only in repo"
102 echo " -s, --skip Display only not installed packages"
103 echo " -h, --help Display usage"
104 }
105
106 if [ $# -lt 1 ]; then
107 usage
108 exit 1
109 fi
110
111 while [ "$#" -ne "0" ]; do
112 case $1 in
113 --installed|-i)
114 ONLY_INSTALLED=1
115 IN="only installed packages"
116 ;;
117 --help|-h)
118 usage
119 exit 0
120 ;;
121 --name|-n)
122 NAME_ONLY=1
123 ;;
124 --nocolor|-m)
125 USE_COLOR=n
126 ;;
127 --skip|-s)
128 NOT_INSTALLED=1
129 IN="only not installed packages"
130 ;;
131 --skip-repo|-sr)
132 SKIP_REPO[${#SKIP_REPO[*]}]=$2
133 shift
134 ;;
135 --in-repo|-ir)
136 IN_REPO[${#IN_REPO[*]}]=$2
137 shift
138 ;;
139 *)
140 search_string[${#search_string[*]}]=$1
141 ;;
142 esac
143 shift
144 done
145
146 if [ "$NOT_INSTALLED" = "1" -a "$ONLY_INSTALLED" = "1" ]; then
147 error "Error : Skip or show installed packages, but not both..."
148 exit 0
149 fi
150
151 nb=0
152
153 msg "Looking for \""${search_string[*]:-nothing}\""\n\tin ${IN}\n\tof ${IN_REPO[*]:-all repos}\n\texcluding ${SKIP_REPO[*]:-no repo}..."
154 search_token=${search_string[0]}
155 unset search_string[0]
156 for word in ${search_string[*]}; do
157 search_token="$search_token|$word"
158 done
159 search_token="($search_token)"
160
161 for repo in `find * -maxdepth 0 -type d`; do
162 if ! [ $repo = 'local' ]; then
163 if [ "$(echo ${SKIP_REPO[@]} | grep $repo)" ]; then
164 continue
165 fi
166 if [ "$(echo ${IN_REPO[@]} | grep -v $repo)" ]; then
167 continue
168 fi
169 cd $repo
170 for pkg in `/bin/ls`; do
171 if [ -f $pkg/desc ]; then
172 if [ "$NAME_ONLY" = "1" ]; then
173 if [ "$(echo $pkg | grep -E $search_token)" ]; then
174 NAME=`grep -A 1 NAME $pkg/desc | tail -1`
175 VERSION=`grep -A 1 VERSION $pkg/desc | tail -1`
176 if [ -d $PACMAN_SOURCES/local/$pkg ]; then
177 if [ "$NOT_INSTALLED" = "0" ]; then
178 installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
179 fi
180 else
181 if [ "$ONLY_INSTALLED" = "0" ]; then
182 not_installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
183 fi
184 fi
185 fi
186 else
187 if [ "$(grep -E -i $search_token $pkg/desc)" ]; then
188 NAME=`grep -A 1 NAME $pkg/desc | tail -1`
189 VERSION=`grep -A 1 VERSION $pkg/desc | tail -1`
190 if [ -d $PACMAN_SOURCES/local/$pkg ]; then
191 if [ "$NOT_INSTALLED" = "0" ]; then
192 installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
193 fi
194 else
195 if [ "$ONLY_INSTALLED" = "0" ]; then
196 not_installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
197 fi
198 fi
199 fi
200 fi
201 fi
202 done
203 cd ..
204 fi
205 done
206
207 echo
208 echo "$nb packages found.."
209
210 cd $PWD
This script use the EnvBash object from this snipett : http://www.friendsnippets.com/snippet/9/
1 #!/usr/bin/ruby
2
3 # Copyright (C) 2006 enki <enki@crocobox.org>
4
5 # This program is free software; you can redistribute it
6 # and/or modify it under the terms of the GNU General
7 # Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at
9 # your option) any later version.
10 #
11 # This program is distributed in the hope that it will be
12 # useful, but WITHOUT ANY WARRANTY; without even the
13 # implied warranty of MERCHANTABILITY or FITNESS FOR A
14 # PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with this program; if not, write
19 # to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
21
22
23 class MultiRegexp < Regexp
24 def matches(str)
25 str.scan(self) do
26 yield Regexp.last_match
27 end
28 end
29 end
30
31
32 class EnvBash
33 attr_reader :bash_env
34
35 def initialize(source)
36 bash_script ="\
37 source #{source}
38 set
39 "
40 @bash_env = `#{bash_script}`
41 end
42
43 def vars
44 bvars=Hash.new
45 re=MultiRegexp.new('(\w*)=(.*)$', true)
46 re.matches(@bash_env) { |i|
47 if (i[1] != "")
48 if i[2] =~ /\(([^\)]*)\)/
49 ta=Array.new
50 re_a=MultiRegexp.new('\[\d*\]=\"([^"]*)\"', true)
51 re_a.matches($1) { |j|
52 ta.push(j[1])
53 }
54 bvars[i[1]]=ta
55 elsif
56 bvars[i[1]]=i[2]
57 end
58 end
59 }
60 return bvars
61 end
62 end
63
64 class PKGBUILD
65 attr_reader :properties, :vars
66
67 def initialize(file)
68 @properties=Hash.new
69
70 properties['name']=""
71 properties['version']=""
72 properties['rel']=""
73 properties['desc']=""
74 properties['depends']=""
75 properties['url']=""
76 properties['source']=""
77
78
79 # get all vars
80 vpkgs=EnvBash.new(file)
81 vars=vpkgs.vars
82
83 properties['name'] = vars['pkgname']
84 properties['version'] = vars['pkgver']
85 properties['rel'] = vars['pkgrel']
86 properties['desc'] = vars['pkgdesc']
87 properties['depends'] = vars['depends']
88
89 url=""
90 if vars['url'].instance_of?(Array)
91 vars['url'].each { |u| url +="#{u} " }
92 else
93 url = vars['url']
94 end
95
96 properties['url'] = url
97
98 source=""
99 if vars['source'].instance_of?(Array)
100 vars['source'].each { |s| source +="#{s} " }
101 else
102 source = vars['source']
103 end
104
105
106 properties['source'] = source
107 end
108
109
110 def name
111 return properties['name']
112 end
113
114 end
115
116
117 pkg = PKGBUILD.new("/var/abs/kernels/kernel26/PKGBUILD")
118 pkg.properties.each do |k,v|
119 puts "#{k}=#{v}"
120 end
Pages : 1