<commit_before>import sys, os, time, logging, shlex, signal, subprocess, errno
import pyev
from ..config import config, get_boolean, get_env
from ..utils import parse_signals, expandvars, enable_nonblocking, disable_nonblocking, get_python_exec, get_signal_name
from ..cnscom import program_state_enum, svrcall_error
from .logmed import log_mediator
from .singleton import get_svrapp


if sys.platform == 'win32':
	import msvcrt
	import win32file, win32pipe, pywintypes, winerror # from Python Win32
#

try:
	import resource
except ImportError:
	resource = None

#

L = logging.getLogger("program")
Lmy = logging.getLogger("my") # Message yielding logger

#

class program(object):

	DEFAULTS = {
		'command': None,
		'directory': None,
		'umask': None,
		'starttimeout': 0.5,
		'stoptimeout': 3,
		'killby': 'TERM,INT,TERM,INT,TERM,INT,KILL',
		'stdin': '<null>', # TODO: This can be very probably removed as there is no reasonable use
		'stdout': '<stderr>',
		'stderr': '<logdir>',
		'priority': 100,
		'disabled': False,
		'coredump': False,
		'autorestart': False,
		'processgroup': True,
		'logscan_stdout': '',
		'logscan_stderr': '',
		'notify_fatal': '<global>',
	}

	def __init__(self, svrapp, config_section):
		_, self.ident = config_section.split(':', 2)
		self.state = program_state_enum.STOPPED
		self.subproc = None

		self.launch_cnt = 0
		self.autorestart_cnt = 0
		self.start_time = None
		self.stop_time = None
		self.exit_time = None
		self.exit_status = None
		self.coredump_enabled = None # If true, kill by SIGQUIT -> dump core

		if sys.platform != 'win32':
			# On Windows we are using periodic pipe check in win32_read_stdfd
			self.watchers = [
				pyev.Io(0, 0, svrapp.loop, self.__read_stdfd, 0),
				pyev.Io(0, 0, svrapp.loop, self.__read_stdfd, 1),
			]

		# Build configuration
		self.config = self.DEFAULTS.copy()
		self.config.update(config.items(config_section))

		# Prepare program command line
		cmd = self.config.get('command')
		if cmd is None:
			L.error("Missing command option in {0} -> CFGERROR".format(config_section))
			self.state = program_state_enum.CFGERROR
			return

		if cmd == '<httpfend>':
			cmd = get_python_exec(cmdline=["-u","-m","ramona.httpfend"])
		elif cmd[:1] == '<':
			L.error("Unknown command option '{1}' in {0} -> CFGERROR".format(config_section, cmd))
			self.state = program_state_enum.CFGERROR
			return

		cmd = cmd.replace('\\', '\\\\')
		self.cmdline = shlex.split(cmd)

		# Prepare stop signals
		if sys.platform != 'win32':
			self.stopsignals = parse_signals(self.config['killby'])
			if len(self.stopsignals) == 0: self.stopsignals = [signal.SIGTERM]
			self.act_stopsignals = None

		if self.config['stdin'] != '<null>':
			L.error("Unknown stdin option '{0}' in {1} -> CFGERROR".format(self.config['stdin'], config_section))
			self.state = program_state_enum.CFGERROR
			return

		try:
			self.priority = int(self.config.get('priority'))
		except:
			L.error("Invalid priority option '{0}' in {1} -> CFGERROR".format(self.config['priority'], config_section))
			self.state = program_state_enum.CFGERROR
			return		
		
		try:
			dis = get_boolean(self.config.get('disabled'))
		except ValueError:
			L.error("Unknown 'disabled' option '{0}' in {1} -> CFGERROR".format(dis, config_section))
			self.state = program_state_enum.CFGERROR
			return<commit_msg>Fixing reported error<commit_after>import sys, os, time, logging, shlex, signal, subprocess, errno
import pyev
from ..config import config, get_boolean, get_env
from ..utils import parse_signals, expandvars, enable_nonblocking, disable_nonblocking, get_python_exec, get_signal_name
from ..cnscom import program_state_enum, svrcall_error
from .logmed import log_mediator
from .singleton import get_svrapp


if sys.platform == 'win32':
	import msvcrt
	import win32file, win32pipe, pywintypes, winerror # from Python Win32
#

try:
	import resource
except ImportError:
	resource = None

#

L = logging.getLogger("program")
Lmy = logging.getLogger("my") # Message yielding logger

#

class program(object):

	DEFAULTS = {
		'command': None,
		'directory': None,
		'umask': None,
		'starttimeout': 0.5,
		'stoptimeout': 3,
		'killby': 'TERM,INT,TERM,INT,TERM,INT,KILL',
		'stdin': '<null>', # TODO: This can be very probably removed as there is no reasonable use
		'stdout': '<stderr>',
		'stderr': '<logdir>',
		'priority': 100,
		'disabled': False,
		'coredump': False,
		'autorestart': False,
		'processgroup': True,
		'logscan_stdout': '',
		'logscan_stderr': '',
		'notify_fatal': '<global>',
	}

	def __init__(self, svrapp, config_section):
		_, self.ident = config_section.split(':', 2)
		self.state = program_state_enum.STOPPED
		self.subproc = None

		self.launch_cnt = 0
		self.autorestart_cnt = 0
		self.start_time = None
		self.stop_time = None
		self.exit_time = None
		self.exit_status = None
		self.coredump_enabled = None # If true, kill by SIGQUIT -> dump core

		if sys.platform != 'win32':
			# On Windows we are using periodic pipe check in win32_read_stdfd
			self.watchers = [
				pyev.Io(0, 0, svrapp.loop, self.__read_stdfd, 0),
				pyev.Io(0, 0, svrapp.loop, self.__read_stdfd, 1),
			]

		# Build configuration
		self.config = self.DEFAULTS.copy()
		self.config.update(config.items(config_section))

		# Prepare program command line
		cmd = self.config.get('command')
		if cmd is None:
			L.error("Missing command option in {0} -> CFGERROR".format(config_section))
			self.state = program_state_enum.CFGERROR
			return

		if cmd == '<httpfend>':
			cmd = get_python_exec(cmdline=["-u","-m","ramona.httpfend"])
		elif cmd[:1] == '<':
			L.error("Unknown command option '{1}' in {0} -> CFGERROR".format(config_section, cmd))
			self.state = program_state_enum.CFGERROR
			return

		cmd = cmd.replace('\\', '\\\\')
		self.cmdline = shlex.split(cmd)

		# Prepare stop signals
		if sys.platform != 'win32':
			self.stopsignals = parse_signals(self.config['killby'])
			if len(self.stopsignals) == 0: self.stopsignals = [signal.SIGTERM]
			self.act_stopsignals = None

		if self.config['stdin'] != '<null>':
			L.error("Unknown stdin option '{0}' in {1} -> CFGERROR".format(self.config['stdin'], config_section))
			self.state = program_state_enum.CFGERROR
			return

		try:
			self.priority = int(self.config.get('priority'))
		except:
			L.error("Invalid priority option '{0}' in {1} -> CFGERROR".format(self.config['priority'], config_section))
			self.state = program_state_enum.CFGERROR
			return		
		
		try:
			dis = get_boolean(self.config.get('disabled'))
		except ValueError:
			L.error("Unknown/invalid 'disabled' option '{0}' in {1} -> CFGERROR".format(self.config.get('disabled'), config_section))
			self.state = program_state_enum.CFGERROR
			return
{'fraction_non_alphanumeric': 0.1428358653559726, 'fraction_numerical': 0.011915400655347037, 'mean_word_length': 4.295741324921136, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '628851', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2331, 'n_words': 553}