Commit 5e49155a authored by Arnaud Fontaine's avatar Arnaud Fontaine

Allow to erase an existing Component before importing from filesystem.

parent 4c03bfef
...@@ -47,7 +47,7 @@ class ExtensionComponent(DocumentComponent): ...@@ -47,7 +47,7 @@ class ExtensionComponent(DocumentComponent):
security.declareProtected(Permissions.ModifyPortalContent, security.declareProtected(Permissions.ModifyPortalContent,
'importAllFromFilesystem') 'importAllFromFilesystem')
@classmethod @classmethod
def importAllFromFilesystem(cls, context): def importAllFromFilesystem(cls, context, erase_existing=False):
""" """
Try to import all Extensions as found in INSTANCEHOME/Extensions and Try to import all Extensions as found in INSTANCEHOME/Extensions and
returns error as a dict if any returns error as a dict if any
...@@ -64,7 +64,7 @@ class ExtensionComponent(DocumentComponent): ...@@ -64,7 +64,7 @@ class ExtensionComponent(DocumentComponent):
failed_import_dict = {} failed_import_dict = {}
for extension_path in glob.iglob(extension_path_pattern): for extension_path in glob.iglob(extension_path_pattern):
try: try:
cls.importFromFilesystem(context, extension_path) cls.importFromFilesystem(context, extension_path, erase_existing)
except Exception, e: except Exception, e:
failed_import_dict[extension_path] = str(e) failed_import_dict[extension_path] = str(e)
else: else:
...@@ -76,11 +76,21 @@ class ExtensionComponent(DocumentComponent): ...@@ -76,11 +76,21 @@ class ExtensionComponent(DocumentComponent):
security.declareProtected(Permissions.ModifyPortalContent, security.declareProtected(Permissions.ModifyPortalContent,
'importFromFilesystem') 'importFromFilesystem')
@classmethod @classmethod
def importFromFilesystem(cls, context, path): def importFromFilesystem(cls, context, path, erase_existing=False):
""" """
Import an Extension from the given path into ZODB after checking that the Import an Extension from the given path into ZODB after checking that the
source code is valid source code is valid
""" """
class_name = os.path.basename(path).replace('.py', '')
id = 'erp5.component.extension.%s' % class_name
# XXX-arnau: not efficient at all
if id in context:
if not erase_existing:
return
context.deleteContent(id)
with open(path) as extension_file: with open(path) as extension_file:
source_code = extension_file.read() source_code = extension_file.read()
...@@ -88,8 +98,7 @@ class ExtensionComponent(DocumentComponent): ...@@ -88,8 +98,7 @@ class ExtensionComponent(DocumentComponent):
namespace_dict = {} namespace_dict = {}
exec source_code in namespace_dict exec source_code in namespace_dict
class_name = os.path.basename(path).replace('.py', '') return context.newContent(id=id,
return context.newContent(id='erp5.component.extension.%s' % class_name,
# XXX-arnau: useless field? # XXX-arnau: useless field?
reference=class_name, reference=class_name,
text_content=source_code, text_content=source_code,
......
...@@ -95,8 +95,10 @@ class ComponentTool(BaseTool): ...@@ -95,8 +95,10 @@ class ComponentTool(BaseTool):
for content_portal_type in getattr(type_tool, for content_portal_type in getattr(type_tool,
self.portal_type).getTypeAllowedContentTypeList(): self.portal_type).getTypeAllowedContentTypeList():
try: try:
failed_import_dict.update(getattr(erp5.portal_type, failed_import_dict.update(
content_portal_type).importAllFromFilesystem(self)) getattr(erp5.portal_type,
content_portal_type).importAllFromFilesystem(self,
erase_existing=erase_existing))
except AttributeError: except AttributeError:
LOG("ERP5Type.Tool.ComponentTool", WARNING, "Could not import %ss" % \ LOG("ERP5Type.Tool.ComponentTool", WARNING, "Could not import %ss" % \
content_portal_type) content_portal_type)
......
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