<commit_before>#
# Copyright (c) 2006-2007 rPath, Inc.  All Rights Reserved.
#

import copy
import itertools
import os
import tempfile
import traceback

#conary
from conary.build import cook,loadrecipe,recipe,use
from conary import conarycfg
from conary import conaryclient
from conary.deps import deps
from conary.lib import log,util
from conary.deps.deps import Flavor
from conary.repository import trovesource

#rmake
from rmake import errors
from rmake import failure
from rmake.lib import flavorutil
from rmake.build import buildtrove


def getRecipes(repos, troveTups):
    fileIds = []
    troveSpecs = [ (x[0], x[1], Flavor()) for x in troveTups ]
    troves = repos.getTroves(troveSpecs)
    for i, trove in enumerate(troves):
        filename = trove.getName().split(':')[0] + '.recipe'
        found = False
        for (pathId, filePath, fileId, fileVersion) in trove.iterFileList():
            if filePath == filename:
                fileIds.append((fileId, fileVersion))
                found = True
                break
        if not found:
            raise RuntimeError, 'Could not find recipe for %s' % trove.getName()
    recipes = repos.getFileContents(fileIds)
    recipeList = []
    for i, recipe in enumerate(recipes):
        name = troves[i].getName().split(':')[0]
        (fd, recipeFile) = tempfile.mkstemp(".recipe", 'temp-%s-' %name)
        outF = os.fdopen(fd, "w")
        inF = recipe.get()
        util.copyfileobj(inF, outF)
        outF.close()
        inF.close()
        del inF
        del outF
        recipeList.append(recipeFile)
    return recipeList, troves

def loadRecipe(repos, name, version, flavor, recipeFile=None,
               defaultFlavor=None, loadInstalledSource=None,
               installLabelPath=None, buildLabel=None, groupRecipeSource=None,
               cfg=None):
    name = name.split(':')[0]
    try:
        if defaultFlavor is not None:
            fullFlavor = deps.overrideFlavor(defaultFlavor, flavor)
        else:
            fullFlavor = flavor
        # set up necessary flavors and track used flags before
        # calling loadRecipe, since even loading the class
        # may check some flags that may never be checked inside
        # the recipe
        recipeObj, loader = getRecipeObj(repos, name,
                                       version, fullFlavor, recipeFile,
                                       loadInstalledSource=loadInstalledSource,
                                       installLabelPath=installLabelPath,
                                       buildLabel=buildLabel,
                                       groupRecipeSource=groupRecipeSource,
                                       cfg=cfg)
        relevantFlavor = use.usedFlagsToFlavor(recipeObj.name)
        relevantFlavor = flavorutil.removeInstructionSetFlavor(relevantFlavor)
        # always add in the entire arch flavor.  We need to ensure the
        # relevant flavor is unique per architecture, also, arch flavors
        # can affect the macros used.
        if defaultFlavor is not None:
            relevantFlavor.union(flavor)
        relevantFlavor.union(flavorutil.getArchFlags(fullFlavor))
        relevantFlags = flavorutil.getFlavorUseFlags(relevantFlavor)
        flags = flavorutil.getFlavorUseFlags(fullFlavor)
        use.track(False)

        for flagSet in ('Use',):
        # allow local flags not to be used -- they are set to their default
            if flagSet not in relevantFlags:
                continue
            for flag in relevantFlags[flagSet]:
                if flag not in flags[flagSet]:
                    raise (RuntimeError,
                            "Recipe %s uses Flavor %s but value not known" %(name, flag))
        if 'Arch' in relevantFlags:
            for majarch in relevantFlags['Arch'].keys():
                for subarch in relevantFlags['Arch'][majarch]:
                    if not use.Arch[majarch][subarch]:
                        #negative values for subarches are assumed
                        continue
                    if subarch not in flags['Arch'][majarch]:
                        log.error("arch %s.%s used but not specified" % (
                                                         majarch, subarch))
                        raise RuntimeError, (
                                "arch %s.%s used but not specified" % (
                                                         majarch, subarch))
            use.resetUsed()
    except:
        log.error('Error Loading Recipe (%s, %s, %s):\n%s' %
                                    (name, version, fullFlavor,
                                     ''.join(traceback.format_exc())))
        raise
    return loader, recipeObj, relevantFlavor


