<s> import os <EOL> import re <EOL> import subprocess <EOL> import shutil <EOL> from django . utils . text import slugify <EOL> from django . conf import settings <EOL> from django . core . cache import cache <EOL> from fabric_bolt . hosts . models import SSHConfig <EOL> class BaseTaskRunnerBackend ( object ) : <EOL> special_options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:password>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_urls ( self ) : <EOL> return [ ] <EOL> def get_detail_template ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_task_details ( self , project , task_name ) : <EOL> if task_name : <EOL> for details in self . get_fabric_tasks ( project ) : <EOL> if details [ <NUM_LIT:0> ] == task_name : <EOL> return details <EOL> return None <EOL> def get_special_options ( self ) : <EOL> return self . special_options <EOL> def check_output ( self , command , shell = False ) : <EOL> executable = None <EOL> if shell : <EOL> executable = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return subprocess . check_output ( command , shell = shell , executable = executable ) <EOL> def check_output_with_ssh_key ( self , command ) : <EOL> if getattr ( settings , '<STR_LIT>' , None ) : <EOL> return self . check_output ( <EOL> '<STR_LIT>' . format ( settings . GIT_SSH_KEY_LOCATION , command ) , <EOL> shell = True <EOL> ) <EOL> else : <EOL> return self . check_output ( [ command ] , shell = True ) <EOL> def update_project_git ( self , project , cache_dir , repo_dir ) : <EOL> if not os . path . exists ( repo_dir ) : <EOL> if not os . path . exists ( cache_dir ) : <EOL> os . makedirs ( cache_dir ) <EOL> self . check_output_with_ssh_key ( '<STR_LIT>' . format ( project . repo_url , repo_dir ) ) <EOL> else : <EOL> self . check_output_with_ssh_key ( <EOL> '<STR_LIT>' . format ( repo_dir ) <EOL> ) <EOL> def clean_obsolete_project_git ( self , project ) : <EOL> cache . delete_many ( [ '<STR_LIT>' . format ( project . id ) , <EOL> '<STR_LIT>' . format ( project . id ) ] ) <EOL> repo_dir = os . path . join ( settings . PUBLIC_DIR , '<STR_LIT>' , slugify ( project . name ) ) <EOL> if os . path . exists ( repo_dir ) : <EOL> shutil . rmtree ( repo_dir ) <EOL> def setup_virtual_env_if_needed ( self , repo_dir ) : <EOL> env_dir = os . path . join ( repo_dir , '<STR_LIT>' ) <EOL> if not os . path . exists ( env_dir ) : <EOL> os . makedirs ( env_dir ) <EOL> self . check_output ( "<STR_LIT>" . format ( env_dir ) , shell = True ) <EOL> def update_project_requirements ( self , project , repo_dir , activate_loc ) : <EOL> pip_installs = '<STR_LIT:U+0020>' . join ( project . fabfile_requirements . splitlines ( ) ) <EOL> self . check_output_with_ssh_key ( <EOL> '<STR_LIT>' . format ( activate_loc , repo_dir , pip_installs ) <EOL> ) <EOL> def get_fabfile_path ( self , project ) : <EOL> if project . use_repo_fabfile : <EOL> cache_key = '<STR_LIT>' . format ( project . pk ) <EOL> cached_result = cache . get ( cache_key ) <EOL> if cached_result : <EOL> return cached_result <EOL> cache_dir = os . path . join ( settings . PUBLIC_DIR , '<STR_LIT>' ) <EOL> repo_dir = os . path . join ( cache_dir , slugify ( project . name ) ) <EOL> self . update_project_git ( project , cache_dir , repo_dir ) <EOL> self . setup_virtual_env_if_needed ( repo_dir ) <EOL> activate_loc = os . path . join ( repo_dir , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . update_project_requirements ( project , repo_dir , activate_loc ) <EOL> result = os . path . join ( repo_dir , '<STR_LIT>' ) , activate_loc <EOL> cache . set ( cache_key , result , settings . FABRIC_TASK_CACHE_TIMEOUT ) <EOL> return result <EOL> else : <EOL> return settings . FABFILE_PATH , None <EOL> def parse_task_details ( self , name , task_output ) : <EOL> lines = task_output . splitlines ( ) <EOL> docstring = '<STR_LIT:\n>' . join ( [ line . strip ( ) for line in lines [ <NUM_LIT:2> : - <NUM_LIT:2> ] ] ) . strip ( ) <EOL> arguments_line = lines [ - <NUM_LIT:2> ] . strip ( ) <EOL> if docstring == '<STR_LIT>' : <EOL> docstring = None <EOL> arguments_line = arguments_line [ <NUM_LIT:11> : ] . strip ( ) <EOL> arguments = [ ] <EOL> if arguments_line : <EOL> for arg in arguments_line . split ( '<STR_LIT:U+002CU+0020>' ) : <EOL> m = re . match ( r"<STR_LIT>" , arg ) <EOL> if m . group ( <NUM_LIT:2> ) : <EOL> if m . group ( <NUM_LIT:3> ) == "<STR_LIT:'>" : <EOL> arguments . append ( ( m . group ( <NUM_LIT:1> ) , m . group ( <NUM_LIT:4> ) ) ) <EOL> else : <EOL> arguments . append ( m . group ( <NUM_LIT:1> ) ) <EOL> else : <EOL> arguments . append ( m . group ( <NUM_LIT:1> ) ) <EOL> return name , docstring , arguments <EOL> def get_fabric_tasks ( self , project ) : <EOL> """<STR_LIT>""" <EOL> cache_key = '<STR_LIT>' . format ( project . pk ) <EOL> cached_result = cache . get ( cache_key ) <EOL> if cached_result : <EOL> return cached_result <EOL> try : <EOL> fabfile_path , activate_loc = self . get_fabfile_path ( project ) <EOL> if activate_loc : <EOL> output = self . check_output ( <EOL> '<STR_LIT>' . format ( activate_loc , fabfile_path ) , <EOL> shell = True <EOL> ) <EOL> else : <EOL> output = self . check_output ( <EOL> '<STR_LIT>' . format ( fabfile_path ) , <EOL> shell = True <EOL> ) <EOL> lines = output . splitlines ( ) <EOL> tasks = [ ] <EOL> for line in lines : <EOL> name = line . strip ( ) <EOL> if activate_loc : <EOL> o = self . check_output ( <EOL> '<STR_LIT>' . format ( activate_loc , name , fabfile_path ) , <EOL> shell = True <EOL> ) <EOL> else : <EOL> o = self . check_output ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' . format ( name ) , '<STR_LIT>' . format ( fabfile_path ) ] <EOL> ) <EOL> tasks . append ( self . parse_task_details ( name , o ) ) <EOL> cache . set ( cache_key , tasks , settings . FABRIC_TASK_CACHE_TIMEOUT ) <EOL> except Exception as e : <EOL> tasks = [ ] <EOL> return tasks <EOL> def clean_key_string ( self , key ) : <EOL> key = key . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> key = key . replace ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) <EOL> key = key . replace ( '<STR_LIT:=>' , '<STR_LIT>' ) <EOL> return key <EOL> def clean_value_string ( self , value ) : <EOL> value = value . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> value = value . replace ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) <EOL> value = value . replace ( '<STR_LIT:=>' , '<STR_LIT>' ) <EOL> return value <EOL> def clean_arg_key_string ( self , key ) : <EOL> key = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , key ) <EOL> return key <EOL> def get_key_value_string ( self , key , config ) : <EOL> key = self . clean_key_string ( key ) <EOL> if config . data_type == config . BOOLEAN_TYPE : <EOL> return key + ( '<STR_LIT>' if config . get_value ( ) else '<STR_LIT:=>' ) <EOL> elif config . data_type == config . NUMBER_TYPE : <EOL> return key + '<STR_LIT:=>' + str ( config . get_value ( ) ) <EOL> else : <EOL> return '<STR_LIT>' . format ( key , self . clean_value_string ( config . get_value ( ) ) ) <EOL> def update_config_values_from_session ( self , configs , session ) : <EOL> configs = configs . copy ( ) <EOL> for key , config in configs . iteritems ( ) : <EOL> if session . get ( '<STR_LIT>' , { } ) . get ( key , None ) is not None : <EOL> config . set_value ( session [ '<STR_LIT>' ] [ key ] ) <EOL> del session [ '<STR_LIT>' ] [ key ] <EOL> arg_values = session . get ( '<STR_LIT>' , { } ) <EOL> return configs , arg_values <EOL> def build_command ( self , project , deployment , session , abort_on_prompts = True ) : <EOL> configs = deployment . stage . get_configurations ( ) <EOL> configs , arg_values = self . update_config_values_from_session ( configs , session ) <EOL> task_args = [ key for key , config in configs . iteritems ( ) if config . task_argument and config . task_name == deployment . task . name ] <EOL> task_configs = [ key for key , config in configs . iteritems ( ) if not config . task_argument ] <EOL> command_to_config = { x . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) : x for x in self . get_special_options ( ) } <EOL> normal_task_configs = list ( set ( task_configs ) - set ( command_to_config . keys ( ) ) ) <EOL> special_task_configs = list ( set ( task_configs ) & set ( command_to_config . keys ( ) ) ) <EOL> command = '<STR_LIT>' + deployment . task . name <EOL> task_details = self . get_task_details ( project , deployment . task . name ) <EOL> task_args = list ( set ( task_args + [ x [ <NUM_LIT:0> ] if isinstance ( x , tuple ) else x for x in task_details [ <NUM_LIT:2> ] ] ) ) <EOL> if task_args : <EOL> key_value_strings = [ ] <EOL> for key in task_args : <EOL> if key in configs : <EOL> value = unicode ( configs [ key ] . get_value ( ) ) <EOL> elif key in arg_values : <EOL> value = unicode ( arg_values [ key ] ) <EOL> else : <EOL> continue <EOL> cleaned_key = self . clean_arg_key_string ( key ) <EOL> value = self . clean_value_string ( value ) <EOL> key_value_strings . append ( '<STR_LIT>' . format ( cleaned_key , value ) ) <EOL> if key_value_strings : <EOL> command += '<STR_LIT::>' <EOL> command += '<STR_LIT:U+002C>' . join ( key_value_strings ) <EOL> if normal_task_configs : <EOL> command += '<STR_LIT>' <EOL> command += '<STR_LIT:">' + '<STR_LIT:U+002C>' . join ( self . get_key_value_string ( key , configs [ key ] ) for key in normal_task_configs ) + '<STR_LIT:">' <EOL> if special_task_configs : <EOL> for key in special_task_configs : <EOL> if key == '<STR_LIT>' : <EOL> command += '<STR_LIT>' + configs [ key ] . get_value ( ) <EOL> else : <EOL> command += '<STR_LIT>' + self . get_key_value_string ( command_to_config [ key ] , configs [ key ] ) <EOL> if abort_on_prompts : <EOL> command += '<STR_LIT>' <EOL> hosts = deployment . stage . hosts . values_list ( '<STR_LIT:name>' , flat = True ) <EOL> if hosts : <EOL> command += '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( hosts ) <EOL> if not configs . get ( '<STR_LIT>' ) : <EOL> ssh_config = SSHConfig . objects . first ( ) <EOL> if ssh_config : <EOL> command += '<STR_LIT>' + ssh_config . private_key_file . file . name <EOL> if not configs . get ( '<STR_LIT:user>' ) : <EOL> command += '<STR_LIT>' + ssh_config . remote_user <EOL> fabfile_path , active_loc = self . get_fabfile_path ( project ) <EOL> command += '<STR_LIT>' . format ( fabfile_path ) <EOL> if active_loc : <EOL> return '<STR_LIT>' . format ( active_loc ) + '<STR_LIT:U+0020>' + command <EOL> else : <EOL> return command </s>
