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.
 
 
 
 
 
 

101 lines
2.6 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 == 'all':
  47. return frappe.utils.get_sites()
  48. elif site_arg:
  49. return [site_arg]
  50. elif os.path.exists('currentsite.txt'):
  51. with open('currentsite.txt') as f:
  52. site = f.read().strip()
  53. if site:
  54. return [site]
  55. return []
  56. def get_app_commands(app):
  57. if os.path.exists(os.path.join('..', 'apps', app, app, 'commands.py'))\
  58. or os.path.exists(os.path.join('..', 'apps', app, app, 'commands', '__init__.py')):
  59. try:
  60. app_command_module = importlib.import_module(app + '.commands')
  61. except Exception:
  62. traceback.print_exc()
  63. return []
  64. else:
  65. return []
  66. ret = {}
  67. for command in getattr(app_command_module, 'commands', []):
  68. ret[command.name] = command
  69. return ret
  70. @click.command('get-frappe-commands')
  71. def get_frappe_commands():
  72. commands = list(get_app_commands('frappe'))
  73. for app in get_apps():
  74. app_commands = get_app_commands(app)
  75. if app_commands:
  76. commands.extend(list(app_commands))
  77. print(json.dumps(commands))
  78. @click.command('get-frappe-help')
  79. def get_frappe_help():
  80. print(click.Context(get_app_groups()['frappe']).get_help())
  81. def get_apps():
  82. return frappe.get_all_apps(with_internal_apps=False, sites_path='.')
  83. if __name__ == "__main__":
  84. main()