Moving test_user_dev->user_dev in plugin app configs & adding plugin static serving
[mediagoblin.git] / mediagoblin / gmg_commands / theme.py
CommitLineData
111a9752
CAW
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
17import os
18
19from mediagoblin.init import setup_global_and_app_config
5ccb16ca 20from mediagoblin.gmg_commands import util as commands_util
111a9752 21from mediagoblin.tools.theme import register_themes
a789b713 22from mediagoblin.tools.translate import pass_to_ugettext as _
3da44aeb 23from mediagoblin.tools.common import simple_printer
5ccb16ca 24from mediagoblin.tools import pluginapi
111a9752
CAW
25
26
27def theme_parser_setup(subparser):
28 theme_subparsers = subparser.add_subparsers(
29 dest=u"subcommand",
30 help=u'Theme sub-commands')
31
32 # Install command
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')
37
111a9752
CAW
38 theme_subparsers.add_parser(
39 u'assetlink',
40 help=(
41 u"Link the currently installed theme's assets "
42 u"to the served theme asset directory"))
43
44
3da44aeb
CAW
45###########
46# Utilities
47###########
48
5ccb16ca 49def link_theme_assets(theme, link_dir, printer=simple_printer):
111a9752 50 """
3da44aeb
CAW
51 Returns a list of string of text telling the user what we did
52 which should be printable.
111a9752 53 """
3da44aeb 54 link_dir = link_dir.rstrip(os.path.sep)
9d3e56d5 55 link_parent_dir = os.path.dirname(link_dir)
111a9752 56
3da44aeb
CAW
57 if theme is None:
58 printer(_("Cannot link theme... no theme set\n"))
5ccb16ca 59 return
111a9752 60
8a22617f
CAW
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):
65 os.unlink(link_dir)
66 return True
111a9752 67
5ccb16ca 68 return
111a9752 69
3da44aeb
CAW
70 if theme.get('assets_dir') is None:
71 printer(_("No asset directory for this theme\n"))
8a22617f 72 if _maybe_unlink_link_dir():
3da44aeb
CAW
73 printer(
74 _("However, old link directory symlink found; removed.\n"))
5ccb16ca 75 return
8a22617f
CAW
76
77 _maybe_unlink_link_dir()
78
79 # make the link directory parent dirs if necessary
80 if not os.path.lexists(link_parent_dir):
81 os.makedirs(link_parent_dir)
82
83 os.symlink(
3da44aeb 84 theme['assets_dir'].rstrip(os.path.sep),
8a22617f 85 link_dir)
3da44aeb
CAW
86 printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % (
87 theme['assets_dir'], link_dir))
88
89
5ccb16ca
CAW
90def link_plugin_assets(plugin_static, plugins_link_dir, printer=simple_printer):
91 """
92 Arguments:
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
96 """
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)
101
102 # make the link directory parent dirs if necessary
103 if not os.path.lexists(plugins_link_dir):
104 os.makedirs(plugins_link_dir)
105
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') % (
111 link_dir))
112 return
113
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.') % (
118 plugin_static.name))
119 return
120
121 # Otherwise, it's a link that went to something else... unlink it
122 printer(_('Old link found for "%s"; removing.') % (
123 plugin_static.name))
124 os.unlink(link_dir)
125
126 os.symlink(
127 plugin_static.file_path.rstrip(os.path.sep),
128 link_dir)
129 printer('Linked asset directory for plugin "%s":\n %s\nto:\n %s\n' % (
130 plugin_static.name,
131 plugin_static.file_path.rstrip(os.path.sep),
132 link_dir))
133
134
77790875
CAW
135def install_theme(install_dir, themefile):
136 pass # TODO ;)
3da44aeb
CAW
137
138
139#############
140# Subcommands
141#############
142
143def assetlink_command(args):
144 """
145 Link the asset directory of the currently installed theme
146 """
147 global_config, app_config = setup_global_and_app_config(args.conf_file)
148 theme_registry, current_theme = register_themes(app_config)
5ccb16ca
CAW
149
150 # link theme
151 link_theme_assets(current_theme, app_config['theme_linked_assets_dir'])
152
153 # Set up the app specifically so we can access the plugin infrastructure
154 commands_util.setup_app(args)
155
156 # link plugin assets
157 ## ... probably for this we need the whole application initialized
158 for plugin_static in pluginapi.hook_runall("static_setup"):
159 link_plugin_assets(
160 plugin_static, app_config['plugin_linked_assets_dir'])
3da44aeb
CAW
161
162
163def install_command(args):
164 """
165 Handle the 'install this theme' subcommand
166 """
167 global_config, app_config = setup_global_and_app_config(args.conf_file)
77790875
CAW
168 install_dir = app_config['theme_install_dir']
169 install_theme(install_dir, args.themefile)
111a9752
CAW
170
171
172SUBCOMMANDS = {
3da44aeb
CAW
173 'assetlink': assetlink_command,
174 'install': install_command}
111a9752
CAW
175
176
177def theme(args):
178 SUBCOMMANDS[args.subcommand](args)