update copyright years
[portage.git] / bin / portageq
1 #!/usr/bin/python -O
2 # Copyright 1999-2006 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 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 has_version.uses_root = True
51
52
53 def best_version(argv):
54         """<root> <category/package>
55         Returns category/package-version (without .ebuild).
56         """
57         if (len(argv) < 2):
58                 print "ERROR: insufficient parameters!"
59                 sys.exit(2)
60         try:
61                 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
62                 print portage.best(mylist)
63         except KeyError:
64                 sys.exit(1)
65 best_version.uses_root = True
66
67
68 def mass_best_version(argv):
69         """<root> [<category/package>]+
70         Returns category/package-version (without .ebuild).
71         """
72         if (len(argv) < 2):
73                 print "ERROR: insufficient parameters!"
74                 sys.exit(2)
75         try:
76                 for pack in argv[1:]:
77                         mylist=portage.db[argv[0]]["vartree"].dbapi.match(pack)
78                         print pack+":"+portage.best(mylist)
79         except KeyError:
80                 sys.exit(1)
81 mass_best_version.uses_root = True
82
83
84 def best_visible(argv):
85         """<root> [<category/package>]+
86         Returns category/package-version (without .ebuild).
87         """
88         if (len(argv) < 2):
89                 print "ERROR: insufficient parameters!"
90                 sys.exit(2)
91         try:
92                 mylist=portage.db[argv[0]]["porttree"].dbapi.match(argv[1])
93                 print portage.best(mylist)
94         except KeyError:
95                 sys.exit(1)
96 best_visible.uses_root = True
97
98
99 def mass_best_visible(argv):
100         """<root> [<category/package>]+
101         Returns category/package-version (without .ebuild).
102         """
103         if (len(argv) < 2):
104                 print "ERROR: insufficient parameters!"
105                 sys.exit(2)
106         try:
107                 for pack in argv[1:]:
108                         mylist=portage.db[argv[0]]["porttree"].dbapi.match(pack)
109                         print pack+":"+portage.best(mylist)
110         except KeyError:
111                 sys.exit(1)
112 mass_best_visible.uses_root = True
113
114
115 def all_best_visible(argv):
116         """<root>
117         Returns all best_visible packages (without .ebuild).
118         """
119         if (len(argv) < 1):
120                 print "ERROR: insufficient parameters!"
121         
122         #print portage.db[argv[0]]["porttree"].dbapi.cp_all()
123         for pkg in portage.db[argv[0]]["porttree"].dbapi.cp_all():
124                 mybest=portage.best(portage.db[argv[0]]["porttree"].dbapi.match(pkg))
125                 if mybest:
126                         print mybest
127 all_best_visible.uses_root = True
128
129
130 def match(argv):
131         """<root> <category/package>
132         Returns \n seperated list of category/package-version
133         """
134         if (len(argv) < 2):
135                 print "ERROR: insufficient parameters!"
136                 sys.exit(2)
137         try:
138                 print string.join(portage.db[argv[0]]["vartree"].dbapi.match(argv[1]),"\n")
139         except KeyError:
140                 sys.exit(1)
141 match.uses_root = True
142
143
144 def vdb_path(argv):
145         """
146         Returns the path used for the var(installed) package database for the
147         set environment/configuration options.
148         """
149         print portage.root+portage.VDB_PATH
150
151
152 def gentoo_mirrors(argv):
153         """
154         Returns the mirrors set to use in the portage configuration.
155         """
156         print portage.settings["GENTOO_MIRRORS"]
157
158
159 def portdir(argv):
160         """
161         Returns the PORTDIR path.
162         """
163         print portage.settings["PORTDIR"]
164
165
166 def config_protect(argv):
167         """
168         Returns the CONFIG_PROTECT paths.
169         """
170         print portage.settings["CONFIG_PROTECT"]
171
172
173 def config_protect_mask(argv):
174         """
175         Returns the CONFIG_PROTECT_MASK paths.
176         """
177         print portage.settings["CONFIG_PROTECT_MASK"]
178
179
180 def portdir_overlay(argv):
181         """
182         Returns the PORTDIR_OVERLAY path.
183         """
184         print portage.settings["PORTDIR_OVERLAY"]
185
186
187 def pkgdir(argv):
188         """
189         Returns the PKGDIR path.
190         """
191         print portage.settings["PKGDIR"]
192
193
194 def distdir(argv):
195         """
196         Returns the DISTDIR path.
197         """
198         print portage.settings["DISTDIR"]
199
200
201 def envvar(argv):
202         """<variable>+
203         Returns a specific environment variable as exists prior to ebuild.sh.
204         Similar to: emerge --verbose --info | egrep '^<variable>='
205         """
206         if (argv[0] == "-v"):
207                 verbose=1
208                 argv=argv[1:]
209         else:
210                 verbose=0
211         for arg in argv:
212                 if verbose:
213                         print arg +"='"+ portage.settings[arg] +"'"
214                 else:
215                         print portage.settings[arg]
216
217
218 #-----------------------------------------------------------------------------
219 #
220 # DO NOT CHANGE CODE BEYOND THIS POINT - IT'S NOT NEEDED!
221 #
222
223 def usage(argv):
224         rev="$Revision: 1.13.2.1 $"
225         ver=string.split(rev, ' ')[1]
226         print ">>> Portage information query tool -- version "+ver
227         print ">>> Usage: portageq <command> [<option> ...]"
228         print ""
229         print "Available commands:"
230
231         #
232         # Show our commands -- we do this by scanning the functions in this
233         # file, and formatting each functions documentation.
234         #
235         for name in globals().keys():
236                 # Drop python stuff, modules, and our own support functions.
237                 if (name in ("usage", "__doc__", "__name__", "main", "os", "portage", "sys", "__builtins__", "types", "string")):
238                         continue
239
240                 # Drop non-functions
241                 obj = globals()[name]
242                 if  (type(obj) != types.FunctionType):
243                         continue
244
245                 doc = obj.__doc__
246                 if (doc == None):
247                         print "   "+name
248                         print "      MISSING DOCUMENTATION!"
249                         print ""
250                         continue
251
252                 lines = string.split(doc, '\n')
253                 print "   "+name+" "+string.strip(lines[0])
254                 if (len(sys.argv) > 1):
255                         if ("--help" not in sys.argv):
256                                 lines = lines[:-1]
257                         for line in lines[1:]:
258                                 print "      "+string.strip(line)
259         if (len(sys.argv) == 1):
260                 print "\nRun portageq with --help for info"
261
262 def main():
263         if (len(sys.argv) < 2) or ("-h" in sys.argv or "--help" in sys.argv):
264                 usage(sys.argv)
265                 sys.exit()
266
267         cmd = sys.argv[1]
268         try:
269                 function = globals()[cmd]
270                 uses_root = (getattr(function, "uses_root", False) and len(sys.argv) > 2)
271                 if uses_root:
272                         os.environ["ROOT"] = sys.argv[2]
273                 global portage
274                 import portage
275                 if uses_root:
276                         sys.argv[2] = portage.root
277                 function(sys.argv[2:])
278         except KeyError:
279                 usage(sys.argv)
280                 sys.exit()
281
282 main()
283
284 #-----------------------------------------------------------------------------