anonymous group photoblog software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

177 lines
3.9 KiB

  1. import os
  2. import sys
  3. import inspect
  4. import werkzeug
  5. import util
  6. import fcgi
  7. import config
  8. import board
  9. import interboard
  10. from util import local
  11. COMMANDS = {}
  12. # decorators
  13. def command(f):
  14. COMMANDS[f.__name__] = f
  15. return f
  16. def need_application(f):
  17. f.need_application = True
  18. return f
  19. def need_environment(f):
  20. f.need_environment = True
  21. return f
  22. # commands
  23. @command
  24. @need_environment
  25. def rebuild_cache(board_name):
  26. """
  27. $0 rebuild_cache board_name
  28. """
  29. this_board = board.Board(board_name)
  30. local.environ['waka.board'] = this_board
  31. this_board.rebuild_cache()
  32. @command
  33. @need_environment
  34. def delete_by_ip(ip, boards):
  35. """
  36. $0 delete_by_ip ip boards
  37. """
  38. boards = boards.split(",")
  39. interboard.process_global_delete_by_ip(ip, boards)
  40. @command
  41. @need_environment
  42. def rebuild_global_cache():
  43. """
  44. $0
  45. """
  46. interboard.global_cache_rebuild()
  47. def reset_password(username):
  48. """
  49. $0 reset_password username
  50. """
  51. import staff
  52. new_password = os.urandom(8).encode("base64").strip("=\n")
  53. try:
  54. staff.edit_staff(username, clear_pass=new_password)
  55. except staff.LoginError:
  56. print "No such user %r" % username
  57. else:
  58. print "Password of %r set to %r" % (username, new_password)
  59. @command
  60. @need_application
  61. def http(application, host='', port=8000):
  62. """
  63. $0 http [host [port]]
  64. Defaults to listening on all interfaces, port 8000
  65. """
  66. app_path = os.path.basename(sys.argv[0])
  67. werkzeug.run_simple(host, int(port),
  68. util.wrap_static(application, app_path,
  69. index='wakaba.html'),
  70. use_reloader=True, use_debugger=config.DEBUG)
  71. @command
  72. @need_application
  73. def fcgi_tcp(application, host='127.0.0.1', port=9000):
  74. """
  75. $0 fcgi_tcp [host [port]]
  76. Defaults to listening on 127.0.0.1, port 9000
  77. """
  78. fcgi.WSGIServer(application, bindAddress=(host, int(port))).run()
  79. @command
  80. @need_application
  81. def fcgi_unix(application, path):
  82. """
  83. $0 fcgi_unix path
  84. """
  85. fcgi.WSGIServer(application, bindAddress=path).run()
  86. @command
  87. def help(command=None):
  88. """
  89. $0 help [command]
  90. """
  91. print "Wakarimasen CLI"
  92. f = COMMANDS.get(command, None)
  93. if f is None:
  94. print "Commands:", ', '.join(sorted(COMMANDS.keys()))
  95. else:
  96. docstring = str(f.__doc__).replace("$0", sys.argv[0])
  97. print "Usage:", docstring
  98. def init_environ():
  99. """
  100. Initialize a wsgi-ish environment for commands.
  101. Tries to get variables from the OS env and checks for required ones.
  102. """
  103. ENV_VAR_HELP = """
  104. Environment variables explanation:
  105. - DOCUMENT_ROOT: full filesystem path to html files
  106. ex: /srv/http/imageboard.example.com/
  107. - SCRIPT_NAME: url to wakarimasen.py without host part
  108. ex: /wakarimasen.py
  109. - SERVER_NAME: hostname of the webserver
  110. ex: imageboard.example.com
  111. - SERVER_PORT: port of the webserver (optional)
  112. ex: 80
  113. """
  114. local.environ.update(os.environ)
  115. werkzeug.BaseRequest(local.environ)
  116. local.environ.setdefault('waka.rootpath',
  117. os.path.join('/', config.BOARD_DIR, ''))
  118. local.environ.setdefault('wsgi.url_scheme', 'http')
  119. local.environ.setdefault('SERVER_PORT', '80')
  120. required_vars = ['DOCUMENT_ROOT', 'SCRIPT_NAME', 'SERVER_NAME']
  121. for var in required_vars:
  122. if var not in local.environ:
  123. print "Error: %s not in environment" % (var,)
  124. print ENV_VAR_HELP
  125. sys.exit(1)
  126. def handle_command(args, application):
  127. name = args.pop(0)
  128. f = COMMANDS.get(name, help)
  129. if hasattr(f, 'need_application'):
  130. args.insert(0, application)
  131. if hasattr(f, 'need_environment'):
  132. # Initialize environment
  133. init_environ()
  134. try:
  135. # attempt to call function with specified arguments
  136. inspect.getcallargs(f, *args)
  137. except TypeError:
  138. # it doesn't fit
  139. help(name)
  140. sys.exit(1)
  141. f(*args)