<s> import os <EOL> import sh <EOL> import tempfile <EOL> import logging <EOL> import logging . config <EOL> from helion_configurationprocessor . cp . model . BuilderPlugin import BuilderPlugin <EOL> from helion_configurationprocessor . cp . model . BuilderPlugin import ArtifactMode <EOL> from helion_configurationprocessor . cp . model . CPSecurity import CPSecurity <EOL> from helion_configurationprocessor . cp . model . CPLogging import CPLogging as KenLog <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class AnsEncryptArtifactsBuilder ( BuilderPlugin ) : <EOL> def __init__ ( self , instructions , models , controllers ) : <EOL> super ( AnsEncryptArtifactsBuilder , self ) . __init__ ( <EOL> <NUM_LIT:1> , instructions , models , controllers , <EOL> '<STR_LIT>' ) <EOL> LOG . info ( '<STR_LIT>' % KenLog . fcn ( ) ) <EOL> cloud_config = controllers [ '<STR_LIT>' ] <EOL> self . _output_path = '<STR_LIT>' % cloud_config . get_output_path ( models ) <EOL> self . _modules = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . _pw_file = tempfile . mkstemp ( suffix = '<STR_LIT>' , prefix = '<STR_LIT>' , text = True ) <EOL> def is_compatible_with_cloud ( self , args ) : <EOL> return True <EOL> def build ( self ) : <EOL> LOG . info ( '<STR_LIT>' % KenLog . fcn ( ) ) <EOL> if '<STR_LIT>' not in self . _instructions : <EOL> return True <EOL> self . _create_password_file ( ) <EOL> try : <EOL> for elem_m in self . _modules : <EOL> dir_name = os . path . join ( self . _output_path , elem_m ) <EOL> self . _encrypt_directory ( dir_name ) <EOL> except Exception : <EOL> pass <EOL> finally : <EOL> self . _destroy_password_file ( ) <EOL> return True <EOL> def _create_password_file ( self ) : <EOL> pw_file_name = self . _pw_file [ <NUM_LIT:1> ] <EOL> fp = open ( pw_file_name , '<STR_LIT:w>' ) <EOL> fp . write ( CPSecurity . decode_key ( self . _instructions [ <EOL> '<STR_LIT>' ] ) ) <EOL> fp . close ( ) <EOL> def _destroy_password_file ( self ) : <EOL> pw_file_name = self . _pw_file [ <NUM_LIT:1> ] <EOL> if os . path . exists ( pw_file_name ) : <EOL> fp = open ( pw_file_name , '<STR_LIT:w>' ) <EOL> fp . write ( '<STR_LIT:*>' * <NUM_LIT> ) <EOL> fp . close ( ) <EOL> os . remove ( pw_file_name ) <EOL> def _decrypt_file_contents ( self , file_name ) : <EOL> fp = open ( file_name , '<STR_LIT:r>' ) <EOL> lines = fp . readlines ( ) <EOL> fp . close ( ) <EOL> need_to_write = False <EOL> for i in range ( len ( lines ) ) : <EOL> line = lines [ i ] <EOL> if line . find ( '<STR_LIT:=>' ) : <EOL> tokens = line . split ( ) <EOL> for t in tokens : <EOL> if t . endswith ( '<STR_LIT:=>' ) : <EOL> plaintext = CPSecurity . decrypt ( <EOL> self . _instructions [ '<STR_LIT>' ] , t ) <EOL> if plaintext is not None : <EOL> lines [ i ] = line . replace ( t , plaintext ) <EOL> need_to_write = True <EOL> if need_to_write : <EOL> fp = open ( file_name , '<STR_LIT:w>' ) <EOL> for line in lines : <EOL> fp . write ( line ) <EOL> fp . close ( ) <EOL> def _encrypt_file ( self , file_name ) : <EOL> pw_file_name = self . _pw_file [ <NUM_LIT:1> ] <EOL> pw_file_arg = '<STR_LIT>' % pw_file_name <EOL> self . add_artifact ( file_name , ArtifactMode . ENCRYPTED ) <EOL> sh . ansible_vault . encrypt ( file_name , pw_file_arg ) <EOL> def _encrypt_directory ( self , directory ) : <EOL> for root , dirs , files in os . walk ( directory ) : <EOL> for f in files : <EOL> file_name = os . path . join ( root , f ) <EOL> if os . path . isfile ( file_name ) : <EOL> self . _decrypt_file_contents ( file_name ) <EOL> self . _encrypt_file ( file_name ) <EOL> def get_dependencies ( self ) : <EOL> return [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] </s>
