1010
1111import oracle .weblogic .deploy .aliases .AliasException as AliasException
1212import oracle .weblogic .deploy .json .JsonException as JsonException
13- import oracle .weblogic .deploy .util .PyOrderedDict as PyOrderedDict
1413import oracle .weblogic .deploy .util .VariableException as VariableException
1514
1615import wlsdeploy .util .model as model_sections
3332VARIABLE_FILE_NAME_ARG = 'variable_file_name'
3433VARIABLE_FILE_NAME = 'variables.json'
3534VARIABLE_FILE_APPEND_ARG = 'append_to_variables'
35+ VARIABLE_FILE_APPEND = 'append'
36+ VARIABLE_FILE_UPDATE = 'update'
37+ VARIABLE_FILE_APPEND_VALS = [VARIABLE_FILE_APPEND , VARIABLE_FILE_UPDATE ]
3638# custom keyword in model injector file
3739CUSTOM_KEYWORD = 'CUSTOM'
3840KEYWORD_FILES = 'file-list'
5961
6062class VariableInjector (object ):
6163
62- def __init__ (self , model , model_context = None , version = None ):
64+ def __init__ (self , program_name , model , model_context = None , version = None ):
65+ """
66+ Construct an instance of the injector with the model and information used by the injector.
67+ :param program_name: name of the calling tool
68+ :param model: to be updated with variables
69+ :param model_context: context with command line information
70+ :param version: of model if model context is not provided
71+ """
72+ self .__program_name = program_name
6373 self .__original = copy .deepcopy (model )
6474 self .__model = model
6575 self .__model_context = model_context
@@ -86,7 +96,7 @@ def inject_variables_keyword_file(self, **kwargs):
8696 _logger .entering (class_name = _class_name , method_name = _method_name )
8797
8898 variable_injector_location_file = _get_variable_injector_file_name (** kwargs )
89- variables_injector_dictionary = _load_variables_file (variable_injector_location_file )
99+ variables_injector_dictionary = _load_variable_injector_file (variable_injector_location_file )
90100 variable_keywords_location_file = _get_variable_keywords_file_name (** kwargs )
91101 keywords_dictionary = _load_keywords_file (variable_keywords_location_file )
92102
@@ -101,12 +111,14 @@ def inject_variables_keyword_file(self, **kwargs):
101111 else :
102112 _logger .info ('WLSDPLY-19533' , variable_injector_location_file , class_name = _class_name ,
103113 method_name = _method_name )
104- variable_file_location = self . _replace_tokens (variable_file_location )
114+ append , stage_dictionary = _load_variable_file (variable_file_location , ** kwargs )
105115 injector_file_list = _create_injector_file_list (variables_injector_dictionary , keywords_dictionary ,
106116 _get_keyword_files_location (** kwargs ))
107117 variables_file_dictionary = self .inject_variables_keyword_dictionary (injector_file_list )
108- variables_inserted = _write_variables_file (variables_file_dictionary , variable_file_location ,
109- _get_append_to_variable_file (** kwargs ))
118+ if variables_file_dictionary :
119+ stage_dictionary .update (variables_file_dictionary )
120+ variables_inserted = self ._write_variables_file (stage_dictionary , variable_file_location ,
121+ append )
110122 if variables_inserted :
111123 _logger .info ('WLSDPLY-19518' , variable_file_location , class_name = _class_name ,
112124 method_name = _method_name )
@@ -124,20 +136,20 @@ def inject_variables_keyword_file(self, **kwargs):
124136 def inject_variables_keyword_dictionary (self , injector_file_list ):
125137 """
126138 Takes a variable keyword dictionary and returns a variables for file in a dictionary
127- :param injector_file_list:
128- :return:
139+ :param injector_file_list: list of injector files for processing variable injection
140+ :return: variables_dictionary containing the variable properties to persist to the variable file
129141 """
130142 _method_name = 'inject_variables_keyword_dictionary'
131143 _logger .entering (injector_file_list , class_name = _class_name , method_name = _method_name )
132- variables_dictionary = PyOrderedDict ()
144+ variable_dictionary = dict ()
133145 for filename in injector_file_list :
134146 injector_dictionary = _load_injector_file (self ._replace_tokens (filename ))
135147 entries = self .inject_variables (injector_dictionary )
136148 if entries :
137149 _logger .finer ('WLSDPLY-19513' , filename , class_name = _class_name , method_name = _method_name )
138- variables_dictionary .update (entries )
139- _logger .exiting (class_name = _class_name , method_name = _method_name , result = variables_dictionary )
140- return variables_dictionary
150+ variable_dictionary .update (entries )
151+ _logger .exiting (class_name = _class_name , method_name = _method_name , result = variable_dictionary )
152+ return variable_dictionary
141153
142154 def inject_variables (self , injector_dictionary ):
143155 """
@@ -422,7 +434,7 @@ def _log_mbean_not_found(self, mbean, replacement, location):
422434 code = ValidationCodes .INVALID
423435 try :
424436 code , __ = self .__aliases .is_valid_model_folder_name (location , mbean )
425- except AliasException , ae :
437+ except AliasException :
426438 pass
427439 if code == ValidationCodes .INVALID :
428440 _logger .warning ('WLSDPLY-19515' , mbean , replacement , location .get_folder_path (),
@@ -444,8 +456,46 @@ def _get_variable_file_name(self, variables_injector_dictionary, **kwargs):
444456 _logger .finer ('WLSDPLY-19521' , variable_file_location , class_name = _class_name , method_name = _method_name )
445457 else :
446458 variable_file_location = variables .get_default_variable_file_name (self .__model_context )
459+ if variable_file_location :
460+ variable_file_location = self ._replace_tokens (variable_file_location )
447461 return variable_file_location
448462
463+ def _write_variables_file (self , variables_dictionary , variables_file_name , append ):
464+ _method_name = '_write_variables_file'
465+ _logger .entering (variables_dictionary , variables_file_name , class_name = _class_name , method_name = _method_name )
466+
467+ written = False
468+ if variables_dictionary :
469+ try :
470+ variables .write_variables (self .__program_name , variables_dictionary , variables_file_name , append )
471+ written = True
472+ except VariableException , ve :
473+ _logger .warning ('WLSDPLY-19507' , variables_file_name , ve .getLocalizedMessage (), class_name = _class_name ,
474+ method_name = _method_name )
475+ _logger .exiting (class_name = _class_name , method_name = _method_name , result = written )
476+ return written
477+
478+
479+ def _load_variable_file (variable_file_location , ** kwargs ):
480+ _method_name = '_load_variable_file'
481+ append = False
482+ variable_dictionary = dict ()
483+ if VARIABLE_FILE_APPEND_ARG in kwargs and kwargs [VARIABLE_FILE_APPEND_ARG ] in VARIABLE_FILE_APPEND_VALS :
484+ _logger .fine ('append argument found {0}' , kwargs [VARIABLE_FILE_APPEND_ARG ] )
485+ if kwargs [VARIABLE_FILE_APPEND_ARG ] == VARIABLE_FILE_APPEND :
486+ _logger .fine ('WLSDPLY-19536' , variable_file_location , class_name = _class_name , method_name = _method_name )
487+ append = True
488+ elif kwargs [VARIABLE_FILE_APPEND_ARG ] == VARIABLE_FILE_UPDATE and os .path .isfile (variable_file_location ):
489+ _logger .fine ('WLSDPLY-19534' , variable_file_location , class_name = _class_name , method_name = _method_name )
490+ try :
491+ variable_dictionary = variables .load_variables (variable_file_location )
492+ except VariableException , ve :
493+ _logger .warning ('WLSDPLY-19537' , variable_file_location , ve .getLocalizedMessage (),
494+ class_name = _class_name , method_name = _method_name )
495+ else :
496+ _logger .fine ('WLSDPLY-19535' , variable_file_location , class_name = _class_name , method_name = _method_name )
497+ return append , variable_dictionary
498+
449499
450500def _get_variable_injector_file_name (** kwargs ):
451501 variable_injector_file_name = VARIABLE_INJECTOR_FILE_NAME
@@ -467,7 +517,7 @@ def _get_variable_keywords_file_name(**kwargs):
467517 return os .path .join (_wlsdeploy_location , DEFAULT_FILE_LOCATION , variable_keywords_file_name )
468518
469519
470- def _load_variables_file (variable_injector_location ):
520+ def _load_variable_injector_file (variable_injector_location ):
471521 _method_name = '_load_variables_file'
472522 _logger .entering (variable_injector_location , class_name = _class_name , method_name = _method_name )
473523 variables_dictionary = None
@@ -555,20 +605,6 @@ def _get_append_to_variable_file(**kwargs):
555605 return False
556606
557607
558- def _write_variables_file (variables_dictionary , variables_file_name , append ):
559- _method_name = '_write_variables_file'
560- _logger .entering (variables_dictionary , variables_file_name , class_name = _class_name , method_name = _method_name )
561-
562- written = False
563- if variables_dictionary :
564- try :
565- variables .write_variables (variables_dictionary , variables_file_name , append )
566- written = True
567- except VariableException , ve :
568- _logger .warning ('WLSDPLY-19507' , variables_file_name , ve .getLocalizedMessage (), class_name = _class_name ,
569- method_name = _method_name )
570- _logger .exiting (class_name = _class_name , method_name = _method_name , result = written )
571- return written
572608
573609
574610def _format_variable_value (value ):
0 commit comments