__init__.py 5.8 KB

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