def getRecipeObj(repos, name, version, flavor, recipeFile,
                 loadInstalledSource=None, installLabelPath=None, 
                 loadRecipeSpecs=None, buildLabel = None,
                 groupRecipeSource=None, cfg=None):
    if cfg:
        cfg = copy.deepcopy(cfg)
    else:
        cfg = conarycfg.ConaryConfiguration(False)
    cfg.initializeFlavors()
    branch = version.branch()
    if not buildLabel:
        buildLabel = version.branch().label()
    if not installLabelPath:
        cfg.installLabelPath = [buildLabel]
    else:
        cfg.installLabelPath = installLabelPath
    cfg.buildFlavor = flavor
    name = name.split(':')[0]
    use.LocalFlags._clear()
    assert(flavorutil.getArch(flavor))
    use.setBuildFlagsFromFlavor(name, flavor, error=False)
    use.resetUsed()
    use.track(True)
    ignoreInstalled = not loadInstalledSource
    if recipeFile:
        loader = loadrecipe.RecipeLoader(recipeFile[0], cfg, repos,
                                         name + ':source', branch,
                                         ignoreInstalled=ignoreInstalled,
                                         db=loadInstalledSource,
                                         buildFlavor=flavor)
        recipeClass = loader.getRecipe()
        recipeClass._trove = recipeFile[1]
    else:
        loader = loadrecipe.recipeLoaderFromSourceComponent(name + ':source',
                                               cfg, repos, version.asString(),
                                               labelPath=installLabelPath,
                                               ignoreInstalled=ignoreInstalled,
                                               db=loadInstalledSource,
                                               buildFlavor=flavor)[0]
        recipeClass = loader.getRecipe()
    if recipe.isGroupRecipe(recipeClass):
        recipeObj = recipeClass(repos, cfg, buildLabel, None, None,
                            extraMacros={'buildlabel' : buildLabel.asString()})
        recipeObj.sourceVersion = version
        recipeObj.setup()
        if groupRecipeSource:
            sourceComponents = recipeObj._findSources(groupRecipeSource)
            recipeObj.delayedRequires = sourceComponents
    elif recipe.isPackageRecipe(recipeClass):
        recipeObj = recipeClass(cfg, None, None,
                                {'buildlabel' : buildLabel.asString()},
                                lightInstance=True)
        recipeObj.sourceVersion = version
        if not recipeObj.needsCrossFlags():
            recipeObj.crossRequires = []
        recipeObj.loadPolicy()
        recipeObj.setup()
    elif recipe.isInfoRecipe(recipeClass):
        recipeObj = recipeClass(cfg, None, None,
                                {'buildlabel' : buildLabel.asString()})
        recipeObj.sourceVersion = version
        recipeObj.setup()
    elif recipe.isRedirectRecipe(recipeClass):
        recipeObj = recipeClass(repos, cfg, buildLabel, flavor)
        recipeObj.sourceVersion = version
        recipeObj.setup()
    else:
        raise RuntimeError, 'Unknown class type %s for recipe %s' % (recipeClass, name)
    return recipeObj, loader

