<s> """<STR_LIT>""" <EOL> import contextlib <EOL> import datetime <EOL> import functools <EOL> import inspect <EOL> import json <EOL> import lockfile <EOL> import netaddr <EOL> import os <EOL> import random <EOL> import re <EOL> import shlex <EOL> import socket <EOL> import struct <EOL> import sys <EOL> import time <EOL> import types <EOL> import uuid <EOL> import pyclbr <EOL> from xml . sax import saxutils <EOL> from eventlet import event <EOL> from eventlet import greenthread <EOL> from eventlet import semaphore <EOL> from eventlet . green import subprocess <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova import version <EOL> LOG = logging . getLogger ( "<STR_LIT>" ) <EOL> ISO_TIME_FORMAT = "<STR_LIT>" <EOL> PERFECT_TIME_FORMAT = "<STR_LIT>" <EOL> FLAGS = flags . FLAGS <EOL> def import_class ( import_str ) : <EOL> """<STR_LIT>""" <EOL> mod_str , _sep , class_str = import_str . rpartition ( '<STR_LIT:.>' ) <EOL> try : <EOL> __import__ ( mod_str ) <EOL> return getattr ( sys . modules [ mod_str ] , class_str ) <EOL> except ( ImportError , ValueError , AttributeError ) , exc : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , exc ) <EOL> raise exception . ClassNotFound ( class_name = class_str , exception = exc ) <EOL> def import_object ( import_str ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> __import__ ( import_str ) <EOL> return sys . modules [ import_str ] <EOL> except ImportError : <EOL> cls = import_class ( import_str ) <EOL> return cls ( ) <EOL> def vpn_ping ( address , port , timeout = <NUM_LIT> , session_id = None ) : <EOL> """<STR_LIT>""" <EOL> if session_id is None : <EOL> session_id = random . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> data = struct . pack ( '<STR_LIT>' , <NUM_LIT> , session_id ) <EOL> sock . sendto ( data , ( address , port ) ) <EOL> sock . settimeout ( timeout ) <EOL> try : <EOL> received = sock . recv ( <NUM_LIT> ) <EOL> except socket . timeout : <EOL> return False <EOL> finally : <EOL> sock . close ( ) <EOL> fmt = '<STR_LIT>' <EOL> if len ( received ) != struct . calcsize ( fmt ) : <EOL> print struct . calcsize ( fmt ) <EOL> return False <EOL> ( identifier , server_sess , client_sess ) = struct . unpack ( fmt , received ) <EOL> if identifier == <NUM_LIT> and client_sess == session_id : <EOL> return server_sess <EOL> def fetchfile ( url , target ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) % url ) <EOL> execute ( '<STR_LIT>' , '<STR_LIT>' , url , '<STR_LIT>' , target ) <EOL> def execute ( * cmd , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> process_input = kwargs . pop ( '<STR_LIT>' , None ) <EOL> check_exit_code = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> delay_on_retry = kwargs . pop ( '<STR_LIT>' , True ) <EOL> attempts = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> run_as_root = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if len ( kwargs ) : <EOL> raise exception . Error ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % kwargs ) <EOL> if run_as_root : <EOL> cmd = shlex . split ( FLAGS . root_helper ) + list ( cmd ) <EOL> cmd = map ( str , cmd ) <EOL> while attempts > <NUM_LIT:0> : <EOL> attempts -= <NUM_LIT:1> <EOL> try : <EOL> _PIPE = subprocess . PIPE <EOL> obj = subprocess . Popen ( cmd , <EOL> stdin = _PIPE , <EOL> stdout = _PIPE , <EOL> stderr = _PIPE , <EOL> close_fds = True ) <EOL> result = None <EOL> if process_input is not None : <EOL> result = obj . communicate ( process_input ) <EOL> else : <EOL> result = obj . communicate ( ) <EOL> obj . stdin . close ( ) <EOL> _returncode = obj . returncode <EOL> if _returncode : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) % _returncode ) <EOL> if type ( check_exit_code ) == types . IntType and _returncode != check_exit_code : <EOL> ( stdout , stderr ) = result <EOL> raise exception . ProcessExecutionError ( <EOL> exit_code = _returncode , <EOL> stdout = stdout , <EOL> stderr = stderr , <EOL> cmd = '<STR_LIT:U+0020>' . join ( cmd ) ) <EOL> return result <EOL> except exception . ProcessExecutionError : <EOL> if not attempts : <EOL> raise <EOL> else : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , cmd ) <EOL> if delay_on_retry : <EOL> greenthread . sleep ( random . randint ( <NUM_LIT:20> , <NUM_LIT:200> ) / <NUM_LIT> ) <EOL> finally : <EOL> greenthread . sleep ( <NUM_LIT:0> ) <EOL> def ssh_execute ( ssh , cmd , process_input = None , <EOL> addl_env = None , check_exit_code = True ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , '<STR_LIT:U+0020>' . join ( cmd ) ) <EOL> if addl_env : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) ) <EOL> if process_input : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) ) <EOL> stdin_stream , stdout_stream , stderr_stream = ssh . exec_command ( cmd ) <EOL> channel = stdout_stream . channel <EOL> stdout = stdout_stream . read ( ) <EOL> stderr = stderr_stream . read ( ) <EOL> stdin_stream . close ( ) <EOL> exit_status = channel . recv_exit_status ( ) <EOL> if exit_status != - <NUM_LIT:1> : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) % exit_status ) <EOL> if check_exit_code and exit_status != <NUM_LIT:0> : <EOL> raise exception . ProcessExecutionError ( exit_code = exit_status , <EOL> stdout = stdout , <EOL> stderr = stderr , <EOL> cmd = '<STR_LIT:U+0020>' . join ( cmd ) ) <EOL> return ( stdout , stderr ) <EOL> def abspath ( s ) : <EOL> return os . path . join ( os . path . dirname ( __file__ ) , s ) <EOL> def novadir ( ) : <EOL> import nova <EOL> return os . path . abspath ( nova . __file__ ) . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> def default_flagfile ( filename = '<STR_LIT>' , args = None ) : <EOL> if args is None : <EOL> args = sys . argv <EOL> for arg in args : <EOL> if arg . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> break <EOL> else : <EOL> if not os . path . isabs ( filename ) : <EOL> script_dir = os . path . dirname ( inspect . stack ( ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> filename = os . path . abspath ( os . path . join ( script_dir , filename ) ) <EOL> if not os . path . exists ( filename ) : <EOL> filename = "<STR_LIT>" <EOL> if not os . path . exists ( filename ) : <EOL> filename = '<STR_LIT>' <EOL> if os . path . exists ( filename ) : <EOL> flagfile = '<STR_LIT>' % filename <EOL> args . insert ( <NUM_LIT:1> , flagfile ) <EOL> def debug ( arg ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , arg ) <EOL> return arg <EOL> def runthis ( prompt , * cmd , ** kwargs ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , ( '<STR_LIT:U+0020>' . join ( cmd ) ) ) <EOL> rv , err = execute ( * cmd , ** kwargs ) <EOL> def generate_uid ( topic , size = <NUM_LIT:8> ) : <EOL> characters = '<STR_LIT>' <EOL> choices = [ random . choice ( characters ) for x in xrange ( size ) ] <EOL> return '<STR_LIT>' % ( topic , '<STR_LIT>' . join ( choices ) ) <EOL> DEFAULT_PASSWORD_SYMBOLS = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> EASIER_PASSWORD_SYMBOLS = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def usage_from_instance ( instance_ref , ** kw ) : <EOL> usage_info = dict ( <EOL> project_id = instance_ref [ '<STR_LIT>' ] , <EOL> user_id = instance_ref [ '<STR_LIT>' ] , <EOL> instance_id = instance_ref [ '<STR_LIT:id>' ] , <EOL> instance_type = instance_ref [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> instance_type_id = instance_ref [ '<STR_LIT>' ] , <EOL> display_name = instance_ref [ '<STR_LIT>' ] , <EOL> created_at = str ( instance_ref [ '<STR_LIT>' ] ) , <EOL> launched_at = str ( instance_ref [ '<STR_LIT>' ] ) if instance_ref [ '<STR_LIT>' ] else '<STR_LIT>' , <EOL> image_ref = instance_ref [ '<STR_LIT>' ] ) <EOL> usage_info . update ( kw ) <EOL> return usage_info <EOL> def generate_password ( length = <NUM_LIT:20> , symbols = DEFAULT_PASSWORD_SYMBOLS ) : <EOL> """<STR_LIT>""" <EOL> r = random . SystemRandom ( ) <EOL> return '<STR_LIT>' . join ( [ r . choice ( symbols ) for _i in xrange ( length ) ] ) <EOL> def last_octet ( address ) : <EOL> return int ( address . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) <EOL> def get_my_linklocal ( interface ) : <EOL> try : <EOL> if_str = execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , interface ) <EOL> condition = '<STR_LIT>' <EOL> links = [ re . search ( condition , x ) for x in if_str [ <NUM_LIT:0> ] . split ( '<STR_LIT:\n>' ) ] <EOL> address = [ w . group ( <NUM_LIT:1> ) for w in links if w is not None ] <EOL> if address [ <NUM_LIT:0> ] is not None : <EOL> return address [ <NUM_LIT:0> ] <EOL> else : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) <EOL> % if_str ) <EOL> except Exception as ex : <EOL> raise exception . Error ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> def utcnow ( ) : <EOL> """<STR_LIT>""" <EOL> if utcnow . override_time : <EOL> return utcnow . override_time <EOL> return datetime . datetime . utcnow ( ) <EOL> utcnow . override_time = None <EOL> def is_older_than ( before , seconds ) : <EOL> """<STR_LIT>""" <EOL> return utcnow ( ) - before > datetime . timedelta ( seconds = seconds ) <EOL> def utcnow_ts ( ) : <EOL> """<STR_LIT>""" <EOL> return time . mktime ( utcnow ( ) . timetuple ( ) ) <EOL> def set_time_override ( override_time = datetime . datetime . utcnow ( ) ) : <EOL> """<STR_LIT>""" <EOL> utcnow . override_time = override_time <EOL> def advance_time_delta ( timedelta ) : <EOL> """<STR_LIT>""" <EOL> assert ( not utcnow . override_time is None ) <EOL> utcnow . override_time += timedelta <EOL> def advance_time_seconds ( seconds ) : <EOL> """<STR_LIT>""" <EOL> advance_time_delta ( datetime . timedelta ( <NUM_LIT:0> , seconds ) ) <EOL> def clear_time_override ( ) : <EOL> """<STR_LIT>""" <EOL> utcnow . override_time = None <EOL> def strtime ( at = None , fmt = PERFECT_TIME_FORMAT ) : <EOL> """<STR_LIT>""" <EOL> if not at : <EOL> at = utcnow ( ) <EOL> return at . strftime ( fmt ) <EOL> def parse_strtime ( timestr , fmt = PERFECT_TIME_FORMAT ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . strptime ( timestr , fmt ) <EOL> def isotime ( at = None ) : <EOL> """<STR_LIT>""" <EOL> return strtime ( at , ISO_TIME_FORMAT ) <EOL> def parse_isotime ( timestr ) : <EOL> """<STR_LIT>""" <EOL> return parse_strtime ( timestr , ISO_TIME_FORMAT ) <EOL> def parse_mailmap ( mailmap = '<STR_LIT>' ) : <EOL> mapping = { } <EOL> if os . path . exists ( mailmap ) : <EOL> fp = open ( mailmap , '<STR_LIT:r>' ) <EOL> for l in fp : <EOL> l = l . strip ( ) <EOL> if not l . startswith ( '<STR_LIT:#>' ) and '<STR_LIT:U+0020>' in l : <EOL> canonical_email , alias = l . split ( '<STR_LIT:U+0020>' ) <EOL> mapping [ alias ] = canonical_email <EOL> return mapping <EOL> def str_dict_replace ( s , mapping ) : <EOL> for s1 , s2 in mapping . iteritems ( ) : <EOL> s = s . replace ( s1 , s2 ) <EOL> return s <EOL> class LazyPluggable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pivot , ** backends ) : <EOL> self . __backends = backends <EOL> self . __pivot = pivot <EOL> self . __backend = None <EOL> def __get_backend ( self ) : <EOL> if not self . __backend : <EOL> backend_name = self . __pivot . value <EOL> if backend_name not in self . __backends : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) % backend_name ) <EOL> backend = self . __backends [ backend_name ] <EOL> if type ( backend ) == type ( tuple ( ) ) : <EOL> name = backend [ <NUM_LIT:0> ] <EOL> fromlist = backend [ <NUM_LIT:1> ] <EOL> else : <EOL> name = backend <EOL> fromlist = backend <EOL> self . __backend = __import__ ( name , None , None , fromlist ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , self . __backend ) <EOL> return self . __backend <EOL> def __getattr__ ( self , key ) : <EOL> backend = self . __get_backend ( ) <EOL> return getattr ( backend , key ) <EOL> class LoopingCallDone ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , retvalue = True ) : <EOL> """<STR_LIT>""" <EOL> self . retvalue = retvalue <EOL> class LoopingCall ( object ) : <EOL> def __init__ ( self , f = None , * args , ** kw ) : <EOL> self . args = args <EOL> self . kw = kw <EOL> self . f = f <EOL> self . _running = False <EOL> def start ( self , interval , now = True ) : <EOL> self . _running = True <EOL> done = event . Event ( ) <EOL> def _inner ( ) : <EOL> if not now : <EOL> greenthread . sleep ( interval ) <EOL> try : <EOL> while self . _running : <EOL> self . f ( * self . args , ** self . kw ) <EOL> if not self . _running : <EOL> break <EOL> greenthread . sleep ( interval ) <EOL> except LoopingCallDone , e : <EOL> self . stop ( ) <EOL> done . send ( e . retvalue ) <EOL> except Exception : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> done . send_exception ( * sys . exc_info ( ) ) <EOL> return <EOL> else : <EOL> done . send ( True ) <EOL> self . done = done <EOL> greenthread . spawn ( _inner ) <EOL> return self . done <EOL> def stop ( self ) : <EOL> self . _running = False <EOL> def wait ( self ) : <EOL> return self . done . wait ( ) <EOL> def xhtml_escape ( value ) : <EOL> """<STR_LIT>""" <EOL> return saxutils . escape ( value , { '<STR_LIT:">' : '<STR_LIT>' } ) <EOL> def utf8 ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( '<STR_LIT:utf-8>' ) <EOL> assert isinstance ( value , str ) <EOL> return value <EOL> def to_primitive ( value , convert_instances = False , level = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> nasty = [ inspect . ismodule , inspect . isclass , inspect . ismethod , <EOL> inspect . isfunction , inspect . isgeneratorfunction , <EOL> inspect . isgenerator , inspect . istraceback , inspect . isframe , <EOL> inspect . iscode , inspect . isbuiltin , inspect . isroutine , <EOL> inspect . isabstract ] <EOL> for test in nasty : <EOL> if test ( value ) : <EOL> return unicode ( value ) <EOL> if level > <NUM_LIT:3> : <EOL> return '<STR_LIT:?>' <EOL> try : <EOL> if type ( value ) is type ( [ ] ) or type ( value ) is type ( ( None , ) ) : <EOL> o = [ ] <EOL> for v in value : <EOL> o . append ( to_primitive ( v , convert_instances = convert_instances , <EOL> level = level ) ) <EOL> return o <EOL> elif type ( value ) is type ( { } ) : <EOL> o = { } <EOL> for k , v in value . iteritems ( ) : <EOL> o [ k ] = to_primitive ( v , convert_instances = convert_instances , <EOL> level = level ) <EOL> return o <EOL> elif isinstance ( value , datetime . datetime ) : <EOL> return str ( value ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> return to_primitive ( dict ( value . iteritems ( ) ) , <EOL> convert_instances = convert_instances , <EOL> level = level ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> return to_primitive ( list ( value ) , level ) <EOL> elif convert_instances and hasattr ( value , '<STR_LIT>' ) : <EOL> return to_primitive ( value . __dict__ , <EOL> convert_instances = convert_instances , <EOL> level = level + <NUM_LIT:1> ) <EOL> else : <EOL> return value <EOL> except TypeError , e : <EOL> return unicode ( value ) <EOL> def dumps ( value ) : <EOL> try : <EOL> return json . dumps ( value ) <EOL> except TypeError : <EOL> pass <EOL> return json . dumps ( to_primitive ( value ) ) <EOL> def loads ( s ) : <EOL> return json . loads ( s ) <EOL> try : <EOL> import anyjson <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> anyjson . _modules . append ( ( "<STR_LIT>" , "<STR_LIT>" , TypeError , <EOL> "<STR_LIT>" , ValueError ) ) <EOL> anyjson . force_implementation ( "<STR_LIT>" ) <EOL> _semaphores = { } <EOL> class _NoopContextManager ( object ) : <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> pass <EOL> def synchronized ( name , external = False ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( f ) : <EOL> @ functools . wraps ( f ) <EOL> def inner ( * args , ** kwargs ) : <EOL> if name not in _semaphores : <EOL> _semaphores [ name ] = semaphore . Semaphore ( ) <EOL> sem = _semaphores [ name ] <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' % { '<STR_LIT>' : name , <EOL> '<STR_LIT>' : f . __name__ } ) ) <EOL> with sem : <EOL> if external : <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> { '<STR_LIT>' : name , '<STR_LIT>' : f . __name__ } ) ) <EOL> lock_file_path = os . path . join ( FLAGS . lock_path , <EOL> '<STR_LIT>' % name ) <EOL> lock = lockfile . FileLock ( lock_file_path ) <EOL> else : <EOL> lock = _NoopContextManager ( ) <EOL> with lock : <EOL> retval = f ( * args , ** kwargs ) <EOL> if not sem . balance < <NUM_LIT:1> : <EOL> del _semaphores [ name ] <EOL> return retval <EOL> return inner <EOL> return wrap <EOL> def get_from_path ( items , path ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> raise exception . Error ( '<STR_LIT>' ) <EOL> ( first_token , sep , remainder ) = path . partition ( '<STR_LIT:/>' ) <EOL> if first_token == '<STR_LIT>' : <EOL> raise exception . Error ( '<STR_LIT>' ) <EOL> results = [ ] <EOL> if items is None : <EOL> return results <EOL> if not isinstance ( items , types . ListType ) : <EOL> items = [ items ] <EOL> for item in items : <EOL> if item is None : <EOL> continue <EOL> get_method = getattr ( item , '<STR_LIT>' , None ) <EOL> if get_method is None : <EOL> continue <EOL> child = get_method ( first_token ) <EOL> if child is None : <EOL> continue <EOL> if isinstance ( child , types . ListType ) : <EOL> for x in child : <EOL> results . append ( x ) <EOL> else : <EOL> results . append ( child ) <EOL> if not sep : <EOL> return results <EOL> else : <EOL> return get_from_path ( results , remainder ) <EOL> def flatten_dict ( dict_ , flattened = None ) : <EOL> """<STR_LIT>""" <EOL> flattened = flattened or { } <EOL> for key , value in dict_ . iteritems ( ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> flatten_dict ( value , flattened ) <EOL> else : <EOL> flattened [ key ] = value <EOL> return flattened <EOL> def partition_dict ( dict_ , keys ) : <EOL> """<STR_LIT>""" <EOL> intersection = { } <EOL> difference = { } <EOL> for key , value in dict_ . iteritems ( ) : <EOL> if key in keys : <EOL> intersection [ key ] = value <EOL> else : <EOL> difference [ key ] = value <EOL> return intersection , difference <EOL> def map_dict_keys ( dict_ , key_map ) : <EOL> """<STR_LIT>""" <EOL> mapped = { } <EOL> for key , value in dict_ . iteritems ( ) : <EOL> mapped_key = key_map [ key ] if key in key_map else key <EOL> mapped [ mapped_key ] = value <EOL> return mapped <EOL> def subset_dict ( dict_ , keys ) : <EOL> """<STR_LIT>""" <EOL> subset = partition_dict ( dict_ , keys ) [ <NUM_LIT:0> ] <EOL> return subset <EOL> def check_isinstance ( obj , cls ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , cls ) : <EOL> return obj <EOL> raise Exception ( _ ( '<STR_LIT>' ) % ( str ( cls ) ) ) <EOL> return cls ( ) <EOL> def parse_server_string ( server_str ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if netaddr . valid_ipv6 ( server_str ) : <EOL> return ( server_str , '<STR_LIT>' ) <EOL> if server_str . find ( "<STR_LIT>" ) != - <NUM_LIT:1> : <EOL> ( address , port ) = server_str . replace ( '<STR_LIT:[>' , '<STR_LIT>' , <NUM_LIT:1> ) . split ( '<STR_LIT>' ) <EOL> return ( address , port ) <EOL> if server_str . find ( '<STR_LIT::>' ) == - <NUM_LIT:1> : <EOL> return ( server_str , '<STR_LIT>' ) <EOL> ( address , port ) = server_str . split ( '<STR_LIT::>' ) <EOL> return ( address , port ) <EOL> except Exception : <EOL> LOG . debug ( _ ( '<STR_LIT>' % server_str ) ) <EOL> return ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def gen_uuid ( ) : <EOL> return uuid . uuid4 ( ) <EOL> def is_uuid_like ( val ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( val , basestring ) : <EOL> return False <EOL> return ( len ( val ) == <NUM_LIT> ) and ( val . count ( '<STR_LIT:->' ) == <NUM_LIT:4> ) <EOL> def bool_from_str ( val ) : <EOL> """<STR_LIT>""" <EOL> if not val : <EOL> return False <EOL> try : <EOL> return True if int ( val ) else False <EOL> except ValueError : <EOL> return val . lower ( ) == '<STR_LIT:true>' <EOL> def is_valid_ipv4 ( address ) : <EOL> """<STR_LIT>""" <EOL> parts = address . split ( "<STR_LIT:.>" ) <EOL> if len ( parts ) != <NUM_LIT:4> : <EOL> return False <EOL> for item in parts : <EOL> try : <EOL> if not <NUM_LIT:0> <= int ( item ) <= <NUM_LIT:255> : <EOL> return False <EOL> except ValueError : <EOL> return False <EOL> return True <EOL> def monkey_patch ( ) : <EOL> """<STR_LIT>""" <EOL> if not FLAGS . monkey_patch : <EOL> return <EOL> for module_and_decorator in FLAGS . monkey_patch_modules : <EOL> module , decorator_name = module_and_decorator . split ( '<STR_LIT::>' ) <EOL> decorator = import_class ( decorator_name ) <EOL> __import__ ( module ) <EOL> module_data = pyclbr . readmodule_ex ( module ) <EOL> for key in module_data . keys ( ) : <EOL> if isinstance ( module_data [ key ] , pyclbr . Class ) : <EOL> clz = import_class ( "<STR_LIT>" % ( module , key ) ) <EOL> for method , func in inspect . getmembers ( clz , inspect . ismethod ) : <EOL> setattr ( clz , method , decorator ( "<STR_LIT>" % ( module , key , method ) , func ) ) <EOL> if isinstance ( module_data [ key ] , pyclbr . Function ) : <EOL> func = import_class ( "<STR_LIT>" % ( module , key ) ) <EOL> setattr ( sys . modules [ module ] , key , decorator ( "<STR_LIT>" % ( module , key ) , func ) ) <EOL> def convert_to_list_dict ( lst , label ) : <EOL> """<STR_LIT>""" <EOL> if not lst : <EOL> return None <EOL> if not isinstance ( lst , list ) : <EOL> lst = [ lst ] <EOL> return [ { label : x } for x in lst ] <EOL> @ contextlib . contextmanager <EOL> def save_and_reraise_exception ( ) : <EOL> """<STR_LIT>""" <EOL> type_ , value , traceback = sys . exc_info ( ) <EOL> try : <EOL> yield <EOL> except : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , <EOL> exc_info = ( type_ , value , traceback ) ) <EOL> raise <EOL> raise type_ , value , traceback </s>
