<commit_before>#
# Copyright (c) 2004 Specifix, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any waranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
"""
Provides the output for the "conary query" command
"""

import files
import os
from lib import util
from lib import log

from lib.sha1helper import sha1ToString, md5ToString
from repository import repository

_troveFormat  = "%-39s %s"
_troveFormatWithFlavor  = "%-39s %s%s"
_fileFormat = "    %-35s %s"
_grpFormat  = "  %-37s %s"

class DisplayCache:

    def __init__(self):
        self._cache = {}

    def cache(self, troveName, versionList, fullVersions=False):
        """Add entries to the DisplayCache
        
        Cache the correct display output, given a version list for a
        trove.  The assumption is that the shortest version string
        should be given, starting with the verison-release, followed
        by the label/version-release, and finally falling back and
        displaying the entire version
        """
        _cache = self._cache
        short = {}
        passed = True
        # find the version strings to display for this trove; first
        # choice is just the version/release pair, if there are
        # conflicts try label/verrel, and if that doesn't work
        # conflicts display everything
        if troveName not in _cache:

            _cache[troveName] = {}
        if fullVersions:
            for version in versionList:
                _cache[troveName][version] = version.asString()
            return
        passed = True
        for version in versionList:
            v = version.trailingVersion().asString()
            _cache[troveName][version] = v
            if short.has_key(v):
                # two versions have the same version/release
                passed = False
                break
            short[v] = True

        if not passed:
            short = {}
            passed = True
            for version in versionList:
                if version.hasParent():
                    v = version.branch().label().asString() + '/' + \
                        version.trailingVersion().asString()
                else:
                    v = version.asString()

                _cache[troveName][version] = v
                if short.has_key(v):
                    # two versions have the same label
                    passed = False 
                    break
                short[v] = True

        if not passed:
            for version in versionList:
                _cache[troveName][version] = version.asString()

    def clearCache(self, name):
        """Cleans out the cache entry for a trove for garbage collection"""
        del self._cache[name]

    def cacheAll(self, troveDict, fullVersions=False):
        """Add entries to the DisplayCache from a dictionary
        
        Prepares the version cache to choose the correct 
        version to display. TroveDict should be a trove[name] => [versions]
        dict. 
        XXX eventually this should be a [name] => [(version,flavor)]
        So that we can print the short version if they differ in flavors
        """
        for troveName in troveDict.keys():
            self.cache(troveName, troveDict[troveName], fullVersions)
        
    def __getitem__(self, (troveName, version)):
        return self._cache[troveName][version]

    def __call__(self, troveName, version):
        return self._cache[troveName][version]

def printFile(fileObj, path, prefix='', verbose=True, tags=False, sha1s=False,
              pathId=None, pathIds=False):
    taglist = ''
    sha1 = ''
    id = ''

    if verbose and isinstance(fileObj, files.SymbolicLink):
        name = "%s -> %s" % (path, fileObj.target.value())
    else:
        name = path
    if tags and fileObj.tags:
        taglist = ' [' + ' '.join(fileObj.tags) + ']' 
    if sha1s:
        if hasattr(fileObj, 'contents') and fileObj.contents:
            sha1 = sha1ToString(fileObj.contents.sha1()) + ' '
        else:
            sha1 = ' '*41

    if pathIds and pathId:
        id = md5ToString(pathId) + ' '
    if verbose: 
        print "%s%s%s%s    1 %-8s %-8s %s %s %s%s" % \
          (prefix, id, sha1, fileObj.modeString(), fileObj.inode.owner(), fileObj.inode.group(), 
            fileObj.sizeString(), fileObj.timeString(), name, taglist)
    else:
        print "%s%s%s%s" % (id, sha1,path, taglist)

def parseTroveStrings(troveNameList):
    troveNames = []
    hasVersions = False
    for item in troveNameList:
        if item.find("=") != -1:
            l = item.split("=")
            if len(l) > 2:
                log.error("versions may not contain =")
                return
            troveNames.append(tuple(l))
            hasVersions = True
        else:
            troveNames.append((item, None))
    return troveNames, hasVersions


def _formatFlavor(flavor):
    return '\n   ' + '\n   '.join(str(flavor).split('\n'))


