#
# Copyright (c) 2004-2005 Specifix, Inc.
#
# All Rights Reserved
#
import os
import sys
import tempfile
import deps
import flavorcfg
import repository
import versions
from build import use

sys.path.insert(0, "/home/tgerla/cvs/darby/client/")
from buildsystem import distro

import conarycfg
from conarycfg import ConfigFile

from imagegen import ImageGenerator

class IsoConfig(ConfigFile):
    defaults = {
        'productPath':       'Specifix',
        'productName':       'Specifix Linux',
        'productPrefix':     'spx',
        'templatePath':      None,
        'nfsPath':           None,
        'tftpbootPath':      None,
    }

class InstallableIso(ImageGenerator):
    def getIsoConfig(self):
        isocfg = IsoConfig()
        isocfg.read("installable_iso.conf")

    def write(self):
        isocfg = self.getIsoConfig()
    
        profileId = self.job.getProfileId()

        name, projectId = self.client.server.getProfile(profileId)
        trove, versionStr, frozenFlavor = self.client.server.getTrove(profileId)
        flavor = deps.deps.ThawDependencySet(frozenFlavor)

        project = self.client.getProject(projectId)

        ccfg = project.getConaryConfig()

        flavorConfig = flavorcfg.FlavorConfig(ccfg.useDirs, ccfg.archDirs)
        ccfg.flavor = flavorConfig.toDependency(override=ccfg.flavor)
        insSet = deps.deps.DependencySet()
        for dep in deps.arch.currentArch:
            insSet.addDep(deps.deps.InstructionSetDependency, dep)
        ccfg.flavor.union(insSet)
        ccfg.buildFlavor = ccfg.flavor.copy()
        flavorConfig.populateBuildFlags()
        use.setBuildFlagsFromFlavor(None, ccfg.buildFlavor, error=None)
       
        repos = repository.netclient.NetworkRepositoryClient(ccfg.repositoryMap)

        jobId = self.job.getId()
        releaseVer = self.client.getJobData(jobId, "releaseVer")
        releasePhase = self.client.getJobData(jobId, "releasePhase")

        arch = self.job.getArch()
        assert(arch in ('x86', 'x86_64'))
 
        distroInfo = distro.DistroInfo(isocfg.productPrefix,
                                       isocfg.productPath,
                                       isocfg.productName,
                                       releaseVer, releasePhase,
                                       arch = arch)
        version = versions.VersionFromString(versionStr)
       
        # XXX remove this and pass version as soon as darby can handle a full ver
        label = version.branch().label()
      
        tmpDir = self.itcfg.imagesPath + os.path.join(arch, releasePhase)
        dist = distro.Distribution(arch, repos, ccfg,
                                   distroInfo, (trove, label, flavor),
                                   tmpDir, tmpDir+"/isos/", isocfg.templatePath,
                                   isocfg.nfsPath, isocfg.tftpbootPath, None,
                                   self.itcfg.logPath, False)
                                   
        dist.prep()
        filenames = dist.create()
        return filenames

@@ -62,16 +62,29 @@
 erved%0A#%0A
+import errno%0A
 import o
@@ -791,16 +791,38 @@
 o.conf%22)
+%0A        return isocfg
 %0A%0A    de
@@ -2562,26 +2562,24 @@
 pDir = self.
-it
 cfg.imagesPa
@@ -2725,21 +2725,34 @@
 (trove, 
-label
+version.asString()
 , flavor
@@ -2952,71 +2952,589 @@
     
-self.itcfg.logPath, False)%0A                                   %0A
+None, False)%0A                                   %0A        try:%0A            os.makedirs(self.cfg.logPath)%0A        except OSError, e:%0A            if e.errno != errno.EEXIST:%0A                raise%0A            %0A        logfile = os.path.join(self.cfg.logPath, %22instiso-%25d.log%22 %25 jobId)%0A        logfd = os.open(logfile, os.O_TRUNC %7C os.O_WRONLY %7C os.O_CREAT)%0A        stdout = os.dup(sys.stdout.fileno())%0A        stderr = os.dup(sys.stderr.fileno())%0A        os.dup2(logfd, sys.stdout.fileno())%0A        os.dup2(logfd, sys.stderr.fileno())%0A        os.close(logfd)%0A        %0A        try:%0A    
     
@@ -3557,16 +3557,20 @@
         
+    
 filename
@@ -3587,16 +3587,258 @@
 reate()%0A
+        except:%0A            os.dup2(stdout, sys.stdout.fileno())%0A            os.dup2(stderr, sys.stderr.fileno())    %0A            raise%0A        os.dup2(stdout, sys.stdout.fileno())%0A        os.dup2(stderr, sys.stderr.fileno())    %0A           %0A
         

use a unified log file for installable_iso generation