v0.8.0! Let's get this party started!
[larjonas-mediagoblin.git] / mediagoblin / gmg_commands / assetlink.py
blob148ebe9e9ca6310a5babc1413c6c14c1ebee1221
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 import os
19 from mediagoblin import mg_globals
20 from mediagoblin.init import setup_global_and_app_config
21 from mediagoblin.gmg_commands import util as commands_util
22 from mediagoblin.tools.theme import register_themes
23 from mediagoblin.tools.translate import pass_to_ugettext as _
24 from mediagoblin.tools.common import simple_printer
25 from mediagoblin.tools import pluginapi
28 def assetlink_parser_setup(subparser):
29 # theme_subparsers = subparser.add_subparsers(
30 # dest=u"subcommand",
31 # help=u'Assetlink options')
33 # # Install command
34 # install_parser = theme_subparsers.add_parser(
35 # u'install', help=u'Install a theme to this mediagoblin instance')
36 # install_parser.add_argument(
37 # u'themefile', help=u'The theme archive to be installed')
39 # theme_subparsers.add_parser(
40 # u'assetlink',
41 # help=(
42 # u"Link the currently installed theme's assets "
43 # u"to the served theme asset directory"))
44 pass
47 ###########
48 # Utilities
49 ###########
51 def link_theme_assets(theme, link_dir, printer=simple_printer):
52 """
53 Returns a list of string of text telling the user what we did
54 which should be printable.
55 """
56 link_dir = link_dir.rstrip(os.path.sep)
57 link_parent_dir = os.path.dirname(link_dir)
59 if theme is None:
60 printer(_("Cannot link theme... no theme set\n"))
61 return
63 def _maybe_unlink_link_dir():
64 """unlink link directory if it exists"""
65 if os.path.lexists(link_dir) \
66 and os.path.islink(link_dir):
67 os.unlink(link_dir)
68 return True
70 return
72 if theme.get('assets_dir') is None:
73 printer(_("No asset directory for this theme\n"))
74 if _maybe_unlink_link_dir():
75 printer(
76 _("However, old link directory symlink found; removed.\n"))
77 return
79 _maybe_unlink_link_dir()
81 # make the link directory parent dirs if necessary
82 if not os.path.lexists(link_parent_dir):
83 os.makedirs(link_parent_dir)
85 os.symlink(
86 theme['assets_dir'].rstrip(os.path.sep),
87 link_dir)
88 printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % (
89 theme['assets_dir'], link_dir))
92 def link_plugin_assets(plugin_static, plugins_link_dir, printer=simple_printer):
93 """
94 Arguments:
95 - plugin_static: a mediagoblin.tools.staticdirect.PluginStatic instance
96 representing the static assets of this plugins' configuration
97 - plugins_link_dir: Base directory plugins are linked from
98 """
99 # link_dir is the final directory we'll link to, a combination of
100 # the plugin assetlink directory and plugin_static.name
101 link_dir = os.path.join(
102 plugins_link_dir.rstrip(os.path.sep), plugin_static.name)
104 # make the link directory parent dirs if necessary
105 if not os.path.lexists(plugins_link_dir):
106 os.makedirs(plugins_link_dir)
108 # See if the link_dir already exists.
109 if os.path.lexists(link_dir):
110 # if this isn't a symlink, there's something wrong... error out.
111 if not os.path.islink(link_dir):
112 printer(_('Could not link "%s": %s exists and is not a symlink\n') % (
113 plugin_static.name, link_dir))
114 return
116 # if this is a symlink and the path already exists, skip it.
117 if os.path.realpath(link_dir) == plugin_static.file_path:
118 # Is this comment helpful or not?
119 printer(_('Skipping "%s"; already set up.\n') % (
120 plugin_static.name))
121 return
123 # Otherwise, it's a link that went to something else... unlink it
124 printer(_('Old link found for "%s"; removing.\n') % (
125 plugin_static.name))
126 os.unlink(link_dir)
128 os.symlink(
129 plugin_static.file_path.rstrip(os.path.sep),
130 link_dir)
131 printer('Linked asset directory for plugin "%s":\n %s\nto:\n %s\n' % (
132 plugin_static.name,
133 plugin_static.file_path.rstrip(os.path.sep),
134 link_dir))
137 def assetlink(args):
139 Link the asset directory of the currently installed theme and plugins
141 mgoblin_app = commands_util.setup_app(args)
142 app_config = mg_globals.app_config
144 # link theme
145 link_theme_assets(mgoblin_app.current_theme, app_config['theme_linked_assets_dir'])
147 # link plugin assets
148 ## ... probably for this we need the whole application initialized
149 for plugin_static in pluginapi.hook_runall("static_setup"):
150 link_plugin_assets(
151 plugin_static, app_config['plugin_linked_assets_dir'])