<commit_before>import os.path
import re
import string
import subprocess
import sys

commands = ["attach", "start", "status", "stop"]

#########################
### UTILITY FUNCTIONS ###
#########################
def chdir(dir):
  os.chdir(dir)
  print "Executing chdir to %s" % dir
  
def execCmd(cmd):  
  print "Executing command: %s" % sanitizeString(cmd)

  # Use Popen instead of subprocess.check_output as latter only exists in Python 2.7 onwards
  # output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)  
  output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
  
  # print "For output got: %s" % output
  return output

def findScreen(screenName):
  screenList = ""
  
  try:
    screenList = getScreenList()
  except subprocess.CalledProcessError as cpe:
    screenList = cpe.output
    
  #print "screenList: %s" % screenList
  # TODO: Need to improve this for screens with ( in their name
  return re.search("\s+(\d+\.%s)\s+\(" % screenName, screenList)
  
def getScreenList():
  return execCmd("screen -list")

def sanitizeString(input):
  return input.replace("\r", r"\r")

############
### MAIN ###
############
class osimctrl:
  def __init__(self, binaryPath, screenPath, componentName, screenName):
    self.binaryPath = binaryPath
    self.screenPath = screenPath
    self.componentName = componentName
    self.screenName = screenName
    
  def execCommand(self, command):
    if command == "attach":
      self.attachToComponent()
    elif command == "status":
      self.getComponentStatus()
    elif command == "start":
      self.startComponent()
    elif command == "stop":
      self.stopComponent()
    else:
      print "Command %s not recognized" % command        
        
  def attachToComponent(self):
    screen = findScreen(self.screenName)
    
    if screen == None:
      print "Did not find screen named %s for attach" % self.screenName
    else:
      print "Found screen %s" % screen.group(1) 
      execCmd("%s -x %s" % (self.screenPath, self.screenName))
    
  def getComponentStatus(self):
      
    # We'll check screen even if we found PID so that we can get screen information        
    screen = findScreen(self.screenName)
      
    if screen == None:
      print "Did not find screen named %s" % self.screenName
    else:
      print "Found screen %s" % screen.group(1)      
      
    print "OpenSimulator path: %s" % self.binaryPath
    
    if screen != None:
      print "Status: Running"
    else:
      print "Status: Stopped"
    
  def startComponent(self):
    screen = findScreen(self.screenName)
    
    if screen != None:
      print >> sys.stderr, "Screen session %s already started." % (screen.group(1))
      sys.exit(1)
      
    chdir(self.binaryPath)
    
    execCmd("%s -S %s -d -m mono --debug %s.exe" % (self.screenPath, self.screenName, self.componentName))
    
    screen = findScreen(self.screenName)
    if screen != None:
      print "%s starting in screen instance %s" % (self.componentName, screen.group(1))
    else:
      print >> sys.stderr, "ERROR: %s did not start." % self.componentName
      exit(1)
  
    execCmd("%s -x %s" % (self.screenPath, self.screenName))
    
  def stopComponent(self):
    screen = findScreen(self.screenName)
    
    if screen == None:
      print >> sys.stderr, "No screen session named %s to stop" % self.screenName
      sys.exit(1)
      
    execCmd("%s -S %s -p 0 -X stuff quit$(printf \r)" % (self.screenPath, self.screenName))<commit_msg>Implementing polling for stop command so that we don't exist until the screen instance has disappeared<commit_after>import os.path
import re
import string
import subprocess
import sys
import time

commands = ["attach", "start", "status", "stop"]

#########################
### UTILITY FUNCTIONS ###
#########################
def chdir(dir):
  os.chdir(dir)
  print "Executing chdir to %s" % dir
  
def execCmd(cmd):  
  print "Executing command: %s" % sanitizeString(cmd)

  # Use Popen instead of subprocess.check_output as latter only exists in Python 2.7 onwards
  # output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)  
  output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
  
  # print "For output got: %s" % output
  return output

