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!"
54 if atom_validate_strict and not portage.isvalidatom(argv[1]):
55 portage.writemsg("ERROR: Invalid atom: '%s'\n" % argv[1],
59 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
66 has_version.uses_root = True
69 def best_version(argv):
70 """<root> <category/package>
71 Returns category/package-version (without .ebuild).
74 print "ERROR: insufficient parameters!"
76 if atom_validate_strict and not portage.isvalidatom(argv[1]):
77 portage.writemsg("ERROR: Invalid atom: '%s'\n" % argv[1],
81 mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
82 print portage.best(mylist)
85 best_version.uses_root = True
88 def mass_best_version(argv):
89 """<root> [<category/package>]+
90 Returns category/package-version (without .ebuild).
93 print "ERROR: insufficient parameters!"
97 mylist=portage.db[argv[0]]["vartree"].dbapi.match(pack)
98 print pack+":"+portage.best(mylist)
101 mass_best_version.uses_root = True
104 """<root> <pkgtype> <category/package> [<key>]+
105 Returns metadata values for the specified package.
108 print >> sys.stderr, "ERROR: insufficient parameters!"
111 root, pkgtype, pkgspec = argv[0:3]
116 "installed":"vartree"}
117 if pkgtype not in type_map:
118 print >> sys.stderr, "Unrecognized package type: '%s'" % pkgtype
121 if os.path.realpath(root) == os.path.realpath(portage.settings["ROOT"]):
122 root = portage.settings["ROOT"] # contains the normalized $ROOT
124 values = trees[root][type_map[pkgtype]].dbapi.aux_get(
129 print >> sys.stderr, "Package not found: '%s'" % pkgspec
132 metadata.uses_root = True
135 """<root> <category/package>
136 List the files that are installed for a given package, with
137 one file listed on each line. All file names will begin with
141 print "ERROR: expected 2 parameters, got %d!" % len(argv)
145 vartree = portage.db[root]["vartree"]
146 if not vartree.dbapi.cpv_exists(cpv):
147 sys.stderr.write("Package not found: '%s'\n" % cpv)
149 cat, pkg = portage.catsplit(cpv)
150 db = portage.dblink(cat, pkg, root, vartree.settings,
151 treetype="vartree", vartree=vartree)
152 file_list = db.getcontents().keys()
155 sys.stdout.write("%s\n" % f)
157 contents.uses_root = True
160 """<root> [<filename>]+
161 Given a list of files, print the packages that own the files and which
162 files belong to each package. Files owned by a package are listed on
163 the lines below it, indented by a single tab character (\\t). All file
164 paths must start with <root>. Returns 1 if no owners could be found,
168 sys.stderr.write("ERROR: insufficient parameters!\n")
172 from portage import catsplit, dblink
173 settings = portage.settings
174 root = settings["ROOT"]
175 vardb = portage.db[root]["vartree"].dbapi
185 f = portage.normalize_path(f)
186 if not f.startswith(os.path.sep):
188 sys.stderr.write("ERROR: cwd does not exist!\n")
191 f = os.path.join(cwd, f)
192 f = portage.normalize_path(f)
193 if not f.startswith(root):
194 sys.stderr.write("ERROR: file paths must begin with <root>!\n")
197 files.append(f[len(root):])
199 owners = vardb._owners.get_owners(files)
201 for pkg, owned_files in owners.iteritems():
203 sys.stdout.write("%s\n" % cpv)
204 for f in sorted(owned_files):
205 sys.stdout.write("\t%s\n" % \
206 os.path.join(root, f.lstrip(os.path.sep)))
211 sys.stderr.write("None of the installed packages claim the file(s).\n")
215 owners.uses_root = True
217 def best_visible(argv):
218 """<root> [<category/package>]+
219 Returns category/package-version (without .ebuild).
222 print "ERROR: insufficient parameters!"
225 mylist=portage.db[argv[0]]["porttree"].dbapi.match(argv[1])
226 visible=portage.best(mylist)
234 best_visible.uses_root = True
237 def mass_best_visible(argv):
238 """<root> [<category/package>]+
239 Returns category/package-version (without .ebuild).
242 print "ERROR: insufficient parameters!"
245 for pack in argv[1:]:
246 mylist=portage.db[argv[0]]["porttree"].dbapi.match(pack)
247 print pack+":"+portage.best(mylist)
250 mass_best_visible.uses_root = True
253 def all_best_visible(argv):
255 Returns all best_visible packages (without .ebuild).
258 print "ERROR: insufficient parameters!"
260 #print portage.db[argv[0]]["porttree"].dbapi.cp_all()
261 for pkg in portage.db[argv[0]]["porttree"].dbapi.cp_all():
262 mybest=portage.best(portage.db[argv[0]]["porttree"].dbapi.match(pkg))
265 all_best_visible.uses_root = True
270 Returns a \\n separated list of category/package-version.
271 When given an empty string, all installed packages will
275 print "ERROR: expected 2 parameters, got %d!" % len(argv)
279 if atom_validate_strict and not portage.isvalidatom(atom):
280 portage.writemsg("ERROR: Invalid atom: '%s'\n" % atom,
283 results = portage.db[root]["vartree"].dbapi.match(atom)
285 results = portage.db[root]["vartree"].dbapi.cpv_all()
289 match.uses_root = True
294 Returns the path used for the var(installed) package database for the
295 set environment/configuration options.
298 out.write(os.path.join(portage.settings["ROOT"], portage.VDB_PATH) + "\n")
302 def gentoo_mirrors(argv):
304 Returns the mirrors set to use in the portage configuration.
306 print portage.settings["GENTOO_MIRRORS"]
311 Returns the PORTDIR path.
313 print portage.settings["PORTDIR"]
316 def config_protect(argv):
318 Returns the CONFIG_PROTECT paths.
320 print portage.settings["CONFIG_PROTECT"]
323 def config_protect_mask(argv):
325 Returns the CONFIG_PROTECT_MASK paths.
327 print portage.settings["CONFIG_PROTECT_MASK"]
330 def portdir_overlay(argv):
332 Returns the PORTDIR_OVERLAY path.
334 print portage.settings["PORTDIR_OVERLAY"]
339 Returns the PKGDIR path.
341 print portage.settings["PKGDIR"]
346 Returns the DISTDIR path.
348 print portage.settings["DISTDIR"]
353 Returns a specific environment variable as exists prior to ebuild.sh.
354 Similar to: emerge --verbose --info | egrep '^<variable>='
356 verbose = "-v" in argv
358 argv.pop(argv.index("-v"))
361 print "ERROR: insufficient parameters!"
366 print arg +"='"+ portage.settings[arg] +"'"
368 print portage.settings[arg]
372 Returns all repos with names (repo_name file) argv[0] = $ROOT
375 print "ERROR: insufficient parameters!"
377 print " ".join(portage.db[argv[0]]["porttree"].dbapi.getRepositories())
379 def get_repo_path(argv):
381 Returns the path to the repo named argv[1], argv[0] = $ROOT
384 print "ERROR: insufficient parameters!"
387 print portage.db[argv[0]]["porttree"].dbapi.getRepositoryPath(argv[1])
389 def list_preserved_libs(argv):
391 Print a list of libraries preserved during a package update in the form
392 package: path. Returns 0 if no preserved libraries could be found,
397 print "ERROR: wrong number of arguments"
399 mylibs = portage.db[argv[0]]["vartree"].dbapi.plib_registry.getPreservedLibs()
403 for path in mylibs[cpv]:
408 list_preserved_libs.uses_root = True
410 #-----------------------------------------------------------------------------
412 # DO NOT CHANGE CODE BEYOND THIS POINT - IT'S NOT NEEDED!
416 print ">>> Portage information query tool"
418 print ">>> Usage: portageq <command> [<option> ...]"
420 print "Available commands:"
423 # Show our commands -- we do this by scanning the functions in this
424 # file, and formatting each functions documentation.
426 commands = [x for x in globals() if x not in \
427 ("usage", "__doc__", "__name__", "main", "os", "portage", \
428 "sys", "__builtins__", "types", "string","exithandler")]
431 for name in commands:
433 obj = globals()[name]
434 if (type(obj) != types.FunctionType):
440 print " MISSING DOCUMENTATION!"
444 lines = doc.split("\n")
445 print " "+name+" "+lines[0].strip()
446 if (len(sys.argv) > 1):
447 if ("--help" not in sys.argv):
449 for line in lines[1:]:
450 print " "+line.strip()
451 if (len(sys.argv) == 1):
452 print "\nRun portageq with --help for info"
454 atom_validate_strict = "EBUILD_PHASE" in os.environ
457 if "-h" in sys.argv or "--help" in sys.argv:
460 elif len(sys.argv) < 2:
462 sys.exit(os.EX_USAGE)
465 function = globals().get(cmd)
468 sys.exit(os.EX_USAGE)
469 function = globals()[cmd]
470 uses_root = getattr(function, "uses_root", False) and len(sys.argv) > 2
472 if not os.path.isdir(sys.argv[2]):
473 sys.stderr.write("Not a directory: '%s'\n" % sys.argv[2])
474 sys.stderr.write("Run portageq with --help for info\n")
476 sys.exit(os.EX_USAGE)
477 os.environ["ROOT"] = sys.argv[2]
483 from os import path as osp
484 sys.path.insert(0, osp.join(osp.dirname(
485 osp.dirname(osp.realpath(__file__))), "pym"))
490 sys.argv[2] = portage.settings["ROOT"]
491 retval = function(sys.argv[2:])
494 except portage.exception.PermissionDenied, e:
495 sys.stderr.write("Permission denied: '%s'\n" % str(e))
497 except portage.exception.ParseError, e:
498 sys.stderr.write("%s\n" % str(e))
500 except ValueError, e:
502 not hasattr(e.args[0], "__len__") or \
505 # Multiple matches thrown from cpv_expand
507 # An error has occurred so we writemsg to stderr and exit nonzero.
508 portage.writemsg("You specified an unqualified atom that matched multiple packages:\n", noiselevel=-1)
510 portage.writemsg("* %s\n" % pkg, noiselevel=-1)
511 portage.writemsg("\nPlease use a more specific atom.\n", noiselevel=-1)
516 #-----------------------------------------------------------------------------