<commit_before>#
# Copyright (c) 2006 rPath, 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 warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
import errno
import copy
import os
import sys
import tempfile
import time

#conary
from conary.lib import util

#rmake
from rmake import constants
from rmake import errors
from rmake.worker.chroot import rootserver
from rmake.worker.chroot import rootfactory
from rmake.lib import flavorutil
from rmake.lib import logger as logger_
from rmake.lib import repocache

class ChrootManager(object):
    def __init__(self, serverCfg, logger=None):
        self.serverCfg = serverCfg
        self.baseDir =  os.path.realpath(serverCfg.getChrootDir())
        self.archiveDir =  os.path.realpath(serverCfg.getChrootArchiveDir())
        self.chrootHelperPath = serverCfg.chrootHelperPath
        cacheDir = serverCfg.getCacheDir()
        util.mkdirChain(cacheDir)
        self.csCache = repocache.RepositoryCache(cacheDir)
        self.chroots = {}
        if logger is None:
            logger = logger_.Logger()
        self.logger = logger

    def listChroots(self):
        chroots = []
        if os.path.exists(self.baseDir):
            for name in os.listdir(self.baseDir):
                path = self.baseDir + '/' + name
                if os.path.isdir(path):
                    chroots.append(name)
        if os.path.exists(self.archiveDir):
            for name in os.listdir(self.archiveDir):
                chroots.append('archive/' + name)
        return chroots

    def createRootPath(self, buildTrove):
        name = buildTrove.getName().rsplit(':', 1)[0]
        path =  self.baseDir + '/%s' % name
        count = 1
        if path not in self.chroots:
            return path
        path = path + '-%s'
        while True:
            if path % count in self.chroots:
                count += 1
            else:
                return path % count

    def rootFinished(self, chroot):
        root = chroot.getRoot()
        if root in self.chroots:
            del self.chroots[root]

    def getRootFactory(self, cfg, jobList, buildTrove):
        cfg = copy.deepcopy(cfg)
        cfg.threaded = False

        cfg.logFile = '/var/log/conary'
        cfg.dbPath = '/var/lib/conarydb'

        setArch, targetArch = flavorutil.getTargetArch(buildTrove.flavor)

        if not setArch:
            targetArch = None

        rootDir = self.createRootPath(buildTrove)
        if (not cfg.strictMode and
              (buildTrove.isRedirectRecipe() or buildTrove.isGroupRecipe()
               or buildTrove.isFilesetRecipe())):
            # we don't need to actually instantiate a root to cook
            # these packages - if we're not worried about using the 
            # "correct" conary, conary-policy, etc.
            jobList = []
            util.mkdirChain(self.baseDir)
            os.chdir(self.baseDir)
            chrootClass = rootfactory.FakeRmakeRoot
        else:
            chrootClass = rootfactory.FullRmakeChroot
        cfg.root = rootDir
        copyInConary = not targetArch and not cfg.strictMode

        chroot = chrootClass(buildTrove,
                             self.chrootHelperPath,
                             cfg, self.serverCfg, jobList, self.logger,
                             csCache=self.csCache,
                             copyInConary=copyInConary)
        buildLogPath = self.serverCfg.getBuildLogPath(buildTrove.jobId)
        chrootServer = rMakeChrootServer(chroot, targetArch,
                                         useTmpfs=self.serverCfg.useTmpfs,
                                         buildLogPath=buildLogPath,
                                         reuseRoots=cfg.reuseRoots,
                                         strictMode=cfg.strictMode, 
                                         logger=self.logger)

        self.chroots[rootDir] = chrootServer
        return chrootServer

    def useExistingChroot(self, chrootPath, useChrootUser=True):
        if not chrootPath.startswith(self.baseDir):
            chrootPath = self.baseDir + '/' +  chrootPath
        assert(os.path.exists(chrootPath))
        chroot = rootfactory.ExistingChroot(chrootPath, self.logger,
                                             self.chrootHelperPath)
        chrootServer = rMakeChrootServer(chroot, targetArch=None,
                                         useTmpfs=self.serverCfg.useTmpfs,
                                         buildLogPath=None, reuseRoots=True,
                                         useChrootUser=useChrootUser,
                                         logger=logger)
        self.chroots[chrootPath] = chrootServer
        return chrootServer

    def archiveChroot(self, chrootPath, newPath):
        chrootPath = os.path.realpath(self.baseDir + '/' + chrootPath)
        newPath = os.path.realpath(self.archiveDir + '/' + newPath)
        assert(os.path.dirname(chrootPath) == self.baseDir)
        assert(os.path.dirname(newPath) == self.archiveDir)
        os.system('/bin/mv %s %s' % (chrootPath, newPath))

    def deleteChroot(self, chrootPath):
        if chrootPath.startswith('archive/'):
            chrootPath = self.archiveDir + '/' + chrootPath[len('archive/'):]
            chrootPath = os.path.realpath(chrootPath)
            assert(os.path.dirname(chrootPath) == self.archiveDir)
        else:
            chrootPath = os.path.realpath(self.baseDir +'/' +  chrootPath)
            assert(os.path.dirname(chrootPath) == self.baseDir)<commit_msg>logger is a instance variable<commit_after>#
# Copyright (c) 2006 rPath, 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 warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
import errno
import copy
import os
import sys
import tempfile
import time

