ModuleCfgCheck.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: UTF-8 -*-
  2. """
  3. @Project :pytest_src
  4. @File :ModuleCfgCheck.py
  5. @Author :haojiang
  6. @Date :2022/9/23 9:02
  7. """
  8. import logging
  9. from Base.Arxml import Arxml
  10. from Base.CfgCheckBase import CfgCheckBase
  11. from lxml import etree
  12. import os
  13. from typing import List
  14. class ModuleCfgCheck(CfgCheckBase):
  15. def __init__(self, module_name: str, project_path: str):
  16. super().__init__()
  17. self.module_name = module_name
  18. self.project_path = project_path
  19. self.registered_check_items = dict()
  20. self.load_arxml()
  21. def load_arxml(self):
  22. arxml_name = ''
  23. configure_files = list()
  24. for iter_dirPath, dirs, files in os.walk(self.project_path):
  25. configure_files = files.copy()
  26. break
  27. for file in configure_files:
  28. if f'{self.module_name}_ecuc' in file:
  29. arxml_name = os.path.join(self.project_path, file)
  30. if arxml_name != '':
  31. self.arxml_root = Arxml(arxml_name).root
  32. def register_param_check_item(self, check_ID: int, check_items: List):
  33. '''
  34. :param check_ID:
  35. :param check_items: [(item_name1, value1), (item_name2, value2), ...]
  36. :return:
  37. '''
  38. current_id_items = self.registered_check_items.get(check_ID, [])
  39. current_id_items.extend(check_items) # extend 实现原地拓展,返回值为None,所以不能将返回值作为拓展后的结果
  40. self.registered_check_items[check_ID] = current_id_items
  41. def do_all_param_check(self):
  42. for check_ID, check_items in self.registered_check_items.items():
  43. logging.info(f'Checking Item ID {check_ID}:')
  44. self.do_param_check(check_items)
  45. def do_param_check(self, check_items: List, search_pattern=None, get_match_element_function=None) -> bool:
  46. match_res = False
  47. for param_name, param_expect_value in check_items:
  48. cfg_check = CfgCheckBase()
  49. if get_match_element_function != None:
  50. match_element = get_match_element_function(check_items, **search_pattern)
  51. else:
  52. match_element = self.arxml_root
  53. match_res = cfg_check.parameter_match(match_element, param_name, param_expect_value)
  54. return match_res
  55. def get_detail_element(self, check_items, xpath_str, xpath_text='', xpath_rule=None):
  56. ret = None
  57. param_name, expect_value = check_items[0]
  58. match_element = Arxml.get_all_match_element(self.arxml_root, xpath_str, xpath_text, xpath_rule)
  59. if match_element is None:
  60. logging.error(f'By Pattern {xpath_str}-{xpath_text}-{xpath_rule}, not find valud element!')
  61. ret = None
  62. else:
  63. for elem in match_element:
  64. parent_elem = Arxml.get_parent_element(elem)
  65. match_string = etree.tostring(parent_elem).decode()
  66. if param_name in match_string:
  67. ret = parent_elem
  68. logging.info(f'Find Valid Element: {match_string}')
  69. break
  70. else:
  71. logging.info(match_string)
  72. return ret
  73. def check_module_param_check(project: str, module_name: str, check_item: tuple, search_pattern=None) -> bool:
  74. mcc = ModuleCfgCheck(module_name, project)
  75. if search_pattern != None:
  76. costum_func = mcc.get_detail_element
  77. else:
  78. costum_func = None
  79. cmp_result = mcc.do_param_check([check_item], search_pattern, costum_func)
  80. return cmp_result
  81. if __name__ == '__main__':
  82. logging.basicConfig(level=logging.INFO)
  83. # mcc = ModuleCfgCheck('Dem', r'D:\01_Work\02_WP\EP39_EREV\branch\src\Config\ECUC')
  84. # mcc.register_param_check_item(0, [('DemClearDTCBehavior', 'DEM_CLRRESP_V1OLATILE')])
  85. # mcc.register_param_check_item(1, [('DemClearDTCBehavior', 'DEM_CLRRESP_V1OLATILE')])
  86. # mcc.register_param_check_item(2, [('DemClearDTCBehavior', 'DEM_CLRRESP_V1OLATILE')])
  87. # mcc.register_param_check_item(3, [('DemClearDTCBehavior', 'DEM_CLRRESP_V1OLATILE')])
  88. # mcc.register_param_check_item(4, [('DemMaxNumberEventEntryPrimary', '40')])
  89. # mcc.register_param_check_item(5, [('DemImmediateNvStorage', 'true')])
  90. mcc = ModuleCfgCheck('CanTp', r'D:\01_Work\02_WP\EP39_EREV\branch\src\Config\ECUC')
  91. mcc.register_param_check_item(0, [('CanTpChannelMode', 'CANTP_MODE_FULL_DUPLEX')])
  92. mcc.register_param_check_item(0, [('CanTpMainFunctionPeriod', '0.006')])
  93. mcc.do_all_param_check()