WIP: ZODB Components: Filesystem modules imports compatibility
@nexedi Until now,
fix_imports command line script was used to fix imports in existing code after migration from filesystem to ZODB Components (eg fixing
from Products.ERP5.Document.Foo import ... to
erp5.component.document.Foo import ... for example). However, @jp and @romain do not like this and so here is a PoC implementing Products imports compatibility by using existing ZODB Components import hooks. Even though this does not require much code, it is a very intrusive change so you should read carefully until the end...
Foo which has been migrated from
Products.ERP5.Document.Foo (this is an example with
Documents but this is not specific to
portal_components/document.erp5.Foo and now importable as
erp5.component.document.Foo. What we want is being able to import
Products.ERP5.Document.Foo and get
erp5.component.document.Foo module. However, this requires to access
site.portal_components. At the same time we also need to consider that
Products.ERP5.Document.Foo could have been imported by any modules of any Products.
The only solution I could come up with is:
- At Zope startup, import and install only Zope and ERP5Type Products (IOW, the minimum to get access to ERP5Site). This is done by a tiny patch (not a monkey patch) on
ERP5Site.__of__()(AFAIK, the earliest place where
site.portal_componentsis accessible): 1. Create a mapping between the old filesystem imports stored in
source_referenceand the new ZODB Component imports, used in import hooks. 2. Import and install all remaining Products (
ERP5Catalog...). Products with modules importing
Products.ERP5.Document.Foowill go through ZODB Components import hooks and return
erp5.component.document.Foo, just like any ZODB Component module.
Some important points:
- This means that ERP5Type must contain everything necessary to start ERP5 and use ZODB Components. Therefore, I've had to move several modules to ERP5Type, including
BaseCategory.py. But these would have had to be moved eventually to
ERP5Typeanyway as we progressively get rid of all filesystem Products.
- This won't and cannot work with
runUnitTestso you still need to run a script to fix imports, currently
fix_importscommand line script but this could be part of upgrader (even though I don't really like the idea of modifying source code automatically on a live instance).
- This requires a patch on Zope.
Any ideas or suggestions for improvement welcome!