# Path: journeyman/builds/models.py
# class Build(models.Model):
#     project = models.ForeignKey(Project)
#     node = models.ForeignKey(BuildNode)
#     revision = models.CharField(max_length=255, blank=True)
#     short_revision = models.CharField(max_length=255, blank=True)
# 
#     started = models.DateTimeField(null=True, blank=True)
#     finished = models.DateTimeField(null=True, blank=True)
# 
#     state = models.CharField(max_length=20, default=BuildState.UNKNOWN,
#         choices=BuildState.choices())
# 
#     def state_css_class(self):
#         # Determine the correct css class
#         if self.state == BuildState.STABLE:
#             return 'success'
#         elif self.state in [BuildState.QUEUED, BuildState.RUNNING]:
#             return 'notice'
#         else: 
#             return 'error'
# 
#     def __unicode__(self):
#         return u'%s/%s/%s (%s)' % (self.project, self.node, self.state,
#             self.started)
# 
#     def queue_build(self):
#         # Create a celery task and set the state to QUEUED
#         BuildTask.delay(self.pk)
#         self.state = BuildState.QUEUED
#         self.save()
# 
#     @property
#     def build_steps(self):
#         return self.buildstep_set.all().order_by('started')
# 
# class BuildState(Options):
#     QUEUED = 'queued'
#     RUNNING = 'running'
#     FAILED = 'failed'
#     UNSTABLE = 'unstable'
#     STABLE = 'stable'
#     UNKNOWN = 'unknown'
# 
# Path: journeyman/buildrunner/registry.py
# LOADING = False
#     LOADING = True
#     LOADING = False
# class StepAlreadyRegistered(Exception):
# class StepNotFound(Exception):
# class Registry(object):
#     def add_step(self, name, func):
#     def list_steps(self):
#     def get_step(self, name):
#         def get_inner_step():
# def autodiscover():


import sys, tempfile, traceback
from StringIO import StringIO
from datetime import datetime
from fabric.state import env
from fabric.network import disconnect_all
from django.utils import simplejson as json
from journeyman.builds.models import Build, BuildState
from journeyman.buildrunner.registry import registry, StepNotFound




class InvalidBuildException(Exception):
    pass

class BuildRunner(object):
    worker_ssh_key = None
    config = None

    build = None
    build_ve_name = None
    build_ve_path = None
    build_src = None

    repo_head_id = None
    repo_head_short_id = None

    def __init__(self, build):
        # We accept Build objects and Build ids
        if type(build) == int:
            try:
                self.build = Build.objects.get(pk=build)

# Path: journeyman/builds/models.py
# class Build(models.Model):
#     project = models.ForeignKey(Project)
#     node = models.ForeignKey(BuildNode)
#     revision = models.CharField(max_length=255, blank=True)
#     short_revision = models.CharField(max_length=255, blank=True)
# 
#     started = models.DateTimeField(null=True, blank=True)
#     finished = models.DateTimeField(null=True, blank=True)
# 
#     state = models.CharField(max_length=20, default=BuildState.UNKNOWN,
#         choices=BuildState.choices())
# 
#     def state_css_class(self):
#         # Determine the correct css class
#         if self.state == BuildState.STABLE:
#             return 'success'
#         elif self.state in [BuildState.QUEUED, BuildState.RUNNING]:
#             return 'notice'
#         else: 
#             return 'error'
# 
#     def __unicode__(self):
#         return u'%s/%s/%s (%s)' % (self.project, self.node, self.state,
#             self.started)
# 
#     def queue_build(self):
#         # Create a celery task and set the state to QUEUED
#         BuildTask.delay(self.pk)
#         self.state = BuildState.QUEUED
#         self.save()
# 
#     @property
#     def build_steps(self):
#         return self.buildstep_set.all().order_by('started')
# 
# class BuildState(Options):
#     QUEUED = 'queued'
#     RUNNING = 'running'
#     FAILED = 'failed'
#     UNSTABLE = 'unstable'
#     STABLE = 'stable'
#     UNKNOWN = 'unknown'
# 
# Path: journeyman/buildrunner/registry.py
# LOADING = False
#     LOADING = True
#     LOADING = False
# class StepAlreadyRegistered(Exception):
# class StepNotFound(Exception):
# class Registry(object):
#     def add_step(self, name, func):
#     def list_steps(self):
#     def get_step(self, name):
#         def get_inner_step():
# def autodiscover():


# Path: journeyman/buildrunner/base.py
import sys, tempfile, traceback
from StringIO import StringIO
from datetime import datetime
from fabric.state import env
from fabric.network import disconnect_all
from django.utils import simplejson as json
from journeyman.builds.models import Build, BuildState
from journeyman.buildrunner.registry import registry, StepNotFound





class InvalidBuildException(Exception):
    pass

class BuildRunner(object):
    worker_ssh_key = None
    config = None

    build = None
    build_ve_name = None
    build_ve_path = None
    build_src = None

    repo_head_id = None
    repo_head_short_id = None

    def __init__(self, build):
        # We accept Build objects and Build ids
        if type(build) == int:
            try:
                self.build = Build.objects.get(pk=build)

            except Build.DoesNotExist: