<s> import time <EOL> import os <EOL> from datetime import datetime <EOL> from flask import url_for , current_app <EOL> from . . extensions import db <EOL> from . . tasks . models import Task <EOL> from . constants import STATUSES , BOOTSTRAP_ALERTS <EOL> deployments_tasks_table = db . Table ( '<STR_LIT>' , db . Model . metadata , <EOL> db . Column ( '<STR_LIT>' , db . Integer , <EOL> db . ForeignKey ( '<STR_LIT>' ) ) , <EOL> db . Column ( '<STR_LIT>' , db . Integer , <EOL> db . ForeignKey ( '<STR_LIT>' ) ) ) <EOL> class Deployment ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> status = db . Column ( db . SmallInteger , default = STATUSES [ '<STR_LIT>' ] ) <EOL> branch = db . Column ( db . String ( <NUM_LIT:32> ) , default = '<STR_LIT>' ) <EOL> commit = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> started_at = db . Column ( db . DateTime ( ) , default = datetime . now ) <EOL> finished_at = db . Column ( db . DateTime ( ) ) <EOL> code = db . Column ( db . Text ( ) ) <EOL> log = db . Column ( db . Text ( ) ) <EOL> stage_id = db . Column ( db . Integer ( ) , <EOL> db . ForeignKey ( '<STR_LIT>' ) , nullable = False ) <EOL> user_id = db . Column ( db . Integer ( ) , <EOL> db . ForeignKey ( '<STR_LIT>' ) , nullable = False ) <EOL> tasks = db . relationship ( Task , <EOL> secondary = deployments_tasks_table , <EOL> backref = "<STR_LIT>" ) <EOL> def get_tmp_path ( self ) : <EOL> return os . path . join ( current_app . config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' . format ( self . id ) ) <EOL> def bootstrap_status ( self ) : <EOL> return BOOTSTRAP_ALERTS [ self . status ] <EOL> def show_status ( self ) : <EOL> for status , number in STATUSES . iteritems ( ) : <EOL> if number == self . status : <EOL> return status <EOL> def is_running ( self ) : <EOL> return self . status == STATUSES [ '<STR_LIT>' ] <EOL> def show_tasks_list ( self ) : <EOL> template = '<STR_LIT>' <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ template . format ( url_for ( '<STR_LIT>' , id = task . id ) , <EOL> task . name ) for task in self . tasks ] ) <EOL> def get_log_path ( self ) : <EOL> return os . path . join ( self . get_tmp_path ( ) , '<STR_LIT>' ) <EOL> def get_log_lines ( self ) : <EOL> if self . log : <EOL> return self . log . split ( '<STR_LIT:\n>' ) <EOL> path = os . path . join ( self . get_tmp_path ( ) , '<STR_LIT>' ) <EOL> if os . path . exists ( path ) : <EOL> return open ( path ) . readlines ( ) <EOL> return [ ] <EOL> def show_duration ( self ) : <EOL> delta = self . finished_at - self . started_at <EOL> return time . strftime ( "<STR_LIT>" , time . gmtime ( delta . seconds ) ) <EOL> def show_commit ( self ) : <EOL> return "<STR_LIT>" . format ( self . commit [ : <NUM_LIT:10> ] ) if self . commit else '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Deployment , self ) . __init__ ( * args , ** kwargs ) <EOL> self . code = [ self . stage . project . code , self . stage . code ] <EOL> for task in self . stage . tasks : <EOL> self . code . append ( task . code ) <EOL> self . code = '<STR_LIT:\n>' . join ( self . code ) </s>
