#
# Copyright (c) 2008 rPath, Inc.
#
# All rights reserved.
#

import os.path

from conary import conarycfg, conaryclient
from rmake.build import buildcfg
from rmake.build import buildjob
from rmake.cmdline import buildcmd

from bob import config
from bob import flavors
from bob.loader import conary_loader
from bob.loader import hg_loader

class CookBob(object):
    def __init__(self):
        self.cfg = config.BobConfig()
        self.macros = {}
        self.targets = {}
        self.tests = {}
        
        # repo / build info
        self.job = None
        self.hg = {}

        # conary client
        self.conarycfg = conarycfg.ConaryConfiguration(True)
        self.cc = conaryclient.ConaryClient()
        self.nc = self.cc.getRepos()

        # rmake client
        self.buildcfg = buildcfg.BuildConfiguration(True)
        self.rc = client.rMakeClient(self.buildcfg.rmakeUrl)

    def readPlan(self, plan):
        if plan.startsWith('http://') or plan.startswith('https://'):
            self.cfg.readUrl(plan)
        else:
            self.cfg.read(plan)

        for name, section in self.cfg._sections:
            sectype, name = name.split(':', 1)
            if sectype == 'target':
                self.targets[name] = CookTarget(name, section)
            elif sectype == 'test':
                self.tests[name] = CookTest(name, section)
            else:
                assert False

    def run(self):
        # Get versions of all hg repositories
        for name, repos in self.cfg.hg.iteritems():
            node = hg_loader.getNode(repos)
            self.hg[name] = node

        # Determine the top-level trove specs to build
        troveSpecs = []
        for targetName, targetCfg in self.targets:
            for flavor in flavors.expandByTarget(targetCfg):
                troveSpecs += (targetName, self.cfg.sourceLabel, flavor, '')

        # Pre-build configuration
        self.rc.addRepository(buildConfig)
        self.buildcfg.limitToLabels(self.cfg.sourceLabel)

        cfg = copy.deepcopy(self.buildcfg) # XXX necessary?
        cfg.dropContexts()
        cfg.initializeFlavors()
        cfg.buildLabel = self.getLabelFromTag('test')
        use.setBuildFlagsFromFlavor(None, cfg.buildFlavor, error=False)

        # Create rMake job
        job = buildjob.BuildJob()
        job.setMainConfig(cfg)

        # Determine which troves to build
        troveList = buildcmd.getTrovesToBuild(cfg, self.cc, troveSpecs,
            recurseGroups=buildcmd.BUILD_RECURSE_GROUPS_SOURCE,
            matchSpecs=cfg.matchTroveRule)

    def getLabelFromTag(self, stage='test'):
        return self.cfg.labelPrefix + self.cfg.tag + '-' + stage

@@ -104,22 +104,69 @@
 ycfg
-, conaryclient
+%0Afrom conary import conaryclient%0Afrom conary.deps import deps
 %0Afro
@@ -1469,194 +1469,21 @@
 def 
-run(self):%0A        # Get versions of all hg repositories%0A        for name, repos in self.cfg.hg.iteritems():%0A            node = hg_loader.getNode(repos)%0A            self.hg%5Bname%5D = node%0A
+getJob(self):
 %0A   
@@ -2453,110 +2453,564 @@
     
-def getLabelFromTag(self, stage='test'):%0A        return self.cfg.labelPrefix + self.cfg.tag + '-' + stage
+    # Add troves to job%0A        for name, version, flavor in troveList:%0A            if flavor is None:%0A                flavor = deps.parseFlavor('')%0A            job.addTrove(name, version, flavor, '', bt)%0A%0A        return job%0A%0A    def getLabelFromTag(self, stage='test'):%0A        return self.cfg.labelPrefix + self.cfg.tag + '-' + stage%0A%0A    def run(self):%0A        # Get versions of all hg repositories%0A        for name, repos in self.cfg.hg.iteritems():%0A            node = hg_loader.getNode(repos)%0A            self.hg%5Bname%5D = node%0A%0A        job = self.getJob()
 %0A

Move job creation to getJob()