Skip to content

Commit 521afb2

Browse files
authored
Fix compareModel stack dump when variables file not specified (#734)
* JIRA WDT-467 - Pass empty variables map to validate to prevent crash; remove PEP-8 warnings * JIRA WDT-467 - Re-ordered some imports
1 parent 95011ca commit 521afb2

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

core/src/main/python/compare_model.py

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,42 @@
1111
#
1212
# If the flag is not provided then all output is written to the standard out.
1313
#
14-
#
15-
14+
import os
1615
import sets
17-
import sys, os, traceback
16+
import sys
17+
import traceback
1818

19-
from java.lang import System
2019
import java.io.File as JFile
2120
import java.io.FileOutputStream as JFileOutputStream
2221
import java.io.IOException as JIOException
2322
import java.io.PrintWriter as JPrintWriter
24-
import oracle.weblogic.deploy.util.TranslateException as TranslateException
23+
from java.lang import System
24+
from oracle.weblogic.deploy.aliases import AliasException
25+
from oracle.weblogic.deploy.compare import CompareException
26+
from oracle.weblogic.deploy.exception import ExceptionHelper
2527
from oracle.weblogic.deploy.util import CLAException
2628
from oracle.weblogic.deploy.util import FileUtils
2729
from oracle.weblogic.deploy.util import PyOrderedDict
28-
from oracle.weblogic.deploy.util import VariableException
29-
from oracle.weblogic.deploy.compare import CompareException
30-
from oracle.weblogic.deploy.exception import ExceptionHelper
31-
from oracle.weblogic.deploy.aliases import AliasException
3230
from oracle.weblogic.deploy.util import PyWLSTException
33-
from wlsdeploy.exception import exception_helper
31+
from oracle.weblogic.deploy.util import VariableException
32+
from oracle.weblogic.deploy.validate import ValidateException
3433

34+
import oracle.weblogic.deploy.util.TranslateException as TranslateException
35+
from wlsdeploy.aliases.aliases import Aliases
36+
from wlsdeploy.aliases.location_context import LocationContext
37+
from wlsdeploy.aliases.wlst_modes import WlstModes
38+
from wlsdeploy.exception import exception_helper
39+
from wlsdeploy.exception.expection_types import ExceptionType
40+
from wlsdeploy.json.json_translator import COMMENT_MATCH
41+
from wlsdeploy.json.json_translator import PythonToJson
3542
from wlsdeploy.logging.platform_logger import PlatformLogger
43+
from wlsdeploy.tool.validate.validator import Validator
3644
from wlsdeploy.util import cla_helper
3745
from wlsdeploy.util import variables
38-
from wlsdeploy.json.json_translator import COMMENT_MATCH
39-
from wlsdeploy.util.model_translator import FileToPython
40-
from wlsdeploy.yaml.yaml_translator import PythonToYaml
41-
from wlsdeploy.json.json_translator import PythonToJson
4246
from wlsdeploy.util.cla_utils import CommandLineArgUtil
43-
from wlsdeploy.aliases.aliases import Aliases
44-
from wlsdeploy.aliases.wlst_modes import WlstModes
45-
from wlsdeploy.aliases.location_context import LocationContext
4647
from wlsdeploy.util.model_context import ModelContext
47-
from wlsdeploy.tool.validate.validator import Validator
48-
from oracle.weblogic.deploy.validate import ValidateException
49-
from wlsdeploy.exception.expection_types import ExceptionType
48+
from wlsdeploy.util.model_translator import FileToPython
49+
from wlsdeploy.yaml.yaml_translator import PythonToYaml
5050

5151
VALIDATION_FAIL = 2
5252
PATH_TOKEN = '|'
@@ -136,49 +136,49 @@ def recursive_changed_detail(self, key, token, root):
136136
"""
137137
debug("DEBUG: Entering recursive_changed_detail key=%s token=%s root=%s", key, token, root)
138138

139-
a=ModelDiffer(self.current_dict[key], self.past_dict[key])
140-
diff=a.changed()
141-
added=a.added()
142-
removed=a.removed()
143-
saved_token=token
139+
a = ModelDiffer(self.current_dict[key], self.past_dict[key])
140+
diff = a.changed()
141+
added = a.added()
142+
removed = a.removed()
143+
saved_token = token
144144

145145
debug('DEBUG: In recursive changed detail %s', diff)
146146
debug('DEBUG: In recursive added detail %s', added)
147147
if len(diff) > 0:
148148
for o in diff:
149-
token=saved_token
149+
token = saved_token
150150
# The token is a | separated string that is used to parse and rebuilt the structure later
151151
debug('DEBUG: in recursive changed detail walking down 1 %s', o)
152-
token=token+PATH_TOKEN+o
152+
token = token + PATH_TOKEN + o
153153
if a.is_dict(o):
154154
debug('DEBUG: in recursive changed detail walking down 2 %s', token)
155-
a.recursive_changed_detail(o,token, root)
156-
last=token.rfind(PATH_TOKEN)
157-
token=root
155+
a.recursive_changed_detail(o, token, root)
156+
last = token.rfind(PATH_TOKEN)
157+
token = root
158158
else:
159159
all_changes.append(token)
160-
last=token.rfind(PATH_TOKEN)
161-
token=root
160+
last = token.rfind(PATH_TOKEN)
161+
token = root
162162

163163
# already out of recursive calls, add all entries from current dictionary
164164
# resources.JDBCSubsystemResources.* (note it may not have the lower level nodes
165-
added_token=token
166-
debug('DEBUG: current added token %s' , added_token)
165+
added_token = token
166+
debug('DEBUG: current added token %s', added_token)
167167
if len(added) > 0:
168168
for item in added:
169-
token=saved_token
169+
token = saved_token
170170
debug('DEBUG: recursive added token %s item %s ', token, item)
171171
all_added.append(token + PATH_TOKEN + item)
172172

173173
# We don't really care about this, just put something here is enough
174174
if len(removed) > 0:
175175
for item in removed:
176-
token=saved_token
176+
token = saved_token
177177
debug('DEBUG: removed %s', item)
178178
all_removed.append(token + PATH_TOKEN + item)
179179
debug('DEBUG: Exiting recursive_changed_detail')
180180

181-
def is_dict(self,key):
181+
def is_dict(self, key):
182182
"""
183183
Check to see if the ke in the current dictionary is a dictionary.
184184
:param key: key of the dictionary
@@ -238,7 +238,7 @@ def calculate_changed_model(self):
238238
raise ex
239239
except AliasException, ae:
240240
_logger.severe('WLSDPLY-05709', ae.getLocalizedMessage(),
241-
error=ae, class_name=_class_name, method_name=_method_name)
241+
error=ae, class_name=_class_name, method_name=_method_name)
242242
ex = exception_helper.create_compare_exception(ae.getLocalizedMessage(), error=ae)
243243
_logger.throwing(ex, class_name=_class_name, method_name=_method_name)
244244
raise ex
@@ -251,7 +251,7 @@ def _is_alias_folder(self, path):
251251
"""
252252
debug("DEBUG: Entering is_alias_folder %s", path)
253253
path_tokens = path.split(PATH_TOKEN)
254-
model_context = ModelContext("test", { })
254+
model_context = ModelContext("test", {})
255255
location = LocationContext()
256256
last_token = path_tokens[-1]
257257
aliases = Aliases(model_context, wlst_mode=WlstModes.OFFLINE, exception_type=ExceptionType.COMPARE)
@@ -290,9 +290,9 @@ def _add_results(self, ar_changes, is_delete=False, is_change=False):
290290
# Skipp adding if it is a delete of an attribute
291291
found_in_allowable_delete = self._is_alias_folder(item)
292292
if not found_in_allowable_delete:
293-
compare_msgs.add(('WLSDPLY-05701',item))
293+
compare_msgs.add(('WLSDPLY-05701', item))
294294
continue
295-
splitted = item.split(PATH_TOKEN,1)
295+
splitted = item.split(PATH_TOKEN, 1)
296296
n = len(splitted)
297297
result = PyOrderedDict()
298298
walked = []
@@ -310,12 +310,12 @@ def _add_results(self, ar_changes, is_delete=False, is_change=False):
310310
else:
311311
result = tmp
312312
walked.append(splitted[0])
313-
splitted = splitted[1].split(PATH_TOKEN,1)
313+
splitted = splitted[1].split(PATH_TOKEN, 1)
314314
n = len(splitted)
315315
#
316316
# result is the dictionary format
317317
#
318-
leaf=result
318+
leaf = result
319319
if is_change:
320320
value_tree = self.past_dict
321321
else:
@@ -343,7 +343,7 @@ def _add_results(self, ar_changes, is_delete=False, is_change=False):
343343
if is_delete:
344344
is_folder_path = self._is_alias_folder(item)
345345
split_delete = item.split(PATH_TOKEN)
346-
#allowable_delete_length = len(allowable_delete.split(PATH_TOKEN))
346+
# allowable_delete_length = len(allowable_delete.split(PATH_TOKEN))
347347
split_delete_length = len(split_delete)
348348
if is_folder_path:
349349
app_key = split_delete[split_delete_length - 1]
@@ -453,8 +453,10 @@ def compare(self):
453453
arg_map[CommandLineArgUtil.MODEL_FILE_SWITCH] = model_file_name
454454
model_context_copy = self.model_context.copy(arg_map)
455455
val_copy = Validator(model_context_copy, aliases, wlst_mode=WlstModes.OFFLINE)
456-
return_code = val_copy.validate_in_standalone_mode(model_dictionary,
457-
None,
456+
457+
# any variables should have been substituted at this point
458+
validate_variables = {}
459+
return_code = val_copy.validate_in_standalone_mode(model_dictionary, validate_variables,
458460
archive_file_name=None)
459461

460462
if return_code == Validator.ReturnCode.STOP:
@@ -470,8 +472,7 @@ def compare(self):
470472
arg_map[CommandLineArgUtil.MODEL_FILE_SWITCH] = model_file_name
471473
model_context_copy = self.model_context.copy(arg_map)
472474
val_copy = Validator(model_context_copy, aliases, wlst_mode=WlstModes.OFFLINE)
473-
return_code = val_copy.validate_in_standalone_mode(model_dictionary,
474-
None,
475+
return_code = val_copy.validate_in_standalone_mode(model_dictionary, validate_variables,
475476
archive_file_name=None)
476477

477478
if return_code == Validator.ReturnCode.STOP:
@@ -480,19 +481,19 @@ def compare(self):
480481
past_dict = model_dictionary
481482
except ValidateException, te:
482483
_logger.severe('WLSDPLY-20009', _program_name, model_file_name, te.getLocalizedMessage(),
483-
error=te, class_name=_class_name, method_name=_method_name)
484+
error=te, class_name=_class_name, method_name=_method_name)
484485
ex = exception_helper.create_compare_exception(te.getLocalizedMessage(), error=te)
485486
_logger.throwing(ex, class_name=_class_name, method_name=_method_name)
486487
return VALIDATION_FAIL
487488
except VariableException, ve:
488489
_logger.severe('WLSDPLY-20009', _program_name, model_file_name, ve.getLocalizedMessage(),
489-
error=ve, class_name=_class_name, method_name=_method_name)
490+
error=ve, class_name=_class_name, method_name=_method_name)
490491
ex = exception_helper.create_compare_exception(ve.getLocalizedMessage(), error=ve)
491492
_logger.throwing(ex, class_name=_class_name, method_name=_method_name)
492493
return VALIDATION_FAIL
493494
except TranslateException, pe:
494495
_logger.severe('WLSDPLY-20009', _program_name, model_file_name, pe.getLocalizedMessage(),
495-
error=pe, class_name=_class_name, method_name=_method_name)
496+
error=pe, class_name=_class_name, method_name=_method_name)
496497
ex = exception_helper.create_compare_exception(pe.getLocalizedMessage(), error=pe)
497498
_logger.throwing(ex, class_name=_class_name, method_name=_method_name)
498499
return VALIDATION_FAIL
@@ -534,7 +535,7 @@ def compare(self):
534535
if writer:
535536
writer.close()
536537
_logger.severe('WLSDPLY-05708', file_name, ioe.getLocalizedMessage(),
537-
error=ioe, class_name=_class_name, method_name=_method_name)
538+
error=ioe, class_name=_class_name, method_name=_method_name)
538539
return 2
539540
else:
540541
print format_message('WLSDPLY-05707')
@@ -583,7 +584,7 @@ def main():
583584
model1 = model_context.get_trailing_argument(0)
584585
model2 = model_context.get_trailing_argument(1)
585586

586-
for f in [ model1, model2 ]:
587+
for f in [model1, model2]:
587588
if not os.path.exists(f):
588589
raise CLAException("Model %s does not exists" % f)
589590
if os.path.isdir(f):
@@ -596,7 +597,7 @@ def main():
596597
raise CLAException("Model extension must be either yaml or json")
597598

598599
if not (FileUtils.isYamlFile(model1_file) and FileUtils.isYamlFile(model2_file)
599-
or FileUtils.isJsonFile(model1_file) and FileUtils.isJsonFile(model2_file)):
600+
or FileUtils.isJsonFile(model1_file) and FileUtils.isJsonFile(model2_file)):
600601
ext = os.path.splitext(model1)[1]
601602
raise CLAException("Model %s is not a %s file " % (model2, ext))
602603

@@ -620,7 +621,7 @@ def main():
620621
for line in compare_msgs:
621622
msg_key = line[0]
622623
msg_value = line[1]
623-
writer.println( "%s. %s" % (index, format_message(msg_key,msg_value.replace(PATH_TOKEN, "-->"))))
624+
writer.println("%s. %s" % (index, format_message(msg_key,msg_value.replace(PATH_TOKEN, "-->"))))
624625
index = index + 1
625626
writer.println(BLANK_LINE)
626627
fos.close()
@@ -631,7 +632,7 @@ def main():
631632
if writer:
632633
writer.close()
633634
_logger.severe('WLSDPLY-05708', file_name, ioe.getLocalizedMessage(),
634-
error=ioe, class_name=_class_name, method_name=_method_name)
635+
error=ioe, class_name=_class_name, method_name=_method_name)
635636
else:
636637
if len(compare_msgs) > 0:
637638
print BLANK_LINE
@@ -650,7 +651,7 @@ def main():
650651
exit_code = 2
651652
if exit_code != CommandLineArgUtil.HELP_EXIT_CODE:
652653
_logger.severe('WLSDPLY-20008', _program_name, ex.getLocalizedMessage(), error=ex,
653-
class_name=_class_name, method_name=_method_name)
654+
class_name=_class_name, method_name=_method_name)
654655
cla_helper.clean_up_temp_files()
655656
sys.exit(exit_code)
656657
except CompareException, ce:
@@ -681,5 +682,3 @@ def format_message(key, *args):
681682

682683
if __name__ == "__main__":
683684
main()
684-
685-

0 commit comments

Comments
 (0)