|
| 1 | +import inspect |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
1 | 5 | import click |
| 6 | +import pkg_resources |
| 7 | +import toml |
2 | 8 | from click.exceptions import ClickException |
3 | 9 | from cookiecutter.exceptions import OutputDirExistsException |
4 | 10 | from cookiecutter.main import cookiecutter |
5 | 11 |
|
6 | | -from connect.cli.plugins.project.constants import PROJECT_EXTENSION_BOILERPLATE_URL |
| 12 | +from connect.cli.plugins.project.constants import ( |
| 13 | + CAPABILITY_ALLOWED_STATUSES, |
| 14 | + CAPABILITY_METHOD_MAP, |
| 15 | + PROJECT_EXTENSION_BOILERPLATE_URL, |
| 16 | +) |
7 | 17 | from connect.cli.plugins.project import utils |
8 | 18 |
|
9 | 19 |
|
@@ -41,3 +51,110 @@ def bootstrap_extension_project(data_dir: str): |
41 | 51 | '\nif you would like to use that name, please delete ' |
42 | 52 | 'the directory or use another location.', |
43 | 53 | ) |
| 54 | + |
| 55 | + |
| 56 | +def validate_extension_project(project_dir: str): |
| 57 | + click.secho(f'Validating project {project_dir}...\n', fg='blue') |
| 58 | + |
| 59 | + extension_dict = _project_descriptor_validations(project_dir) |
| 60 | + _entrypoint_validations(project_dir, extension_dict) |
| 61 | + |
| 62 | + click.secho(f'Extension Project {project_dir} has been successfully validated.', fg='green') |
| 63 | + |
| 64 | + |
| 65 | +def _project_descriptor_validations(project_dir): |
| 66 | + descriptor_file = os.path.join(project_dir, 'pyproject.toml') |
| 67 | + if not os.path.isfile(descriptor_file): |
| 68 | + raise ClickException( |
| 69 | + f'The directory `{project_dir}` does not look like an extension project directory, ' |
| 70 | + 'the mandatory `pyproject.toml` project descriptor file is not present.', |
| 71 | + ) |
| 72 | + try: |
| 73 | + data = toml.load(descriptor_file) |
| 74 | + except toml.TomlDecodeError: |
| 75 | + raise ClickException( |
| 76 | + 'The extension project descriptor file `pyproject.toml` is not valid.', |
| 77 | + ) |
| 78 | + |
| 79 | + extension_dict = data['tool']['poetry']['plugins']['connect.eaas.ext'] |
| 80 | + if not isinstance(extension_dict, dict): |
| 81 | + raise ClickException( |
| 82 | + 'The extension definition on [tool.poetry.plugins."connect.eaas.ext"] `pyproject.toml` section ' |
| 83 | + 'is not well configured. It should be as following: "extension" = "your_package.extension:YourExtension"', |
| 84 | + ) |
| 85 | + if 'extension' not in extension_dict.keys(): |
| 86 | + raise ClickException( |
| 87 | + 'The extension definition on [tool.poetry.plugins."connect.eaas.ext"] `pyproject.toml` section ' |
| 88 | + 'does not have "extension" defined. Reminder: "extension" = "your_package.extension:YourExtension"', |
| 89 | + ) |
| 90 | + return extension_dict |
| 91 | + |
| 92 | + |
| 93 | +def _entrypoint_validations(project_dir, extension_dict): |
| 94 | + package_name = extension_dict['extension'].rsplit('.', 1)[0] |
| 95 | + descriptor_file = os.path.join(f'{project_dir}/{package_name}', 'extension.json') |
| 96 | + ext_class = next(pkg_resources.iter_entry_points('connect.eaas.ext', 'extension'), None) |
| 97 | + if not ext_class: |
| 98 | + raise ClickException('\nThe extension could not be loaded, Did you execute `poetry install`?') |
| 99 | + |
| 100 | + CustomExtension = ext_class.load() |
| 101 | + if not inspect.isclass(CustomExtension): |
| 102 | + raise ClickException(f'\nThe extension class {CustomExtension} does not seem a class, please check it') |
| 103 | + |
| 104 | + all_methods = CustomExtension.__dict__ |
| 105 | + methods = [method for method in all_methods.keys() if not method.startswith('__')] |
| 106 | + |
| 107 | + try: |
| 108 | + ext_descriptor = json.load(open(descriptor_file, 'r')) |
| 109 | + except json.JSONDecodeError: |
| 110 | + raise ClickException( |
| 111 | + '\nThe extension descriptor file `extension.json` could not be loaded.', |
| 112 | + ) |
| 113 | + |
| 114 | + capabilities = ext_descriptor['capabilities'] |
| 115 | + |
| 116 | + errors = _have_capabilities_proper_stats(capabilities) |
| 117 | + if errors: |
| 118 | + raise ClickException(f'Capability errors: {errors}') |
| 119 | + |
| 120 | + if not _have_methods_proper_capabilities(methods, capabilities): |
| 121 | + raise ClickException( |
| 122 | + '\nThere is some mismatch between capabilities on `extension.json` ' |
| 123 | + 'and the methods defined on your extension class on `extension.py`, ' |
| 124 | + 'please check it.', |
| 125 | + ) |
| 126 | + |
| 127 | + _have_methods_proper_type(CustomExtension, capabilities) |
| 128 | + |
| 129 | + |
| 130 | +def _have_methods_proper_type(cls, capabilities): |
| 131 | + guess_async = [ |
| 132 | + inspect.iscoroutinefunction(getattr(cls, CAPABILITY_METHOD_MAP[name])) |
| 133 | + for name in capabilities.keys() |
| 134 | + ] |
| 135 | + if all(guess_async): |
| 136 | + return |
| 137 | + if not any(guess_async): |
| 138 | + return |
| 139 | + |
| 140 | + raise ClickException('An Extension class can only have sync or async methods not a mix of both.') |
| 141 | + |
| 142 | + |
| 143 | +def _have_capabilities_proper_stats(capabilities): |
| 144 | + errors = [] |
| 145 | + for capability, stats in capabilities.items(): |
| 146 | + if capability == 'product_action_execution' or capability == 'product_custom_event_processing': |
| 147 | + if isinstance(stats, list) and len(stats) != 0: |
| 148 | + errors.append(f'Capability `{capability}` must not have status.') |
| 149 | + continue |
| 150 | + if not stats: |
| 151 | + errors.append(f'Capability `{capability}` must have at least one allowed status.') |
| 152 | + for stat in stats: |
| 153 | + if stat not in CAPABILITY_ALLOWED_STATUSES: |
| 154 | + errors.append(f'Status `{stat}` on capability `{capability}` is not allowed.') |
| 155 | + return errors |
| 156 | + |
| 157 | + |
| 158 | +def _have_methods_proper_capabilities(methods, capabilities): |
| 159 | + capability_list = list(capabilities.keys()) |
| 160 | + return [CAPABILITY_METHOD_MAP[capability] for capability in capability_list] == methods |
0 commit comments