<commit_before>r"""Represents a ShutIt background command.

    - options to:
        - cancel command
        - get status (running, suspended etc)
        - check_timeout
"""

from __future__ import print_function
import sys
import time
import logging
import traceback
import shutit_global
import shutit_util


class ShutItBackgroundCommand(object):
	"""Background command in ShutIt
	"""
	def __init__(self,
	             sendspec):
		# Stub this with a simple command for now
		self.sendspec               = sendspec
		self.block_other_commands   = sendspec.block_other_commands
		self.retry                  = sendspec.retry
		self.tries                  = 0
		self.pid                    = None
		self.return_value           = None
		self.start_time             = None
		self.run_state              = 'N' # State as per ps man page, but 'C' == Complete, 'N' == not started, 'F' == failed, 'S' == sleeping/running, 'T' == timed out by ShutIt
		self.cwd                    = self.sendspec.shutit_pexpect_child.send_and_get_output(' command pwd', ignore_background=True)
		self.id                     = shutit_util.random_id()
		self.output_file            = '/tmp/shutit_background_' + self.id + '_output.log'
		self.exit_code_file         = '/tmp/shutit_background_' + self.id + '_exit_code_file.log'
		self.command_file           = '/tmp/shutit_background_' + self.id + '_command.log'
		if self.sendspec.run_in_background:
			self.sendspec.send          = ' set +m && { : $(echo "' + self.sendspec.original_send + '" >' + self.command_file + ' && command cd "' + self.cwd + '">' + self.output_file + ' && ' + self.sendspec.send + ' >>' + self.output_file + ' 2>&1; echo $? >' + self.exit_code_file + ') & } 2>/dev/null'


	def __str__(self):
		string = str(self.sendspec)
		string += '\n---- Background object BEGIN ----'
		string += '\nblock_other_commands: ' + str(self.block_other_commands)
		string += '\ncwd:                  ' + str(self.cwd)
		string += '\npid:                  ' + str(self.pid)
		string += '\nretry:                ' + str(self.retry)
		string += '\nreturn_value:         ' + str(self.return_value)
		string += '\nrun_state:            ' + str(self.run_state)
		string += '\nstart_time:           ' + str(self.start_time)
		string += '\ntries:                ' + str(self.tries)
		string += '\n----                   END   ----'
		return string


	def run_background_command(self):
		# reset object
		self.pid              = None
		self.return_value     = None
		self.run_state        = 'N'
		self.start_time = time.time() # record start time

		# run command
		self.tries            += 1
		if self.sendspec.run_in_background:
			# Required to reset terminal before a background send. (TODO: why?)
			self.sendspec.shutit_pexpect_child.reset_terminal()
			# Run in the background
			self.sendspec.shutit_pexpect_child.quick_send(self.sendspec.send)
			# Put into an 'S' state as that means 'running'
			self.run_state        = 'S'
			# Required to reset terminal after a background send. (TODO: why?)
			self.sendspec.shutit_pexpect_child.reset_terminal()
			# record pid
			self.pid = self.sendspec.shutit_pexpect_child.send_and_get_output(" echo ${!}",ignore_background=True)
		else:
			# Run synchronously and mark complete
			# We need to set this to ignore background before we run it, so that
			# it does not block itself and end up in an infinite loop.
			self.sendspec.ignore_background = True
			self.sendspec.shutit_pexpect_child.send(self.sendspec)
			self.run_state = 'C'

		self.sendspec.started = True

		assert self.run_state in ('C','S','F')
		return True


	def check_background_command_state(self):<commit_msg>try not resetting<commit_after>r"""Represents a ShutIt background command.

    - options to:
        - cancel command
        - get status (running, suspended etc)
        - check_timeout
"""

from __future__ import print_function
import sys
import time
import logging
import traceback
import shutit_global
import shutit_util


class ShutItBackgroundCommand(object):
	"""Background command in ShutIt
	"""
	def __init__(self,
	             sendspec):
		# Stub this with a simple command for now
		self.sendspec               = sendspec
		self.block_other_commands   = sendspec.block_other_commands
		self.retry                  = sendspec.retry
		self.tries                  = 0
		self.pid                    = None
		self.return_value           = None
		self.start_time             = None
		self.run_state              = 'N' # State as per ps man page, but 'C' == Complete, 'N' == not started, 'F' == failed, 'S' == sleeping/running, 'T' == timed out by ShutIt
		self.cwd                    = self.sendspec.shutit_pexpect_child.send_and_get_output(' command pwd', ignore_background=True)
		self.id                     = shutit_util.random_id()
		self.output_file            = '/tmp/shutit_background_' + self.id + '_output.log'
		self.exit_code_file         = '/tmp/shutit_background_' + self.id + '_exit_code_file.log'
		self.command_file           = '/tmp/shutit_background_' + self.id + '_command.log'
		if self.sendspec.run_in_background:
			self.sendspec.send          = ' set +m && { : $(echo "' + self.sendspec.original_send + '" >' + self.command_file + ' && command cd "' + self.cwd + '">' + self.output_file + ' && ' + self.sendspec.send + ' >>' + self.output_file + ' 2>&1; echo $? >' + self.exit_code_file + ') & } 2>/dev/null'


	def __str__(self):
		string = str(self.sendspec)
		string += '\n---- Background object BEGIN ----'
		string += '\nblock_other_commands: ' + str(self.block_other_commands)
		string += '\ncwd:                  ' + str(self.cwd)
		string += '\npid:                  ' + str(self.pid)
		string += '\nretry:                ' + str(self.retry)
		string += '\nreturn_value:         ' + str(self.return_value)
		string += '\nrun_state:            ' + str(self.run_state)
		string += '\nstart_time:           ' + str(self.start_time)
		string += '\ntries:                ' + str(self.tries)
		string += '\n----                   END   ----'
		return string


	def run_background_command(self):
		# reset object
		self.pid              = None
		self.return_value     = None
		self.run_state        = 'N'
		self.start_time = time.time() # record start time

		# run command
		self.tries            += 1
		if self.sendspec.run_in_background:
			## Required to reset terminal before a background send. (TODO: why?)
			#self.sendspec.shutit_pexpect_child.reset_terminal()
			# Run in the background
			self.sendspec.shutit_pexpect_child.quick_send(self.sendspec.send)
			# Put into an 'S' state as that means 'running'
			self.run_state        = 'S'
			## Required to reset terminal after a background send. (TODO: why?)
			#self.sendspec.shutit_pexpect_child.reset_terminal()
			# record pid
			self.pid = self.sendspec.shutit_pexpect_child.send_and_get_output(" echo ${!}",ignore_background=True)
		else:
			# Run synchronously and mark complete
			# We need to set this to ignore background before we run it, so that
			# it does not block itself and end up in an infinite loop.
			self.sendspec.ignore_background = True
			self.sendspec.shutit_pexpect_child.send(self.sendspec)
			self.run_state = 'C'

		self.sendspec.started = True

		assert self.run_state in ('C','S','F')
		return True


	def check_background_command_state(self):
{'fraction_non_alphanumeric': 0.1195414847161572, 'fraction_numerical': 0.0013646288209606986, 'mean_word_length': 2.5184829572731635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '1638470', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2327, 'n_words': 545}