<commit_before>
###############################################################################
#   Copyright 2012-2014 The University of Texas at Austin                     #
#                                                                             #
#   Licensed under the Apache License, Version 2.0 (the "License");           #
#   you may not use this file except in compliance with the License.          #
#   You may obtain a copy of the License at                                   #
#                                                                             #
#       http://www.apache.org/licenses/LICENSE-2.0                            #
#                                                                             #
#   Unless required by applicable law or agreed to in writing, software       #
#   distributed under the License is distributed on an "AS IS" BASIS,         #
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  #
#   See the License for the specific language governing permissions and       #
#   limitations under the License.                                            #
###############################################################################

import logging
import os
import sys
import time

from ipf.paths import IPF_VAR_PATH

logger = logging.getLogger(__name__)

##############################################################################################################

class OneProcessOnly:
    def __init__(self, pidfile):
        self.pid_file_name = pidfile

    def start(self):
        if self._isRunning():
            logger.error("process is already running at %s" % self.pid_file_name)
            return
        self._writePid()
        self.run()
        self._removePid()

    def _isRunning(self):
        if self.pid_file_name is None:
            # no way to tell
            False
        try:
            pid_file = open(self.pid_file_name,"r")
            pid_str = pid_file.readline()
            pid_file.close()
            logger.debug("pid is "+pid_str)
        except IOError:
            logger.debug("no pid file")
            return False

        if (pid_str != None) and os.path.exists("/proc/"+pid_str):
            # could check /proc/pid_str/cmdline and ...
            logger.debug("found running daemon")
            return True
        logger.debug("no running daemon")
        return False

    def _writePid(self):
        if self.pid_file_name is None:
            return
        # save process id in the .pid file
        pid_file = open(self.pid_file_name,"w")
        pid_file.write(str(os.getpid()))
        pid_file.close()

    def _removePid(self):
        if self.pid_file_name is None:
            return
        try:
            os.remove(self.pid_file_name)
        except IOError:
            logger.warning("failed to remove pid file %s" % self.pid_file_name)

    def _redirect(self, stdin_file_name, stdout_file_name, stderr_file_name):
        sys.stdout.flush()
        sys.stderr.flush()
        si = file(stdin_file_name, 'r')
        so = file(stdout_file_name, 'a+')
        se = file(stderr_file_name, 'a+')
        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())<commit_msg>XCI-789 use "open" instead of "file" to fix python3 daemonized workflow bug<commit_after>
###############################################################################
#   Copyright 2012-2014 The University of Texas at Austin                     #
#                                                                             #
#   Licensed under the Apache License, Version 2.0 (the "License");           #
#   you may not use this file except in compliance with the License.          #
#   You may obtain a copy of the License at                                   #
#                                                                             #
#       http://www.apache.org/licenses/LICENSE-2.0                            #
#                                                                             #
#   Unless required by applicable law or agreed to in writing, software       #
#   distributed under the License is distributed on an "AS IS" BASIS,         #
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  #
#   See the License for the specific language governing permissions and       #
#   limitations under the License.                                            #
###############################################################################

import logging
import os
import sys
import time

from ipf.paths import IPF_VAR_PATH

logger = logging.getLogger(__name__)

##############################################################################################################

class OneProcessOnly:
    def __init__(self, pidfile):
        self.pid_file_name = pidfile

    def start(self):
        if self._isRunning():
            logger.error("process is already running at %s" % self.pid_file_name)
            return
        self._writePid()
        self.run()
        self._removePid()

    def _isRunning(self):
        if self.pid_file_name is None:
            # no way to tell
            False
        try:
            pid_file = open(self.pid_file_name,"r")
            pid_str = pid_file.readline()
            pid_file.close()
            logger.debug("pid is "+pid_str)
        except IOError:
            logger.debug("no pid file")
            return False

        if (pid_str != None) and os.path.exists("/proc/"+pid_str):
            # could check /proc/pid_str/cmdline and ...
            logger.debug("found running daemon")
            return True
        logger.debug("no running daemon")
        return False

    def _writePid(self):
        if self.pid_file_name is None:
            return
        # save process id in the .pid file
        pid_file = open(self.pid_file_name,"w")
        pid_file.write(str(os.getpid()))
        pid_file.close()

    def _removePid(self):
        if self.pid_file_name is None:
            return
        try:
            os.remove(self.pid_file_name)
        except IOError:
            logger.warning("failed to remove pid file %s" % self.pid_file_name)

    def _redirect(self, stdin_file_name, stdout_file_name, stderr_file_name):
        sys.stdout.flush()
        sys.stderr.flush()
        si = open(stdin_file_name, 'r')
        so = open(stdout_file_name, 'a+')
        se = open(stderr_file_name, 'a+')
        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())
{'fraction_non_alphanumeric': 0.16023339317773788, 'fraction_numerical': 0.005086774386594853, 'mean_word_length': 1.7521613832853027, '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': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '709341', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1656, 'n_words': 464}