<s> import copy <EOL> from distutils . version import StrictVersion <EOL> import itertools <EOL> from nailgun import consts <EOL> from nailgun . logger import logger <EOL> from nailgun . orchestrator . orchestrator_graph import GraphSolver <EOL> TASK_START_TEMPLATE = '<STR_LIT>' <EOL> TASK_END_TEMPLATE = '<STR_LIT>' <EOL> def _get_role ( task ) : <EOL> return task . get ( '<STR_LIT>' , task . get ( '<STR_LIT>' ) ) <EOL> def _get_task_stage ( task ) : <EOL> return task [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> def _get_task_stage_and_priority ( task ) : <EOL> stage_list = task [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) <EOL> stage = stage_list [ <NUM_LIT:0> ] <EOL> priority = stage_list [ - <NUM_LIT:1> ] if len ( stage_list ) > <NUM_LIT:1> else <NUM_LIT:0> <EOL> try : <EOL> priority = float ( priority ) <EOL> except ValueError : <EOL> logger . warn ( <EOL> '<STR_LIT>' , <EOL> task , priority ) <EOL> priority = <NUM_LIT:0> <EOL> return stage , priority <EOL> def _join_groups ( groups ) : <EOL> for group in groups . values ( ) : <EOL> for req in group . get ( '<STR_LIT>' , ( ) ) : <EOL> if req in groups : <EOL> group [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:name>' : TASK_END_TEMPLATE . format ( req ) , <EOL> '<STR_LIT>' : _get_role ( groups [ req ] ) <EOL> } ) <EOL> for req in group . get ( '<STR_LIT>' , ( ) ) : <EOL> if req in groups : <EOL> groups [ req ] [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:name>' : TASK_END_TEMPLATE . format ( group [ '<STR_LIT:id>' ] ) , <EOL> '<STR_LIT>' : _get_role ( group ) <EOL> } ) <EOL> def _get_group_start ( group ) : <EOL> return { <EOL> '<STR_LIT:id>' : TASK_START_TEMPLATE . format ( group [ '<STR_LIT:id>' ] ) , <EOL> '<STR_LIT:type>' : consts . ORCHESTRATOR_TASK_TYPES . skipped , <EOL> '<STR_LIT:version>' : consts . TASK_CROSS_DEPENDENCY , <EOL> '<STR_LIT>' : _get_role ( group ) , <EOL> '<STR_LIT>' : group [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:name>' : TASK_END_TEMPLATE . format ( group [ '<STR_LIT:id>' ] ) , '<STR_LIT>' : '<STR_LIT>' <EOL> } ] , <EOL> } <EOL> def _get_group_end ( group ) : <EOL> return { <EOL> '<STR_LIT:id>' : TASK_END_TEMPLATE . format ( group [ '<STR_LIT:id>' ] ) , <EOL> '<STR_LIT:type>' : consts . ORCHESTRATOR_TASK_TYPES . skipped , <EOL> '<STR_LIT:version>' : consts . TASK_CROSS_DEPENDENCY , <EOL> '<STR_LIT>' : _get_role ( group ) <EOL> } <EOL> def _join_task_to_group ( task , groups ) : <EOL> task [ '<STR_LIT:version>' ] = consts . TASK_CROSS_DEPENDENCY <EOL> task [ '<STR_LIT>' ] = [ <EOL> { '<STR_LIT:name>' : TASK_START_TEMPLATE . format ( g ) , '<STR_LIT>' : '<STR_LIT>' } for g in groups <EOL> ] <EOL> return task <EOL> def adapt_legacy_tasks ( deployment_tasks , legacy_plugin_tasks , role_resolver ) : <EOL> """<STR_LIT>""" <EOL> min_task_version = StrictVersion ( consts . TASK_CROSS_DEPENDENCY ) <EOL> groups = { } <EOL> sync_points = GraphSolver ( ) <EOL> legacy_tasks = [ ] <EOL> for task in deployment_tasks : <EOL> task_type = task . get ( '<STR_LIT:type>' ) <EOL> task_version = StrictVersion ( task . get ( '<STR_LIT:version>' , '<STR_LIT>' ) ) <EOL> if task_type == consts . ORCHESTRATOR_TASK_TYPES . group : <EOL> groups [ task [ '<STR_LIT:id>' ] ] = dict ( task , cross_depends = [ ] ) <EOL> elif task_type == consts . ORCHESTRATOR_TASK_TYPES . stage : <EOL> sync_points . add_task ( task ) <EOL> else : <EOL> task = task . copy ( ) <EOL> required_for = copy . copy ( task . get ( '<STR_LIT>' , [ ] ) ) <EOL> required_for . extend ( <EOL> TASK_END_TEMPLATE . format ( x ) <EOL> for x in role_resolver . get_all_roles ( _get_role ( task ) ) <EOL> ) <EOL> task [ '<STR_LIT>' ] = required_for <EOL> if task_version < min_task_version : <EOL> legacy_tasks . append ( task ) <EOL> continue <EOL> yield task <EOL> if not ( legacy_tasks or legacy_plugin_tasks ) : <EOL> return <EOL> _join_groups ( groups ) <EOL> for group in groups . values ( ) : <EOL> yield _get_group_start ( group ) <EOL> yield _get_group_end ( group ) <EOL> for task in legacy_tasks : <EOL> logger . warning ( "<STR_LIT>" , task [ '<STR_LIT:id>' ] ) <EOL> yield _join_task_to_group ( <EOL> task , role_resolver . get_all_roles ( _get_role ( task ) ) <EOL> ) <EOL> if not legacy_plugin_tasks : <EOL> return <EOL> legacy_plugin_tasks . sort ( key = _get_task_stage_and_priority ) <EOL> tasks_per_stage = itertools . groupby ( <EOL> legacy_plugin_tasks , key = _get_task_stage <EOL> ) <EOL> for stage , tasks in tasks_per_stage : <EOL> sync_point_name = TASK_END_TEMPLATE . format ( stage ) <EOL> cross_depends = [ { '<STR_LIT:name>' : sync_point_name , '<STR_LIT>' : None } ] <EOL> successors = sync_points . successors ( sync_point_name ) <EOL> if successors : <EOL> logger . debug ( <EOL> '<STR_LIT>' , <EOL> sync_point_name , successors [ <NUM_LIT:0> ] <EOL> ) <EOL> cross_depended_by = [ { '<STR_LIT:name>' : successors [ <NUM_LIT:0> ] , '<STR_LIT>' : None } ] <EOL> else : <EOL> logger . debug ( <EOL> '<STR_LIT>' , sync_point_name <EOL> ) <EOL> cross_depended_by = [ ] <EOL> for idx , task in enumerate ( tasks ) : <EOL> new_task = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' . format ( stage , idx ) , <EOL> '<STR_LIT:type>' : task [ '<STR_LIT:type>' ] , <EOL> '<STR_LIT>' : _get_role ( task ) , <EOL> '<STR_LIT:version>' : consts . TASK_CROSS_DEPENDENCY , <EOL> '<STR_LIT>' : cross_depends , <EOL> '<STR_LIT>' : cross_depended_by , <EOL> '<STR_LIT>' : task . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : task . get ( '<STR_LIT>' , True ) <EOL> } <EOL> cross_depends = [ <EOL> { '<STR_LIT:name>' : new_task [ '<STR_LIT:id>' ] , '<STR_LIT>' : new_task [ '<STR_LIT>' ] } <EOL> ] <EOL> yield new_task </s>
