The `initialize_sys_path` is the function responsible of getting the custom Odoo import hooks to work, i.e. addons import via `import odoo.addons`. This function is called by some other module related functions to ensure the paths are correctly setup. We are confident it is useless to re-initialize the path in many places. The `load_openerp_module` function is called during server bootup by both `odoo-bin server` and `odoo-bin shell`, both command parse the configuration before starting the server which initialize the paths already. Most call to `get_module`, `get_module_path` and `get_resource_path` are done in models or controllers where a registry is setup already. There is one notable exception which is the subcommand discovery done during the bootup, for that specific case, we initialize the paths with a partially loaded configuration. closes odoo/odoo#49715 Task: 2200956 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
from __future__ import print_function
|
|
import logging
|
|
import sys
|
|
import os
|
|
from os.path import join as joinpath, isdir
|
|
|
|
import odoo
|
|
from odoo.modules import get_modules, get_module_path, initialize_sys_path
|
|
|
|
commands = {}
|
|
|
|
class CommandType(type):
|
|
def __init__(cls, name, bases, attrs):
|
|
super(CommandType, cls).__init__(name, bases, attrs)
|
|
name = getattr(cls, name, cls.__name__.lower())
|
|
cls.name = name
|
|
if name != 'command':
|
|
commands[name] = cls
|
|
|
|
Command = CommandType('Command', (object,), {'run': lambda self, args: None})
|
|
|
|
class Help(Command):
|
|
"""Display the list of available commands"""
|
|
def run(self, args):
|
|
print("Available commands:\n")
|
|
names = list(commands)
|
|
padding = max([len(k) for k in names]) + 2
|
|
for k in sorted(names):
|
|
name = k.ljust(padding, ' ')
|
|
doc = (commands[k].__doc__ or '').strip()
|
|
print(" %s%s" % (name, doc))
|
|
print("\nUse '%s <command> --help' for individual command help." % sys.argv[0].split(os.path.sep)[-1])
|
|
|
|
def main():
|
|
args = sys.argv[1:]
|
|
|
|
# The only shared option is '--addons-path=' needed to discover additional
|
|
# commands from modules
|
|
if len(args) > 1 and args[0].startswith('--addons-path=') and not args[1].startswith("-"):
|
|
# parse only the addons-path, do not setup the logger...
|
|
odoo.tools.config._parse_config([args[0]])
|
|
args = args[1:]
|
|
|
|
# Default legacy command
|
|
command = "server"
|
|
|
|
# TODO: find a way to properly discover addons subcommands without importing the world
|
|
# Subcommand discovery
|
|
if len(args) and not args[0].startswith("-"):
|
|
logging.disable(logging.CRITICAL)
|
|
initialize_sys_path()
|
|
for module in get_modules():
|
|
if isdir(joinpath(get_module_path(module), 'cli')):
|
|
__import__('odoo.addons.' + module)
|
|
logging.disable(logging.NOTSET)
|
|
command = args[0]
|
|
args = args[1:]
|
|
|
|
if command in commands:
|
|
o = commands[command]()
|
|
o.run(args)
|
|
else:
|
|
sys.exit('Unknown command %r' % (command,))
|