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

package_name_re_string = "setup\(name=\'(?P<name>[\d\w]+).+"
package_name_re = re.compile(package_name_re_string)
package_name = None

if package_name is None:
    with file('setup.py') as setup:
        pre = package_name_re
        for line in setup:
            match = pre.match(line.strip())
            if match:
                package_name = match.groupdict()['name']
                break
    if package_name is None:
        raise RuntimeError, "Unable to determine package name, please set it in this file"

print "Package is", package_name

javascript_directory = os.path.join(package_name, 'static/js/')

coffee_cmd = ['coffee', '-wc', '-o', javascript_directory, 'coffee/']
compass_cmd = ['compass', 'watch']

coffee_proc = subprocess.Popen(coffee_cmd)
compass_proc = subprocess.Popen(compass_cmd)

while not (coffee_proc.poll() or compass_proc.poll()):
    time.sleep(5)

# check which one stopped
coffee_ret = coffee_proc.returncode
compass_ret = compass_proc.returncode

if coffee_ret is None:
    print "Stopping coffee"
    coffee_proc.terminate()
else:
    print "coffee returned", coffee_ret


if compass_ret is None:
    print "Stopping compass"
    compass_proc.terminate()
else:
    print "compass returned", compass_ret


