启动文件 odoo-bin.py
主要过程:
1.导入os,设置环境上下文
2.执行cli目录下的main()方法
#!/usr/bin/env python3 # set server timezone in UTC before time module imported __import__('os').environ['TZ'] = 'UTC' #此处是导入os模块并设置全局环境 import odoo if __name__ == "__main__": odoo.cli.main() #启动脚本, 执行main()函数
cli目录下的 command.py
cli.main()执行的是command.py中的 main()方法
主要过程:
1.获取系统启动参数
2.获取模块信息并导入
3.根据命令,实例化服务并启动
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" # 默认启动命令的方式 为‘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) 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]() #获取命令并执行, 实际是实例了一个 Server对象 o.run(args) # 调用Server的run()方法 else: sys.exit('Unknow command %r' % (command,))
cli目录下的 server.py
Server类用于启动odoo服务,启动方法为run,而run 执行的是server.py中的 main()方法
主要过程:
1.检查用户(包括数据库用户)以及设置config参数
2.创建数据库(此处后续文章详细说明)
3.启动odoo的http服务,执行service目录下的server.py中的start()方法
def main(args): check_root_user() #检查root用户 odoo.tools.config.parse_config(args) #解析配置参数 check_postgres_user() # 检查数据库用户, 针对 postgres用户 report_configuration() #记录配置的值 config = odoo.tools.config # the default limit for CSV fields in the module is 128KiB, which is not # quite sufficient to import images to store in attachment. 500MiB is a # bit overkill, but better safe than sorry I guess csv.field_size_limit(500 * 1024 * 1024) preload = [] if config['db_name']: preload = config['db_name'].split(',') for db_name in preload: try: odoo.service.db._create_empty_database(db_name) #创建空的数据库 config['init']['base'] = True except ProgrammingError as err: if err.pgcode == errorcodes.INSUFFICIENT_PRIVILEGE: # We use an INFO loglevel on purpose in order to avoid # reporting unnecessary warnings on build environment # using restricted database access. _logger.info("Could not determine if database %s exists, " "skipping auto-creation: %s", db_name, err) else: raise err except odoo.service.db.DatabaseExists: pass if config["translate_out"]: export_translation() sys.exit(0) if config["translate_in"]: import_translation() sys.exit(0) # This needs to be done now to ensure the use of the multiprocessing # signaling mecanism for registries loaded with -d if config['workers']: odoo.multi_process = True stop = config["stop_after_init"] setup_pid_file() rc = odoo.service.server.start(preload=preload, stop=stop) #启动odoo的http服务 sys.exit(rc) class Server(Command): """Start the odoo server (default command)""" def run(self, args): main(args)
service目录下的 server.py
start用于启动odoo的http服务
主要过程:
1.设置一个全局的server对象
2.实例server对象(wsgi_server)
3.启动wsgi_server(实际是实例化root对象,位于http.py中)
4.启动http服务后,便会加载模块,路由分发等等
def start(preload=None, stop=False): """ Start the odoo http server and cron processor. """ global server load_server_wide_modules() odoo.service.wsgi_server._patch_xmlrpc_marshaller() if odoo.evented: server = GeventServer(odoo.service.wsgi_server.application) elif config['workers']: if config['test_enable'] or config['test_file']: _logger.warning("Unit testing in workers mode could fail; use --workers 0.") server = PreforkServer(odoo.service.wsgi_server.application) # Workaround for Python issue24291, fixed in 3.6 (see Python issue26721) if sys.version_info[:2] == (3,5): # turn on buffering also for wfile, to avoid partial writes (Default buffer = 8k) werkzeug.serving.WSGIRequestHandler.wbufsize = -1 else: server = ThreadedServer(odoo.service.wsgi_server.application) watcher = None if 'reload' in config['dev_mode'] and not odoo.evented: if inotify: watcher = FSWatcherInotify() watcher.start() elif watchdog: watcher = FSWatcherWatchdog() watcher.start() else: if os.name == 'posix' and platform.system() != 'Darwin': module = 'inotify' else: module = 'watchdog' _logger.warning("'%s' module not installed. Code autoreload feature is disabled", module) if 'werkzeug' in config['dev_mode']: server.app = DebuggedApplication(server.app, evalex=True) rc = server.run(preload, stop) if watcher: watcher.stop() # like the legend of the phoenix, all ends with beginnings if getattr(odoo, 'phoenix', False): _reexec() return rc if rc else 0
以上,为启动的大致过程。其实加载模块,也是应用了python本身的导入机制。后续抽空研究odoo的模块加载机制,路由分发机制等等。