def findScreen(screenName):
  screenList = ""
  
  try:
    screenList = getScreenList()
  except subprocess.CalledProcessError as cpe:
    screenList = cpe.output
    
  #print "screenList: %s" % screenList
  # TODO: Need to improve this for screens with ( in their name
  return re.search("\s+(\d+\.%s)\s+\(" % screenName, screenList)
  
def getScreenList():
  return execCmd("screen -list")

def sanitizeString(input):
  return input.replace("\r", r"\r")

############
### MAIN ###
############
class osimctrl:
  @property
  def pollingTimeMax(self):
    return self._pollingTimeMax
  
  @pollingTimeMax.setter
  def pollingTimeMax(self, value):
    if value < 0:
      raise "Max polling time %s invalid as one cannot set a value less than 0" % value
    
    self._pollingTimeMax = value
  
  def __init__(self, binaryPath, screenPath, componentName, screenName):
    self._binaryPath = binaryPath
    self._screenPath = screenPath
    self._componentName = componentName
    self._screenName = screenName
    self._pollingTimeMax = 300
    self._pollingInterval = 1
    self._pollingNotificationInterval = 5
        
  def execCommand(self, command):
    if command == "attach":
      self.attachToComponent()
    elif command == "status":
      self.getComponentStatus()
    elif command == "start":
      self.startComponent()
    elif command == "stop":
      self.stopComponent()
    else:
      print "Command %s not recognized" % command        
        
  def attachToComponent(self):
    screen = findScreen(self._screenName)
    
    if screen == None:
      print "Did not find screen named %s for attach" % self._screenName
    else:
      print "Found screen %s" % screen.group(1) 
      execCmd("%s -x %s" % (self._screenPath, self._screenName))
    
  def getComponentStatus(self):
      
    # We'll check screen even if we found PID so that we can get screen information        
    screen = findScreen(self._screenName)
      
    if screen == None:
      print "Did not find screen named %s" % self._screenName
    else:
      print "Found screen %s" % screen.group(1)      
      
    print "OpenSimulator path: %s" % self._binaryPath
    
    if screen != None:
      print "Status: Running"
    else:
      print "Status: Stopped"
    
  def startComponent(self):
    screen = findScreen(self._screenName)
    
    if screen != None:
      print >> sys.stderr, "Screen session %s already started." % (screen.group(1))
      sys.exit(1)
      
    chdir(self._binaryPath)
    
    execCmd("%s -S %s -d -m mono --debug %s.exe" % (self._screenPath, self._screenName, self._componentName))
    
    screen = findScreen(self._screenName)
    if screen != None:
      print "%s starting in screen instance %s" % (self._componentName, screen.group(1))
    else:
      print >> sys.stderr, "ERROR: %s did not start." % self._componentName
      exit(1)
  
    execCmd("%s -x %s" % (self._screenPath, self._screenName))
    
  def stopComponent(self):
    screen = findScreen(self._screenName)
    
    if screen == None:
      print >> sys.stderr, "No screen session named %s to stop" % self._screenName
      sys.exit(1)
      
    execCmd("%s -S %s -p 0 -X stuff quit$(printf \r)" % (self._screenPath, self._screenName))
    
    timeElapsed = 0
    
    while timeElapsed < self._pollingTimeMax:        
      time.sleep(self._pollingInterval)
      timeElapsed += self._pollingInterval      
      
      screen = findScreen(self._screenName)
      
      if screen == None:
        print "Screen instance %s terminated." % self._screenName
        return
      
      if timeElapsed % self._pollingNotificationInterval == 0:
        print "Waited %s seconds for screen named %s to terminate" % (timeElapsed, self._screenName)
        
    print >> sys.stderr, "Screen %s has not terminated after %s seconds.  Please investigate." % (self._screenName, self._pollingTimeMax)

{'fraction_non_alphanumeric': 0.12979779634338298, 'fraction_numerical': 0.0037534810509746944, 'mean_word_length': 2.97497593840231, 'pattern_counts': {'":': 8, '<': 5, '<?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': '253051', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2508, 'n_words': 727}