convert.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2021 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import argparse
  18. import json
  19. import os
  20. import subprocess
  21. import sys
  22. from zap_execution import ZapTool
  23. CHIP_ROOT_DIR = os.path.realpath(
  24. os.path.join(os.path.dirname(__file__), '../../..'))
  25. def checkPythonVersion():
  26. if sys.version_info[0] < 3:
  27. print('Must use Python 3. Current version is ' +
  28. str(sys.version_info[0]))
  29. exit(1)
  30. def checkFileExists(path):
  31. if not os.path.isfile(path):
  32. print('Error: ' + path + ' does not exists or is not a file.')
  33. exit(1)
  34. def checkDirExists(path):
  35. if not os.path.isdir(path):
  36. print('Error: ' + path + ' does not exists or is not a directory.')
  37. exit(1)
  38. def getFilePath(name):
  39. fullpath = os.path.join(CHIP_ROOT_DIR, name)
  40. checkFileExists(fullpath)
  41. return fullpath
  42. def getDirPath(name):
  43. fullpath = os.path.join(CHIP_ROOT_DIR, name)
  44. checkDirExists(fullpath)
  45. return fullpath
  46. def runArgumentsParser():
  47. parser = argparse.ArgumentParser(
  48. description='Convert .zap files to the current zap version')
  49. parser.add_argument('zap', help='Path to the application .zap file')
  50. parser.add_argument('--run-bootstrap', default=None, action='store_true',
  51. help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
  52. args = parser.parse_args()
  53. zap_file = getFilePath(args.zap)
  54. return zap_file, args.run_bootstrap
  55. def detectZclFile(zapFile):
  56. print(f"Searching for zcl file from {zapFile}")
  57. path = 'src/app/zap-templates/zcl/zcl.json'
  58. data = json.load(open(zapFile))
  59. for package in data["package"]:
  60. if package["type"] != "zcl-properties":
  61. continue
  62. # found the right path, try to figure out the actual path
  63. if package["pathRelativity"] == "relativeToZap":
  64. path = os.path.abspath(os.path.join(
  65. os.path.dirname(zapFile), package["path"]))
  66. else:
  67. path = package["path"]
  68. return getFilePath(path)
  69. def runConversion(zap_file):
  70. templates_file = getFilePath('src/app/zap-templates/app-templates.json')
  71. zcl_file = detectZclFile(zap_file)
  72. tool = ZapTool()
  73. tool.run('convert', '-z', zcl_file, '-g',
  74. templates_file, '-o', zap_file, zap_file)
  75. def main():
  76. checkPythonVersion()
  77. zap_file, run_bootstrap = runArgumentsParser()
  78. if run_bootstrap:
  79. subprocess.check_call(getFilePath(
  80. "scripts/tools/zap/zap_bootstrap.sh"), shell=True)
  81. os.chdir(CHIP_ROOT_DIR)
  82. runConversion(zap_file)
  83. if __name__ == '__main__':
  84. main()