<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import subprocess <EOL> import mock <EOL> import zope . interface <EOL> from certbot import configuration <EOL> from certbot import errors as le_errors <EOL> from certbot_apache import configurator <EOL> from certbot_compatibility_test import errors <EOL> from certbot_compatibility_test import interfaces <EOL> from certbot_compatibility_test import util <EOL> from certbot_compatibility_test . configurators import common as configurators_common <EOL> APACHE_VERSION_REGEX = re . compile ( r"<STR_LIT>" , re . IGNORECASE ) <EOL> APACHE_COMMANDS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> @ zope . interface . implementer ( interfaces . IConfiguratorProxy ) <EOL> class Proxy ( configurators_common . Proxy ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args ) : <EOL> """<STR_LIT>""" <EOL> super ( Proxy , self ) . __init__ ( args ) <EOL> self . le_config . apache_le_vhost_ext = "<STR_LIT>" <EOL> self . _setup_mock ( ) <EOL> self . modules = self . server_root = self . test_conf = self . version = None <EOL> self . _apache_configurator = self . _all_names = self . _test_names = None <EOL> def _setup_mock ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_subprocess = mock . MagicMock ( ) <EOL> mock_subprocess . check_call = self . check_call <EOL> mock_subprocess . Popen = self . popen <EOL> mock . patch ( <EOL> "<STR_LIT>" , <EOL> mock_subprocess ) . start ( ) <EOL> mock . patch ( <EOL> "<STR_LIT>" , <EOL> mock_subprocess ) . start ( ) <EOL> mock . patch ( <EOL> "<STR_LIT>" , <EOL> mock_subprocess ) . start ( ) <EOL> mock . patch ( <EOL> "<STR_LIT>" , <EOL> _is_apache_command ) . start ( ) <EOL> patch = mock . patch ( <EOL> "<STR_LIT>" ) <EOL> mock_display = patch . start ( ) <EOL> mock_display . side_effect = le_errors . PluginError ( <EOL> "<STR_LIT>" ) <EOL> def check_call ( self , command , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if _is_apache_command ( command ) : <EOL> command = _modify_command ( command ) <EOL> return super ( Proxy , self ) . check_call ( command , * args , ** kwargs ) <EOL> else : <EOL> return subprocess . check_call ( command , * args , ** kwargs ) <EOL> def popen ( self , command , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if _is_apache_command ( command ) : <EOL> command = _modify_command ( command ) <EOL> return super ( Proxy , self ) . popen ( command , * args , ** kwargs ) <EOL> else : <EOL> return subprocess . Popen ( command , * args , ** kwargs ) <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> method = getattr ( self . _apache_configurator , name , None ) <EOL> if callable ( method ) : <EOL> return method <EOL> else : <EOL> raise AttributeError ( ) <EOL> def load_config ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . le_config , "<STR_LIT>" ) : <EOL> try : <EOL> self . check_call ( [ self . le_config . apache_init_script , "<STR_LIT>" ] ) <EOL> except errors . Error : <EOL> raise errors . Error ( <EOL> "<STR_LIT>" ) <EOL> config = super ( Proxy , self ) . load_config ( ) <EOL> self . modules = _get_modules ( config ) <EOL> self . version = _get_version ( config ) <EOL> self . _all_names , self . _test_names = _get_names ( config ) <EOL> server_root = _get_server_root ( config ) <EOL> with open ( os . path . join ( config , "<STR_LIT>" ) ) as f : <EOL> config_file = os . path . join ( server_root , f . readline ( ) . rstrip ( ) ) <EOL> self . test_conf = _create_test_conf ( server_root , config_file ) <EOL> self . preprocess_config ( server_root ) <EOL> self . _prepare_configurator ( server_root , config_file ) <EOL> try : <EOL> self . check_call ( "<STR_LIT>" . format ( <EOL> server_root , config_file ) ) <EOL> except errors . Error : <EOL> raise errors . Error ( <EOL> "<STR_LIT>" . format ( <EOL> config ) ) <EOL> return config <EOL> def preprocess_config ( self , server_root ) : <EOL> """<STR_LIT>""" <EOL> find = subprocess . Popen ( <EOL> [ "<STR_LIT>" , server_root , "<STR_LIT>" , "<STR_LIT:f>" ] , <EOL> stdout = subprocess . PIPE ) <EOL> subprocess . check_call ( [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] , stdin = find . stdout ) <EOL> def _prepare_configurator ( self , server_root , config_file ) : <EOL> """<STR_LIT>""" <EOL> self . le_config . apache_server_root = server_root <EOL> self . le_config . apache_ctl = "<STR_LIT>" . format ( <EOL> server_root , config_file ) <EOL> self . le_config . apache_enmod = "<STR_LIT>" . format ( server_root ) <EOL> self . le_config . apache_dismod = "<STR_LIT>" . format ( server_root ) <EOL> self . le_config . apache_init_script = self . le_config . apache_ctl + "<STR_LIT>" <EOL> self . _apache_configurator = configurator . ApacheConfigurator ( <EOL> config = configuration . NamespaceConfig ( self . le_config ) , <EOL> name = "<STR_LIT>" ) <EOL> self . _apache_configurator . prepare ( ) <EOL> def cleanup_from_tests ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( Proxy , self ) . cleanup_from_tests ( ) <EOL> mock . patch . stopall ( ) <EOL> def get_all_names_answer ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _all_names : <EOL> return self . _all_names <EOL> else : <EOL> raise errors . Error ( "<STR_LIT>" ) <EOL> def get_testable_domain_names ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _test_names : <EOL> return self . _test_names <EOL> else : <EOL> return { "<STR_LIT>" } <EOL> def deploy_cert ( self , domain , cert_path , key_path , chain_path = None , <EOL> fullchain_path = None ) : <EOL> """<STR_LIT>""" <EOL> cert_path , key_path , chain_path = self . copy_certs_and_keys ( <EOL> cert_path , key_path , chain_path ) <EOL> self . _apache_configurator . deploy_cert ( <EOL> domain , cert_path , key_path , chain_path , fullchain_path ) <EOL> def _is_apache_command ( command ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( command , list ) : <EOL> command = command [ <NUM_LIT:0> ] <EOL> for apache_command in APACHE_COMMANDS : <EOL> if command . startswith ( apache_command ) : <EOL> return True <EOL> return False <EOL> def _modify_command ( command ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( command , list ) : <EOL> for i in xrange ( len ( command ) ) : <EOL> if command [ i ] == "<STR_LIT>" : <EOL> command [ i ] = "<STR_LIT>" <EOL> else : <EOL> command = command . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return command <EOL> def _create_test_conf ( server_root , apache_config ) : <EOL> """<STR_LIT>""" <EOL> test_conf = os . path . join ( server_root , "<STR_LIT>" ) <EOL> open ( test_conf , "<STR_LIT:w>" ) . close ( ) <EOL> subprocess . check_call ( <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , apache_config ] ) <EOL> return test_conf <EOL> def _get_server_root ( config ) : <EOL> """<STR_LIT>""" <EOL> subdirs = [ <EOL> name for name in os . listdir ( config ) <EOL> if os . path . isdir ( os . path . join ( config , name ) ) ] <EOL> if len ( subdirs ) != <NUM_LIT:1> : <EOL> errors . Error ( "<STR_LIT>" . format ( config ) ) <EOL> return os . path . join ( config , subdirs [ <NUM_LIT:0> ] . rstrip ( ) ) <EOL> def _get_names ( config ) : <EOL> """<STR_LIT>""" <EOL> all_names = set ( ) <EOL> non_ip_names = set ( ) <EOL> with open ( os . path . join ( config , "<STR_LIT>" ) ) as f : <EOL> for line in f : <EOL> if line [ <NUM_LIT:0> ] . isspace ( ) : <EOL> words = line . split ( ) <EOL> if words [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> all_names . add ( words [ <NUM_LIT:1> ] ) <EOL> non_ip_names . add ( words [ <NUM_LIT:1> ] ) <EOL> elif words [ <NUM_LIT:1> ] == "<STR_LIT>" and not util . IP_REGEX . match ( words [ <NUM_LIT:3> ] ) : <EOL> all_names . add ( words [ <NUM_LIT:3> ] ) <EOL> non_ip_names . add ( words [ <NUM_LIT:3> ] ) <EOL> elif "<STR_LIT>" not in line : <EOL> words = line . split ( ) <EOL> if ( words [ <NUM_LIT:0> ] . endswith ( "<STR_LIT:*>" ) or words [ <NUM_LIT:0> ] . endswith ( "<STR_LIT>" ) and <EOL> not util . IP_REGEX . match ( words [ <NUM_LIT:1> ] ) and <EOL> words [ <NUM_LIT:1> ] . find ( "<STR_LIT:.>" ) != - <NUM_LIT:1> ) : <EOL> all_names . add ( words [ <NUM_LIT:1> ] ) <EOL> return all_names , non_ip_names <EOL> def _get_modules ( config ) : <EOL> """<STR_LIT>""" <EOL> modules = [ ] <EOL> with open ( os . path . join ( config , "<STR_LIT>" ) ) as f : <EOL> for line in f : <EOL> if line [ <NUM_LIT:0> ] . isspace ( ) : <EOL> words = line . split ( ) <EOL> modules . append ( words [ <NUM_LIT:0> ] [ : - <NUM_LIT:7> ] ) <EOL> return modules <EOL> def _get_version ( config ) : <EOL> """<STR_LIT>""" <EOL> with open ( os . path . join ( config , "<STR_LIT:version>" ) ) as f : <EOL> matches = APACHE_VERSION_REGEX . findall ( f . readline ( ) ) <EOL> if len ( matches ) != <NUM_LIT:1> : <EOL> raise errors . Error ( "<STR_LIT>" ) <EOL> return tuple ( [ int ( i ) for i in matches [ <NUM_LIT:0> ] . split ( "<STR_LIT:.>" ) ] ) </s>
