Magellan Linux

Annotation of /trunk/mage/usr/lib/mage/magequery.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (hide annotations) (download) (as text)
Tue Dec 20 12:50:13 2011 UTC (12 years, 4 months ago) by niro
File MIME type: application/x-sh
File size: 4344 byte(s)
- fix [ character on regex searches
1 niro 32 #!/bin/bash
2    
3 niro 1290 # query mage database for installed packages
4     # $Id$
5 niro 32
6     print_usage()
7     {
8     echo "$(basename $0 .sh) querys the mage database for installed packages."
9     echo
10     echo " -n NAME searches for name NAME"
11     echo " -v VER searches for version VER, needs -n"
12     echo " -b BUILD searches for build number BUILD, needs -n -v"
13 niro 777 echo " -c CAT shows all packages of given categorie"
14 niro 210 echo " -i shows an inventory of all installed packages"
15 niro 1503 echo " -f PATH searches packages which own given file"
16     echo " -e PATH searches the package which owns the exact given filename"
17 niro 32 echo
18     echo " Examples:"
19     echo
20     echo " $(basename $0 .sh) -n xorg -v 6.8.0 -b r1"
21     echo " searches for package xorg-6.8.0-r1"
22     echo
23     }
24    
25 niro 210 # default args:
26     GET_INVENTORY=false
27 niro 304 SEARCH_ONLY_PATH=false
28 niro 777 SEARCH_ONLY_CAT=false
29 niro 1503 SEARCH_EXACT_FILENAME=false
30 niro 210
31 niro 1290 # no params given, or is only an arg not ar param, exit
32     if [[ $# -lt 1 ]] || [[ ${1:0:1} != - ]]
33     then
34     print_usage
35     exit 1
36     fi
37    
38     # very basic getops
39     for i in $*
40 niro 1206 do
41 niro 1290 case $1 in
42     -n) shift; S_PNAME="$1" ;;
43     -v) shift; S_PVER="$1" ;;
44     -b) shift; S_PBUILD="$1" ;;
45     -i) GET_INVENTORY="true" ;;
46     -f) SEARCH_ONLY_PATH="true"; shift; SEARCH_PATH="$1" ;;
47 niro 1503 -e) SEARCH_ONLY_PATH="true"; SEARCH_EXACT_FILENAME="true"; shift; SEARCH_PATH="$1" ;;
48 niro 1290 -c) SEARCH_ONLY_CAT="true"; shift; SEARCH_CAT="$1" ;;
49     -h) print_usage; exit 0 ;;
50     -*) print_usage; exit 1 ;;
51 niro 32 esac
52 niro 1290 shift
53 niro 32 done
54    
55 niro 304 source /etc/mage.rc.global
56 niro 229 source /etc/mage.rc
57 niro 32 RETVAL=1
58    
59 niro 304 if [[ ${SEARCH_ONLY_PATH} = true ]]
60     then
61     unset S_PACKAGES
62     unset i pkg
63 niro 1503 if [[ ${SEARCH_EXACT_FILENAME} = true ]]
64     then
65 niro 1509 # fix ++, which gets interpreted as a regex
66     SEARCH_PATH="${SEARCH_PATH//+/\\+}"
67 niro 1540 # fix [, which gets interpreted as a regex
68     SEARCH_PATH="${SEARCH_PATH//[/\\[}"
69 niro 1507 S_CANDIDATES=$(egrep -rl "^${SEARCH_PATH}§" ${INSTALLDB})
70 niro 1503 else
71     S_CANDIDATES=$(fgrep -rl "${SEARCH_PATH}" ${INSTALLDB})
72     fi
73     for i in ${S_CANDIDATES}
74 niro 304 do
75 niro 1288 # ignore magefiles!
76     case ${i} in
77     *.mage) continue ;;
78     esac
79    
80 niro 332 # print categorie and pkgname
81     pkg="$(basename ${i%/*/*})/$(basename ${i%/*})"
82 niro 304 if [[ -z $(echo ${S_PACKAGES} | grep ${pkg}) ]]
83     then
84     S_PACKAGES="${S_PACKAGES} ${pkg}"
85     fi
86     done
87    
88     # show packages
89     for pkg in ${S_PACKAGES}
90     do
91     echo "${pkg}"
92     done
93    
94     exit 0
95     fi
96    
97 niro 777 if [[ ${SEARCH_ONLY_CAT} = true ]]
98     then
99     # no packages of SEARCH_CAT are installed
100     [[ ! -d ${MROOT}${INSTALLDB}/${SEARCH_CAT} ]] && exit 1
101    
102     for i in ${INSTALLDB}/${SEARCH_CAT}/*
103     do
104     # print categorie and pkgname
105     echo "$(basename ${i%/*})/$(basename ${i})"
106     done
107    
108     exit 0
109     fi
110    
111 niro 210 if [[ ${GET_INVENTORY} = true ]]
112     then
113 niro 228 for package in $(find ${MROOT}${INSTALLDB} -mindepth 2 -maxdepth 2 -type d -printf "%h,%f\n" | sort)
114 niro 210 do
115     pcat="$(basename $(echo ${package} | cut -d, -f1))"
116     pname="$(echo ${package} | cut -d, -f2)"
117 niro 1290 if [[ -z ${invlist} ]]
118 niro 210 then
119     invlist="${pcat}/${pname}"
120     else
121     invlist="${invlist}
122     ${pcat}/${pname}"
123     fi
124     done
125    
126     # now show the list
127     echo "${invlist}"
128     exit 0
129     fi
130    
131 niro 228 for i in ${MROOT}${INSTALLDB}/*/*
132 niro 32 do
133     INST_PNAME=no
134     INST_PVER=no
135     INST_PBUILD=no
136    
137     x=$(basename ${i})
138     PNAME=${x%-*-*}
139 niro 1290 PVER=$(echo ${x#${PNAME}-} | cut -d- -f1)
140     PBUILD=$(echo ${x#${PNAME}-} | cut -d- -f2)
141 niro 32
142     if [[ ${PNAME} == ${S_PNAME} ]]
143     then
144     INST_PNAME=yes
145    
146 niro 1290 if [[ -n ${S_PVER} ]]
147 niro 32 then
148     if [[ ${PVER} == ${S_PVER} ]]
149     then
150     INST_PVER=yes
151     fi
152    
153 niro 1290 if [[ -n ${S_PBUILD} ]]
154 niro 32 then
155     if [[ ${PBUILD} == ${S_PBUILD} ]]
156     then
157     INST_PBUILD=yes
158     fi
159     fi
160     fi
161    
162     # search for pname only
163 niro 1290 if [[ -n ${S_PNAME} ]] && [[ -z ${S_PVER} ]] && [[ -z ${S_PBUILD} ]]
164 niro 32 then
165 niro 1290 if [[ ${INST_PNAME} = yes ]]
166 niro 32 then
167     echo "${S_PNAME} is installed [ ${PNAME}-${PVER}-${PBUILD} ]"
168     RETVAL=0
169     break
170     fi
171     fi
172    
173     # search for pname and pver
174 niro 1290 if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -z ${S_PBUILD} ]]
175 niro 32 then
176 niro 1290 if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]]
177 niro 32 then
178     echo "${S_PNAME}-${S_PVER} is installed [ ${PNAME}-${PVER}-${PBUILD} ]"
179     RETVAL=0
180     break
181     fi
182     fi
183    
184     # search for pname, pver and pbuild
185 niro 1290 if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -n ${S_PBUILD} ]]
186 niro 32 then
187 niro 1290 if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] && [[ ${INST_PBUILD} = yes ]]
188 niro 32 then
189     echo "${S_PNAME}-${S_PVER}-${S_PBUILD} is installed [ ${PNAME}-${PVER}-${PBUILD} ]"
190     RETVAL=0
191     break
192     fi
193     fi
194     fi
195     done
196    
197     exit ${RETVAL}