convert_ini.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env -S python3 -B
  2. #
  3. # Copyright (c) 2021 Project CHIP Authors
  4. # All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import json
  19. import logging
  20. import re
  21. import typing
  22. from configparser import ConfigParser
  23. from os.path import exists
  24. import click
  25. def convert_ini_to_json(ini_dir: str, json_path: str):
  26. """ Converts chip-tool INI files found in 'ini_dir' into the JSON format expected by the matter python REPL
  27. and associated test infrastructure.
  28. Specifically, this will take the three INI files that correspond to the three example CAs (alpha, beta, gamma)
  29. and munge them together into a single REPL-compatible JSON file.
  30. """
  31. python_json_store = {}
  32. ini_file_paths = ['/chip_tool_config.alpha.ini', '/chip_tool_config.beta.ini', '/chip_tool_config.gamma.ini']
  33. counter = 1
  34. for path in ini_file_paths:
  35. full_path = ini_dir + path
  36. if (exists(full_path)):
  37. logging.critical(f"Found chip tool INI file at: {full_path} - Converting...")
  38. create_repl_config_from_init(ini_file=full_path,
  39. json_dict=python_json_store, replace_suffix=str(counter))
  40. counter = counter + 1
  41. json_file = open(json_path, 'w')
  42. json.dump(python_json_store, json_file, ensure_ascii=True, indent=4)
  43. def create_repl_config_from_init(ini_file: str, json_dict: typing.Dict, replace_suffix: str):
  44. ''' This updates a provided JSON dictionary to create a REPL compliant configuration store that
  45. contains the correct 'repl-config' and 'sdk-config' keys built from the provided chip-tool
  46. INI file that contains the root public keys. The INI file will typically be named
  47. with the word 'alpha', 'beta' or 'gamma' in the name.
  48. ini_file: Path to source INI file
  49. json_dict: JSON dictionary to be updated. Multiple passes through this function using
  50. the same dictionary is possible.
  51. replace_suffix: The credentials in the INI file typically have keys that end with 0. This suffix
  52. can be replaced with a different number.
  53. '''
  54. if ('repl-config' not in json_dict):
  55. json_dict['repl-config'] = {}
  56. if ('caList' not in json_dict['repl-config']):
  57. json_dict['repl-config']['caList'] = {}
  58. json_dict['repl-config']['caList'][replace_suffix] = [
  59. {'fabricId': int(replace_suffix), 'vendorId': 0XFFF1}
  60. ]
  61. if ('sdk-config' not in json_dict):
  62. json_dict['sdk-config'] = {}
  63. load_ini_into_dict(ini_file=ini_file, json_dict=json_dict['sdk-config'], replace_suffix=replace_suffix)
  64. def load_ini_into_dict(ini_file: str, json_dict: typing.Dict, replace_suffix: str):
  65. """ Loads the specific INI file containing CA credential information into the provided dictionary. A 'replace_suffix' string
  66. has to be provided to convert the existing numerical suffix to a different value.
  67. NOTE: This does not do any conversion of the keys into a format acceptable by the Python REPL environment. Please see
  68. create_repl_config_from_init above if that is desired.
  69. """
  70. config = ConfigParser()
  71. # Enable case-sensitive keys.
  72. config.optionxform = str
  73. config.read(ini_file)
  74. for key in config['Default']:
  75. value = config['Default'][key]
  76. key = re.sub(r'(.*?)[0-9]+', r'\1', key) + replace_suffix
  77. json_dict[key] = value
  78. @click.command()
  79. @click.option('--ini-dir', type=click.Path(exists=True), show_default=True, default='/tmp', help='Path to directory containing INI files that chip-tool uses for its tests')
  80. @click.option('--json-path', type=click.Path(exists=False), show_default=True, default='/tmp/repl-storage.json', help='Path to JSON file used by Python infrastructure')
  81. def main(ini_dir: str, json_path: str):
  82. convert_ini_to_json(ini_dir, json_path)
  83. return 0
  84. if __name__ == '__main__':
  85. main(auto_envvar_prefix='CHIP')