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/>.
19 from mediagoblin
.init
import setup_global_and_app_config
20 from mediagoblin
.gmg_commands
import util
as commands_util
21 from mediagoblin
.tools
.theme
import register_themes
22 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
23 from mediagoblin
.tools
.common
import simple_printer
24 from mediagoblin
.tools
import pluginapi
27 def theme_parser_setup(subparser
):
28 theme_subparsers
= subparser
.add_subparsers(
30 help=u
'Theme sub-commands')
33 install_parser
= theme_subparsers
.add_parser(
34 u
'install', help=u
'Install a theme to this mediagoblin instance')
35 install_parser
.add_argument(
36 u
'themefile', help=u
'The theme archive to be installed')
38 theme_subparsers
.add_parser(
41 u
"Link the currently installed theme's assets "
42 u
"to the served theme asset directory"))
49 def link_theme_assets(theme
, link_dir
, printer
=simple_printer
):
51 Returns a list of string of text telling the user what we did
52 which should be printable.
54 link_dir
= link_dir
.rstrip(os
.path
.sep
)
55 link_parent_dir
= os
.path
.dirname(link_dir
)
58 printer(_("Cannot link theme... no theme set\n"))
61 def _maybe_unlink_link_dir():
62 """unlink link directory if it exists"""
63 if os
.path
.lexists(link_dir
) \
64 and os
.path
.islink(link_dir
):
70 if theme
.get('assets_dir') is None:
71 printer(_("No asset directory for this theme\n"))
72 if _maybe_unlink_link_dir():
74 _("However, old link directory symlink found; removed.\n"))
77 _maybe_unlink_link_dir()
79 # make the link directory parent dirs if necessary
80 if not os
.path
.lexists(link_parent_dir
):
81 os
.makedirs(link_parent_dir
)
84 theme
['assets_dir'].rstrip(os
.path
.sep
),
86 printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % (
87 theme
['assets_dir'], link_dir
))
90 def link_plugin_assets(plugin_static
, plugins_link_dir
, printer
=simple_printer
):
93 - plugin_static: a mediagoblin.tools.staticdirect.PluginStatic instance
94 representing the static assets of this plugins' configuration
95 - plugins_link_dir: Base directory plugins are linked from
97 # link_dir is the final directory we'll link to, a combination of
98 # the plugin assetlink directory and plugin_static.name
99 link_dir
= os
.path
.sep
.join(
100 plugins_link_dir
.rstrip(os
.path
.sep
), plugin_static
.name
)
102 # make the link directory parent dirs if necessary
103 if not os
.path
.lexists(plugins_link_dir
):
104 os
.makedirs(plugins_link_dir
)
106 # See if the link_dir already exists.
107 if os
.path
.lexists(link_dir
):
108 # if this isn't a symlink, there's something wrong... error out.
109 if not os
.path
.islink(link_dir
):
110 printer(_('Could not link "%s": %s exists and is not a symlink') % (
114 # if this is a symlink and the path already exists, skip it.
115 if os
.path
.realpath(link_dir
) == plugin_static
.file_path
:
116 # Is this comment helpful or not?
117 printer(_('Skipping "%s"; already set up.') % (
121 # Otherwise, it's a link that went to something else... unlink it
122 printer(_('Old link found for "%s"; removing.') % (
127 plugin_static
.file_path
.rstrip(os
.path
.sep
),
129 printer('Linked asset directory for plugin "%s":\n %s\nto:\n %s\n' % (
131 plugin_static
.file_path
.rstrip(os
.path
.sep
),
135 def install_theme(install_dir
, themefile
):
143 def assetlink_command(args
):
145 Link the asset directory of the currently installed theme
147 global_config
, app_config
= setup_global_and_app_config(args
.conf_file
)
148 theme_registry
, current_theme
= register_themes(app_config
)
151 link_theme_assets(current_theme
, app_config
['theme_linked_assets_dir'])
153 # Set up the app specifically so we can access the plugin infrastructure
154 commands_util
.setup_app(args
)
157 ## ... probably for this we need the whole application initialized
158 for plugin_static
in pluginapi
.hook_runall("static_setup"):
160 plugin_static
, app_config
['plugin_linked_assets_dir'])
163 def install_command(args
):
165 Handle the 'install this theme' subcommand
167 global_config
, app_config
= setup_global_and_app_config(args
.conf_file
)
168 install_dir
= app_config
['theme_install_dir']
169 install_theme(install_dir
, args
.themefile
)
173 'assetlink': assetlink_command
,
174 'install': install_command
}
178 SUBCOMMANDS
[args
.subcommand
](args
)