2 # Copyright 1999-2006 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
7 # This block ensures that ^C interrupts are handled quietly.
11 def exithandler(signum, frame):
12 signal.signal(signal.SIGINT, signal.SIG_IGN)
13 signal.signal(signal.SIGTERM, signal.SIG_IGN)
16 signal.signal(signal.SIGINT, exithandler)
17 signal.signal(signal.SIGTERM, exithandler)
19 except KeyboardInterrupt:
26 #-----------------------------------------------------------------------------
28 # To add functionality to this tool, add a function below.
30 # The format for functions is:
33 # """<list of options for this function>
34 # <description of the function>
38 # "argv" is an array of the command line parameters provided after the command.
40 # Make sure you document the function in the right format. The documentation
41 # is used to display help on the function.
43 # You do not need to add the function to any lists, this tool is introspective,
44 # and will automaticly add a command by the same name as the function!
47 def has_version(argv):
48 """<root> <category/package>
49 Return code 0 if it's available, 1 otherwise.
52 print "ERROR: insufficient parameters!"
55 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
62 has_version.uses_root = True
65 def best_version(argv):
66 """<root> <category/package>
67 Returns category/package-version (without .ebuild).
70 print "ERROR: insufficient parameters!"
73 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
74 print portage.best(mylist)
77 best_version.uses_root = True
80 def mass_best_version(argv):
81 """<root> [<category/package>]+
82 Returns category/package-version (without .ebuild).
85 print "ERROR: insufficient parameters!"
89 mylist=portage.db[argv[0]]["vartree"].dbapi.match(pack)
90 print pack+":"+portage.best(mylist)
93 mass_best_version.uses_root = True
96 """<root> <pkgtype> <category/package> [<key>]+
97 Returns metadata values for the specified package.
100 print >> sys.stderr, "ERROR: insufficient parameters!"
103 root, pkgtype, pkgspec = argv[0:3]
108 "installed":"vartree"}
109 if pkgtype not in type_map:
110 print >> sys.stderr, "Unrecognized package type: '%s'" % pkgtype
113 if os.path.realpath(root) == os.path.realpath(portage.settings["ROOT"]):
114 root = portage.settings["ROOT"] # contains the normalized $ROOT
116 values = trees[root][type_map[pkgtype]].dbapi.aux_get(
121 print >> sys.stderr, "Package not found: '%s'" % pkgspec
124 metadata.uses_root = True
127 """<root> [<filename>]+
128 Given a list of files, print the packages that own the files and which
129 files belong to each package. Files owned by a package are listed on
130 the lines below it, indented by a single tab character (\\t). All file
131 paths must start with <root>. Returns 1 if no owners could be found,
135 sys.stderr.write("ERROR: insufficient parameters!\n")
139 from portage import catsplit, dblink
140 settings = portage.settings
141 root = settings["ROOT"]
142 vardb = portage.db[root]["vartree"].dbapi
152 f = portage.normalize_path(f)
153 if not f.startswith(os.path.sep):
155 sys.stderr.write("ERROR: cwd does not exist!\n")
158 f = os.path.join(cwd, f)
159 f = portage.normalize_path(f)
160 if not f.startswith(root):
161 sys.stderr.write("ERROR: file paths must begin with <root>!\n")
164 files.append(f[len(root):])
167 for cpv in vardb.cpv_all():
168 cat, pkg = catsplit(cpv)
169 mylink = dblink(cat, pkg, root, settings, vartree=vardb.vartree)
172 if mylink.isowner(f, root):
176 sys.stdout.write("%s\n" % cpv)
178 sys.stdout.write("\t%s\n" % \
179 os.path.join(root, f.lstrip(os.path.sep)))
182 sys.stderr.write("None of the installed packages claim the file(s).\n")
187 owners.uses_root = True
189 def best_visible(argv):
190 """<root> [<category/package>]+
191 Returns category/package-version (without .ebuild).
194 print "ERROR: insufficient parameters!"
197 mylist=portage.db[argv[0]]["porttree"].dbapi.match(argv[1])
198 visible=portage.best(mylist)
206 best_visible.uses_root = True
209 def mass_best_visible(argv):
210 """<root> [<category/package>]+
211 Returns category/package-version (without .ebuild).
214 print "ERROR: insufficient parameters!"
217 for pack in argv[1:]:
218 mylist=portage.db[argv[0]]["porttree"].dbapi.match(pack)
219 print pack+":"+portage.best(mylist)
222 mass_best_visible.uses_root = True
225 def all_best_visible(argv):
227 Returns all best_visible packages (without .ebuild).
230 print "ERROR: insufficient parameters!"
232 #print portage.db[argv[0]]["porttree"].dbapi.cp_all()
233 for pkg in portage.db[argv[0]]["porttree"].dbapi.cp_all():
234 mybest=portage.best(portage.db[argv[0]]["porttree"].dbapi.match(pkg))
237 all_best_visible.uses_root = True
241 """<root> <category/package>
242 Returns \n seperated list of category/package-version
245 print "ERROR: insufficient parameters!"
248 print "\n".join(portage.db[argv[0]]["vartree"].dbapi.match(argv[1]))
250 portage.writemsg("%s\n" % str(e), noiselevel=-1)
252 match.uses_root = True
257 Returns the path used for the var(installed) package database for the
258 set environment/configuration options.
260 print portage.root+portage.VDB_PATH
263 def gentoo_mirrors(argv):
265 Returns the mirrors set to use in the portage configuration.
267 print portage.settings["GENTOO_MIRRORS"]
272 Returns the PORTDIR path.
274 print portage.settings["PORTDIR"]
277 def config_protect(argv):
279 Returns the CONFIG_PROTECT paths.
281 print portage.settings["CONFIG_PROTECT"]
284 def config_protect_mask(argv):
286 Returns the CONFIG_PROTECT_MASK paths.
288 print portage.settings["CONFIG_PROTECT_MASK"]
291 def portdir_overlay(argv):
293 Returns the PORTDIR_OVERLAY path.
295 print portage.settings["PORTDIR_OVERLAY"]
300 Returns the PKGDIR path.
302 print portage.settings["PKGDIR"]
307 Returns the DISTDIR path.
309 print portage.settings["DISTDIR"]
314 Returns a specific environment variable as exists prior to ebuild.sh.
315 Similar to: emerge --verbose --info | egrep '^<variable>='
317 verbose = "-v" in argv
319 argv.pop(argv.index("-v"))
322 print "ERROR: insufficient parameters!"
327 print arg +"='"+ portage.settings[arg] +"'"
329 print portage.settings[arg]
333 Returns all repos with names (repo_name file) argv[0] = $ROOT
336 print "ERROR: insufficient parameters!"
338 print " ".join(portage.db[argv[0]]["porttree"].dbapi.getRepositories())
340 def get_repo_path(argv):
342 Returns the path to the repo named argv[1], argv[0] = $ROOT
345 print "ERROR: insufficient parameters!"
348 print portage.db[argv[0]]["porttree"].dbapi.getRepositoryPath(argv[1])
350 def list_preserved_libs(argv):
352 Print a list of libraries preserved during a package update in the form
353 package: path. Returns 0 if no preserved libraries could be found,
358 print "ERROR: wrong number of arguments"
360 mylibs = portage.db[argv[0]]["vartree"].dbapi.plib_registry.getPreservedLibs()
364 for path in mylibs[cpv]:
369 list_preserved_libs.uses_root = True
371 #-----------------------------------------------------------------------------
373 # DO NOT CHANGE CODE BEYOND THIS POINT - IT'S NOT NEEDED!
377 rev="$Revision: 1.13.2.1 $"
378 ver= rev.split(' ')[1]
379 print ">>> Portage information query tool -- version "+ver
380 print ">>> Usage: portageq <command> [<option> ...]"
382 print "Available commands:"
385 # Show our commands -- we do this by scanning the functions in this
386 # file, and formatting each functions documentation.
388 commands = [x for x in globals() if x not in \
389 ("usage", "__doc__", "__name__", "main", "os", "portage", \
390 "sys", "__builtins__", "types", "string","exithandler")]
393 for name in commands:
395 obj = globals()[name]
396 if (type(obj) != types.FunctionType):
402 print " MISSING DOCUMENTATION!"
406 lines = doc.split("\n")
407 print " "+name+" "+lines[0].strip()
408 if (len(sys.argv) > 1):
409 if ("--help" not in sys.argv):
411 for line in lines[1:]:
412 print " "+line.strip()
413 if (len(sys.argv) == 1):
414 print "\nRun portageq with --help for info"
417 if "-h" in sys.argv or "--help" in sys.argv:
420 elif len(sys.argv) < 2:
422 sys.exit(os.EX_USAGE)
426 function = globals()[cmd]
427 uses_root = (getattr(function, "uses_root", False) and len(sys.argv) > 2)
429 os.environ["ROOT"] = sys.argv[2]
434 from os import path as osp
435 sys.path.insert(0, osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym"))
438 sys.argv[2] = portage.root
439 retval = function(sys.argv[2:])
444 sys.exit(os.EX_USAGE)
445 except portage.exception.PermissionDenied, e:
446 sys.stderr.write("Permission denied: '%s'\n" % str(e))
448 except ValueError, e:
450 not hasattr(e.args[0], "__len__") or \
453 # Multiple matches thrown from cpv_expand
455 # An error has occurred so we writemsg to stderr and exit nonzero.
456 portage.writemsg("The following packages available:\n", noiselevel=-1)
458 portage.writemsg("* %s\n" % pkg, noiselevel=-1)
459 portage.writemsg("\nPlease use a more specific atom.\n", noiselevel=-1)
464 #-----------------------------------------------------------------------------