• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""This module manages the global plugin registry for pw_cli."""
15
16import argparse
17import os
18from pathlib import Path
19import sys
20from typing import Iterable
21
22from pw_cli import arguments, plugins
23
24_plugin_registry = plugins.Registry(validator=plugins.callable_with_no_args)
25REGISTRY_FILE = 'PW_PLUGINS'
26
27
28def _register_builtin_plugins(registry: plugins.Registry) -> None:
29    """Registers the commands that are included with pw by default."""
30
31    # Register these by name to avoid circular dependencies.
32    registry.register_by_name('bloat', 'pw_bloat.__main__', 'main')
33    registry.register_by_name('doctor', 'pw_doctor.doctor', 'main')
34    registry.register_by_name('format', 'pw_presubmit.format_code', 'main')
35    registry.register_by_name('keep-sorted', 'pw_presubmit.keep_sorted', 'main')
36    registry.register_by_name('logdemo', 'pw_cli.log', 'main')
37    registry.register_by_name('module', 'pw_module.__main__', 'main')
38    registry.register_by_name(
39        'python-packages', 'pw_env_setup.python_packages', 'main'
40    )
41    registry.register_by_name('test', 'pw_unit_test.test_runner', 'main')
42    registry.register_by_name('watch', 'pw_watch.watch', 'main')
43
44    registry.register('help', _help_command)
45
46
47def _help_command():
48    """Display detailed information about pw commands."""
49    parser = argparse.ArgumentParser(description=_help_command.__doc__)
50    parser.add_argument(
51        'plugins',
52        metavar='plugin',
53        nargs='*',
54        help='command for which to display detailed info',
55    )
56
57    print(arguments.format_help(_plugin_registry), file=sys.stderr)
58
59    for line in _plugin_registry.detailed_help(**vars(parser.parse_args())):
60        print(line, file=sys.stderr)
61
62
63def register(directory: Path) -> None:
64    _register_builtin_plugins(_plugin_registry)
65    _plugin_registry.register_directory(
66        directory, REGISTRY_FILE, Path(os.environ.get('PW_PROJECT_ROOT', ''))
67    )
68
69
70def errors() -> dict:
71    return _plugin_registry.errors()
72
73
74def format_help() -> str:
75    return arguments.format_help(_plugin_registry)
76
77
78def run(name: str, args: Iterable[str]) -> int:
79    return _plugin_registry.run_with_argv(name, args)
80