Tolerate missing plugin functions (#570)

This commit is contained in:
David Boreham 2023-10-10 15:32:07 -06:00 committed by GitHub
parent 2486003361
commit 1446e54f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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