def loadRecipeClass(repos, name, version, flavor, recipeFile=None,<commit_msg>Set buildbranch to ensure derivedrecipes are set up right when loading.<commit_after>#
# Copyright (c) 2006-2007 rPath, Inc.  All Rights Reserved.
#

import copy
import itertools
import os
import tempfile
import traceback

#conary
from conary.build import cook,loadrecipe,recipe,use
from conary import conarycfg
from conary import conaryclient
from conary.deps import deps
from conary.lib import log,util
from conary.deps.deps import Flavor
from conary.repository import trovesource

#rmake
from rmake import errors
from rmake import failure
from rmake.lib import flavorutil
from rmake.build import buildtrove


def getRecipes(repos, troveTups):
    fileIds = []
    troveSpecs = [ (x[0], x[1], Flavor()) for x in troveTups ]
    troves = repos.getTroves(troveSpecs)
    for i, trove in enumerate(troves):
        filename = trove.getName().split(':')[0] + '.recipe'
        found = False
        for (pathId, filePath, fileId, fileVersion) in trove.iterFileList():
            if filePath == filename:
                fileIds.append((fileId, fileVersion))
                found = True
                break
        if not found:
            raise RuntimeError, 'Could not find recipe for %s' % trove.getName()
    recipes = repos.getFileContents(fileIds)
    recipeList = []
    for i, recipe in enumerate(recipes):
        name = troves[i].getName().split(':')[0]
        (fd, recipeFile) = tempfile.mkstemp(".recipe", 'temp-%s-' %name)
        outF = os.fdopen(fd, "w")
        inF = recipe.get()
        util.copyfileobj(inF, outF)
        outF.close()
        inF.close()
        del inF
        del outF
        recipeList.append(recipeFile)
    return recipeList, troves

def loadRecipe(repos, name, version, flavor, recipeFile=None,
               defaultFlavor=None, loadInstalledSource=None,
               installLabelPath=None, buildLabel=None, groupRecipeSource=None,
               cfg=None):
    name = name.split(':')[0]
    try:
        if defaultFlavor is not None:
            fullFlavor = deps.overrideFlavor(defaultFlavor, flavor)
        else:
            fullFlavor = flavor
        # set up necessary flavors and track used flags before
        # calling loadRecipe, since even loading the class
        # may check some flags that may never be checked inside
        # the recipe
        recipeObj, loader = getRecipeObj(repos, name,
                                       version, fullFlavor, recipeFile,
                                       loadInstalledSource=loadInstalledSource,
                                       installLabelPath=installLabelPath,
                                       buildLabel=buildLabel,
                                       groupRecipeSource=groupRecipeSource,
                                       cfg=cfg)
        relevantFlavor = use.usedFlagsToFlavor(recipeObj.name)
        relevantFlavor = flavorutil.removeInstructionSetFlavor(relevantFlavor)
        # always add in the entire arch flavor.  We need to ensure the
        # relevant flavor is unique per architecture, also, arch flavors
        # can affect the macros used.
        if defaultFlavor is not None:
            relevantFlavor.union(flavor)
        relevantFlavor.union(flavorutil.getArchFlags(fullFlavor))
        relevantFlags = flavorutil.getFlavorUseFlags(relevantFlavor)
        flags = flavorutil.getFlavorUseFlags(fullFlavor)
        use.track(False)

        for flagSet in ('Use',):
        # allow local flags not to be used -- they are set to their default
            if flagSet not in relevantFlags:
                continue
            for flag in relevantFlags[flagSet]:
                if flag not in flags[flagSet]:
                    raise (RuntimeError,
                            "Recipe %s uses Flavor %s but value not known" %(name, flag))
        if 'Arch' in relevantFlags:
            for majarch in relevantFlags['Arch'].keys():
                for subarch in relevantFlags['Arch'][majarch]:
                    if not use.Arch[majarch][subarch]:
                        #negative values for subarches are assumed
                        continue
                    if subarch not in flags['Arch'][majarch]:
                        log.error("arch %s.%s used but not specified" % (
                                                         majarch, subarch))
                        raise RuntimeError, (
                                "arch %s.%s used but not specified" % (
                                                         majarch, subarch))
            use.resetUsed()
    except:
        log.error('Error Loading Recipe (%s, %s, %s):\n%s' %
                                    (name, version, fullFlavor,
                                     ''.join(traceback.format_exc())))
        raise
    return loader, recipeObj, relevantFlavor


def getRecipeObj(repos, name, version, flavor, recipeFile,
                 loadInstalledSource=None, installLabelPath=None, 
                 loadRecipeSpecs=None, buildLabel = None,
                 groupRecipeSource=None, cfg=None):
    if cfg:
        cfg = copy.deepcopy(cfg)
    else:
        cfg = conarycfg.ConaryConfiguration(False)
    cfg.initializeFlavors()
    branch = version.branch()
    if not buildLabel:
        buildLabel = version.branch().label()
    if not installLabelPath:
        cfg.installLabelPath = [buildLabel]
    else:
        cfg.installLabelPath = installLabelPath
    cfg.buildFlavor = flavor
    name = name.split(':')[0]
    use.LocalFlags._clear()
    assert(flavorutil.getArch(flavor))
    use.setBuildFlagsFromFlavor(name, flavor, error=False)
    use.resetUsed()
    use.track(True)
    ignoreInstalled = not loadInstalledSource
    macros = {'buildlabel' : buildLabel.asString(),
              'buildbranch' : version.asString()}
    if recipeFile:
        loader = loadrecipe.RecipeLoader(recipeFile[0], cfg, repos,
                                         name + ':source', branch,
                                         ignoreInstalled=ignoreInstalled,
                                         db=loadInstalledSource,
                                         buildFlavor=flavor)
        recipeClass = loader.getRecipe()
        recipeClass._trove = recipeFile[1]
    else:
        loader = loadrecipe.recipeLoaderFromSourceComponent(name + ':source',
                                               cfg, repos, version.asString(),
                                               labelPath=installLabelPath,
                                               ignoreInstalled=ignoreInstalled,
                                               db=loadInstalledSource,
                                               buildFlavor=flavor)[0]
        recipeClass = loader.getRecipe()
    if recipe.isGroupRecipe(recipeClass):
        recipeObj = recipeClass(repos, cfg, buildLabel, None, None,
                            extraMacros=macros)
        recipeObj.sourceVersion = version
        recipeObj.setup()
        if groupRecipeSource:
            sourceComponents = recipeObj._findSources(groupRecipeSource)
            recipeObj.delayedRequires = sourceComponents
    elif recipe.isPackageRecipe(recipeClass):
        recipeObj = recipeClass(cfg, None, None, macros, lightInstance=True)
        recipeObj.sourceVersion = version
        if not recipeObj.needsCrossFlags():
            recipeObj.crossRequires = []
        recipeObj.loadPolicy()
        recipeObj.setup()
    elif recipe.isInfoRecipe(recipeClass):
        recipeObj = recipeClass(cfg, None, None, macros)
        recipeObj.sourceVersion = version
        recipeObj.setup()
    elif recipe.isRedirectRecipe(recipeClass):
        recipeObj = recipeClass(repos, cfg, buildLabel, flavor)
        recipeObj.sourceVersion = version
        recipeObj.setup()
    else:
        raise RuntimeError, 'Unknown class type %s for recipe %s' % (recipeClass, name)
    return recipeObj, loader

def loadRecipeClass(repos, name, version, flavor, recipeFile=None,
{'fraction_non_alphanumeric': 0.08105566700100302, 'fraction_numerical': 0.002131394182547643, 'mean_word_length': 1.8421521468020667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4788113', 'n_tokens_mistral': 4138, 'n_tokens_neox': 4062, 'n_words': 1051}