<s> from __future__ import absolute_import <EOL> import functools <EOL> import posixpath <EOL> from threading import Lock <EOL> import rb <EOL> from pkg_resources import resource_string <EOL> from redis . client import Script <EOL> from redis . connection import ConnectionPool <EOL> from sentry import options <EOL> from sentry . exceptions import InvalidConfiguration <EOL> from sentry . utils import warnings <EOL> from sentry . utils . warnings import DeprecatedSettingWarning <EOL> from sentry . utils . versioning import Version , check_versions <EOL> _pool_cache = { } <EOL> _pool_lock = Lock ( ) <EOL> def _shared_pool ( ** opts ) : <EOL> if '<STR_LIT:host>' in opts : <EOL> key = '<STR_LIT>' % ( <EOL> opts [ '<STR_LIT:host>' ] , <EOL> opts [ '<STR_LIT:port>' ] , <EOL> opts [ '<STR_LIT>' ] , <EOL> ) <EOL> else : <EOL> key = '<STR_LIT>' % ( <EOL> opts [ '<STR_LIT:path>' ] , <EOL> opts [ '<STR_LIT>' ] <EOL> ) <EOL> pool = _pool_cache . get ( key ) <EOL> if pool is not None : <EOL> return pool <EOL> with _pool_lock : <EOL> pool = _pool_cache . get ( key ) <EOL> if pool is not None : <EOL> return pool <EOL> pool = ConnectionPool ( ** opts ) <EOL> _pool_cache [ key ] = pool <EOL> return pool <EOL> _make_rb_cluster = functools . partial ( rb . Cluster , pool_cls = _shared_pool ) <EOL> def make_rb_cluster ( * args , ** kwargs ) : <EOL> import warnings <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> DeprecationWarning , <EOL> ) <EOL> return _make_rb_cluster ( * args , ** kwargs ) <EOL> class ClusterManager ( object ) : <EOL> def __init__ ( self , options_manager ) : <EOL> self . __clusters = { } <EOL> self . __options_manager = options_manager <EOL> def get ( self , key ) : <EOL> cluster = self . __clusters . get ( key ) <EOL> if cluster is None : <EOL> configuration = self . __options_manager . get ( '<STR_LIT>' ) . get ( key ) <EOL> if configuration is None : <EOL> raise KeyError ( '<STR_LIT>' . format ( key ) ) <EOL> cluster = self . __clusters [ key ] = _make_rb_cluster ( ** configuration ) <EOL> return cluster <EOL> clusters = ClusterManager ( options . default_manager ) <EOL> def get_cluster_from_options ( setting , options , cluster_manager = clusters ) : <EOL> cluster_option_name = '<STR_LIT>' <EOL> default_cluster_name = '<STR_LIT:default>' <EOL> cluster_constructor_option_names = frozenset ( ( '<STR_LIT>' , ) ) <EOL> options = options . copy ( ) <EOL> cluster_options = { key : options . pop ( key ) for key in set ( options . keys ( ) ) . intersection ( cluster_constructor_option_names ) } <EOL> if cluster_options : <EOL> if cluster_option_name in options : <EOL> raise InvalidConfiguration ( <EOL> '<STR_LIT>' . format ( <EOL> cluster_option_name , <EOL> '<STR_LIT:U+002CU+0020>' . join ( map ( repr , cluster_constructor_option_names ) ) , <EOL> ) <EOL> ) <EOL> else : <EOL> warnings . warn ( <EOL> DeprecatedSettingWarning ( <EOL> '<STR_LIT>' . format ( <EOL> '<STR_LIT:U+002CU+0020>' . join ( map ( repr , cluster_constructor_option_names ) ) , <EOL> setting , <EOL> ) , <EOL> '<STR_LIT>' . format ( <EOL> setting , <EOL> cluster_option_name , <EOL> ) , <EOL> removed_in_version = '<STR_LIT>' , <EOL> ) , <EOL> stacklevel = <NUM_LIT:2> <EOL> ) <EOL> cluster = rb . Cluster ( pool_cls = _shared_pool , ** cluster_options ) <EOL> else : <EOL> cluster = cluster_manager . get ( options . pop ( cluster_option_name , default_cluster_name ) ) <EOL> return cluster , options <EOL> def check_cluster_versions ( cluster , required , recommended = None , label = None ) : <EOL> try : <EOL> with cluster . all ( ) as client : <EOL> results = client . info ( ) <EOL> except Exception as e : <EOL> raise InvalidConfiguration ( unicode ( e ) ) <EOL> versions = { } <EOL> for id , info in results . value . items ( ) : <EOL> host = cluster . hosts [ id ] <EOL> key = '<STR_LIT>' . format ( host = host . host , port = host . port ) <EOL> versions [ key ] = Version ( map ( int , info [ '<STR_LIT>' ] . split ( '<STR_LIT:.>' , <NUM_LIT:3> ) ) ) <EOL> check_versions ( <EOL> '<STR_LIT>' if label is None else '<STR_LIT>' % ( label , ) , <EOL> versions , <EOL> required , <EOL> recommended , <EOL> ) <EOL> def load_script ( path ) : <EOL> script = Script ( None , resource_string ( '<STR_LIT>' , posixpath . join ( '<STR_LIT>' , path ) ) ) <EOL> def call_script ( client , keys , args ) : <EOL> """<STR_LIT>""" . format ( path ) <EOL> return script ( keys , args , client ) <EOL> return call_script </s>
