assetlink.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import os
  17. from mediagoblin import mg_globals
  18. from mediagoblin.init import setup_global_and_app_config
  19. from mediagoblin.gmg_commands import util as commands_util
  20. from mediagoblin.tools.theme import register_themes
  21. from mediagoblin.tools.translate import pass_to_ugettext as _
  22. from mediagoblin.tools.common import simple_printer
  23. from mediagoblin.tools import pluginapi
  24. def assetlink_parser_setup(subparser):
  25. # theme_subparsers = subparser.add_subparsers(
  26. # dest=u"subcommand",
  27. # help=u'Assetlink options')
  28. # # Install command
  29. # install_parser = theme_subparsers.add_parser(
  30. # u'install', help=u'Install a theme to this mediagoblin instance')
  31. # install_parser.add_argument(
  32. # u'themefile', help=u'The theme archive to be installed')
  33. # theme_subparsers.add_parser(
  34. # u'assetlink',
  35. # help=(
  36. # u"Link the currently installed theme's assets "
  37. # u"to the served theme asset directory"))
  38. pass
  39. ###########
  40. # Utilities
  41. ###########
  42. def link_theme_assets(theme, link_dir, printer=simple_printer):
  43. """
  44. Returns a list of string of text telling the user what we did
  45. which should be printable.
  46. """
  47. link_dir = link_dir.rstrip(os.path.sep)
  48. link_parent_dir = os.path.dirname(link_dir)
  49. if theme is None:
  50. printer(_("Cannot link theme... no theme set\n"))
  51. return
  52. def _maybe_unlink_link_dir():
  53. """unlink link directory if it exists"""
  54. if os.path.lexists(link_dir) \
  55. and os.path.islink(link_dir):
  56. os.unlink(link_dir)
  57. return True
  58. return
  59. if theme.get('assets_dir') is None:
  60. printer(_("No asset directory for this theme\n"))
  61. if _maybe_unlink_link_dir():
  62. printer(
  63. _("However, old link directory symlink found; removed.\n"))
  64. return
  65. _maybe_unlink_link_dir()
  66. # make the link directory parent dirs if necessary
  67. if not os.path.lexists(link_parent_dir):
  68. os.makedirs(link_parent_dir)
  69. os.symlink(
  70. theme['assets_dir'].rstrip(os.path.sep),
  71. link_dir)
  72. printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % (
  73. theme['assets_dir'], link_dir))
  74. def link_plugin_assets(plugin_static, plugins_link_dir, printer=simple_printer):
  75. """
  76. Arguments:
  77. - plugin_static: a mediagoblin.tools.staticdirect.PluginStatic instance
  78. representing the static assets of this plugins' configuration
  79. - plugins_link_dir: Base directory plugins are linked from
  80. """
  81. # link_dir is the final directory we'll link to, a combination of
  82. # the plugin assetlink directory and plugin_static.name
  83. link_dir = os.path.join(
  84. plugins_link_dir.rstrip(os.path.sep), plugin_static.name)
  85. # make the link directory parent dirs if necessary
  86. if not os.path.lexists(plugins_link_dir):
  87. os.makedirs(plugins_link_dir)
  88. # See if the link_dir already exists.
  89. if os.path.lexists(link_dir):
  90. # if this isn't a symlink, there's something wrong... error out.
  91. if not os.path.islink(link_dir):
  92. printer(_('Could not link "%s": %s exists and is not a symlink\n') % (
  93. plugin_static.name, link_dir))
  94. return
  95. # if this is a symlink and the path already exists, skip it.
  96. if os.path.realpath(link_dir) == plugin_static.file_path:
  97. # Is this comment helpful or not?
  98. printer(_('Skipping "%s"; already set up.\n') % (
  99. plugin_static.name))
  100. return
  101. # Otherwise, it's a link that went to something else... unlink it
  102. printer(_('Old link found for "%s"; removing.\n') % (
  103. plugin_static.name))
  104. os.unlink(link_dir)
  105. os.symlink(
  106. plugin_static.file_path.rstrip(os.path.sep),
  107. link_dir)
  108. printer('Linked asset directory for plugin "%s":\n %s\nto:\n %s\n' % (
  109. plugin_static.name,
  110. plugin_static.file_path.rstrip(os.path.sep),
  111. link_dir))
  112. def assetlink(args):
  113. """
  114. Link the asset directory of the currently installed theme and plugins
  115. """
  116. mgoblin_app = commands_util.setup_app(args)
  117. app_config = mg_globals.app_config
  118. # link theme
  119. link_theme_assets(mgoblin_app.current_theme, app_config['theme_linked_assets_dir'])
  120. # link plugin assets
  121. ## ... probably for this we need the whole application initialized
  122. for plugin_static in pluginapi.hook_runall("static_setup"):
  123. link_plugin_assets(
  124. plugin_static, app_config['plugin_linked_assets_dir'])