Commit 2d8b3838 authored by Arnaud Fontaine's avatar Arnaud Fontaine

Complete comments in source code of ZODB Components.

parent fe42a522
......@@ -3437,8 +3437,8 @@ class ModuleTemplateItem(BaseTemplateItem):
# Do not remove any module for safety.
pass
# XXX-arnau: when everything has been migrated to Components, everything in
# this class should be moved to DocumentTemplateItem
# XXX-arnau: when everything has been migrated to Components, this class
# should be renamed to DocumentTemplateItem
class FilesystemDocumentTemplateItem(BaseTemplateItem):
local_file_reader_name = staticmethod(readLocalDocument)
local_file_writer_name = staticmethod(writeLocalDocument)
......@@ -3667,6 +3667,10 @@ class FilesystemToZodbTemplateItem(FilesystemDocumentTemplateItem,
preinstall = _filesystemCompatibilityWrapper('preinstall', '_objects')
def _importFile(self, file_name, *args, **kw):
"""
Import file by calling the appropriate base class according to the file
name extensions
"""
if file_name.endswith('.xml'):
return ObjectTemplateItem._importFile(self, file_name, *args, **kw)
else:
......@@ -3899,10 +3903,18 @@ class DocumentTemplateItem(FilesystemToZodbTemplateItem):
The migration is performed in two steps:
1/ Copy the Business Template to be migrated;
1/ Copy the Business Template to be migrated;
2/ Run the migration script which will update properly the Document IDs in
the Business Template.
Upon import or export, two files will be created:
2/ Run the migration script which will update properly the Document IDs in
the Business Template.
- XML file: contains metadata
- Python file: contains the source code itself
This allows to keep Git history and having readable source code instead of
being crippled into an XML file
"""
_tool_id = 'portal_components'
......@@ -3918,6 +3930,12 @@ class DocumentTemplateItem(FilesystemToZodbTemplateItem):
class_id + ".py")
def _importFile(self, file_name, file_obj):
"""
Upon import, only consider XML file for ZODB Components (as the Python
source file will be read and set to text_content property on the new
object when the XML will be processed) and for backward compatibility,
handle non-migrated Document as well
"""
if file_name.endswith('.py'):
# If portal_components/XXX.py, then ignore it as it will be handled when
# the .xml file will be processed
......@@ -3942,6 +3960,10 @@ class DocumentTemplateItem(FilesystemToZodbTemplateItem):
LOG('Business Template', 0, 'Skipping file "%s"' % file_name)
def export(self, context, bta, **kw):
"""
Export a Document as two files for ZODB Components, one for metadata
(.xml) and the other for the Python source code (.py)
"""
path = self.__class__.__name__ + '/'
for key, obj in self._objects.iteritems():
# Back compatibility with filesystem Documents
......@@ -3964,6 +3986,10 @@ class DocumentTemplateItem(FilesystemToZodbTemplateItem):
bta.addObject(f, key, path=path)
def getTemplateIdList(self):
"""
Getter for Document property on the Business Template, must be overriden
in children classes (e.g. ExtensionDocumentTemplateItem for example)
"""
return self.getTemplateDocumentIdList()
def build(self, context, **kw):
......@@ -4025,6 +4051,11 @@ class ExtensionTemplateItem(DocumentTemplateItem):
return self.getTemplateExtensionIdList()
class TestTemplateItem(DocumentTemplateItem):
"""
Live Tests are now stored in ZODB rather than on the filesystem. However,
some Business Templates may still have filesystem Live Tests which need to
be migrated to the ZODB.
"""
local_file_reader_name = staticmethod(readLocalTest)
local_file_writer_name = staticmethod(writeLocalTest)
# Test needs no import
......@@ -5912,6 +5943,11 @@ Business Template is a set of definitions, such as skins, portal types and categ
component_portal_type_dict,
erase_existing=False,
**kw):
"""
Migrate the given components from filesystem to ZODB by calling the
appropriate importFromFilesystem according to the destination Portal
Type and then update the Business Template property with migrated IDs
"""
if not component_portal_type_dict:
return {}
......
......@@ -447,16 +447,19 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin):
'getVersionPriorityList')
def getVersionPriorityList(self):
"""
Return the Component version priorities defined on the site
Return the Component version priorities defined on the site in descending
order. Whatever happens, a version must always be returned otherwise it
may render the site unusable when all Products will have been migrated
"""
# Whatever happens, a version must always be returned otherwise it may
# render the site unusable when all Products will have been migrated
return self._version_priority_list or ('erp5 | 0.0',)
security.declareProtected(Permissions.ModifyPortalContent,
'setVersionPriorityList' )
def setVersionPriorityList(self, version_priority_tuple):
"""
Set Version Priority List and make sure that erp5 version is always
defined whatever the given value is
XXX-arnau: must be written through an interaction workflow when ERP5Site
will become a real ERP5 object...
"""
......@@ -479,6 +482,7 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin):
except AttributeError:
pass
# Make sure that reset is not performed when creating a new site
if not getattr(self, '_v_bootstrapping', False):
self.portal_components.resetOnceAtTransactionBoundary()
......
......@@ -35,13 +35,16 @@ import zope.interface
from Products.ERP5Type.interfaces.component import IComponent
class DocumentComponent(ComponentMixin):
# CMF Type Definition
"""
ZODB Component for Documents in bt5 only for now (which used to be installed
in INSTANCE_HOME/Document) but this will also be used later on for Documents
in Products
"""
meta_type = 'ERP5 Document Component'
portal_type = 'Document Component'
zope.interface.implements(IComponent)
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
......
......@@ -35,13 +35,15 @@ import zope.interface
from Products.ERP5Type.interfaces.component import IComponent
class ExtensionComponent(ComponentMixin):
# CMF Type Definition
"""
ZODB Component for Extensions previously defined in the bt5 and installed in
INSTANCE_HOME/Extensions
"""
meta_type = 'ERP5 Extension Component'
portal_type = 'Extension Component'
zope.interface.implements(IComponent)
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
......
......@@ -35,13 +35,16 @@ import zope.interface
from Products.ERP5Type.interfaces.component import IComponent
class TestComponent(ComponentMixin):
# CMF Type Definition
"""
ZODB Component for Live Tests only (previously defined in the bt5 and
installed in INSTANCE_HOME/tests) as other kind of Tests should be
deprecated at some point
"""
meta_type = 'ERP5 Test Component'
portal_type = 'Test Component'
zope.interface.implements(IComponent)
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
......
......@@ -146,4 +146,7 @@ AddERP5Content = AddPortalContent # Since we put come CPS content in ERP5 docume
# Source Code Management - this is the highest possible permission
ManageExtensions = "Manage extensions"
# Permission for resetting ZODB Components, this is required to allow a
# Manager to reset Dynamic Classes but not to modify the source code of
# Components
ResetDynamicClasses = "Reset dynamic classes"
......@@ -64,33 +64,45 @@ class ComponentTool(BaseTool):
security.declareProtected(Permissions.ResetDynamicClasses, 'reset')
def reset(self, force=False, reset_portal_type=False):
"""
XXX-arnau: global reset
Reset all ZODB Component packages. A cache cookie is used to check whether
the reset is necessary when force is not specified. This allows to make
sure that all ZEO clients get reset (checked in __of__ on ERP5Site) when
one given ZEO client gets reset when Component(s) are modified or
invalidated.
Also, as resetting ZODB Components Package usually implies to reset Portal
Type as Classes (because the former are used as bases), perform the reset
by default.
XXX-arnau: for now, this is a global reset but it might be improved in the
future if required...
"""
portal = self.getPortalObject()
# XXX-arnau: copy/paste from portal_type_class, but is this really
# necessary as even for Portal Type classes, synchronizeDynamicModules
# seems to always called with force=True?
global last_sync
if force:
# hard invalidation to force sync between nodes
# Hard invalidation to force sync between nodes
portal.newCacheCookie('component_packages')
last_sync = portal.getCacheCookie('component_packages')
else:
cookie = portal.getCacheCookie('component_packages')
if cookie == last_sync:
return False
last_sync = cookie
LOG("ERP5Type.Tool.ComponentTool", INFO, "Resetting Components")
type_tool = portal.portal_types
# One Component Package per allowed Portal Types on Component Tool
allowed_content_type_list = type_tool.getTypeInfo(
self.getPortalType()).getTypeAllowedContentTypeList()
import erp5.component
# Make sure that it is not possible to load Components or load Portal Type
# class when Components are reset through aq_method_lock
with Base.aq_method_lock:
for content_type in allowed_content_type_list:
package_name = content_type.split(' ')[0].lower()
......@@ -112,10 +124,10 @@ class ComponentTool(BaseTool):
'resetOnceAtTransactionBoundary')
def resetOnceAtTransactionBoundary(self):
"""
Schedule a single reset at the end of the transaction, only once. The
idea behind this is that a reset is (very) costly and that we want to do
it as little often as possible. Moreover, doing it twice in a transaction
is useless (but still twice as costly).
Schedule a single reset at the end of the transaction. The idea behind
this is that a reset is (very) costly and that we want to do it as little
often as possible. Moreover, doing it twice in a transaction is useless
(but still twice as costly).
"""
tv = getTransactionalVariable()
key = 'ComponentTool.resetOnceAtTransactionBoundary'
......
......@@ -201,7 +201,8 @@ class TypesTool(TypeProvider):
security.declareProtected(Permissions.AccessContentsInformation, 'getDocumentTypeList')
def getDocumentTypeList(self):
"""
Return a list of Document types that can be used as Base classes
Return a list of Document types (including filesystem and ZODB Component
Documents) that can be used as Base classes
"""
from Products.ERP5Type import document_class_registry
document_type_set = set(document_class_registry)
......
......@@ -86,29 +86,25 @@ class ComponentDynamicPackage(ModuleType):
"""
Create the component registry, this is very similar to
Products.ERP5Type.document_class_registry and avoids checking whether a
Component exists at each call at the expense to increase startup
time. Moreover, it allows to handle reference easily.
XXX-arnau: handle different versions of a Component, perhaps something
like erp5.component.extension.VERSION.REFERENCE perhaps but there should
be a a way to specify priorities such as portal_skins maybe?
Component exists at each call at the expense of being slower when being
re-generated after a reset. Moreover, it allows to handle reference
easily.
"""
if not self.__registry_dict:
portal = getSite()
try:
component_tool = portal.portal_components
# XXX-arnau: When installing ERP5 site, erp5_core_components has not
# been installed yet, thus this will obviously failed...
# When installing ERP5 site, erp5_core_components has not been installed
# yet, thus this will obviously failed...
except AttributeError:
return {}
version_priority_set = set(portal.getVersionPriorityNameList())
# objectValues should not be used for a large number of objects, but
# this is only done at startup or upon reset, moreover using the Catalog
# is too risky as it lags behind and depends upon objects being
# reindexed
# this is only done upon reset, moreover using the Catalog is too risky
# as it lags behind and depends upon objects being reindexed
with Base.aq_method_lock:
for component in component_tool.objectValues(portal_type=self._portal_type):
# Only consider modified or validated states as state transition will
......@@ -138,6 +134,16 @@ class ComponentDynamicPackage(ModuleType):
module.__file__[1:-1]).getTextContent(validated_only=True)
def find_module(self, fullname, path=None):
"""
PEP-302 Finder which determines which packages and modules will be handled
by this class. It must be done carefully to avoid handling packages and
modules the Loader (load_module()) will not be handled later as the latter
would raise ImportError...
As per PEP-302, returns None if this Finder cannot handle the given name,
perhaps because the Finder of another Component Package could do it or
because this is a filesystem module...
"""
# Ignore imports with a path which are filesystem-only and any
# absolute imports which does not start with this package prefix,
# None there means that "normal" sys.path will be used
......@@ -162,7 +168,7 @@ class ComponentDynamicPackage(ModuleType):
except KeyError:
return None
# Skip components not available, otherwise Products for example could be
# Skip unavailable components, otherwise Products for example could be
# wrongly considered as importable and thus the actual filesystem class
# ignored
elif (name not in self._registry_dict and
......@@ -172,6 +178,13 @@ class ComponentDynamicPackage(ModuleType):
return self
def _getVersionPackage(self, version):
"""
Get the version package (NAMESPACE.VERSION_version) for the given version
and create it if it does not already exist
"""
# Version are appended with '_version' to distinguish them from top-level
# Component modules (Component checkConsistency() forbids Component name
# ending with _version)
version += '_version'
version_package = getattr(self, version, None)
if version_package is None:
......@@ -185,12 +198,24 @@ class ComponentDynamicPackage(ModuleType):
def __load_module(self, fullname):
"""
Load a module with given fullname (see PEP 302) if it's not
already in sys.modules. It is assumed that imports are filtered
properly in find_module().
Load a module with given fullname (see PEP 302) if it's not already in
sys.modules. It is assumed that imports are filtered properly in
find_module().
Also, when the top-level Component module is requested
(erp5.component.XXX.COMPONENT_NAME), the Component with the highest
version priority will be loaded into the Version package
(erp5.component.XXX.VERSION_version.COMPONENT_NAME. Therefore, the
top-level Component module will just be an alias of the versioned one.
As per PEP-302, raise an ImportError if the Loader could not load the
module for any reason...
"""
site = getSite()
name = fullname[len(self._namespace_prefix):]
# if only Version package (erp5.component.XXX.VERSION_version) is
# requested to be loaded, then create it if necessary
if name.endswith('_version'):
version = name[:-self.__version_suffix_len]
return (version in site.getVersionPriorityNameList() and
......@@ -198,6 +223,8 @@ class ComponentDynamicPackage(ModuleType):
module_fullname_alias = None
version_package_name = name[:-self.__version_suffix_len]
# If a specific version of the Component has been requested
if '.' in name:
try:
version, name = name.split('.')
......@@ -212,6 +239,7 @@ class ComponentDynamicPackage(ModuleType):
raise ImportError("%s: version %s of Component %s could not be found" % \
(fullname, version, name))
# Otherwise, find the Component with the highest version priority
else:
try:
component_version_dict = self._registry_dict[name]
......@@ -219,6 +247,7 @@ class ComponentDynamicPackage(ModuleType):
raise ImportError("%s: Component %s could not be found" % (fullname,
name))
# Version priority name list is ordered in descending order
for version in site.getVersionPriorityNameList():
component = component_version_dict.get(version)
if component is not None:
......@@ -227,6 +256,8 @@ class ComponentDynamicPackage(ModuleType):
raise ImportError("%s: no version of Component %s in Site priority" % \
(fullname, name))
# Check whether this module has already been loaded before for a
# specific version, if so, just add it to the upper level
try:
module = getattr(getattr(self, version + '_version'), name)
except AttributeError:
......@@ -264,6 +295,8 @@ class ComponentDynamicPackage(ModuleType):
module.__loader__ = self
module.__name__ = module_fullname
# Add the newly created module to the Version package and add it as an
# alias to the top-level package as well
setattr(self._getVersionPackage(version), name, module)
if module_fullname_alias:
setattr(self, name, module)
......@@ -287,7 +320,7 @@ class ComponentDynamicPackage(ModuleType):
if sub_package:
package = sub_package
else:
# Clear the Component registry
# Clear the Component registry only once
self.__registry_dict.clear()
package = self
......@@ -306,7 +339,8 @@ class ComponentDynamicPackage(ModuleType):
# the meantime
del sys.modules[module_name]
# Delete linecache data
# Delete linecache data to get updated source code (__file__ attribute
# (<ComponentID>) is used as linecache key)
import linecache
try:
del linecache.cache[getattr(package, name).__file__]
......@@ -314,5 +348,4 @@ class ComponentDynamicPackage(ModuleType):
except (AttributeError, KeyError):
pass
# And finally remove the module
delattr(package, name)
......@@ -86,9 +86,14 @@ def initializeDynamicModules():
erp5.accessor_holder.portal_type
holds accessors holders of Portal Types
erp5.component:
holds component modules
holds ZODB Component packages
erp5.component.document:
holds Document modules previously found in bt5 in $INSTANCE_HOME/Document
erp5.component.extension:
holds extension classes previously found in bt5 in instancehome/Extensions
holds Extension modules previously found in bt5 in
$INSTANCE_HOME/Extensions
erp5.component.test:
holds Live Test modules previously found in bt5 in $INSTANCE_HOME/test
"""
erp5 = ModuleType("erp5")
sys.modules["erp5"] = erp5
......@@ -121,7 +126,7 @@ def initializeDynamicModules():
erp5.temp_portal_type = registerDynamicModule('erp5.temp_portal_type',
loadTempPortalTypeClass)
# Components
# ZODB Components
erp5.component = ModuleType("erp5.component")
sys.modules["erp5.component"] = erp5.component
......
......@@ -116,6 +116,22 @@ class RecordablePropertyMetaClass(ExtensionClass):
class ComponentMixin(PropertyRecordableMixin, Base):
"""
Mixin used for all ZODB Components. Most of the code is generic, thus actual
ZODB Components should have almost nothing to defined...
From a security point of view, only Developer Role defined on Component Tool
can manage Components (as exec is used and anything potentially damaging
could be done on the filesystem), while only Manager or Developer Roles can
reset Component Packages (see ERP5Type.Permissions). All the permissions are
defined on Component Tool itself and newly created Components just inherits
permissions defined on the former.
The Developer Role is not a typical Role as only users defined in Zope
configuration can be added to this Role (which is displayed in the list of
available Roles in ZMI). This is achieved by two monkey patches
(ERP5Type.patches.{User,PropertiedUser}) and modifications in
ERP5Security.ERP5UserFactory.
XXX-arnau: add tests to ERP5 itself to make sure all securities are defined
properly everywhere (see naming convention test)
"""
......@@ -157,7 +173,15 @@ class ComponentMixin(PropertyRecordableMixin, Base):
security.declareProtected(Permissions.ModifyPortalContent, 'checkConsistency')
def checkConsistency(self, *args, **kw):
"""
XXX-arnau: should probably be in a separate Constraint class?
Check the consistency of the Component upon validate or when being
modified after being validated.
Some keywords are forbidden for reference and version. As Version package
always ends with '_version', reference is checked more carefully to avoid
clashing with existing method names (such as the ones required for PEP
302).
XXX-arnau: separate Constraint class?
"""
error_list = super(ComponentMixin, self).checkConsistency(*args, **kw)
object_relative_url = self.getRelativeUrl()
......@@ -201,6 +225,7 @@ class ComponentMixin(PropertyRecordableMixin, Base):
else:
message = None
try:
# Check for any error in the source code by trying to load it
self.load({}, text_content=text_content)
except SyntaxError, e:
mapping = dict(error_message=str(e),
......@@ -226,10 +251,11 @@ class ComponentMixin(PropertyRecordableMixin, Base):
'checkConsistencyAndValidate')
def checkConsistencyAndValidate(self):
"""
When a Component is in validated or modified validation state and
it is modified, modified state is set then this checks whether the
Component can be validated again if checkConsistency returns no
error
When a Component is in validated or modified validation state and it is
modified, modified state is set then this checks whether the Component can
be validated again if checkConsistency returns no error. Otherwise, it
stays in modified state and previously validated values are used for
reference, version and text_content
"""
error_list = self.checkConsistency()
if error_list:
......@@ -249,7 +275,7 @@ class ComponentMixin(PropertyRecordableMixin, Base):
def hasErrorMessageList(self):
"""
Check whether there are error messages, useful to display errors in the UI
without calling getErrorMessageList() which translates error messages
without calling getErrorMessageList() as it translates error messages
"""
workflow = self.workflow_history['component_validation_workflow'][-1]
return bool(workflow['error_message'])
......@@ -274,10 +300,8 @@ class ComponentMixin(PropertyRecordableMixin, Base):
__dict__ is given rather than creating an empty dict and returning it.
Initially, namespace_dict default parameter value was an empty dict to
allow checking the source code before validate, but this introduces a bug
when namespace_dict was not given because the first call would exec
directly into function namespace_dict default parameter, thus the second
call would have namespace_dict default value to the previous call.
allow checking the source code before validate, but this is completely
wrong as the object reference is kept accross each call
"""
if text_content is None:
text_content = self.getTextContent(validated_only=validated_only)
......@@ -312,7 +336,7 @@ class ComponentMixin(PropertyRecordableMixin, Base):
Get source for FTP/Webdav. The default implementation of GET for Webdav,
available in webdav.Resource, calls manage_FTPget
XXX-arnau: encoding?
XXX-arnau: encoding issue?
"""
return self.getTextContent()
......@@ -322,8 +346,9 @@ class ComponentMixin(PropertyRecordableMixin, Base):
def importFromFilesystem(cls, context, reference, version,
erase_existing=False):
"""
Import a Component from the filesystem into ZODB after checking that the
source code is valid
Import a Component from the filesystem into ZODB and validate it so it can
be loaded straightaway provided validate() does not raise any error of
course
"""
object_id = '%s.%s.%s' % (cls._getDynamicModuleNamespace(), version,
reference)
......@@ -358,7 +383,7 @@ class ComponentMixin(PropertyRecordableMixin, Base):
# straightaway as there should be no error
new_component.validate()
# XXX-arnau: is it really safe?
# Remove now useless Component on filesystem
os.remove(path)
return new_component
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
from Products.ExternalMethod.ExternalMethod import ExternalMethod
from App.Extensions import FuncCode, getObject
def getFunction(self, reload=False, f=None):
"""
Patch to get ZODB Component Extension function if available, otherwise
fallback on filesystem Extension
"""
if f is None:
import erp5.component.extension
try:
......@@ -21,6 +38,11 @@ ExternalMethod.getFunction = getFunction
ExternalMethod__call__ = ExternalMethod.__call__
def __call__(self, *args, **kw):
"""
Patch to call ZODB Component Extension, by trying first to import ZODB
Component Extension if available, otherwise fallback on filesystem
Extension
"""
try:
f = getattr(__import__('erp5.component.extension.' + self._module,
fromlist=['erp5.component.extension'],
......
......@@ -94,7 +94,7 @@ def getRolesInContext(self, object):
continue
break
# Patched: Developer role should not never be available as local role
# Patched: Developer role should never be available as local role
local.pop('Developer', None)
roles=list(roles) + local.keys()
return roles
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment