from celery import task
import os
import sys
import subprocess

@task()
def build(build, giturl, branch):
    # Use subprocess to execute a build, update the db with results
    local = os.path.dirname(sys.argv[0])
    buildpack = os.path.join(local, 'bin/build_package')
    deployfile = build.project.deploy_file

    print "Executing build %s %s" % (giturl, branch)

    args = [buildpack, '--branch', branch]

    if build.project.deploy_file:
        args.extend(['--deploy-file', build.project.deploy_file])

    if build.project.release_stream:
        args.extend(['--push', build.project.release_stream.push_command])

    args.append(giturl)

    builder = subprocess.Popen(args, 
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=local)
    builder.wait()

    build.log = builder.stdout.read()

    if builder.returncode != 0:
        build.state = 2
    else:
        build.state = 1
    
    build.save()

@@ -682,17 +682,16 @@
 en(args,
- 
 %0A       
@@ -760,24 +760,55 @@
     
-builder.wait
+stdoutdata, stderrdata = builder.communicate
 ()%0A
-%0A
     
@@ -823,29 +823,18 @@
 g = 
-builder.stdout.read()
+stdoutdata
 %0A%0A  
@@ -921,20 +921,16 @@
 ate = 1%0A
-    
 %0A    bui

Use communicate so process out does not deadlock