def _printOneTrove(db, troveName, troveVersions, fullVersions):
    displayC = DisplayCache()
    if troveVersions is None:
        troveVersions = db.getTroveVersionList(troveName)
    if not troveVersions:
        log.error("%s is not installed", troveName)
        return
    versionDict = {}.fromkeys(troveVersions)
    troveDict = db.getTroveVersionFlavors({troveName: troveVersions})
    displayC.cache(troveName, troveVersions, fullVersions)

    for version in troveVersions:
        if len(troveDict[troveName][version]) > 1:
            # if there is more than one instance of a version
            # installed, show the flavor information
            for flavor in troveDict[troveName][version]:
                print _troveFormatWithFlavor %(troveName,
                                               displayC[troveName, version],
                                               _formatFlavor(flavor))
        else:
            print _troveFormat %(troveName, displayC[troveName, version])

def displayTroves(db, troveNameList = [], pathList = [], ls = False, 
                  ids = False, sha1s = False, fullVersions = False, 
                  tags = False):
    (troveNames, hasVersions) = parseTroveStrings(troveNameList)
    pathList = [os.path.abspath(util.normpath(x)) for x in pathList]
    
    if not troveNames and not pathList:
	troveNames = [ (x, None) for x in db.iterAllTroveNames() ]
	troveNames.sort()

    if not hasVersions and not ls and not ids and not sha1s and not tags:
        for path in pathList:
            for trove in db.iterTrovesByPath(path):
                troveNames.append((trove.getName(), [ trove.getVersion() ]))

        for troveName, versionList in troveNames:
            _printOneTrove(db, troveName, versionList, fullVersions)
        return

    for path in pathList:
        for trove in db.iterTrovesByPath(path):
	    _displayTroveInfo(db, trove, ls, ids, sha1s, fullVersions)

    for (troveName, versionStr) in troveNames:
        try:
            for trove in db.findTrove(troveName, versionStr):
                _displayTroveInfo(db, trove, ls, ids, sha1s, fullVersions, tags)
        except repository.PackageNotFound:
            if versionStr:
                log.error("version %s of trove %s is not installed",
                          versionStr, troveName)
            else:
                log.error("trove %s is not installed", troveName)
        
def _displayTroveInfo(db, trove, ls, ids, sha1s, fullVersions, tags):

    version = trove.getVersion()<commit_msg>call _displayTroveInfo with the correct number of arguments<commit_after>#
# Copyright (c) 2004 Specifix, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any waranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
"""
Provides the output for the "conary query" command
"""

import files
import os
from lib import util
from lib import log

from lib.sha1helper import sha1ToString, md5ToString
from repository import repository

_troveFormat  = "%-39s %s"
_troveFormatWithFlavor  = "%-39s %s%s"
_fileFormat = "    %-35s %s"
_grpFormat  = "  %-37s %s"

class DisplayCache:

    def __init__(self):
        self._cache = {}

    def cache(self, troveName, versionList, fullVersions=False):
        """Add entries to the DisplayCache
        
        Cache the correct display output, given a version list for a
        trove.  The assumption is that the shortest version string
        should be given, starting with the verison-release, followed
        by the label/version-release, and finally falling back and
        displaying the entire version
        """
        _cache = self._cache
        short = {}
        passed = True
        # find the version strings to display for this trove; first
        # choice is just the version/release pair, if there are
        # conflicts try label/verrel, and if that doesn't work
        # conflicts display everything
        if troveName not in _cache:

            _cache[troveName] = {}
        if fullVersions:
            for version in versionList:
                _cache[troveName][version] = version.asString()
            return
        passed = True
        for version in versionList:
            v = version.trailingVersion().asString()
            _cache[troveName][version] = v
            if short.has_key(v):
                # two versions have the same version/release
                passed = False
                break
            short[v] = True

        if not passed:
            short = {}
            passed = True
            for version in versionList:
                if version.hasParent():
                    v = version.branch().label().asString() + '/' + \
                        version.trailingVersion().asString()
                else:
                    v = version.asString()

                _cache[troveName][version] = v
                if short.has_key(v):
                    # two versions have the same label
                    passed = False 
                    break
                short[v] = True

        if not passed:
            for version in versionList:
                _cache[troveName][version] = version.asString()

    def clearCache(self, name):
        """Cleans out the cache entry for a trove for garbage collection"""
        del self._cache[name]

    def cacheAll(self, troveDict, fullVersions=False):
        """Add entries to the DisplayCache from a dictionary
        
        Prepares the version cache to choose the correct 
        version to display. TroveDict should be a trove[name] => [versions]
        dict. 
        XXX eventually this should be a [name] => [(version,flavor)]
        So that we can print the short version if they differ in flavors
        """
        for troveName in troveDict.keys():
            self.cache(troveName, troveDict[troveName], fullVersions)
        
    def __getitem__(self, (troveName, version)):
        return self._cache[troveName][version]

    def __call__(self, troveName, version):
        return self._cache[troveName][version]

