<s> from __future__ import absolute_import <EOL> import logging <EOL> from datetime import datetime , timedelta <EOL> from flask import current_app <EOL> from changes . artifacts . coverage import CoverageHandler <EOL> from changes . artifacts . xunit import XunitHandler <EOL> from changes . backends . base import UnrecoverableException <EOL> from changes . buildsteps . base import BuildStep <EOL> from changes . config import db <EOL> from changes . constants import Status <EOL> from . builder import JenkinsBuilder <EOL> from . generic_builder import JenkinsGenericBuilder <EOL> class JenkinsBuildStep ( BuildStep ) : <EOL> builder_cls = JenkinsBuilder <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def __init__ ( self , job_name = None , jenkins_url = None , jenkins_diff_url = None , <EOL> auth_keyname = None , verify = True , cluster = None , debug_config = None , <EOL> cpus = <NUM_LIT:4> , memory = <NUM_LIT:8> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if job_name is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if any ( int_field and type ( int_field ) != int for int_field in ( cpus , memory ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not isinstance ( jenkins_url , ( list , tuple ) ) : <EOL> if jenkins_url : <EOL> jenkins_url = [ jenkins_url ] <EOL> else : <EOL> jenkins_url = [ ] <EOL> if not isinstance ( jenkins_diff_url , ( list , tuple ) ) : <EOL> if jenkins_diff_url : <EOL> jenkins_diff_url = [ jenkins_diff_url ] <EOL> else : <EOL> jenkins_diff_url = [ ] <EOL> self . job_name = job_name <EOL> self . jenkins_urls = jenkins_url <EOL> self . jenkins_diff_urls = jenkins_diff_url <EOL> self . auth_keyname = auth_keyname <EOL> self . verify = verify <EOL> self . cluster = cluster <EOL> self . debug_config = debug_config or { } <EOL> self . _resources = { } <EOL> if cpus : <EOL> self . _resources [ '<STR_LIT>' ] = cpus <EOL> if memory : <EOL> self . _resources [ '<STR_LIT>' ] = memory <EOL> def get_builder ( self , app = current_app , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> args = self . get_builder_options ( ) . copy ( ) <EOL> args . update ( kwargs ) <EOL> return self . builder_cls ( app = app , ** args ) <EOL> def get_builder_options ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : self . jenkins_urls , <EOL> '<STR_LIT>' : self . jenkins_diff_urls , <EOL> '<STR_LIT>' : self . job_name , <EOL> '<STR_LIT>' : self . verify , <EOL> '<STR_LIT>' : self . auth_keyname , <EOL> '<STR_LIT>' : self . cluster , <EOL> '<STR_LIT>' : self . debug_config , <EOL> } <EOL> def get_label ( self ) : <EOL> return '<STR_LIT>' . format ( self . job_name ) <EOL> def execute ( self , job ) : <EOL> """<STR_LIT>""" <EOL> builder = self . get_builder ( ) <EOL> builder . create_job ( job ) <EOL> def update ( self , job ) : <EOL> builder = self . get_builder ( ) <EOL> builder . sync_job ( job ) <EOL> def update_step ( self , step ) : <EOL> builder = self . get_builder ( ) <EOL> try : <EOL> builder . sync_step ( step ) <EOL> except UnrecoverableException : <EOL> if step . date_created < datetime . utcnow ( ) - timedelta ( minutes = <NUM_LIT:5> ) : <EOL> return <EOL> raise <EOL> def cancel_step ( self , step ) : <EOL> builder = self . get_builder ( ) <EOL> builder . cancel_step ( step ) <EOL> step . status = Status . finished <EOL> step . date_finished = datetime . utcnow ( ) <EOL> db . session . add ( step ) <EOL> def create_replacement_jobstep ( self , step ) : <EOL> builder = self . get_builder ( ) <EOL> return builder . create_job ( step . job , replaces = step ) <EOL> def fetch_artifact ( self , artifact ) : <EOL> """<STR_LIT>""" <EOL> builder = self . get_builder ( ) <EOL> builder . sync_artifact ( artifact ) <EOL> def can_snapshot ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_builder ( ) . can_snapshot ( ) <EOL> def get_resource_limits ( self ) : <EOL> return self . _resources . copy ( ) <EOL> def get_artifact_manager ( self , jobstep ) : <EOL> builder = self . get_builder ( ) <EOL> return builder . get_artifact_manager ( jobstep ) <EOL> def verify_final_artifacts ( self , jobstep , artifacts ) : <EOL> builder = self . get_builder ( ) <EOL> return builder . verify_final_artifacts ( jobstep , artifacts ) <EOL> def prefer_artifactstore ( self ) : <EOL> return self . debug_config . get ( '<STR_LIT>' , False ) <EOL> SERVICE_LOG_FILE_PATTERNS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class JenkinsGenericBuildStep ( JenkinsBuildStep ) : <EOL> builder_cls = JenkinsGenericBuilder <EOL> def __init__ ( self , job_name = None , script = None , cluster = None , path = '<STR_LIT>' , <EOL> workspace = '<STR_LIT>' , reset_script = '<STR_LIT>' , build_type = None , <EOL> setup_script = '<STR_LIT>' , teardown_script = '<STR_LIT>' , clean = True , <EOL> artifacts = XunitHandler . FILENAMES + CoverageHandler . FILENAMES + <EOL> SERVICE_LOG_FILE_PATTERNS , <EOL> snapshot_script = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if None in ( job_name , script , cluster ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . setup_script = setup_script <EOL> self . script = script <EOL> self . teardown_script = teardown_script <EOL> self . reset_script = reset_script <EOL> self . snapshot_script = snapshot_script <EOL> self . path = path <EOL> self . workspace = workspace <EOL> self . build_type = build_type <EOL> self . artifacts = artifacts <EOL> self . clean = clean <EOL> super ( JenkinsGenericBuildStep , self ) . __init__ ( job_name = job_name , cluster = cluster , ** kwargs ) <EOL> def get_lxc_config ( self , jobstep ) : <EOL> """<STR_LIT>""" <EOL> return self . get_builder ( ) . get_lxc_config ( jobstep ) <EOL> def get_builder_options ( self ) : <EOL> options = super ( JenkinsGenericBuildStep , self ) . get_builder_options ( ) <EOL> options . update ( { <EOL> '<STR_LIT>' : self . setup_script , <EOL> '<STR_LIT>' : self . script , <EOL> '<STR_LIT>' : self . teardown_script , <EOL> '<STR_LIT>' : self . reset_script , <EOL> '<STR_LIT>' : self . snapshot_script , <EOL> '<STR_LIT:path>' : self . path , <EOL> '<STR_LIT>' : self . workspace , <EOL> '<STR_LIT>' : self . build_type , <EOL> '<STR_LIT>' : self . artifacts , <EOL> '<STR_LIT>' : self . clean , <EOL> } ) <EOL> return options </s>
