header changes, $Header: -> $Id:
[portage.git] / bin / portageq
1 #!/usr/bin/python -O
2 # Copyright 1999-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Id: /var/cvsroot/gentoo-src/portage/bin/portageq,v 1.13.2.1 2005/04/12 12:23:41 jstubbs Exp $
5
6 import sys, os
7 os.environ["PORTAGE_CALLER"] = "portageq"
8 sys.path = ["/usr/lib/portage/pym"]+sys.path
9
10 import portage,types,string
11
12
13 #-----------------------------------------------------------------------------
14 #
15 # To add functionality to this tool, add a function below.
16 #
17 # The format for functions is:
18 #
19 #   def function(argv):
20 #       """<list of options for this function>
21 #       <description of the function>
22 #       """
23 #       <code>
24 #
25 # "argv" is an array of the command line parameters provided after the command.
26 #
27 # Make sure you document the function in the right format.  The documentation
28 # is used to display help on the function.
29 #
30 # You do not need to add the function to any lists, this tool is introspective,
31 # and will automaticly add a command by the same name as the function!
32 #
33
34
35 def has_version(argv):
36         """<root> <category/package>
37         Return code 0 if it's available, 1 otherwise.
38         """
39         if (len(argv) < 2):
40                 print "ERROR: insufficient parameters!"
41                 sys.exit(2)
42         try:
43                 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
44                 if mylist:
45                         sys.exit(0)
46                 else:
47                         sys.exit(1)
48         except KeyError:
49                 sys.exit(1)
50
51
52 def best_version(argv):
53         """<root> <category/package>
54         Returns category/package-version (without .ebuild).
55         """
56         if (len(argv) < 2):
57                 print "ERROR: insufficient parameters!"
58                 sys.exit(2)
59         try:
60                 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
61                 print portage.best(mylist)
62         except KeyError:
63                 sys.exit(1)
64
65
66 def mass_best_version(argv):
67         """<root> [<category/package>]+
68         Returns category/package-version (without .ebuild).
69         """
70         if (len(argv) < 2):
71                 print "ERROR: insufficient parameters!"
72                 sys.exit(2)
73         try:
74                 for pack in argv[1:]:
75                         mylist=portage.db[argv[0]]["vartree"].dbapi.match(pack)
76                         print pack+":"+portage.best(mylist)
77         except KeyError:
78                 sys.exit(1)
79
80
81 def best_visible(argv):
82         """<root> [<category/package>]+
83         Returns category/package-version (without .ebuild).
84         """
85         if (len(argv) < 2):
86                 print "ERROR: insufficient parameters!"
87                 sys.exit(2)
88         try:
89                 mylist=portage.db[argv[0]]["porttree"].dbapi.match(argv[1])
90                 print portage.best(mylist)
91         except KeyError:
92                 sys.exit(1)
93
94
95 def mass_best_visible(argv):
96         """<root> [<category/package>]+
97         Returns category/package-version (without .ebuild).
98         """
99         if (len(argv) < 2):
100                 print "ERROR: insufficient parameters!"
101                 sys.exit(2)
102         try:
103                 for pack in argv[1:]:
104                         mylist=portage.db[argv[0]]["porttree"].dbapi.match(pack)
105                         print pack+":"+portage.best(mylist)
106         except KeyError:
107                 sys.exit(1)
108
109
110 def all_best_visible(argv):
111         """<root>
112         Returns all best_visible packages (without .ebuild).
113         """
114         if (len(argv) < 1):
115                 print "ERROR: insufficient parameters!"
116         
117         #print portage.db[argv[0]]["porttree"].dbapi.cp_all()
118         for pkg in portage.db[argv[0]]["porttree"].dbapi.cp_all():
119                 mybest=portage.best(portage.db[argv[0]]["porttree"].dbapi.match(pkg))
120                 if mybest:
121                         print mybest
122
123 def match(argv):
124         """<root> <category/package>
125         Returns \n seperated list of category/package-version
126         """
127         if (len(argv) < 2):
128                 print "ERROR: insufficient parameters!"
129                 sys.exit(2)
130         try:
131                 print string.join(portage.db[argv[0]]["vartree"].dbapi.match(argv[1]),"\n")
132         except KeyError:
133                 sys.exit(1)
134
135
136 def vdb_path(argv):
137         """
138         Returns the path used for the var(installed) package database for the
139         set environment/configuration options.
140         """
141         print portage.root+portage.VDB_PATH
142
143 def gentoo_mirrors(argv):
144         """
145         Returns the mirrors set to use in the portage configuration.
146         """
147         print portage.settings["GENTOO_MIRRORS"]
148
149
150 def portdir(argv):
151         """
152         Returns the PORTDIR path as defined in the portage configuration.
153         """
154         print portage.settings["PORTDIR"]
155
156
157 def config_protect(argv):
158         """
159         Returns the CONFIG_PROTECT paths as defined in the portage configuration.
160         """
161         print portage.settings["CONFIG_PROTECT"]
162
163
164 def config_protect_mask(argv):
165         """
166         Returns the CONFIG_PROTECT_MASK paths as defined in the portage configuration.
167         """
168         print portage.settings["CONFIG_PROTECT_MASK"]
169
170
171 def portdir_overlay(argv):
172         """
173         Returns the PORTDIR_OVERLAY path as defined in the portage configuration.
174         """
175         print portage.settings["PORTDIR_OVERLAY"]
176
177
178 def pkgdir(argv):
179         """
180         Returns the PKGDIR path as defined in the portage configuration.
181         """
182         print portage.settings["PKGDIR"]
183
184
185 def distdir(argv):
186         """
187         Returns the DISTDIR path as defined in the portage configuration.
188         """
189         print portage.settings["DISTDIR"]
190
191
192 def envvar(argv):
193         """<variable>
194         Returns a specific environment variable as exists prior to ebuild.sh.
195         Similar to: emerge --verbose --info | egrep '^<variable>='
196         """
197         print portage.settings[argv[0]]
198
199
200 #-----------------------------------------------------------------------------
201 #
202 # DO NOT CHANGE CODE BEYOND THIS POINT - IT'S NOT NEEDED!
203 #
204
205 def usage():
206         rev="$Revision: 1.13.2.1 $"
207         ver=string.split(rev, ' ')[1]
208         print ">>> Portage information query tool -- version "+ver
209         print ">>> Usage: portageq <command> [<option> ...]"
210         print ""
211         print "Available commands:"
212
213         #
214         # Show our commands -- we do this by scanning the functions in this
215         # file, and formatting each functions documentation.
216         #
217         for name in globals().keys():
218                 # Drop python stuff, modules, and our own support functions.
219                 if (name in ("usage", "__doc__", "__name__", "main", "os", "portage", "sys", "__builtins__", "types", "string")):
220                         continue
221
222                 # Drop non-functions
223                 obj = globals()[name]
224                 if  (type(obj) != types.FunctionType):
225                         continue
226
227                 doc = obj.__doc__
228                 if (doc == None):
229                         print "   "+name
230                         print "      MISSING DOCUMENTATION!"
231                         print ""
232                         continue
233
234                 lines = string.split(doc, '\n')
235                 print "   "+name+" "+string.strip(lines[0])
236                 for line in lines[1:]:
237                         print "      "+string.strip(line)
238
239
240 def main():
241         if (len(sys.argv) < 2):
242                 usage()
243                 sys.exit()
244         
245         cmd = sys.argv[1]
246         try:
247                 function = globals()[cmd]
248                 function(sys.argv[2:])
249         except KeyError:
250                 usage()
251                 sys.exit()
252
253 main()
254
255
256 #-----------------------------------------------------------------------------