<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 os
import resource
import signal
import sys
import tempfile
import time
import traceback

from conary.build import cook,use
from conary.deps import deps
from conary.lib import epdb
from conary.lib import log,util
from conary import conaryclient
from conary import versions
from conary.deps.deps import ThawFlavor

from rmake.build.failure import BuildFailed
from rmake.lib import flavorutil
from rmake.lib import logfile
from rmake.lib import recipeutil
from rmake.lib.apiutils import thaw, freeze

class CookResults(object):
    def __init__(self, name, version, flavor):
        self.name = name
        self.version = version
        self.flavor = flavor
        self.status = ''
        self.csFile = ''
        self.pid = 0
        self.failureReason = None

    def setExitStatus(self, status):
        self.status = status

    def setChangeSetFile(self, csFile):
        self.csFile = csFile

    def getChangeSetFile(self):
        return self.csFile

    def setFailureReason(self, reason):
        self.failureReason = reason

    def getFailureReason(self):
        return self.failureReason

    def isBuildSuccess(self):
        return not self.status

    def __freeze__(self):
        d = self.__dict__.copy()
        d['pid'] = self.pid
        d['version'] = str(self.version)
        d['flavor'] = self.flavor.freeze()
        d['failureReason'] = freeze('FailureReason', self.failureReason)
        return d

    @staticmethod
    def __thaw__(d):
        d = d.copy()
        new = CookResults(d.pop('name'),
                          versions.VersionFromString(d.pop('version')),
                          ThawFlavor(d.pop('flavor')))
        new.__dict__.update(d)
        new.failureReason = thaw('FailureReason', new.failureReason)
        return new


def cookTrove(cfg, name, version, flavor, targetLabel):
    util.mkdirChain(cfg.root + '/tmp')
    fd, csFile = tempfile.mkstemp(dir=cfg.root + '/tmp',
                                  prefix='rmake-%s-' % name,
                                  suffix='.ccs')
    os.close(fd)
    os.chmod(csFile, 0644) # we need to be able to read this file 
                           # from the rmake server

    logPath = cfg.root + '/tmp/%s-%s.log' % (name, version.trailingRevision())
    logFile = logfile.LogFile(logPath)

    results = CookResults(name, version, flavor)

    # ignore child output problems
    signal.signal(signal.SIGTTOU, signal.SIG_IGN)

    inF, outF = os.pipe()
    pid = os.fork()
    if not pid:
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        os.close(inF)
        try:
            try:
                os.setpgrp()
                # don't accidentally make world writable files
                os.umask(0022)
                # don't allow us to create core dumps
                resource.setrlimit(resource.RLIMIT_CORE, (0,0))
                logFile.redirectOutput()

                _cookTrove(cfg, name, version, flavor, targetLabel, csFile,
                           failureFd=outF)
            except Exception, msg:
                errMsg = 'Error cooking %s=%s[%s]: %s' % \
                                        (name, version, flavor, str(msg))
                _buildFailed(outF, errMsg, traceback.format_exc())
                os._exit(1)
            else:
                os._exit(0)
        finally:
            # some kind of error occurred if we get here.
            os._exit(1)
    else:
        os.close(outF)
        return logPath, pid, (results, pid, inF, csFile)


def getResults(results, pid, inF, csFile):
    (gotResult, status) = os.waitpid(pid, os.WNOHANG)
    if not gotResult:
        return None

    results.setExitStatus(os.WEXITSTATUS(status))
    if results.isBuildSuccess():
        results.setChangeSetFile(csFile)
    else:
        errReason = []
        buffer = os.read(inF, 1024)
        while buffer:
            errReason.append(buffer)
            buffer = os.read(inF, 1024)
        errReason = ''.join(errReason)
        errTag, data = errReason.split('\002', 1)
        results.setFailureReason(thaw('FailureReason', (errTag, data)))
    os.close(inF)
    return results

def stopBuild(results, pid, inF, csFile):
    log.info('killing %s' % pid)
    try:
        os.kill(-pid, signal.SIGTERM)
    except OSError, err:<commit_msg>handle signals raised when cooking.<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 os
import resource
import signal
import sys
import tempfile
import time
import traceback

from conary.build import cook,use
from conary.deps import deps
from conary.lib import epdb
from conary.lib import log,util
from conary import conaryclient
from conary import versions
from conary.deps.deps import ThawFlavor

from rmake.build.failure import BuildFailed, FailureReason
from rmake.lib import flavorutil
from rmake.lib import logfile
from rmake.lib import recipeutil
from rmake.lib.apiutils import thaw, freeze

