command.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import logging
  3. import os
  4. import sys
  5. from pathlib import Path
  6. import odoo
  7. from odoo.modules import get_modules, get_module_path, initialize_sys_path
  8. commands = {}
  9. class Command:
  10. name = None
  11. def __init_subclass__(cls):
  12. cls.name = cls.name or cls.__name__.lower()
  13. commands[cls.name] = cls
  14. ODOO_HELP = """\
  15. Odoo CLI, use '{odoo_bin} --help' for regular server options.
  16. Available commands:
  17. {command_list}
  18. Use '{odoo_bin} <command> --help' for individual command help."""
  19. class Help(Command):
  20. """ Display the list of available commands """
  21. def run(self, args):
  22. padding = max([len(cmd) for cmd in commands]) + 2
  23. command_list = "\n ".join([
  24. " {}{}".format(name.ljust(padding), (command.__doc__ or "").strip())
  25. for name, command in sorted(commands.items())
  26. ])
  27. print(ODOO_HELP.format( # pylint: disable=bad-builtin
  28. odoo_bin=Path(sys.argv[0]).name,
  29. command_list=command_list
  30. ))
  31. def main():
  32. args = sys.argv[1:]
  33. # The only shared option is '--addons-path=' needed to discover additional
  34. # commands from modules
  35. if len(args) > 1 and args[0].startswith('--addons-path=') and not args[1].startswith("-"):
  36. # parse only the addons-path, do not setup the logger...
  37. odoo.tools.config._parse_config([args[0]])
  38. args = args[1:]
  39. # Default legacy command
  40. command = "server"
  41. # TODO: find a way to properly discover addons subcommands without importing the world
  42. # Subcommand discovery
  43. if len(args) and not args[0].startswith("-"):
  44. logging.disable(logging.CRITICAL)
  45. initialize_sys_path()
  46. for module in get_modules():
  47. if (Path(get_module_path(module)) / 'cli').is_dir():
  48. __import__('odoo.addons.' + module)
  49. logging.disable(logging.NOTSET)
  50. command = args[0]
  51. args = args[1:]
  52. if command in commands:
  53. o = commands[command]()
  54. o.run(args)
  55. else:
  56. sys.exit('Unknown command %r' % (command,))