Commit | Line | Data |
---|---|---|
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 | 17 | import argparse |
9d0a613b | 18 | import os |
4c03d45f | 19 | import shutil |
029cad45 | 20 | |
386c9c7c BP |
21 | import six |
22 | ||
b43b17fc | 23 | from mediagoblin.tools.common import import_component |
029cad45 | 24 | |
4c03d45f CAW |
25 | import logging |
26 | _log = logging.getLogger(__name__) | |
27 | logging.basicConfig() | |
28 | ||
029cad45 CAW |
29 | |
30 | SUBCOMMAND_MAP = { | |
31 | 'shell': { | |
dbb92c60 CAW |
32 | 'setup': 'mediagoblin.gmg_commands.shell:shell_parser_setup', |
33 | 'func': 'mediagoblin.gmg_commands.shell:shell', | |
029cad45 | 34 | 'help': 'Run a shell with some tools pre-setup'}, |
243c3843 | 35 | 'adduser': { |
96108ea2 AV |
36 | 'setup': 'mediagoblin.gmg_commands.users:adduser_parser_setup', |
37 | 'func': 'mediagoblin.gmg_commands.users:adduser', | |
7f4b4471 | 38 | 'help': 'Creates an user'}, |
96108ea2 AV |
39 | 'makeadmin': { |
40 | 'setup': 'mediagoblin.gmg_commands.users:makeadmin_parser_setup', | |
41 | 'func': 'mediagoblin.gmg_commands.users:makeadmin', | |
b2800598 | 42 | 'help': 'Makes user an admin'}, |
96108ea2 AV |
43 | 'changepw': { |
44 | 'setup': 'mediagoblin.gmg_commands.users:changepw_parser_setup', | |
45 | 'func': 'mediagoblin.gmg_commands.users:changepw', | |
b2800598 | 46 | 'help': 'Changes a user\'s password'}, |
8c7ba963 LLN |
47 | 'deleteuser': { |
48 | 'setup': 'mediagoblin.gmg_commands.users:deleteuser_parser_setup', | |
49 | 'func': 'mediagoblin.gmg_commands.users:deleteuser', | |
50 | 'help': 'Deletes a user'}, | |
3ea1cf36 CAW |
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'}, | |
6afc8364 CAW |
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'}, | |
c3071480 RE |
59 | 'reprocess': { |
60 | 'setup': 'mediagoblin.gmg_commands.reprocess:reprocess_parser_setup', | |
61 | 'func': 'mediagoblin.gmg_commands.reprocess:reprocess', | |
62 | 'help': 'Reprocess media entries'}, | |
b7a8760b CAW |
63 | 'addmedia': { |
64 | 'setup': 'mediagoblin.gmg_commands.addmedia:parser_setup', | |
65 | 'func': 'mediagoblin.gmg_commands.addmedia:addmedia', | |
66 | 'help': 'Reprocess media entries'}, | |
521824da BP |
67 | 'deletemedia': { |
68 | 'setup': 'mediagoblin.gmg_commands.deletemedia:parser_setup', | |
69 | 'func': 'mediagoblin.gmg_commands.deletemedia:deletemedia', | |
70 | 'help': 'Delete media entries'}, | |
051c728c BP |
71 | 'serve': { |
72 | 'setup': 'mediagoblin.gmg_commands.serve:parser_setup', | |
73 | 'func': 'mediagoblin.gmg_commands.serve:serve', | |
74 | 'help': 'PasteScript replacement'}, | |
8aa01597 | 75 | 'batchaddmedia': { |
76 | 'setup': 'mediagoblin.gmg_commands.batchaddmedia:parser_setup', | |
77 | 'func': 'mediagoblin.gmg_commands.batchaddmedia:batchaddmedia', | |
1d09e8b4 | 78 | 'help': 'Add many media entries at once'}, |
6afc8364 CAW |
79 | # 'theme': { |
80 | # 'setup': 'mediagoblin.gmg_commands.theme:theme_parser_setup', | |
81 | # 'func': 'mediagoblin.gmg_commands.theme:theme', | |
82 | # 'help': 'Theming commands', | |
83 | # } | |
029cad45 CAW |
84 | } |
85 | ||
86 | ||
029cad45 CAW |
87 | def main_cli(): |
88 | parser = argparse.ArgumentParser( | |
89 | description='GNU MediaGoblin utilities.') | |
15ac1458 | 90 | parser.add_argument( |
9d0a613b CAW |
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")) | |
029cad45 CAW |
96 | |
97 | subparsers = parser.add_subparsers(help='sub-command help') | |
386c9c7c | 98 | for command_name, command_struct in six.iteritems(SUBCOMMAND_MAP): |
285ffedd | 99 | if 'help' in command_struct: |
029cad45 CAW |
100 | subparser = subparsers.add_parser( |
101 | command_name, help=command_struct['help']) | |
102 | else: | |
103 | subparser = subparsers.add_parser(command_name) | |
104 | ||
b43b17fc CAW |
105 | setup_func = import_component(command_struct['setup']) |
106 | exec_func = import_component(command_struct['func']) | |
029cad45 CAW |
107 | |
108 | setup_func(subparser) | |
109 | ||
110 | subparser.set_defaults(func=exec_func) | |
111 | ||
58bacb33 CAW |
112 | args = parser.parse_args() |
113 | args.orig_conf_file = args.conf_file | |
114 | if args.conf_file is None: | |
9d0a613b CAW |
115 | if os.path.exists('mediagoblin_local.ini') \ |
116 | and os.access('mediagoblin_local.ini', os.R_OK): | |
58bacb33 | 117 | args.conf_file = 'mediagoblin_local.ini' |
9d0a613b | 118 | else: |
58bacb33 | 119 | args.conf_file = 'mediagoblin.ini' |
9d0a613b | 120 | |
4c03d45f CAW |
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... | |
127 | ||
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")) | |
141 | ||
58bacb33 | 142 | args.func(args) |
029cad45 CAW |
143 | |
144 | ||
145 | if __name__ == '__main__': | |
146 | main_cli() |