選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

96 行
2.4 KiB

  1. from __future__ import unicode_literals, print_function
  2. import click
  3. import frappe
  4. import os
  5. import json
  6. import importlib
  7. import frappe.utils
  8. import traceback
  9. click.disable_unicode_literals_warning = True
  10. def main():
  11. commands = get_app_groups()
  12. commands.update({
  13. 'get-frappe-commands': get_frappe_commands,
  14. 'get-frappe-help': get_frappe_help
  15. })
  16. click.Group(commands=commands)(prog_name='bench')
  17. def get_app_groups():
  18. '''Get all app groups, put them in main group "frappe" since bench is
  19. designed to only handle that'''
  20. commands = dict()
  21. for app in get_apps():
  22. app_commands = get_app_commands(app)
  23. if app_commands:
  24. commands.update(app_commands)
  25. ret = dict(frappe=click.group(name='frappe', commands=commands)(app_group))
  26. return ret
  27. def get_app_group(app):
  28. app_commands = get_app_commands(app)
  29. if app_commands:
  30. return click.group(name=app, commands=app_commands)(app_group)
  31. @click.option('--site')
  32. @click.option('--profile', is_flag=True, default=False, help='Profile')
  33. @click.option('--verbose', is_flag=True, default=False, help='Verbose')
  34. @click.option('--force', is_flag=True, default=False, help='Force')
  35. @click.pass_context
  36. def app_group(ctx, site=False, force=False, verbose=False, profile=False):
  37. ctx.obj = {
  38. 'sites': get_sites(site),
  39. 'force': force,
  40. 'verbose': verbose,
  41. 'profile': profile
  42. }
  43. if ctx.info_name == 'frappe':
  44. ctx.info_name = ''
  45. def get_sites(site_arg):
  46. if site_arg and site_arg == 'all':
  47. return frappe.utils.get_sites()
  48. else:
  49. if site_arg:
  50. return [site_arg]
  51. if os.path.exists('currentsite.txt'):
  52. with open('currentsite.txt') as f:
  53. return [f.read().strip()]
  54. def get_app_commands(app):
  55. try:
  56. app_command_module = importlib.import_module(app + '.commands')
  57. except ImportError as e:
  58. if not 'No module named commands' in str(e):
  59. traceback.print_exc()
  60. return []
  61. ret = {}
  62. for command in getattr(app_command_module, 'commands', []):
  63. ret[command.name] = command
  64. return ret
  65. @click.command('get-frappe-commands')
  66. def get_frappe_commands():
  67. commands = list(get_app_commands('frappe').keys())
  68. for app in get_apps():
  69. app_commands = get_app_commands(app)
  70. if app_commands:
  71. commands.extend(app_commands.keys())
  72. print(json.dumps(commands))
  73. @click.command('get-frappe-help')
  74. def get_frappe_help():
  75. print(click.Context(get_app_groups()['frappe']).get_help())
  76. def get_apps():
  77. return frappe.get_all_apps(with_internal_apps=False, sites_path='.')
  78. if __name__ == "__main__":
  79. main()