Merge branch 'master' into derek-moore-bug405_email_notifications_for_comments
[mediagoblin.git] / mediagoblin / gmg_commands / __init__.py
CommitLineData
029cad45 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
029cad45
CAW
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
029cad45 17import argparse
9d0a613b 18import os
029cad45 19
b43b17fc 20from mediagoblin.tools.common import import_component
029cad45
CAW
21
22
23SUBCOMMAND_MAP = {
24 'shell': {
dbb92c60
CAW
25 'setup': 'mediagoblin.gmg_commands.shell:shell_parser_setup',
26 'func': 'mediagoblin.gmg_commands.shell:shell',
029cad45 27 'help': 'Run a shell with some tools pre-setup'},
757f37a5
CAW
28 'migrate': {
29 'setup': 'mediagoblin.gmg_commands.migrate:migrate_parser_setup',
30 'func': 'mediagoblin.gmg_commands.migrate:migrate',
31 'help': 'Apply all unapplied bulk migrations to the database'},
243c3843 32 'adduser': {
96108ea2
AV
33 'setup': 'mediagoblin.gmg_commands.users:adduser_parser_setup',
34 'func': 'mediagoblin.gmg_commands.users:adduser',
7f4b4471 35 'help': 'Creates an user'},
96108ea2
AV
36 'makeadmin': {
37 'setup': 'mediagoblin.gmg_commands.users:makeadmin_parser_setup',
38 'func': 'mediagoblin.gmg_commands.users:makeadmin',
7f4b4471 39 'help': 'Changes a user\'s password'},
96108ea2
AV
40 'changepw': {
41 'setup': 'mediagoblin.gmg_commands.users:changepw_parser_setup',
42 'func': 'mediagoblin.gmg_commands.users:changepw',
7f4b4471 43 'help': 'Makes admin an user'},
763cf83f
WKG
44 'wipealldata': {
45 'setup': 'mediagoblin.gmg_commands.wipealldata:wipe_parser_setup',
46 'func': 'mediagoblin.gmg_commands.wipealldata:wipe',
47 'help': 'Wipes **all** the data for this MediaGoblin instance'},
e86d4f5d
JW
48 'env_export': {
49 'setup': 'mediagoblin.gmg_commands.import_export:import_export_parse_setup',
50 'func': 'mediagoblin.gmg_commands.import_export:env_export',
51 'help': 'Exports the data for this MediaGoblin instance'},
52 'env_import': {
53 'setup': 'mediagoblin.gmg_commands.import_export:import_export_parse_setup',
54 'func': 'mediagoblin.gmg_commands.import_export:env_import',
55 'help': 'Exports the data for this MediaGoblin instance'},
3ea1cf36
CAW
56 'dbupdate': {
57 'setup': 'mediagoblin.gmg_commands.dbupdate:dbupdate_parse_setup',
58 'func': 'mediagoblin.gmg_commands.dbupdate:dbupdate',
59 'help': 'Set up or update the SQL database'},
98913512
E
60 'convert_mongo_to_sql': {
61 'setup': 'mediagoblin.gmg_commands.mongosql:mongosql_parser_setup',
62 'func': 'mediagoblin.gmg_commands.mongosql:mongosql',
63 'help': 'Convert Mongo DB data to SQL DB data'},
029cad45
CAW
64 }
65
66
029cad45
CAW
67def main_cli():
68 parser = argparse.ArgumentParser(
69 description='GNU MediaGoblin utilities.')
15ac1458 70 parser.add_argument(
9d0a613b
CAW
71 '-cf', '--conf_file', default=None,
72 help=(
73 "Config file used to set up environment. "
74 "Default to mediagoblin_local.ini if readable, "
75 "otherwise mediagoblin.ini"))
029cad45
CAW
76
77 subparsers = parser.add_subparsers(help='sub-command help')
78 for command_name, command_struct in SUBCOMMAND_MAP.iteritems():
285ffedd 79 if 'help' in command_struct:
029cad45
CAW
80 subparser = subparsers.add_parser(
81 command_name, help=command_struct['help'])
82 else:
83 subparser = subparsers.add_parser(command_name)
84
b43b17fc
CAW
85 setup_func = import_component(command_struct['setup'])
86 exec_func = import_component(command_struct['func'])
029cad45
CAW
87
88 setup_func(subparser)
89
90 subparser.set_defaults(func=exec_func)
91
92 args = parser.parse_args()
9d0a613b
CAW
93 if args.conf_file is None:
94 if os.path.exists('mediagoblin_local.ini') \
95 and os.access('mediagoblin_local.ini', os.R_OK):
96 args.conf_file = 'mediagoblin_local.ini'
97 else:
98 args.conf_file = 'mediagoblin.ini'
99
029cad45
CAW
100 args.func(args)
101
102
103if __name__ == '__main__':
104 main_cli()