<commit_before>import subprocess
import operator
import sys
from time import sleep
from datetime import datetime

from command_output_pipe_base import CommandOutputPipeBase

class Commander:

  def __init__(self, command_output_pipe = None):
    self.command_output_pipe = command_output_pipe

  def run_command(self, args):
    if self.command_output_pipe:
      self.command_output_pipe.start()

    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    if self.command_output_pipe:
      while True:
        line = p.stdout.readline()
        if line != '':
          try:
            self.command_output_pipe.put_line(line)
          except RuntimeError as e:
            p.terminate()
            raise e
        else:
          break
    p.wait()
    if self.command_output_pipe:
      self.command_output_pipe.stop()
    return p.returncode

  """ commands is an array of touples with command+args and array with acceptable
  error codes. An example: [(["echo", "Hello World"], []), (["exit", "1"], [1])]"""
  def run_chained_commands(self, commands):
    if self.command_output_pipe:
      self.command_output_pipe.start()
    proc_list = []
    for (args, err_codes) in commands:
      if 0 not in err_codes:
        err_codes.append(0)

      if not proc_list:
        p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      else:
        p = subprocess.Popen(args, stdin=proc_list[-1][0].stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        proc_list[-1][0].stdout.close()
      proc_list.append((p, err_codes))

    if self.command_output_pipe:
      while True:<commit_msg>Print the command when in debug mode<commit_after>import subprocess
import operator
import sys
from time import sleep
from datetime import datetime

from command_output_pipe_base import CommandOutputPipeBase

class Commander:

  def __init__(self, command_output_pipe = None, debug = False):
    self.command_output_pipe = command_output_pipe
    self.debug = debug

  def __debug_command(self, args):
    if self.debug:
      self.command_output_pipe.put_line("running command: {}\n".format(" ".join(args)));

  def run_command(self, args):
    if self.command_output_pipe:
      self.command_output_pipe.start()

    self.__debug_command(args)

    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    if self.command_output_pipe:
      while True:
        line = p.stdout.readline()
        if line != '':
          try:
            self.command_output_pipe.put_line(line)
          except RuntimeError as e:
            p.terminate()
            raise e
        else:
          break
    p.wait()
    if self.command_output_pipe:
      self.command_output_pipe.stop()
    return p.returncode

  """ commands is an array of touples with command+args and array with acceptable
  error codes. An example: [(["echo", "Hello World"], []), (["exit", "1"], [1])]"""
  def run_chained_commands(self, commands):
    if self.command_output_pipe:
      self.command_output_pipe.start()
    proc_list = []
    for (args, err_codes) in commands:
      if 0 not in err_codes:
        err_codes.append(0)

      self.__debug_command(args)

      if not proc_list:
        p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      else:
        p = subprocess.Popen(args, stdin=proc_list[-1][0].stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        proc_list[-1][0].stdout.close()
      proc_list.append((p, err_codes))

    if self.command_output_pipe:
      while True:
{'fraction_non_alphanumeric': 0.1044525343041165, 'fraction_numerical': 0.004480537664519743, 'mean_word_length': 3.077625570776256, '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': '5578165', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1112, 'n_words': 262}