def printFile(fileObj, path, prefix='', verbose=True, tags=False, sha1s=False,
              pathId=None, pathIds=False):
    taglist = ''
    sha1 = ''
    id = ''

    if verbose and isinstance(fileObj, files.SymbolicLink):
        name = "%s -> %s" % (path, fileObj.target.value())
    else:
        name = path
    if tags and fileObj.tags:
        taglist = ' [' + ' '.join(fileObj.tags) + ']' 
    if sha1s:
        if hasattr(fileObj, 'contents') and fileObj.contents:
            sha1 = sha1ToString(fileObj.contents.sha1()) + ' '
        else:
            sha1 = ' '*41

    if pathIds and pathId:
        id = md5ToString(pathId) + ' '
    if verbose: 
        print "%s%s%s%s    1 %-8s %-8s %s %s %s%s" % \
          (prefix, id, sha1, fileObj.modeString(), fileObj.inode.owner(), fileObj.inode.group(), 
            fileObj.sizeString(), fileObj.timeString(), name, taglist)
    else:
        print "%s%s%s%s" % (id, sha1,path, taglist)

def parseTroveStrings(troveNameList):
    troveNames = []
    hasVersions = False
    for item in troveNameList:
        if item.find("=") != -1:
            l = item.split("=")
            if len(l) > 2:
                log.error("versions may not contain =")
                return
            troveNames.append(tuple(l))
            hasVersions = True
        else:
            troveNames.append((item, None))
    return troveNames, hasVersions


def _formatFlavor(flavor):
    return '\n   ' + '\n   '.join(str(flavor).split('\n'))


def _printOneTrove(db, troveName, troveVersions, fullVersions):
    displayC = DisplayCache()
    if troveVersions is None:
        troveVersions = db.getTroveVersionList(troveName)
    if not troveVersions:
        log.error("%s is not installed", troveName)
        return
    versionDict = {}.fromkeys(troveVersions)
    troveDict = db.getTroveVersionFlavors({troveName: troveVersions})
    displayC.cache(troveName, troveVersions, fullVersions)

    for version in troveVersions:
        if len(troveDict[troveName][version]) > 1:
            # if there is more than one instance of a version
            # installed, show the flavor information
            for flavor in troveDict[troveName][version]:
                print _troveFormatWithFlavor %(troveName,
                                               displayC[troveName, version],
                                               _formatFlavor(flavor))
        else:
            print _troveFormat %(troveName, displayC[troveName, version])

def displayTroves(db, troveNameList = [], pathList = [], ls = False, 
                  ids = False, sha1s = False, fullVersions = False, 
                  tags = False):
    (troveNames, hasVersions) = parseTroveStrings(troveNameList)
    pathList = [os.path.abspath(util.normpath(x)) for x in pathList]
    
    if not troveNames and not pathList:
	troveNames = [ (x, None) for x in db.iterAllTroveNames() ]
	troveNames.sort()

    if not hasVersions and not ls and not ids and not sha1s and not tags:
        for path in pathList:
            for trove in db.iterTrovesByPath(path):
                troveNames.append((trove.getName(), [ trove.getVersion() ]))

        for troveName, versionList in troveNames:
            _printOneTrove(db, troveName, versionList, fullVersions)
        return

    for path in pathList:
        for trove in db.iterTrovesByPath(path):
	    _displayTroveInfo(db, trove, ls, ids, sha1s, fullVersions, tags)

    for (troveName, versionStr) in troveNames:
        try:
            for trove in db.findTrove(troveName, versionStr):
                _displayTroveInfo(db, trove, ls, ids, sha1s, fullVersions, tags)
        except repository.PackageNotFound:
            if versionStr:
                log.error("version %s of trove %s is not installed",
                          versionStr, troveName)
            else:
                log.error("trove %s is not installed", troveName)
        
def _displayTroveInfo(db, trove, ls, ids, sha1s, fullVersions, tags):

    version = trove.getVersion()
{'fraction_non_alphanumeric': 0.09247352496217852, 'fraction_numerical': 0.005042864346949067, 'mean_word_length': 2.1484421512204803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '554838', 'n_tokens_mistral': 4697, 'n_tokens_neox': 4447, 'n_words': 1405}