Tolerate missing plugin functions #570

Merged
telackey merged 1 commits from dboreham/tolerate-missing-plugin-functions into main 2023-10-10 21:32:07 +00:00

View File

@ -111,6 +111,11 @@ def _commands_plugin_path(ctx: DeployCommandContext):
return plugin_path.joinpath("deploy", "commands.py") return plugin_path.joinpath("deploy", "commands.py")
# See: https://stackoverflow.com/a/54625079/1701505
def _has_method(o, name):
return callable(getattr(o, name, None))
def call_stack_deploy_init(deploy_command_context): def call_stack_deploy_init(deploy_command_context):
# Link with the python file in the stack # Link with the python file in the stack
# Call a function in it # Call a function in it
@ -120,6 +125,7 @@ def call_stack_deploy_init(deploy_command_context):
spec = util.spec_from_file_location("commands", python_file_path) spec = util.spec_from_file_location("commands", python_file_path)
imported_stack = util.module_from_spec(spec) imported_stack = util.module_from_spec(spec)
spec.loader.exec_module(imported_stack) spec.loader.exec_module(imported_stack)
if _has_method(imported_stack, "init"):
return imported_stack.init(deploy_command_context) return imported_stack.init(deploy_command_context)
else: else:
return None return None
@ -136,6 +142,7 @@ def call_stack_deploy_setup(deploy_command_context, parameters: LaconicStackSetu
spec = util.spec_from_file_location("commands", python_file_path) spec = util.spec_from_file_location("commands", python_file_path)
imported_stack = util.module_from_spec(spec) imported_stack = util.module_from_spec(spec)
spec.loader.exec_module(imported_stack) spec.loader.exec_module(imported_stack)
if _has_method(imported_stack, "setup"):
return imported_stack.setup(deploy_command_context, parameters, extra_args) return imported_stack.setup(deploy_command_context, parameters, extra_args)
else: else:
return None return None
@ -151,6 +158,7 @@ def call_stack_deploy_create(deployment_context, extra_args):
spec = util.spec_from_file_location("commands", python_file_path) spec = util.spec_from_file_location("commands", python_file_path)
imported_stack = util.module_from_spec(spec) imported_stack = util.module_from_spec(spec)
spec.loader.exec_module(imported_stack) spec.loader.exec_module(imported_stack)
if _has_method(imported_stack, "create"):
return imported_stack.create(deployment_context, extra_args) return imported_stack.create(deployment_context, extra_args)
else: else:
return None return None