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> <category/package>
128 List the files that are installed for a given package, with
129 one file listed on each line. All file names will begin with
133 print "ERROR: expected 2 parameters, got %d!" % len(argv)
137 vartree = portage.db[root]["vartree"]
138 if not vartree.dbapi.cpv_exists(cpv):
139 sys.stderr.write("Package not found: '%s'\n" % cpv)
141 cat, pkg = portage.catsplit(cpv)
142 db = portage.dblink(cat, pkg, root, vartree.settings,
143 treetype="vartree", vartree=vartree)
144 file_list = db.getcontents().keys()
147 sys.stdout.write("%s\n" % f)
149 contents.uses_root = True
152 """<root> [<filename>]+
153 Given a list of files, print the packages that own the files and which
154 files belong to each package. Files owned by a package are listed on
155 the lines below it, indented by a single tab character (\\t). All file
156 paths must start with <root>. Returns 1 if no owners could be found,
160 sys.stderr.write("ERROR: insufficient parameters!\n")
164 from portage import catsplit, dblink
165 settings = portage.settings
166 root = settings["ROOT"]
167 vardb = portage.db[root]["vartree"].dbapi
177 f = portage.normalize_path(f)
178 if not f.startswith(os.path.sep):
180 sys.stderr.write("ERROR: cwd does not exist!\n")
183 f = os.path.join(cwd, f)
184 f = portage.normalize_path(f)
185 if not f.startswith(root):
186 sys.stderr.write("ERROR: file paths must begin with <root>!\n")
189 files.append(f[len(root):])
192 for cpv in vardb.cpv_all():
193 cat, pkg = catsplit(cpv)
194 mylink = dblink(cat, pkg, root, settings, vartree=vardb.vartree)
197 if mylink.isowner(f, root):
201 sys.stdout.write("%s\n" % cpv)
203 sys.stdout.write("\t%s\n" % \
204 os.path.join(root, f.lstrip(os.path.sep)))
207 sys.stderr.write("None of the installed packages claim the file(s).\n")
212 owners.uses_root = True
214 def best_visible(argv):
215 """<root> [<category/package>]+
216 Returns category/package-version (without .ebuild).
219 print "ERROR: insufficient parameters!"
222 mylist=portage.db[argv[0]]["porttree"].dbapi.match(argv[1])
223 visible=portage.best(mylist)
231 best_visible.uses_root = True
234 def mass_best_visible(argv):
235 """<root> [<category/package>]+
236 Returns category/package-version (without .ebuild).
239 print "ERROR: insufficient parameters!"
242 for pack in argv[1:]:
243 mylist=portage.db[argv[0]]["porttree"].dbapi.match(pack)
244 print pack+":"+portage.best(mylist)
247 mass_best_visible.uses_root = True
250 def all_best_visible(argv):
252 Returns all best_visible packages (without .ebuild).
255 print "ERROR: insufficient parameters!"
257 #print portage.db[argv[0]]["porttree"].dbapi.cp_all()
258 for pkg in portage.db[argv[0]]["porttree"].dbapi.cp_all():
259 mybest=portage.best(portage.db[argv[0]]["porttree"].dbapi.match(pkg))
262 all_best_visible.uses_root = True
267 Returns a \\n separated list of category/package-version.
268 When given an empty string, all installed packages will
272 print "ERROR: expected 2 parameters, got %d!" % len(argv)
276 results = portage.db[root]["vartree"].dbapi.match(atom)
278 results = portage.db[root]["vartree"].dbapi.cpv_all()
282 match.uses_root = True
287 Returns the path used for the var(installed) package database for the
288 set environment/configuration options.
290 print portage.root+portage.VDB_PATH
293 def gentoo_mirrors(argv):
295 Returns the mirrors set to use in the portage configuration.
297 print portage.settings["GENTOO_MIRRORS"]
302 Returns the PORTDIR path.
304 print portage.settings["PORTDIR"]
307 def config_protect(argv):
309 Returns the CONFIG_PROTECT paths.
311 print portage.settings["CONFIG_PROTECT"]
314 def config_protect_mask(argv):
316 Returns the CONFIG_PROTECT_MASK paths.
318 print portage.settings["CONFIG_PROTECT_MASK"]
321 def portdir_overlay(argv):
323 Returns the PORTDIR_OVERLAY path.
325 print portage.settings["PORTDIR_OVERLAY"]
330 Returns the PKGDIR path.
332 print portage.settings["PKGDIR"]
337 Returns the DISTDIR path.
339 print portage.settings["DISTDIR"]
344 Returns a specific environment variable as exists prior to ebuild.sh.
345 Similar to: emerge --verbose --info | egrep '^<variable>='
347 verbose = "-v" in argv
349 argv.pop(argv.index("-v"))
352 print "ERROR: insufficient parameters!"
357 print arg +"='"+ portage.settings[arg] +"'"
359 print portage.settings[arg]
363 Returns all repos with names (repo_name file) argv[0] = $ROOT
366 print "ERROR: insufficient parameters!"
368 print " ".join(portage.db[argv[0]]["porttree"].dbapi.getRepositories())
370 def get_repo_path(argv):
372 Returns the path to the repo named argv[1], argv[0] = $ROOT
375 print "ERROR: insufficient parameters!"
378 print portage.db[argv[0]]["porttree"].dbapi.getRepositoryPath(argv[1])
380 def list_preserved_libs(argv):
382 Print a list of libraries preserved during a package update in the form
383 package: path. Returns 0 if no preserved libraries could be found,
388 print "ERROR: wrong number of arguments"
390 mylibs = portage.db[argv[0]]["vartree"].dbapi.plib_registry.getPreservedLibs()
394 for path in mylibs[cpv]:
399 list_preserved_libs.uses_root = True
401 #-----------------------------------------------------------------------------
403 # DO NOT CHANGE CODE BEYOND THIS POINT - IT'S NOT NEEDED!
407 print ">>> Portage information query tool"
409 print ">>> Usage: portageq <command> [<option> ...]"
411 print "Available commands:"
414 # Show our commands -- we do this by scanning the functions in this
415 # file, and formatting each functions documentation.
417 commands = [x for x in globals() if x not in \
418 ("usage", "__doc__", "__name__", "main", "os", "portage", \
419 "sys", "__builtins__", "types", "string","exithandler")]
422 for name in commands:
424 obj = globals()[name]
425 if (type(obj) != types.FunctionType):
431 print " MISSING DOCUMENTATION!"
435 lines = doc.split("\n")
436 print " "+name+" "+lines[0].strip()
437 if (len(sys.argv) > 1):
438 if ("--help" not in sys.argv):
440 for line in lines[1:]:
441 print " "+line.strip()
442 if (len(sys.argv) == 1):
443 print "\nRun portageq with --help for info"
446 if "-h" in sys.argv or "--help" in sys.argv:
449 elif len(sys.argv) < 2:
451 sys.exit(os.EX_USAGE)
454 function = globals().get(cmd)
457 sys.exit(os.EX_USAGE)
458 function = globals()[cmd]
459 uses_root = getattr(function, "uses_root", False) and len(sys.argv) > 2
461 if not os.path.isdir(sys.argv[2]):
462 sys.stderr.write("Not a directory: '%s'\n" % sys.argv[2])
463 sys.stderr.write("Run portageq with --help for info\n")
465 sys.exit(os.EX_USAGE)
466 os.environ["ROOT"] = sys.argv[2]
470 # First import the main portage module without legacy globals since it
471 # is almost certain to succeed in that case. This provides access to
472 # the portage.exception namespace which is needed for later exception
473 # handling, like if portage.exception.PermissionDenied is raised when
474 # constructing the legacy global config instance.
475 os.environ["PORTAGE_LEGACY_GLOBALS"] = "false"
477 del os.environ["PORTAGE_LEGACY_GLOBALS"]
482 from os import path as osp
483 sys.path.insert(0, osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym"))
486 sys.argv[2] = portage.root
487 retval = function(sys.argv[2:])
490 except portage.exception.PermissionDenied, e:
491 sys.stderr.write("Permission denied: '%s'\n" % str(e))
493 except portage.exception.ParseError, e:
494 sys.stderr.write("%s\n" % str(e))
496 except ValueError, e:
498 not hasattr(e.args[0], "__len__") or \
501 # Multiple matches thrown from cpv_expand
503 # An error has occurred so we writemsg to stderr and exit nonzero.
504 portage.writemsg("The following packages available:\n", noiselevel=-1)
506 portage.writemsg("* %s\n" % pkg, noiselevel=-1)
507 portage.writemsg("\nPlease use a more specific atom.\n", noiselevel=-1)
512 #-----------------------------------------------------------------------------