<s> from pipes import quote <EOL> import logging <EOL> import os <EOL> import sys <EOL> from mock import patch <EOL> import pytest <EOL> from fabric . api import env , hide , lcd , local , settings <EOL> from fabric . state import connections <EOL> from fabtools . vagrant import version as _vagrant_version <EOL> HERE = os . path . dirname ( __file__ ) <EOL> VAGRANT_VERSION = _vagrant_version ( ) <EOL> MIN_VAGRANT_VERSION = ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> @ pytest . yield_fixture ( scope = '<STR_LIT>' , autouse = True ) <EOL> def setup_package ( ) : <EOL> _check_vagrant_version ( ) <EOL> vagrant_box = os . environ . get ( '<STR_LIT>' ) <EOL> if not vagrant_box : <EOL> pytest . skip ( "<STR_LIT>" ) <EOL> vagrant_provider = os . environ . get ( '<STR_LIT>' ) <EOL> reuse_vm = os . environ . get ( '<STR_LIT>' ) <EOL> _configure_logging ( ) <EOL> _allow_fabric_to_access_the_real_stdin ( ) <EOL> if not reuse_vm : <EOL> _stop_vagrant_machine ( ) <EOL> _init_vagrant_machine ( vagrant_box ) <EOL> _start_vagrant_machine ( vagrant_provider ) <EOL> _target_vagrant_machine ( ) <EOL> _set_optional_http_proxy ( ) <EOL> _update_package_index ( ) <EOL> yield <EOL> if not reuse_vm : <EOL> _stop_vagrant_machine ( ) <EOL> def _check_vagrant_version ( ) : <EOL> if VAGRANT_VERSION is None : <EOL> pytest . skip ( "<STR_LIT>" ) <EOL> elif VAGRANT_VERSION < MIN_VAGRANT_VERSION : <EOL> pytest . skip ( "<STR_LIT>" % "<STR_LIT:.>" . join ( map ( str , MIN_VAGRANT_VERSION ) ) ) <EOL> def _configure_logging ( ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . setLevel ( logging . WARN ) <EOL> def _allow_fabric_to_access_the_real_stdin ( ) : <EOL> patcher = patch ( '<STR_LIT>' ) <EOL> mock_sys = patcher . start ( ) <EOL> mock_sys . stdin = sys . __stdin__ <EOL> _VAGRANTFILE_TEMPLATE = """<STR_LIT>""" <EOL> def _init_vagrant_machine ( base_box ) : <EOL> path = os . path . join ( HERE , '<STR_LIT>' ) <EOL> contents = _VAGRANTFILE_TEMPLATE % base_box <EOL> with open ( path , '<STR_LIT:w>' ) as vagrantfile : <EOL> vagrantfile . write ( contents ) <EOL> def _start_vagrant_machine ( provider ) : <EOL> if provider : <EOL> options = '<STR_LIT>' % quote ( provider ) <EOL> else : <EOL> options = '<STR_LIT>' <EOL> with lcd ( HERE ) : <EOL> with settings ( hide ( '<STR_LIT>' ) ) : <EOL> local ( '<STR_LIT>' + options ) <EOL> def _stop_vagrant_machine ( ) : <EOL> with lcd ( HERE ) : <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> local ( '<STR_LIT>' ) <EOL> local ( '<STR_LIT>' ) <EOL> def _target_vagrant_machine ( ) : <EOL> config = _vagrant_ssh_config ( ) <EOL> _set_fabric_env ( <EOL> host = config [ '<STR_LIT>' ] , <EOL> port = config [ '<STR_LIT>' ] , <EOL> user = config [ '<STR_LIT>' ] , <EOL> key_filename = config [ '<STR_LIT>' ] . strip ( '<STR_LIT:">' ) , <EOL> ) <EOL> _clear_fabric_connection_cache ( ) <EOL> def _vagrant_ssh_config ( ) : <EOL> with lcd ( HERE ) : <EOL> with settings ( hide ( '<STR_LIT>' ) ) : <EOL> output = local ( '<STR_LIT>' , capture = True ) <EOL> config = { } <EOL> for line in output . splitlines ( ) [ <NUM_LIT:1> : ] : <EOL> key , value = line . strip ( ) . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> config [ key ] = value <EOL> return config <EOL> def _set_fabric_env ( host , port , user , key_filename ) : <EOL> env . host_string = env . host = "<STR_LIT>" % ( host , port ) <EOL> env . user = user <EOL> env . key_filename = key_filename <EOL> env . disable_known_hosts = True <EOL> env . abort_on_prompts = True <EOL> def _set_optional_http_proxy ( ) : <EOL> http_proxy = os . environ . get ( '<STR_LIT>' ) <EOL> if http_proxy is not None : <EOL> env . shell_env [ '<STR_LIT>' ] = http_proxy <EOL> def _clear_fabric_connection_cache ( ) : <EOL> if env . host_string in connections : <EOL> del connections [ env . host_string ] <EOL> def _update_package_index ( ) : <EOL> from fabtools . system import distrib_family <EOL> family = distrib_family ( ) <EOL> if family == '<STR_LIT>' : <EOL> from fabtools . require . deb import uptodate_index <EOL> uptodate_index ( ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' , autouse = True ) <EOL> def allow_sudo_user ( setup_package ) : <EOL> """<STR_LIT>""" <EOL> from fabtools . require import file as require_file <EOL> require_file ( <EOL> '<STR_LIT>' , <EOL> contents = "<STR_LIT>" , <EOL> owner = '<STR_LIT:root>' , <EOL> mode = '<STR_LIT>' , <EOL> use_sudo = True , <EOL> ) </s>
