Commit | Line | Data |
---|---|---|
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 | ||
17 | import os | |
18 | ||
6afc8364 | 19 | from mediagoblin import mg_globals |
111a9752 | 20 | from mediagoblin.init import setup_global_and_app_config |
5ccb16ca | 21 | from mediagoblin.gmg_commands import util as commands_util |
111a9752 | 22 | from mediagoblin.tools.theme import register_themes |
a789b713 | 23 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
3da44aeb | 24 | from mediagoblin.tools.common import simple_printer |
5ccb16ca | 25 | from mediagoblin.tools import pluginapi |
111a9752 CAW |
26 | |
27 | ||
6afc8364 CAW |
28 | def assetlink_parser_setup(subparser): |
29 | # theme_subparsers = subparser.add_subparsers( | |
30 | # dest=u"subcommand", | |
31 | # help=u'Assetlink options') | |
111a9752 | 32 | |
6afc8364 CAW |
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') | |
111a9752 | 38 | |
6afc8364 CAW |
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 | |
111a9752 CAW |
45 | |
46 | ||
3da44aeb CAW |
47 | ########### |
48 | # Utilities | |
49 | ########### | |
50 | ||
5ccb16ca | 51 | def link_theme_assets(theme, link_dir, printer=simple_printer): |
111a9752 | 52 | """ |
3da44aeb CAW |
53 | Returns a list of string of text telling the user what we did |
54 | which should be printable. | |
111a9752 | 55 | """ |
3da44aeb | 56 | link_dir = link_dir.rstrip(os.path.sep) |
9d3e56d5 | 57 | link_parent_dir = os.path.dirname(link_dir) |
111a9752 | 58 | |
3da44aeb CAW |
59 | if theme is None: |
60 | printer(_("Cannot link theme... no theme set\n")) | |
5ccb16ca | 61 | return |
111a9752 | 62 | |
8a22617f CAW |
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 | |
111a9752 | 69 | |
5ccb16ca | 70 | return |
111a9752 | 71 | |
3da44aeb CAW |
72 | if theme.get('assets_dir') is None: |
73 | printer(_("No asset directory for this theme\n")) | |
8a22617f | 74 | if _maybe_unlink_link_dir(): |
3da44aeb CAW |
75 | printer( |
76 | _("However, old link directory symlink found; removed.\n")) | |
5ccb16ca | 77 | return |
8a22617f CAW |
78 | |
79 | _maybe_unlink_link_dir() | |
80 | ||
81 | # make the link directory parent dirs if necessary | |
82 | if not os.path.lexists(link_parent_dir): | |
83 | os.makedirs(link_parent_dir) | |
84 | ||
85 | os.symlink( | |
3da44aeb | 86 | theme['assets_dir'].rstrip(os.path.sep), |
8a22617f | 87 | link_dir) |
3da44aeb CAW |
88 | printer("Linked the theme's asset directory:\n %s\nto:\n %s\n" % ( |
89 | theme['assets_dir'], link_dir)) | |
90 | ||
91 | ||
5ccb16ca CAW |
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 | |
9230968f CAW |
101 | link_dir = os.path.join( |
102 | plugins_link_dir.rstrip(os.path.sep), plugin_static.name) | |
5ccb16ca CAW |
103 | |
104 | # make the link directory parent dirs if necessary | |
105 | if not os.path.lexists(plugins_link_dir): | |
106 | os.makedirs(plugins_link_dir) | |
107 | ||
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): | |
df69695d CAW |
112 | printer(_('Could not link "%s": %s exists and is not a symlink\n') % ( |
113 | plugin_static.name, link_dir)) | |
5ccb16ca CAW |
114 | return |
115 | ||
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? | |
df69695d | 119 | printer(_('Skipping "%s"; already set up.\n') % ( |
5ccb16ca CAW |
120 | plugin_static.name)) |
121 | return | |
122 | ||
123 | # Otherwise, it's a link that went to something else... unlink it | |
df69695d | 124 | printer(_('Old link found for "%s"; removing.\n') % ( |
5ccb16ca CAW |
125 | plugin_static.name)) |
126 | os.unlink(link_dir) | |
127 | ||
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)) | |
135 | ||
136 | ||
6afc8364 | 137 | def assetlink(args): |
3da44aeb | 138 | """ |
247759ca | 139 | Link the asset directory of the currently installed theme and plugins |
3da44aeb | 140 | """ |
6afc8364 CAW |
141 | mgoblin_app = commands_util.setup_app(args) |
142 | app_config = mg_globals.app_config | |
5ccb16ca CAW |
143 | |
144 | # link theme | |
6afc8364 | 145 | link_theme_assets(mgoblin_app.current_theme, app_config['theme_linked_assets_dir']) |
5ccb16ca CAW |
146 | |
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']) |