|
| 1 | +""" |
| 2 | +Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | +The Universal Permissive License (UPL), Version 1.0 |
| 4 | +""" |
| 5 | +import imp |
| 6 | +import javaos as os |
| 7 | +import sys |
| 8 | + |
| 9 | +from wlsdeploy.logging.platform_logger import PlatformLogger |
| 10 | +from wlsdeploy.util import dictionary_utils |
| 11 | +from wlsdeploy.util.model_translator import FileToPython |
| 12 | + |
| 13 | +__class_name = 'filter_helper' |
| 14 | +__logger = PlatformLogger('wlsdeploy.wlst') |
| 15 | +__filter_file_location = os.path.join(os.environ.get('WLSDEPLOY_HOME'), 'lib', 'model_filters.json') |
| 16 | + |
| 17 | +__id_filter_map = { |
| 18 | + # 'filterId': filter_method |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +def apply_filters(model, tool_type): |
| 23 | + """ |
| 24 | + Apply any filters configured for the specified tool type to the specified model. |
| 25 | + :param model: the model to be filtered |
| 26 | + :param tool_type: the name of the filter tool type |
| 27 | + :return: True if any filter was applied, False otherwise |
| 28 | + :raises: BundleAwareException of the specified type: if an error occurs |
| 29 | + """ |
| 30 | + _method_name = 'apply_filters' |
| 31 | + |
| 32 | + filter_applied = False |
| 33 | + |
| 34 | + try: |
| 35 | + if os.path.isfile(__filter_file_location): |
| 36 | + filters_dictionary = FileToPython(__filter_file_location).parse() |
| 37 | + |
| 38 | + if tool_type in filters_dictionary: |
| 39 | + filter_list = filters_dictionary[tool_type] |
| 40 | + for filter in filter_list: |
| 41 | + filter_applied = _apply_filter(model, filter) or filter_applied |
| 42 | + else: |
| 43 | + __logger.info('WLSDPLY-20016', tool_type, __filter_file_location, class_name=__class_name, |
| 44 | + method_name=_method_name) |
| 45 | + else: |
| 46 | + __logger.info('WLSDPLY-20017', __filter_file_location, class_name=__class_name, method_name=_method_name) |
| 47 | + except Exception, ex: |
| 48 | + __logger.severe('WLSDPLY-20018', str(ex), error=ex, class_name=__class_name, method_name=_method_name) |
| 49 | + |
| 50 | + return filter_applied |
| 51 | + |
| 52 | + |
| 53 | +def _apply_filter(model, the_filter): |
| 54 | + """ |
| 55 | + Apply the specified filter to the specified model. |
| 56 | + :param model: the model to be filtered |
| 57 | + :param filter: a dictionary containing the filter parameters |
| 58 | + :return: True if the specified filter was applied, False otherwise |
| 59 | + :raises: BundleAwareException of the specified type: if an error occurs |
| 60 | + """ |
| 61 | + _method_name = '_apply_filter' |
| 62 | + id = dictionary_utils.get_element(the_filter, 'id') |
| 63 | + if id is not None: |
| 64 | + return _apply_id_filter(model, id) |
| 65 | + |
| 66 | + path = dictionary_utils.get_element(the_filter, 'path') |
| 67 | + if path is not None: |
| 68 | + return _apply_path_filter(model, path) |
| 69 | + |
| 70 | + __logger.severe('WLSDPLY-20019', str(__filter_file_location), class_name=__class_name, method_name=_method_name) |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def _apply_id_filter(model, id): |
| 75 | + """ |
| 76 | + Apply the specified ID filter to the specified model. |
| 77 | + :param model: the model to be filtered |
| 78 | + :param id: the ID of the filter to be applied |
| 79 | + :return: True if the specified filter was applied, False otherwise |
| 80 | + :raises: BundleAwareException of the specified type: if an error occurs |
| 81 | + """ |
| 82 | + _method_name = '_apply_id_filter' |
| 83 | + |
| 84 | + filter_method = dictionary_utils.get_element(__id_filter_map, id) |
| 85 | + if filter_method is None: |
| 86 | + __logger.severe('WLSDPLY-20020', str(id), class_name=__class_name, method_name=_method_name) |
| 87 | + return False |
| 88 | + else: |
| 89 | + filter_method(model) |
| 90 | + return True |
| 91 | + |
| 92 | + |
| 93 | +def _apply_path_filter(model, script_path): |
| 94 | + """ |
| 95 | + Apply the specified path filter to the specified model. |
| 96 | + :param model: the model to be filtered |
| 97 | + :param script_path: the path of the filter to be applied |
| 98 | + :return: True if the specified filter was applied, False otherwise |
| 99 | + :raises: BundleAwareException of the specified type: if an error occurs |
| 100 | + """ |
| 101 | + _method_name = '_apply_path_filter' |
| 102 | + |
| 103 | + if not os.path.isfile(script_path): |
| 104 | + __logger.severe('WLSDPLY-20021', str(script_path), class_name=__class_name, method_name=_method_name) |
| 105 | + return False |
| 106 | + |
| 107 | + python_path = os.path.dirname(script_path) |
| 108 | + path_present = python_path in sys.path |
| 109 | + if not path_present: |
| 110 | + sys.path.insert(0, python_path) |
| 111 | + |
| 112 | + try: |
| 113 | + module = imp.load_source('filter_script', script_path) |
| 114 | + module.filter_model(model) |
| 115 | + if not path_present: |
| 116 | + sys.path.remove(python_path) |
| 117 | + return True |
| 118 | + |
| 119 | + except Exception, ex: |
| 120 | + __logger.severe('WLSDPLY-20022', str(script_path), error=ex, class_name=__class_name, method_name=_method_name) |
| 121 | + |
| 122 | + return False |
0 commit comments