class CookResults(object):
    def __init__(self, name, version, flavor):
        self.name = name
        self.version = version
        self.flavor = flavor
        self.status = ''
        self.csFile = ''
        self.pid = 0
        self.failureReason = None
        self.signal = ''

    def exitedNormally(self):
        return not self.signal

    def setExitStatus(self, status):
        self.status = status

    def setExitSignal(self, signal):
        self.signal = signal

    def getExitSignal(self):
        return self.signal

    def getExitStatus(self):
        return self.status

    def setChangeSetFile(self, csFile):
        self.csFile = csFile

    def getChangeSetFile(self):
        return self.csFile

    def setFailureReason(self, reason):
        self.failureReason = reason

    def getFailureReason(self):
        return self.failureReason

    def isBuildSuccess(self):
        return self.exitedNormally() and not self.status

    def __freeze__(self):
        d = self.__dict__.copy()
        d['pid'] = self.pid
        d['version'] = str(self.version)
        d['flavor'] = self.flavor.freeze()
        d['failureReason'] = freeze('FailureReason', self.failureReason)
        return d

    @staticmethod
    def __thaw__(d):
        d = d.copy()
        new = CookResults(d.pop('name'),
                          versions.VersionFromString(d.pop('version')),
                          ThawFlavor(d.pop('flavor')))
        new.__dict__.update(d)
        new.failureReason = thaw('FailureReason', new.failureReason)
        return new


def cookTrove(cfg, name, version, flavor, targetLabel):
    util.mkdirChain(cfg.root + '/tmp')
    fd, csFile = tempfile.mkstemp(dir=cfg.root + '/tmp',
                                  prefix='rmake-%s-' % name,
                                  suffix='.ccs')
    os.close(fd)
    os.chmod(csFile, 0644) # we need to be able to read this file 
                           # from the rmake server

    logPath = cfg.root + '/tmp/%s-%s.log' % (name, version.trailingRevision())
    logFile = logfile.LogFile(logPath)

    results = CookResults(name, version, flavor)

    # ignore child output problems
    signal.signal(signal.SIGTTOU, signal.SIG_IGN)

    inF, outF = os.pipe()
    pid = os.fork()
    if not pid:
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        os.close(inF)
        try:
            try:
                os.setpgrp()
                # don't accidentally make world writable files
                os.umask(0022)
                # don't allow us to create core dumps
                resource.setrlimit(resource.RLIMIT_CORE, (0,0))
                logFile.redirectOutput()

                _cookTrove(cfg, name, version, flavor, targetLabel, csFile,
                           failureFd=outF)
            except Exception, msg:
                errMsg = 'Error cooking %s=%s[%s]: %s' % \
                                        (name, version, flavor, str(msg))
                _buildFailed(outF, errMsg, traceback.format_exc())
                os._exit(1)
            else:
                os._exit(0)
        finally:
            # some kind of error occurred if we get here.
            os._exit(1)
    else:
        os.close(outF)
        return logPath, pid, (results, pid, inF, csFile)


def getResults(results, pid, inF, csFile):
    (gotResult, status) = os.waitpid(pid, os.WNOHANG)
    if not gotResult:
        return None

    if os.WIFSIGNALED(status):
        results.setExitSignal(os.WTERMSIG(status))
    else:
        assert(os.WIFEXITED(status))
        results.setExitStatus(os.WEXITSTATUS(status))

    if results.isBuildSuccess():
        results.setChangeSetFile(csFile)
    elif results.getExitSignal():
        results.setFailureReason(BuildFailed('Build exited with signal %s' % results.getExitSignal()))
    else:
        errReason = []
        buffer = os.read(inF, 1024)
        while buffer:
            errReason.append(buffer)
            buffer = os.read(inF, 1024)
        errReason = ''.join(errReason)
        errTag, data = errReason.split('\002', 1)
        results.setFailureReason(thaw('FailureReason', (errTag, data)))
    os.close(inF)
    return results

def stopBuild(results, pid, inF, csFile):
    log.info('killing %s' % pid)
    try:
        os.kill(-pid, signal.SIGTERM)
    except OSError, err:
{'fraction_non_alphanumeric': 0.09838430467397577, 'fraction_numerical': 0.006155029813425659, 'mean_word_length': 2.248672289909403, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3278164', 'n_tokens_mistral': 3120, 'n_tokens_neox': 2991, 'n_words': 854}