import subprocess
import operator
import sys

from log import Log
from colors import Colors

def run_cmd_ret_output(args, formatter):
  cmd = ""
  for a in args:
    cmd += "%s " % a

  formatter.start()

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

  while True:
    line = p.stdout.readline()
    if line != '':
      try:
        formatter.put_line(line)
      except RuntimeError as e:
        p.terminate()
        raise e
    else:
      break
  p.wait()
  formatter.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(commands, formatter):
  formatter.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))

  while True:
    line = proc_list[-1][0].stdout.readline()
    if line:
      Log.print_msg("Stdout", line, Colors.MAGENTA_FG)
    else:
      break

  for (p, err_codes) in proc_list:
    p.wait()
    if p.returncode not in err_codes:
      while True:
        line = p.stderr.readline()
        if line:
          Log.print_msg("Stderr", line, Colors.RED_FG)
        else:
          break
      return p.returncode

  return 0


@@ -37,16 +37,69 @@
 port sys
+%0Afrom time import sleep%0Afrom datetime import datetime
 %0A%0Afrom l
@@ -230,16 +230,34 @@
  %22 %25 a%0A%0A
+  if formatter:%0A  
   format
@@ -349,16 +349,34 @@
 TDOUT)%0A%0A
+  if formatter:%0A  
   while 
@@ -373,32 +373,34 @@
     while True:%0A
+  
     line = p.std
@@ -414,24 +414,26 @@
 dline()%0A    
+ 
+ 
 if line != '
@@ -445,13 +445,17 @@
     
+  
 try:%0A
+  
     
@@ -489,16 +489,18 @@
 )%0A      
+ 
+ 
 except R
@@ -525,16 +525,18 @@
         
+  
 p.termin
@@ -553,16 +553,18 @@
     
+ 
+ 
 raise e%0A
     
@@ -559,16 +559,18 @@
 raise e%0A
+  
     else
@@ -573,24 +573,26 @@
 else:%0A      
+  
 break%0A  p.wa
@@ -596,16 +596,34 @@
 .wait()%0A
+  if formatter:%0A  
   format
@@ -862,19 +862,44 @@
 ormatter
-):%0A
+ = None):%0A  if formatter:%0A  
   format

Add ability to command with formatter = None