trac#687: Add unit tests for `redirect` and `redirect_obj`.
[larjonas-mediagoblin.git] / mediagoblin / gmg_commands / __init__.py
blobe5f9a39546aa2994a70f69cb35931bccc2631615
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 argparse
18 import os
19 import shutil
21 import six
23 from mediagoblin.tools.common import import_component
25 import logging
26 _log = logging.getLogger(__name__)
27 logging.basicConfig()
30 SUBCOMMAND_MAP = {
31 'shell': {
32 'setup': 'mediagoblin.gmg_commands.shell:shell_parser_setup',
33 'func': 'mediagoblin.gmg_commands.shell:shell',
34 'help': 'Run a shell with some tools pre-setup'},
35 'adduser': {
36 'setup': 'mediagoblin.gmg_commands.users:adduser_parser_setup',
37 'func': 'mediagoblin.gmg_commands.users:adduser',
38 'help': 'Creates an user'},
39 'makeadmin': {
40 'setup': 'mediagoblin.gmg_commands.users:makeadmin_parser_setup',
41 'func': 'mediagoblin.gmg_commands.users:makeadmin',
42 'help': 'Makes user an admin'},
43 'changepw': {
44 'setup': 'mediagoblin.gmg_commands.users:changepw_parser_setup',
45 'func': 'mediagoblin.gmg_commands.users:changepw',
46 'help': 'Changes a user\'s password'},
47 'deleteuser': {
48 'setup': 'mediagoblin.gmg_commands.users:deleteuser_parser_setup',
49 'func': 'mediagoblin.gmg_commands.users:deleteuser',
50 'help': 'Deletes a user'},
51 'dbupdate': {
52 'setup': 'mediagoblin.gmg_commands.dbupdate:dbupdate_parse_setup',
53 'func': 'mediagoblin.gmg_commands.dbupdate:dbupdate',
54 'help': 'Set up or update the SQL database'},
55 'assetlink': {
56 'setup': 'mediagoblin.gmg_commands.assetlink:assetlink_parser_setup',
57 'func': 'mediagoblin.gmg_commands.assetlink:assetlink',
58 'help': 'Link assets for themes and plugins for static serving'},
59 'reprocess': {
60 'setup': 'mediagoblin.gmg_commands.reprocess:reprocess_parser_setup',
61 'func': 'mediagoblin.gmg_commands.reprocess:reprocess',
62 'help': 'Reprocess media entries'},
63 'addmedia': {
64 'setup': 'mediagoblin.gmg_commands.addmedia:parser_setup',
65 'func': 'mediagoblin.gmg_commands.addmedia:addmedia',
66 'help': 'Reprocess media entries'},
67 'deletemedia': {
68 'setup': 'mediagoblin.gmg_commands.deletemedia:parser_setup',
69 'func': 'mediagoblin.gmg_commands.deletemedia:deletemedia',
70 'help': 'Delete media entries'},
71 'serve': {
72 'setup': 'mediagoblin.gmg_commands.serve:parser_setup',
73 'func': 'mediagoblin.gmg_commands.serve:serve',
74 'help': 'PasteScript replacement'},
75 'batchaddmedia': {
76 'setup': 'mediagoblin.gmg_commands.batchaddmedia:parser_setup',
77 'func': 'mediagoblin.gmg_commands.batchaddmedia:batchaddmedia',
78 'help': 'Add many media entries at once'},
79 # 'theme': {
80 # 'setup': 'mediagoblin.gmg_commands.theme:theme_parser_setup',
81 # 'func': 'mediagoblin.gmg_commands.theme:theme',
82 # 'help': 'Theming commands',
83 # }
87 def main_cli():
88 parser = argparse.ArgumentParser(
89 description='GNU MediaGoblin utilities.')
90 parser.add_argument(
91 '-cf', '--conf_file', default=None,
92 help=(
93 "Config file used to set up environment. "
94 "Default to mediagoblin_local.ini if readable, "
95 "otherwise mediagoblin.ini"))
97 subparsers = parser.add_subparsers(help='sub-command help')
98 for command_name, command_struct in six.iteritems(SUBCOMMAND_MAP):
99 if 'help' in command_struct:
100 subparser = subparsers.add_parser(
101 command_name, help=command_struct['help'])
102 else:
103 subparser = subparsers.add_parser(command_name)
105 setup_func = import_component(command_struct['setup'])
106 exec_func = import_component(command_struct['func'])
108 setup_func(subparser)
110 subparser.set_defaults(func=exec_func)
112 args = parser.parse_args()
113 args.orig_conf_file = args.conf_file
114 if args.conf_file is None:
115 if os.path.exists('mediagoblin_local.ini') \
116 and os.access('mediagoblin_local.ini', os.R_OK):
117 args.conf_file = 'mediagoblin_local.ini'
118 else:
119 args.conf_file = 'mediagoblin.ini'
121 # This is a hopefully TEMPORARY hack for adding a mediagoblin.ini
122 # if none exists, to make up for a deficiency as we are migrating
123 # our docs to the new "no mediagoblin.ini by default" workflow.
124 # Normally, the docs should provide instructions for this, but
125 # since 0.7.1 docs say "install from master!" and yet we removed
126 # mediagoblin.ini, we need to cover our bases...
128 parent_directory = os.path.split(os.path.abspath(args.conf_file))[0]
129 if os.path.split(args.conf_file)[1] == 'mediagoblin.ini' \
130 and not os.path.exists(args.conf_file) \
131 and os.path.exists(
132 os.path.join(
133 parent_directory, 'mediagoblin.example.ini')):
134 # Do the copy-over of the mediagoblin config for the user
135 _log.warning(
136 "No mediagoblin.ini found and no other path given, "
137 "so making one for you.")
138 shutil.copy(
139 os.path.join(parent_directory, "mediagoblin.example.ini"),
140 os.path.join(parent_directory, "mediagoblin.ini"))
142 args.func(args)
145 if __name__ == '__main__':
146 main_cli()