plugins.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.cli.plugins
  4. ~~~~~~~~~~~~~~~~~~~
  5. This module contains all plugin commands.
  6. :copyright: (c) 2016 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import sys
  10. import os
  11. import shutil
  12. import click
  13. from flask import current_app
  14. from flask_plugins import (get_all_plugins, get_enabled_plugins,
  15. get_plugin_from_all)
  16. from flaskbb.cli.main import flaskbb
  17. from flaskbb.cli.utils import check_cookiecutter, validate_plugin
  18. from flaskbb.extensions import plugin_manager
  19. try:
  20. from cookiecutter.main import cookiecutter
  21. except ImportError:
  22. pass
  23. @flaskbb.group()
  24. def plugins():
  25. """Plugins command sub group."""
  26. pass
  27. @plugins.command("new")
  28. @click.argument("plugin_identifier", callback=check_cookiecutter)
  29. @click.option("--template", "-t", type=click.STRING,
  30. default="https://github.com/sh4nks/cookiecutter-flaskbb-plugin",
  31. help="Path to a cookiecutter template or to a valid git repo.")
  32. def new_plugin(plugin_identifier, template):
  33. """Creates a new plugin based on the cookiecutter plugin
  34. template. Defaults to this template:
  35. https://github.com/sh4nks/cookiecutter-flaskbb-plugin.
  36. It will either accept a valid path on the filesystem
  37. or a URL to a Git repository which contains the cookiecutter template.
  38. """
  39. out_dir = os.path.join(current_app.root_path, "plugins", plugin_identifier)
  40. click.secho("[+] Creating new plugin {}".format(plugin_identifier),
  41. fg="cyan")
  42. cookiecutter(template, output_dir=out_dir)
  43. click.secho("[+] Done. Created in {}".format(out_dir),
  44. fg="green", bold=True)
  45. @plugins.command("install")
  46. @click.argument("plugin_identifier")
  47. def install_plugin(plugin_identifier):
  48. """Installs a new plugin."""
  49. validate_plugin(plugin_identifier)
  50. plugin = get_plugin_from_all(plugin_identifier)
  51. click.secho("[+] Installing plugin {}...".format(plugin.name), fg="cyan")
  52. try:
  53. plugin_manager.install_plugins([plugin])
  54. except Exception as e:
  55. click.secho("[-] Couldn't install plugin because of following "
  56. "exception: \n{}".format(e), fg="red")
  57. @plugins.command("uninstall")
  58. @click.argument("plugin_identifier")
  59. def uninstall_plugin(plugin_identifier):
  60. """Uninstalls a plugin from FlaskBB."""
  61. validate_plugin(plugin_identifier)
  62. plugin = get_plugin_from_all(plugin_identifier)
  63. click.secho("[+] Uninstalling plugin {}...".format(plugin.name), fg="cyan")
  64. try:
  65. plugin_manager.uninstall_plugins([plugin])
  66. except AttributeError:
  67. pass
  68. @plugins.command("remove")
  69. @click.argument("plugin_identifier")
  70. @click.option("--force", "-f", default=False, is_flag=True,
  71. help="Removes the plugin without asking for confirmation.")
  72. def remove_plugin(plugin_identifier, force):
  73. """Removes a plugin from the filesystem."""
  74. validate_plugin(plugin_identifier)
  75. if not force and not \
  76. click.confirm(click.style("Are you sure?", fg="magenta")):
  77. sys.exit(0)
  78. plugin = get_plugin_from_all(plugin_identifier)
  79. click.secho("[+] Uninstalling plugin {}...".format(plugin.name), fg="cyan")
  80. try:
  81. plugin_manager.uninstall_plugins([plugin])
  82. except Exception as e:
  83. click.secho("[-] Couldn't uninstall plugin because of following "
  84. "exception: \n{}".format(e), fg="red")
  85. if not click.confirm(click.style(
  86. "Do you want to continue anyway?", fg="magenta")
  87. ):
  88. sys.exit(0)
  89. click.secho("[+] Removing plugin from filesystem...", fg="cyan")
  90. shutil.rmtree(plugin.path, ignore_errors=False, onerror=None)
  91. @plugins.command("list")
  92. def list_plugins():
  93. """Lists all installed plugins."""
  94. click.secho("[+] Listing all installed plugins...", fg="cyan")
  95. # This is subject to change as I am not happy with the current
  96. # plugin system
  97. enabled_plugins = get_enabled_plugins()
  98. disabled_plugins = set(get_all_plugins()) - set(enabled_plugins)
  99. if len(enabled_plugins) > 0:
  100. click.secho("[+] Enabled Plugins:", fg="blue", bold=True)
  101. for plugin in enabled_plugins:
  102. click.secho(" - {} (version {})".format(
  103. plugin.name, plugin.version), bold=True
  104. )
  105. if len(disabled_plugins) > 0:
  106. click.secho("[+] Disabled Plugins:", fg="yellow", bold=True)
  107. for plugin in disabled_plugins:
  108. click.secho(" - {} (version {})".format(
  109. plugin.name, plugin.version), bold=True
  110. )