| 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 argparse |
| 18 | import os |
| 19 | import shutil |
| 20 | |
| 21 | import six |
| 22 | |
| 23 | from mediagoblin.tools.common import import_component |
| 24 | |
| 25 | import logging |
| 26 | _log = logging.getLogger(__name__) |
| 27 | logging.basicConfig() |
| 28 | |
| 29 | |
| 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 | 'alembic': { |
| 80 | 'setup': 'mediagoblin.gmg_commands.alembic_commands:parser_setup', |
| 81 | 'func': 'mediagoblin.gmg_commands.alembic_commands:raw_alembic_cli', |
| 82 | 'help': ( |
| 83 | 'Run raw alembic commands with our local database. ' |
| 84 | '(Unless you know what you\'re doing, use dbupdate instead!)')}, |
| 85 | # 'theme': { |
| 86 | # 'setup': 'mediagoblin.gmg_commands.theme:theme_parser_setup', |
| 87 | # 'func': 'mediagoblin.gmg_commands.theme:theme', |
| 88 | # 'help': 'Theming commands', |
| 89 | # } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | def main_cli(): |
| 94 | parser = argparse.ArgumentParser( |
| 95 | description='GNU MediaGoblin utilities.') |
| 96 | parser.add_argument( |
| 97 | '-cf', '--conf_file', default=None, |
| 98 | help=( |
| 99 | "Config file used to set up environment. " |
| 100 | "Default to mediagoblin_local.ini if readable, " |
| 101 | "otherwise mediagoblin.ini")) |
| 102 | |
| 103 | subparsers = parser.add_subparsers(help='sub-command help') |
| 104 | for command_name, command_struct in six.iteritems(SUBCOMMAND_MAP): |
| 105 | if 'help' in command_struct: |
| 106 | subparser = subparsers.add_parser( |
| 107 | command_name, help=command_struct['help']) |
| 108 | else: |
| 109 | subparser = subparsers.add_parser(command_name) |
| 110 | |
| 111 | setup_func = import_component(command_struct['setup']) |
| 112 | exec_func = import_component(command_struct['func']) |
| 113 | |
| 114 | setup_func(subparser) |
| 115 | |
| 116 | subparser.set_defaults(func=exec_func) |
| 117 | |
| 118 | args = parser.parse_args() |
| 119 | args.orig_conf_file = args.conf_file |
| 120 | if args.conf_file is None: |
| 121 | if os.path.exists('mediagoblin_local.ini') \ |
| 122 | and os.access('mediagoblin_local.ini', os.R_OK): |
| 123 | args.conf_file = 'mediagoblin_local.ini' |
| 124 | else: |
| 125 | args.conf_file = 'mediagoblin.ini' |
| 126 | |
| 127 | # This is a hopefully TEMPORARY hack for adding a mediagoblin.ini |
| 128 | # if none exists, to make up for a deficiency as we are migrating |
| 129 | # our docs to the new "no mediagoblin.ini by default" workflow. |
| 130 | # Normally, the docs should provide instructions for this, but |
| 131 | # since 0.7.1 docs say "install from master!" and yet we removed |
| 132 | # mediagoblin.ini, we need to cover our bases... |
| 133 | |
| 134 | parent_directory = os.path.split(os.path.abspath(args.conf_file))[0] |
| 135 | if os.path.split(args.conf_file)[1] == 'mediagoblin.ini' \ |
| 136 | and not os.path.exists(args.conf_file) \ |
| 137 | and os.path.exists( |
| 138 | os.path.join( |
| 139 | parent_directory, 'mediagoblin.example.ini')): |
| 140 | # Do the copy-over of the mediagoblin config for the user |
| 141 | _log.warning( |
| 142 | "No mediagoblin.ini found and no other path given, " |
| 143 | "so making one for you.") |
| 144 | shutil.copy( |
| 145 | os.path.join(parent_directory, "mediagoblin.example.ini"), |
| 146 | os.path.join(parent_directory, "mediagoblin.ini")) |
| 147 | |
| 148 | try: |
| 149 | args.func(args) |
| 150 | except AttributeError: # no subcommand or no func of subcommand |
| 151 | parser.print_help() |
| 152 | |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | main_cli() |