<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import glob <EOL> import shutil <EOL> import logging <EOL> import os <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError , SaltInvocationError <EOL> from salt . ext . six import string_types <EOL> KNOWN_BINARY_NAMES = frozenset ( <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> ) <EOL> log = logging . getLogger ( __name__ ) <EOL> __opts__ = { <EOL> '<STR_LIT>' : salt . utils . which_bin ( KNOWN_BINARY_NAMES ) or '<STR_LIT>' <EOL> } <EOL> __pillar__ = { } <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> return __virtualname__ <EOL> def create ( path , <EOL> venv_bin = None , <EOL> system_site_packages = False , <EOL> distribute = False , <EOL> clear = False , <EOL> python = None , <EOL> extra_search_dir = None , <EOL> never_download = None , <EOL> prompt = None , <EOL> pip = False , <EOL> symlinks = None , <EOL> upgrade = None , <EOL> user = None , <EOL> use_vt = False , <EOL> saltenv = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if venv_bin is None : <EOL> venv_bin = __opts__ . get ( '<STR_LIT>' ) or __pillar__ . get ( '<STR_LIT>' ) <EOL> cmd = [ venv_bin ] <EOL> if '<STR_LIT>' not in venv_bin : <EOL> if upgrade is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( venv_bin ) <EOL> ) <EOL> elif symlinks is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( venv_bin ) <EOL> ) <EOL> try : <EOL> import virtualenv <EOL> version = getattr ( virtualenv , '<STR_LIT>' , <EOL> virtualenv . virtualenv_version ) <EOL> virtualenv_version_info = tuple ( <EOL> [ int ( i ) for i in version . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) ] <EOL> ) <EOL> except ImportError : <EOL> version_cmd = [ venv_bin , '<STR_LIT>' ] <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> version_cmd , runas = user , python_shell = False <EOL> ) <EOL> if ret [ '<STR_LIT>' ] > <NUM_LIT:0> or not ret [ '<STR_LIT>' ] . strip ( ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( version_cmd , ret ) <EOL> ) <EOL> virtualenv_version_info = tuple ( <EOL> [ int ( i ) for i in <EOL> ret [ '<STR_LIT>' ] . strip ( ) . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) ] <EOL> ) <EOL> if distribute : <EOL> if virtualenv_version_info >= ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> log . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if python is not None and python . strip ( ) != '<STR_LIT>' : <EOL> if not salt . utils . which ( python ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( python ) <EOL> ) <EOL> cmd . append ( '<STR_LIT>' . format ( python ) ) <EOL> if extra_search_dir is not None : <EOL> if isinstance ( extra_search_dir , string_types ) and extra_search_dir . strip ( ) != '<STR_LIT>' : <EOL> extra_search_dir = [ <EOL> e . strip ( ) for e in extra_search_dir . split ( '<STR_LIT:U+002C>' ) <EOL> ] <EOL> for entry in extra_search_dir : <EOL> cmd . append ( '<STR_LIT>' . format ( entry ) ) <EOL> if never_download is True : <EOL> if virtualenv_version_info >= ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> log . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if prompt is not None and prompt . strip ( ) != '<STR_LIT>' : <EOL> cmd . append ( '<STR_LIT>' . format ( prompt ) ) <EOL> else : <EOL> if python is not None and python . strip ( ) != '<STR_LIT>' : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( venv_bin ) <EOL> ) <EOL> elif extra_search_dir is not None and extra_search_dir . strip ( ) != '<STR_LIT>' : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( venv_bin ) <EOL> ) <EOL> elif never_download is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( venv_bin ) <EOL> ) <EOL> elif prompt is not None and prompt . strip ( ) != '<STR_LIT>' : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( venv_bin ) <EOL> ) <EOL> if upgrade is True : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if symlinks is True : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if clear is True : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if system_site_packages is True : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> cmd . append ( path ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( cmd , runas = user , python_shell = False ) <EOL> if ret [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> return ret <EOL> if salt . utils . is_windows ( ) : <EOL> venv_python = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> venv_pip = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> venv_setuptools = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> venv_python = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> venv_pip = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> venv_setuptools = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( pip or distribute ) and not os . path . exists ( venv_setuptools ) : <EOL> _install_script ( <EOL> '<STR_LIT>' , <EOL> path , venv_python , user , saltenv = saltenv , use_vt = use_vt <EOL> ) <EOL> for fpath in glob . glob ( os . path . join ( path , '<STR_LIT>' ) ) : <EOL> os . unlink ( fpath ) <EOL> if ret [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> return ret <EOL> if pip and not os . path . exists ( venv_pip ) : <EOL> _ret = _install_script ( <EOL> '<STR_LIT>' , <EOL> path , venv_python , user , saltenv = saltenv , use_vt = use_vt <EOL> ) <EOL> ret . update ( <EOL> retcode = _ret [ '<STR_LIT>' ] , <EOL> stdout = '<STR_LIT>' . format ( ret [ '<STR_LIT>' ] , _ret [ '<STR_LIT>' ] ) . strip ( ) , <EOL> stderr = '<STR_LIT>' . format ( ret [ '<STR_LIT>' ] , _ret [ '<STR_LIT>' ] ) . strip ( ) , <EOL> ) <EOL> return ret <EOL> def get_site_packages ( venv ) : <EOL> '''<STR_LIT>''' <EOL> bin_path = _verify_virtualenv ( venv ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> bin_path , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if ret [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( ** ret ) ) <EOL> return ret [ '<STR_LIT>' ] <EOL> def get_distribution_path ( venv , distribution ) : <EOL> '''<STR_LIT>''' <EOL> _verify_safe_py_code ( distribution ) <EOL> bin_path = _verify_virtualenv ( venv ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> bin_path , <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" . format ( <EOL> distribution <EOL> ) <EOL> ) <EOL> if ret [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( ** ret ) ) <EOL> return ret [ '<STR_LIT>' ] <EOL> def get_resource_path ( venv , <EOL> package_or_requirement = None , <EOL> resource_name = None , <EOL> package = None , <EOL> resource = None ) : <EOL> '''<STR_LIT>''' <EOL> if package_or_requirement is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if package is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> package = package_or_requirement <EOL> if resource_name is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if resource is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> resource = resource_name <EOL> _verify_safe_py_code ( package , resource ) <EOL> bin_path = _verify_virtualenv ( venv ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> bin_path , <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" . format ( <EOL> package , <EOL> resource <EOL> ) <EOL> ) <EOL> if ret [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( ** ret ) ) <EOL> return ret [ '<STR_LIT>' ] <EOL> def get_resource_content ( venv , <EOL> package_or_requirement = None , <EOL> resource_name = None , <EOL> package = None , <EOL> resource = None ) : <EOL> '''<STR_LIT>''' <EOL> if package_or_requirement is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if package is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> package = package_or_requirement <EOL> if resource_name is not None : <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if resource is not None : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> resource = resource_name <EOL> _verify_safe_py_code ( package , resource ) <EOL> bin_path = _verify_virtualenv ( venv ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( <EOL> bin_path , <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" . format ( <EOL> package , <EOL> resource <EOL> ) <EOL> ) <EOL> if ret [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( ** ret ) ) <EOL> return ret [ '<STR_LIT>' ] <EOL> def _install_script ( source , cwd , python , user , saltenv = '<STR_LIT>' , use_vt = False ) : <EOL> if not salt . utils . is_windows ( ) : <EOL> tmppath = salt . utils . mkstemp ( dir = cwd ) <EOL> else : <EOL> tmppath = __salt__ [ '<STR_LIT>' ] ( source , saltenv ) <EOL> if not salt . utils . is_windows ( ) : <EOL> fn_ = __salt__ [ '<STR_LIT>' ] ( source , saltenv ) <EOL> shutil . copyfile ( fn_ , tmppath ) <EOL> os . chmod ( tmppath , <NUM_LIT> ) <EOL> os . chown ( tmppath , __salt__ [ '<STR_LIT>' ] ( user ) , - <NUM_LIT:1> ) <EOL> try : <EOL> return __salt__ [ '<STR_LIT>' ] ( <EOL> [ python , tmppath ] , <EOL> runas = user , <EOL> cwd = cwd , <EOL> env = { '<STR_LIT>' : cwd } , <EOL> use_vt = use_vt , <EOL> python_shell = False , <EOL> ) <EOL> finally : <EOL> os . remove ( tmppath ) <EOL> def _verify_safe_py_code ( * args ) : <EOL> for arg in args : <EOL> if not salt . utils . verify . safe_py_code ( arg ) : <EOL> raise SaltInvocationError ( <EOL> '<STR_LIT>' . format ( arg ) <EOL> ) <EOL> def _verify_virtualenv ( venv_path ) : <EOL> bin_path = os . path . join ( venv_path , '<STR_LIT>' ) <EOL> if not os . path . exists ( bin_path ) : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( venv_path ) <EOL> ) <EOL> return bin_path </s>
