--- src/equery/equery.orig 2007-02-18 13:40:18.000000000 +0100 +++ src/equery/equery.orig 2007-02-18 13:41:46.000000000 +0100 @@ -19,6 +19,7 @@ import time import string import types +import bz2 # portage (output module) and gentoolkit need special path modifications sys.path.insert(0, "/usr/lib/portage/pym") @@ -1574,6 +1575,163 @@ " " + pp.localoption("-p, --portage-tree") + " - also search in portage tree (" + gentoolkit.settings["PORTDIR"] + ")\n" + \ " " + pp.localoption("-o, --overlay-tree") + " - also search in overlay tree (" + gentoolkit.settings["PORTDIR_OVERLAY"] + ")\n" +class CmdShowGccInfo(Command): + """Show informations about compiler and flags used to compile a package.""" + + __installedGccSlots = None + + def __init__(self): + self.default_opts = { + "allInstalledPackages": 0, + } + self.__installedGccSlots = [] + expr = re.compile("^gcc-[0-9]") + for n in os.listdir(os.path.join(portage.root, portage.VDB_PATH, "sys-devel")): + if expr.match(n): + self.__installedGccSlots.append(n) + self.__installedGccSlots.sort() + for n in range(len(self.__installedGccSlots)): + self.__installedGccSlots[n] = open(os.path.join(portage.root, portage.VDB_PATH, "sys-devel", self.__installedGccSlots[n], "SLOT"), "r").read() + if self.__installedGccSlots[n][-1] == "\n": + self.__installedGccSlots[n] = self.__installedGccSlots[n][:-1] + self.__installedGccSlots.sort() + + def shortHelp(self): + return pp.localoption("") + " - display the gcc version used to compile a package" + + def longHelp(self): + return "Display the gcc version used to compile a package\n" + \ + "\n" + \ + "Syntax:\n" + \ + " " + pp.command("gcc") + pp.localoption(" ") + "\n" + \ + " " + pp.command("gcc") + pp.pkgquery(" pkgspec") + "\n" + \ + pp.localoption("") + " is either of: \n" + \ + " " + pp.localoption("-A, --all") + " - scan all installed packages\n" + + def parseArgs(self, args): + + query = "" + need_help = 0 + opts = self.default_opts + skip = 0 + + if len(args) == 0: + need_help = 1 + + for i in xrange(len(args)): + + if skip: + skip -= 1 + continue + x = args[i] + + if x in ["-h","--help"]: + need_help = 1 + break + elif x in ["-A", "--all"]: + opts["allInstalledPackages"] = 1 + else: + query = x + + if need_help: + print_info(0, self.longHelp()) + sys.exit(-1) + + return (query, opts) + + def perform(self, args): + (query, opts) = self.parseArgs(args) + if opts["allInstalledPackages"]: + # search for information about ALL installed packages + print_info(0, "[ Searching for gcc used for " + pp.pkgquery("all packages") + "... ]") + result = self.__scanVDB() + print_info(0, " Last installed gcc slot: " + pp.number(self.__installedGccSlots[-1]) ) + print_info(0, " Packages installed: " + pp.number( str(result["NUM_COMPILED_WITH_LATEST_VERSION"] + len(result["COMPILED_WITH_OLD_VERSION"])) ) ) + print_info(0, " Packages compiled with a gcc of an old slot: " + portage.output.yellow( str(len( result["COMPILED_WITH_OLD_VERSION"])) ) ) + if len(result["COMPILED_WITH_OLD_VERSION"]) != 0: + print_info(0, string.rjust("[SLOT]", 13) + string.rjust("[VERSION]", 13)) + for p in result["COMPILED_WITH_OLD_VERSION"]: + print_info(0,string.rjust("[" + pp.number(p[p.keys()[0]]["SLOT"]) +"]", 32) + string.rjust("[" + pp.number(p[p.keys()[0]]["VERSION"]) + "]", 32) + ' ' + pp.cpv(p.keys()[0])) + else: + # search for the given package and print the result + print_info(0, "[ Searching for packages matching " + pp.pkgquery(query + " ") + "... ]") + gccVersion = self.__getGccVersion(query) + for pkg in gccVersion: + print_info(0, "") + print_info(0, pp.section("*") + " gcc version used for " + pp.cpv(pkg)) + sortedKeys = gccVersion[pkg].keys()[:] + sortedKeys.sort() + for k in sortedKeys: + print_info(0, string.rjust(k + " : ", 16) + pp.number(gccVersion[pkg][k])) + + # search for gcc compiler version and C/C++ flags used to compile the package given as parameter + def __getGccVersion(self, pkg): + pkg_split = gentoolkit.split_package_name(pkg) + if pkg_split[2] != "": # if a specific version is given + if pkg_split[0] != "": # if the category of the package is given + if pkg_split[0][0] != "=": # if the category is not preceded by an "=" + found = gentoolkit.find_installed_packages("=" + pkg) # add the "=" + else: + found = gentoolkit.find_installed_packages(pkg) + else: # the category is not given, so we check for the "=" on the package name + if pkg_split[1][0] != "=": # if the name is not preceded by an "=" + found = gentoolkit.find_installed_packages("=" + pkg) # add the "=" + else: + found = gentoolkit.find_installed_packages(pkg) + else: # search for all version for the given package + found = gentoolkit.find_installed_packages(pkg) + # exit if no package has been found + if len(found) == 0: + print_error("Package not found") + sys.exit(1) + else: + retval = {} + # search for compiler infos about every selected package + for p in gentoolkit.sort_package_list(found): + basePath = os.path.join( portage.root, portage.VDB_PATH, p.get_category(), p.get_name() + '-' + p.get_version() ) + # set the initial retval in case we can't find the compiler version used (probably this never happens...) + retval[p.get_cpv()] = { "VERSION" : portage.output.red("!!! ") + "unknown", "SLOT" : portage.output.red("!!! ") + "unknown" } + # match the line containing the PATH variable declaration + matchedString = re.search( "^PATH=\"?.*?\"?\n", bz2.BZ2File( os.path.join( basePath, "environment.bz2" )).read(), re.M ).group()[:] + # remove the \n character, if found + if matchedString[-1] == "\n": + matchedString = matchedString[:-1] + # match the compiler version in the string matched before + for x in matchedString.split(":"): + tmp = re.search("gcc-bin", x) + if tmp != None: + matchedString = tmp + # save the version + retval[p.get_cpv()]["VERSION"] = matchedString.string.split("gcc-bin")[1][1:] + tmp = retval[p.get_cpv()]["VERSION"].split(".") + # save the slot + retval[p.get_cpv()]["SLOT"] = tmp[0] + "." + tmp[1] + # save compiler flags, if stored in vdb + for f in [ "CHOST" , "CC", "CFLAGS", "CXX", "CXXFLAGS" ]: + if os.access( os.path.join( basePath, f ) , os.R_OK): + retval[p.get_cpv()][f] = open(os.path.join( basePath, f )).read() + if retval[p.get_cpv()][f][-1] == "\n": + retval[p.get_cpv()][f] = retval[p.get_cpv()][f][:-1] + return retval + + # scan vdb searching for gcc version about every installed package + def __scanVDB(self): + numCompiledWithLastVersion = 0 + compiledWithAnOldVersion = [] + categoriesList = os.listdir( os.path.join( portage.root, portage.VDB_PATH ) ) + categoriesList.sort() + for category in categoriesList: + packagesList = os.listdir( os.path.join( portage.root, portage.VDB_PATH, category ) ) + packagesList.sort() + for package in packagesList: + pkgGccVersion = self.__getGccVersion(os.path.join( category, package )) + if pkgGccVersion[os.path.join( category, package )]["SLOT"] == self.__installedGccSlots[-1]: + numCompiledWithLastVersion += 1 + else: + compiledWithAnOldVersion.append(pkgGccVersion) + return { "NUM_COMPILED_WITH_LATEST_VERSION" : numCompiledWithLastVersion, "COMPILED_WITH_OLD_VERSION" : compiledWithAnOldVersion } + + # # Command line tokens to commands mapping # @@ -1591,7 +1749,8 @@ "check" : CmdCheckIntegrity(), "stats" : CmdDisplayStatistics(), "glsa" : CmdListGLSAs(), - "which": CmdWhich() + "which": CmdWhich(), + "gcc" : CmdShowGccInfo() } # Short command line tokens @@ -1609,7 +1768,8 @@ "s" : "size", "t" : "stats", "u" : "uses", - "w" : "which" + "w" : "which", + "v" : "gcc" } from gentoolkit import Config