Commit | Line | Data |
---|---|---|
029cad45 CAW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011 Free Software Foundation, Inc | |
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 code | |
18 | import argparse | |
19 | import os | |
20 | ||
21 | from paste.deploy.loadwsgi import NicerConfigParser | |
22 | ||
23 | from mediagoblin.celery_setup import setup_celery_from_config | |
24 | from mediagoblin import app, util | |
25 | from mediagoblin import globals as mgoblin_globals | |
26 | ||
27 | ||
28 | SUBCOMMAND_MAP = { | |
29 | 'shell': { | |
30 | 'setup': 'mediagoblin.gmg_commands:shell_parser_setup', | |
31 | 'func': 'mediagoblin.gmg_commands:shell', | |
32 | 'help': 'Run a shell with some tools pre-setup'}, | |
33 | } | |
34 | ||
35 | ||
36 | def shell_parser_setup(subparser): | |
37 | subparser.add_argument( | |
38 | '-cf', '--conf_file', default='mediagoblin.ini', | |
39 | help="Config file used to set up environment") | |
40 | subparser.add_argument( | |
41 | '-cs', '--app_section', default='app:mediagoblin', | |
42 | help="Section of the config file where the app config is stored.") | |
43 | ||
44 | ||
45 | SHELL_BANNER = """\ | |
46 | GNU MediaGoblin shell! | |
47 | ---------------------- | |
48 | Available vars: | |
49 | - mgoblin_app: instantiated mediagoblin application | |
50 | - mgoblin_globals: mediagoblin.globals | |
51 | - db: database instance | |
52 | """ | |
53 | ||
54 | ||
55 | def shell(args): | |
56 | """ | |
57 | """ | |
58 | # Duplicated from from_celery.py, remove when we have the generic util | |
59 | parser = NicerConfigParser(args.conf_file) | |
60 | parser.read(args.conf_file) | |
61 | parser._defaults.setdefault( | |
62 | 'here', os.path.dirname(os.path.abspath(args.conf_file))) | |
63 | parser._defaults.setdefault( | |
64 | '__file__', os.path.abspath(args.conf_file)) | |
65 | ||
66 | mgoblin_section = dict(parser.items(args.app_section)) | |
67 | mgoblin_conf = dict( | |
68 | [(section_name, dict(parser.items(section_name))) | |
69 | for section_name in parser.sections()]) | |
70 | ||
71 | mgoblin_app = app.paste_app_factory( | |
72 | mgoblin_conf, **mgoblin_section) | |
73 | ||
74 | code.interact( | |
75 | banner=SHELL_BANNER, | |
76 | local={ | |
77 | 'mgoblin_app': mgoblin_app, | |
78 | 'mgoblin_globals': mgoblin_globals, | |
79 | 'db': mgoblin_globals.database}) | |
80 | ||
81 | ||
82 | def main_cli(): | |
83 | parser = argparse.ArgumentParser( | |
84 | description='GNU MediaGoblin utilities.') | |
85 | ||
86 | subparsers = parser.add_subparsers(help='sub-command help') | |
87 | for command_name, command_struct in SUBCOMMAND_MAP.iteritems(): | |
88 | if command_struct.has_key('help'): | |
89 | subparser = subparsers.add_parser( | |
90 | command_name, help=command_struct['help']) | |
91 | else: | |
92 | subparser = subparsers.add_parser(command_name) | |
93 | ||
94 | setup_func = util.import_component(command_struct['setup']) | |
95 | exec_func = util.import_component(command_struct['func']) | |
96 | ||
97 | setup_func(subparser) | |
98 | ||
99 | subparser.set_defaults(func=exec_func) | |
100 | ||
101 | args = parser.parse_args() | |
102 | args.func(args) | |
103 | ||
104 | ||
105 | if __name__ == '__main__': | |
106 | main_cli() |