chiptool.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env -S python3 -B
  2. # Copyright (c) 2023 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import relative_importer # isort: split # noqa: F401
  16. import sys
  17. from typing import List
  18. import click
  19. from paths_finder import PathsFinder
  20. from runner import CONTEXT_SETTINGS, chiptool
  21. from tests_tool import send_raw_command, send_yaml_command
  22. _DEFAULT_EXTENSIONS_DIR = 'scripts/tests/yaml/extensions'
  23. _DEFAULT_PICS_FILE = 'src/app/tests/suites/certification/ci-pics-values'
  24. _DEFAULT_SPECIFICATIONS_DIR = 'src/app/zap-templates/zcl/data-model/chip/*.xml'
  25. def chiptool_runner_options(f):
  26. f = click.option('--server_path', type=click.Path(exists=True), default=None,
  27. help='Path to an websocket server to run at launch.')(f)
  28. f = click.option('--server_name', type=str, default='chip-tool',
  29. help='Name of a websocket server to run at launch.')(f)
  30. f = click.option('--server_arguments', type=str, default='interactive server',
  31. help='Optional arguments to pass to the websocket server at launch.')(f)
  32. f = click.option('--show_adapter_logs', type=bool, default=False, show_default=True,
  33. help='Show additional logs provided by the adapter.')(f)
  34. f = click.option('--trace_file', type=click.Path(), default=None,
  35. help='Optional file path to save the tracing output to.')(f)
  36. f = click.option('--trace_decode', type=bool, default=True,
  37. help='Decode the tracing ouput to a human readable format.')(f)
  38. f = click.option('--delay-in-ms', '--delayInMs', type=int, default=0, show_default=True,
  39. help='Add a delay between each test suite steps.')(f)
  40. f = click.option('--continueOnFailure', type=bool, default=False, show_default=True,
  41. help='Do not stop running the test suite on first error.')(f)
  42. f = click.option('--specifications_paths', type=click.Path(), show_default=True, default=_DEFAULT_SPECIFICATIONS_DIR,
  43. help='Path to a set of files containing clusters definitions.')(f)
  44. f = click.option('--PICS', type=click.Path(exists=True), show_default=True, default=_DEFAULT_PICS_FILE,
  45. help='Path to the PICS file to use.')(f)
  46. f = click.option('--additional_pseudo_clusters_directory', type=click.Path(), show_default=True, default=_DEFAULT_EXTENSIONS_DIR,
  47. help='Path to a directory containing additional pseudo clusters.')(f)
  48. return f
  49. CONTEXT_SETTINGS['ignore_unknown_options'] = True
  50. CONTEXT_SETTINGS['default_map']['chiptool']['use_test_harness_log_format'] = True
  51. def maybe_update_server_arguments(ctx):
  52. if ctx.params['trace_file']:
  53. ctx.params['server_arguments'] += ' --trace_file {}'.format(ctx.params['trace_file'])
  54. if ctx.params['trace_decode']:
  55. ctx.params['server_arguments'] += ' --trace_decode 1'
  56. del ctx.params['trace_file']
  57. del ctx.params['trace_decode']
  58. return ctx.params['server_arguments']
  59. def maybe_update_stop_on_error(ctx):
  60. if ctx.params['continueonfailure']:
  61. ctx.params['stop_on_error'] = False
  62. del ctx.params['continueonfailure']
  63. @click.command(context_settings=CONTEXT_SETTINGS)
  64. @click.argument('commands', nargs=-1)
  65. @chiptool_runner_options
  66. @click.pass_context
  67. def chiptool_py(ctx, commands: List[str], server_path: str, server_name: str, server_arguments: str, show_adapter_logs: bool, trace_file: str, trace_decode: bool, delay_in_ms: int, continueonfailure: bool, specifications_paths: str, pics: str, additional_pseudo_clusters_directory: str):
  68. success = False
  69. server_arguments = maybe_update_server_arguments(ctx)
  70. maybe_update_stop_on_error(ctx)
  71. if len(commands) > 1 and commands[0] == 'tests':
  72. success = send_yaml_command(chiptool, commands[1], server_path, server_arguments, show_adapter_logs, specifications_paths, pics,
  73. additional_pseudo_clusters_directory, commands[2:])
  74. else:
  75. if server_path is None and server_name:
  76. paths_finder = PathsFinder()
  77. server_path = paths_finder.get(server_name)
  78. success = send_raw_command(' '.join(commands), server_path, server_arguments)
  79. sys.exit(0 if success else 1)
  80. if __name__ == '__main__':
  81. chiptool_py()