<commit_before>#
# Copyright (c) 2006-2007 rPath, Inc.  All Rights Reserved.
#
import os
import select
import signal
import socket
import sys
import time

from conary.lib import util
from rmake import errors

class LogFile(object):

    def __init__(self, path, mode='a'):
        self.fd = None
        self.tee = None
        self.stdout = None
        self.open(path, mode)

    def __del__(self):
        if self.fd:
            self.close()

    def open(self, path, mode):
        if isinstance(path, int):
            logfd = path
        else:
            util.mkdirChain(os.path.dirname(path))
            logfd = os.open(path, os.O_CREAT | os.O_APPEND | os.O_WRONLY)
        self.fd = logfd

    def write(self, data):
        os.write(self.fd, data)

    def close(self):
        if self.stdout:
            self.restoreOutput()
        if self.fd:
            file = os.fdopen(self.fd, 'w')
            file.flush()
            file.close()
            self.fd = None
        if self.tee:
            self.tee.close()
            self.tee = None

    def teeOutput(self):
        self.tee = Tee()
        outFile = self.tee.tee(self.fd, sys.stdout.fileno())
        os.close(self.fd)
        self.fd = outFile
        self.redirectOutput()

    def redirectOutput(self, close=False):
        sys.stdout.flush()
        sys.stderr.flush()
        if not close:
            self.stdout = os.dup(sys.stdout.fileno())
            self.stderr = os.dup(sys.stderr.fileno())

        os.dup2(self.fd, sys.stdout.fileno())
        os.dup2(self.fd, sys.stderr.fileno())

    def logToPort(self, host, port, key=None):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        if key:
            s.send(key + '\n')
        status = s.recv(3)
        if status != 'OK\n':
            raise errors.OpenError("Could not connect to socket")
        socketFd = s.fileno()
        self.tee = Tee()
        outFile = self.tee.tee(self.fd, socketFd)
        os.close(self.fd)
        self.fd = outFile
        self.redirectOutput()

    def restoreOutput(self):
        sys.stdout.flush()
        sys.stderr.flush()<commit_msg>branch merge<commit_after>#
# Copyright (c) 2006-2007 rPath, Inc.  All Rights Reserved.
#
import os
import select
import signal
import socket
import sys
import time

from conary.lib import util
from rmake import errors

class LogFile(object):

    def __init__(self, path, mode='a'):
        self.fd = None
        self.tee = None
        self.stdout = None
        self.open(path, mode)

    def __del__(self):
        if self.fd:
            self.close()

    def open(self, path, mode):
        if isinstance(path, int):
            logfd = path
        else:
            util.mkdirChain(os.path.dirname(path))
            logfd = os.open(path, os.O_CREAT | os.O_APPEND | os.O_WRONLY)
        self.fd = logfd

    def write(self, data):
        os.write(self.fd, data)

    def close(self):
        if self.stdout:
            self.restoreOutput()
        if self.fd:
            file = os.fdopen(self.fd, 'w')
            file.flush()
            file.close()
            self.fd = None
        if self.tee:
            self.tee.close()
            self.tee = None

    def teeOutput(self):
        self.tee = Tee()
        outFile = self.tee.tee(self.fd, sys.stdout.fileno())
        os.close(self.fd)
        self.fd = outFile
        self.redirectOutput()

    def redirectOutput(self, close=False):
        sys.stdout.flush()
        sys.stderr.flush()
        if not close:
            self.stdout = os.dup(sys.stdout.fileno())
            self.stderr = os.dup(sys.stderr.fileno())

        os.dup2(self.fd, sys.stdout.fileno())
        os.dup2(self.fd, sys.stderr.fileno())

    def logToPort(self, host, port, key=None):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        if key:
            s.send(key + '\n')
            status = s.recv(3)
            if status != 'OK\n':
                raise errors.OpenError("Could not connect to socket")
        socketFd = s.fileno()
        self.tee = Tee()
        outFile = self.tee.tee(self.fd, socketFd)
        os.close(self.fd)
        self.fd = outFile
        self.redirectOutput()

    def restoreOutput(self):
        sys.stdout.flush()
        sys.stderr.flush()
{'fraction_non_alphanumeric': 0.12433801519686852, 'fraction_numerical': 0.005065622841353903, 'mean_word_length': 1.7774936061381075, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5768797', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1365, 'n_words': 314}