Three fixes to collection adding view, one of them a serious security bug
[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
20from mediagoblin.tools.theme import register_themes
3da44aeb
CAW
21from mediagoblin.tools.translate import pass_to_ugettext as _
22from mediagoblin.tools.common import simple_printer
111a9752
CAW
23
24
25def theme_parser_setup(subparser):
26 theme_subparsers = subparser.add_subparsers(
27 dest=u"subcommand",
28 help=u'Theme sub-commands')
29
30 # Install command
31 install_parser = theme_subparsers.add_parser(
32 u'install', help=u'Install a theme to this mediagoblin instance')
33 install_parser.add_argument(
34 u'themefile', help=u'The theme archive to be installed')
35
111a9752
CAW
36 theme_subparsers.add_parser(
37 u'assetlink',
38 help=(
39 u"Link the currently installed theme's assets "
40 u"to the served theme asset directory"))
41
42
3da44aeb
CAW
43###########
44# Utilities
45###########
46
47def link_assets(theme, link_dir, printer=simple_printer):
111a9752 48 """
3da44aeb
CAW
49 Returns a list of string of text telling the user what we did
50 which should be printable.
111a9752 51 """
3da44aeb 52 link_dir = link_dir.rstrip(os.path.sep)
9d3e56d5 53 link_parent_dir = os.path.dirname(link_dir)
111a9752 54
3da44aeb
CAW
55 results = []
56
57 if theme is None:
58 printer(_("Cannot link theme... no theme set\n"))
59 return results
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
3da44aeb 68 return results
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"))
75 return results
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
77790875
CAW
90def install_theme(install_dir, themefile):
91 pass # TODO ;)
3da44aeb
CAW
92
93
94#############
95# Subcommands
96#############
97
98def assetlink_command(args):
99 """
100 Link the asset directory of the currently installed theme
101 """
102 global_config, app_config = setup_global_and_app_config(args.conf_file)
103 theme_registry, current_theme = register_themes(app_config)
104 link_assets(current_theme, app_config['theme_linked_assets_dir'])
105
106
107def install_command(args):
108 """
109 Handle the 'install this theme' subcommand
110 """
111 global_config, app_config = setup_global_and_app_config(args.conf_file)
77790875
CAW
112 install_dir = app_config['theme_install_dir']
113 install_theme(install_dir, args.themefile)
111a9752
CAW
114
115
116SUBCOMMANDS = {
3da44aeb
CAW
117 'assetlink': assetlink_command,
118 'install': install_command}
111a9752
CAW
119
120
121def theme(args):
122 SUBCOMMANDS[args.subcommand](args)