#!/usr/bin/python
import os
import subprocess
import sys
import time

START_DELAY=1.0

def grab(argv):
  p = subprocess.Popen(argv, stdout=subprocess.PIPE)
  out = p.stdout.read()
  p.wait()
  return out

def main():
  start = time.time()
  sys.stderr.write('running: %r\n' % sys.argv[1:])
  p = subprocess.Popen(sys.argv[1:], stdout=os.dup(2))
  rv = None
  amounts = []
  while rv is None:
    out = grab(['ps', '-o', 'rss=', str(p.pid)])
    amount = int(out)
    if amount != '0':
      amounts.append(amount)
      sys.stderr.write(' %d kb\n' % amount)
    time.sleep(0.1)
    rv = p.poll()
  end = time.time()
  elapsed = '%.1f' % (end-start-START_DELAY)
  sys.stderr.write('elapsed: %s\n' % elapsed)
  print ','.join([' '.join(sys.argv[1:]),
                  elapsed,
                  str(max(amounts)),
                  ''] + [str(i) for i in amounts])
  return rv

rv = main()
sys.stderr.write('exit code: %d\n' % rv)
sys.exit(rv)