#conary
from conary.lib import util

#rmake
from rmake import constants
from rmake import errors
from rmake.worker.chroot import rootserver
from rmake.worker.chroot import rootfactory
from rmake.lib import flavorutil
from rmake.lib import logger as logger_
from rmake.lib import repocache

class ChrootManager(object):
    def __init__(self, serverCfg, logger=None):
        self.serverCfg = serverCfg
        self.baseDir =  os.path.realpath(serverCfg.getChrootDir())
        self.archiveDir =  os.path.realpath(serverCfg.getChrootArchiveDir())
        self.chrootHelperPath = serverCfg.chrootHelperPath
        cacheDir = serverCfg.getCacheDir()
        util.mkdirChain(cacheDir)
        self.csCache = repocache.RepositoryCache(cacheDir)
        self.chroots = {}
        if logger is None:
            logger = logger_.Logger()
        self.logger = logger

    def listChroots(self):
        chroots = []
        if os.path.exists(self.baseDir):
            for name in os.listdir(self.baseDir):
                path = self.baseDir + '/' + name
                if os.path.isdir(path):
                    chroots.append(name)
        if os.path.exists(self.archiveDir):
            for name in os.listdir(self.archiveDir):
                chroots.append('archive/' + name)
        return chroots

    def createRootPath(self, buildTrove):
        name = buildTrove.getName().rsplit(':', 1)[0]
        path =  self.baseDir + '/%s' % name
        count = 1
        if path not in self.chroots:
            return path
        path = path + '-%s'
        while True:
            if path % count in self.chroots:
                count += 1
            else:
                return path % count

    def rootFinished(self, chroot):
        root = chroot.getRoot()
        if root in self.chroots:
            del self.chroots[root]

    def getRootFactory(self, cfg, jobList, buildTrove):
        cfg = copy.deepcopy(cfg)
        cfg.threaded = False

        cfg.logFile = '/var/log/conary'
        cfg.dbPath = '/var/lib/conarydb'

        setArch, targetArch = flavorutil.getTargetArch(buildTrove.flavor)

        if not setArch:
            targetArch = None

        rootDir = self.createRootPath(buildTrove)
        if (not cfg.strictMode and
              (buildTrove.isRedirectRecipe() or buildTrove.isGroupRecipe()
               or buildTrove.isFilesetRecipe())):
            # we don't need to actually instantiate a root to cook
            # these packages - if we're not worried about using the 
            # "correct" conary, conary-policy, etc.
            jobList = []
            util.mkdirChain(self.baseDir)
            os.chdir(self.baseDir)
            chrootClass = rootfactory.FakeRmakeRoot
        else:
            chrootClass = rootfactory.FullRmakeChroot
        cfg.root = rootDir
        copyInConary = not targetArch and not cfg.strictMode

        chroot = chrootClass(buildTrove,
                             self.chrootHelperPath,
                             cfg, self.serverCfg, jobList, self.logger,
                             csCache=self.csCache,
                             copyInConary=copyInConary)
        buildLogPath = self.serverCfg.getBuildLogPath(buildTrove.jobId)
        chrootServer = rMakeChrootServer(chroot, targetArch,
                                         useTmpfs=self.serverCfg.useTmpfs,
                                         buildLogPath=buildLogPath,
                                         reuseRoots=cfg.reuseRoots,
                                         strictMode=cfg.strictMode, 
                                         logger=self.logger)

        self.chroots[rootDir] = chrootServer
        return chrootServer

    def useExistingChroot(self, chrootPath, useChrootUser=True):
        if not chrootPath.startswith(self.baseDir):
            chrootPath = self.baseDir + '/' +  chrootPath
        assert(os.path.exists(chrootPath))
        chroot = rootfactory.ExistingChroot(chrootPath, self.logger,
                                             self.chrootHelperPath)
        chrootServer = rMakeChrootServer(chroot, targetArch=None,
                                         useTmpfs=self.serverCfg.useTmpfs,
                                         buildLogPath=None, reuseRoots=True,
                                         useChrootUser=useChrootUser,
                                         logger=self.logger)
        self.chroots[chrootPath] = chrootServer
        return chrootServer

    def archiveChroot(self, chrootPath, newPath):
        chrootPath = os.path.realpath(self.baseDir + '/' + chrootPath)
        newPath = os.path.realpath(self.archiveDir + '/' + newPath)
        assert(os.path.dirname(chrootPath) == self.baseDir)
        assert(os.path.dirname(newPath) == self.archiveDir)
        os.system('/bin/mv %s %s' % (chrootPath, newPath))

    def deleteChroot(self, chrootPath):
        if chrootPath.startswith('archive/'):
            chrootPath = self.archiveDir + '/' + chrootPath[len('archive/'):]
            chrootPath = os.path.realpath(chrootPath)
            assert(os.path.dirname(chrootPath) == self.archiveDir)
        else:
            chrootPath = os.path.realpath(self.baseDir +'/' +  chrootPath)
            assert(os.path.dirname(chrootPath) == self.baseDir)
{'fraction_non_alphanumeric': 0.08563281053426151, 'fraction_numerical': 0.0016774301769688837, 'mean_word_length': 1.9471082550667327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6795978', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3237, 'n_words': 787}