25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

105 satır
2.7 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. import warnings
  10. click.disable_unicode_literals_warning = True
  11. def main():
  12. commands = get_app_groups()
  13. commands.update({
  14. 'get-frappe-commands': get_frappe_commands,
  15. 'get-frappe-help': get_frappe_help
  16. })
  17. click.Group(commands=commands)(prog_name='bench')
  18. def get_app_groups():
  19. '''Get all app groups, put them in main group "frappe" since bench is
  20. designed to only handle that'''
  21. commands = dict()
  22. for app in get_apps():
  23. app_commands = get_app_commands(app)
  24. if app_commands:
  25. commands.update(app_commands)
  26. ret = dict(frappe=click.group(name='frappe', commands=commands)(app_group))
  27. return ret
  28. def get_app_group(app):
  29. app_commands = get_app_commands(app)
  30. if app_commands:
  31. return click.group(name=app, commands=app_commands)(app_group)
  32. @click.option('--site')
  33. @click.option('--profile', is_flag=True, default=False, help='Profile')
  34. @click.option('--verbose', is_flag=True, default=False, help='Verbose')
  35. @click.option('--force', is_flag=True, default=False, help='Force')
  36. @click.pass_context
  37. def app_group(ctx, site=False, force=False, verbose=False, profile=False):
  38. ctx.obj = {
  39. 'sites': get_sites(site),
  40. 'force': force,
  41. 'verbose': verbose,
  42. 'profile': profile
  43. }
  44. if ctx.info_name == 'frappe':
  45. ctx.info_name = ''
  46. def get_sites(site_arg):
  47. if site_arg == 'all':
  48. return frappe.utils.get_sites()
  49. elif site_arg:
  50. return [site_arg]
  51. elif os.environ.get('FRAPPE_SITE'):
  52. return [os.environ.get('FRAPPE_SITE')]
  53. elif os.path.exists('currentsite.txt'):
  54. with open('currentsite.txt') as f:
  55. site = f.read().strip()
  56. if site:
  57. return [site]
  58. return []
  59. def get_app_commands(app):
  60. if os.path.exists(os.path.join('..', 'apps', app, app, 'commands.py'))\
  61. or os.path.exists(os.path.join('..', 'apps', app, app, 'commands', '__init__.py')):
  62. try:
  63. app_command_module = importlib.import_module(app + '.commands')
  64. except Exception:
  65. traceback.print_exc()
  66. return []
  67. else:
  68. return []
  69. ret = {}
  70. for command in getattr(app_command_module, 'commands', []):
  71. ret[command.name] = command
  72. return ret
  73. @click.command('get-frappe-commands')
  74. def get_frappe_commands():
  75. commands = list(get_app_commands('frappe'))
  76. for app in get_apps():
  77. app_commands = get_app_commands(app)
  78. if app_commands:
  79. commands.extend(list(app_commands))
  80. print(json.dumps(commands))
  81. @click.command('get-frappe-help')
  82. def get_frappe_help():
  83. print(click.Context(get_app_groups()['frappe']).get_help())
  84. def get_apps():
  85. return frappe.get_all_apps(with_internal_apps=False, sites_path='.')
  86. if __name__ == "__main__":
  87. if not frappe._dev_server:
  88. warnings.simplefilter('ignore')
  89. main()