<s> from __future__ import division , unicode_literals , print_function <EOL> import re <EOL> from copy import deepcopy <EOL> from hashlib import sha1 <EOL> from types import FunctionType <EOL> import six <EOL> from jinja2 import Template <EOL> from . attrs import AttrBase <EOL> BLOCKSIZE = <NUM_LIT> <EOL> FILES = { } <EOL> TEMPLATES = { } <EOL> def get_file ( filename ) : <EOL> '''<STR_LIT>''' <EOL> if filename in FILES : <EOL> FILES [ filename ] . seek ( <NUM_LIT:0> ) <EOL> return FILES [ filename ] <EOL> FILES [ filename ] = open ( filename , '<STR_LIT:r>' ) <EOL> return FILES [ filename ] <EOL> def get_template ( string ) : <EOL> '''<STR_LIT>''' <EOL> cache_key = sha1_hash ( string ) <EOL> if cache_key in TEMPLATES : <EOL> return TEMPLATES [ cache_key ] <EOL> TEMPLATES [ cache_key ] = Template ( string , keep_trailing_newline = True ) <EOL> return TEMPLATES [ cache_key ] <EOL> def underscore ( name ) : <EOL> '''<STR_LIT>''' <EOL> s1 = re . sub ( '<STR_LIT>' , r'<STR_LIT>' , name ) <EOL> return re . sub ( '<STR_LIT>' , r'<STR_LIT>' , s1 ) . lower ( ) <EOL> def sha1_hash ( string ) : <EOL> '''<STR_LIT>''' <EOL> hasher = sha1 ( ) <EOL> hasher . update ( string . encode ( ) ) <EOL> return hasher . hexdigest ( ) <EOL> def make_command ( command , env = None , sudo = False , sudo_user = None ) : <EOL> '''<STR_LIT>''' <EOL> if env : <EOL> env_string = '<STR_LIT:U+0020>' . join ( [ <EOL> '<STR_LIT>' . format ( key , value ) <EOL> for key , value in six . iteritems ( env ) <EOL> ] ) <EOL> command = '<STR_LIT>' . format ( env_string , command ) <EOL> command = command . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> if not sudo : <EOL> command = "<STR_LIT>" . format ( command ) <EOL> else : <EOL> if sudo_user : <EOL> command = "<STR_LIT>" . format ( sudo_user , command ) <EOL> else : <EOL> command = "<STR_LIT>" . format ( command ) <EOL> return command <EOL> def get_arg_value ( state , host , arg ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( arg , six . string_types ) : <EOL> template = get_template ( arg ) <EOL> data = { <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT>' : state . inventory <EOL> } <EOL> return template . render ( data ) <EOL> elif isinstance ( arg , list ) : <EOL> return [ get_arg_value ( state , host , value ) for value in arg ] <EOL> elif isinstance ( arg , dict ) : <EOL> return { <EOL> key : get_arg_value ( state , host , value ) <EOL> for key , value in six . iteritems ( arg ) <EOL> } <EOL> return arg <EOL> def get_arg_name ( arg ) : <EOL> '''<STR_LIT>''' <EOL> return ( <EOL> arg . pyinfra_attr_key <EOL> if isinstance ( arg , AttrBase ) <EOL> else arg . __name__ <EOL> if isinstance ( arg , FunctionType ) <EOL> else arg <EOL> ) <EOL> def make_hash ( obj ) : <EOL> '''<STR_LIT>''' <EOL> if type ( obj ) in ( set , tuple , list ) : <EOL> return hash ( tuple ( [ make_hash ( e ) for e in obj ] ) ) <EOL> elif not isinstance ( obj , dict ) : <EOL> return hash ( obj ) <EOL> new_obj = deepcopy ( obj ) <EOL> for k , v in new_obj . items ( ) : <EOL> new_obj [ k ] = make_hash ( v ) <EOL> return hash ( tuple ( set ( new_obj . items ( ) ) ) ) <EOL> def get_file_sha1 ( io ) : <EOL> '''<STR_LIT>''' <EOL> io . seek ( <NUM_LIT:0> ) <EOL> buff = io . read ( BLOCKSIZE ) <EOL> hasher = sha1 ( ) <EOL> while len ( buff ) > <NUM_LIT:0> : <EOL> hasher . update ( buff . encode ( ) ) <EOL> buff = io . read ( BLOCKSIZE ) <EOL> io . seek ( <NUM_LIT:0> ) <EOL> return hasher . hexdigest ( ) <EOL> def read_buffer ( buff , print_output = False , print_func = False ) : <EOL> '''<STR_LIT>''' <EOL> out = [ ] <EOL> for line in buff : <EOL> if not isinstance ( line , six . text_type ) : <EOL> line = line . decode ( '<STR_LIT:utf-8>' ) <EOL> line = line . strip ( ) <EOL> out . append ( line ) <EOL> if print_output : <EOL> if print_func : <EOL> print ( print_func ( line ) ) <EOL> else : <EOL> print ( line ) <EOL> return out </s>
