Commit 121e389d authored by Yusei Tahara's avatar Yusei Tahara

Fixed a test error. Now can read data from both of tar file and extracted directory.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15301 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 319b5b53
...@@ -54,7 +54,6 @@ bt5_path = os.path.join(INSTANCE_HOME, 'bt5') ...@@ -54,7 +54,6 @@ bt5_path = os.path.join(INSTANCE_HOME, 'bt5')
target_business_templates = ( target_business_templates = (
'erp5_base', 'erp5_base',
'erp5_trade', 'erp5_trade',
'erp5_mysql_innodb_catalog',
'erp5_pdf_editor', 'erp5_pdf_editor',
'erp5_pdf_style', 'erp5_pdf_style',
...@@ -146,10 +145,17 @@ def makeTestMethod(module_id, portal_type, view_name): ...@@ -146,10 +145,17 @@ def makeTestMethod(module_id, portal_type, view_name):
def addTestMethodDynamically(): def addTestMethodDynamically():
from Products.ERP5.tests.utils import BusinessTemplateInfo from Products.ERP5.tests.utils import BusinessTemplateInfoTar
from Products.ERP5.tests.utils import BusinessTemplateInfoDir
for i in target_business_templates: for i in target_business_templates:
business_template = os.path.join(bt5_path, '%s.bt5' % i) business_template = os.path.join(bt5_path, i)
business_template_info = BusinessTemplateInfo(business_template) if os.path.isdir(business_template):
business_template_info = BusinessTemplateInfoDir(business_template)
elif os.path.isfile(business_template+'.bt5'):
business_template_info = BusinessTemplateInfoTar(business_template+'.bt5')
else:
raise KeyError, "Can't find the business template:%s." % i
for module_id, module_portal_type in business_template_info.modules.items(): for module_id, module_portal_type in business_template_info.modules.items():
for portal_type in business_template_info.allowed_content_types.get( for portal_type in business_template_info.allowed_content_types.get(
module_portal_type, ()): module_portal_type, ()):
...@@ -165,7 +171,6 @@ def addTestMethodDynamically(): ...@@ -165,7 +171,6 @@ def addTestMethodDynamically():
addTestMethodDynamically() addTestMethodDynamically()
if __name__ == '__main__': if __name__ == '__main__':
framework() framework()
else: else:
......
...@@ -25,16 +25,17 @@ ...@@ -25,16 +25,17 @@
# #
############################################################################## ##############################################################################
import os.path
import tarfile import tarfile
import xml.parsers.expat import xml.parsers.expat
import xml.dom.minidom import xml.dom.minidom
from urllib import url2pathname from urllib import url2pathname
class BusinessTemplateInfo: class BusinessTemplateInfoBase:
def __init__(self, tar_path): def __init__(self, target):
self.tar = tarfile.open(tar_path, 'r:gz') self.target = target
self.setUp() self.setUp()
def setUp(self): def setUp(self):
...@@ -48,28 +49,41 @@ class BusinessTemplateInfo: ...@@ -48,28 +49,41 @@ class BusinessTemplateInfo:
self.setUpAllowedContentTypes() self.setUpAllowedContentTypes()
self.setUpActions() self.setUpActions()
def findFileInfosByName(self, startswith='', endswith=''):
raise NotImplementedError
def getFileInfo(self, name):
raise NotImplementedError
def getFileInfoName(self, fileinfo):
raise NotImplementedError
def readFileInfo(self, fileinfo):
raise NotImplementedError
def getPrefix(self):
raise NotImplementedError
def setUpTitle(self): def setUpTitle(self):
for tarinfo in self.tar.getmembers(): for i in self.findFileInfosByName(endswith='/bt/title'):
if tarinfo.name.endswith('/bt/title'): self.title = self.readFileInfo(i)
self.title = self.tar.extractfile(tarinfo).read()
def setUpModules(self): def setUpModules(self):
name = '%s/ModuleTemplateItem/' % self.title name = '%s/ModuleTemplateItem/' % self.getPrefix()
for tarinfo in self.tar.getmembers(): for i in self.findFileInfosByName(startswith=name):
if tarinfo.name.startswith(name) and tarinfo.type==tarfile.REGTYPE: source = self.readFileInfo(i)
source = self.tar.extractfile(tarinfo).read() doc = xml.dom.minidom.parseString(source)
doc = xml.dom.minidom.parseString(source) module_id = doc.getElementsByTagName('id')[0].childNodes[0].data
module_id = doc.getElementsByTagName('id')[0].childNodes[0].data portal_type = doc.getElementsByTagName('portal_type')[0].childNodes[0].data
portal_type = doc.getElementsByTagName('portal_type')[0].childNodes[0].data self.modules[module_id] = portal_type
self.modules[module_id] = portal_type
def setUpAllowedContentTypes(self): def setUpAllowedContentTypes(self):
name = '%s/PortalTypeAllowedContentTypeTemplateItem/allowed_content_types.xml' % self.title name = '%s/PortalTypeAllowedContentTypeTemplateItem/allowed_content_types.xml' % self.getPrefix()
try: try:
tarinfo = self.tar.getmember(name) fileinfo = self.getFileInfo(name)
except KeyError: except NotFoundError:
return return
source = self.tar.extractfile(tarinfo).read() source = self.readFileInfo(fileinfo)
doc = xml.dom.minidom.parseString(source) doc = xml.dom.minidom.parseString(source)
for portal_type_node in doc.getElementsByTagName('portal_type'): for portal_type_node in doc.getElementsByTagName('portal_type'):
portal_type = portal_type_node.getAttribute('id') portal_type = portal_type_node.getAttribute('id')
...@@ -130,11 +144,74 @@ class BusinessTemplateInfo: ...@@ -130,11 +144,74 @@ class BusinessTemplateInfo:
p.Parse(source) p.Parse(source)
return handler.data return handler.data
name = '%s/ActionTemplateItem/portal_types/' % self.title name = '%s/ActionTemplateItem/portal_types/' % self.getPrefix()
for tarinfo in self.tar.getmembers(): for i in self.findFileInfosByName(startswith=name):
if tarinfo.name.startswith(name) and tarinfo.type==tarfile.REGTYPE: portal_type = url2pathname(self.getFileInfoName(i).split('/')[-2])
portal_type = url2pathname(tarinfo.name.split('/')[-2]) if not portal_type in self.actions:
if not portal_type in self.actions: self.actions[portal_type] = []
self.actions[portal_type] = [] data = parse(self.readFileInfo(i))
data = parse(self.tar.extractfile(tarinfo).read()) self.actions[portal_type].append(data)
self.actions[portal_type].append(data)
class NotFoundError(Exception):
"""FileInfo does not exists."""
class BusinessTemplateInfoTar(BusinessTemplateInfoBase):
def __init__(self, target):
self.target = tarfile.open(target, 'r:gz')
self.setUp()
def getPrefix(self):
return self.title
def findFileInfosByName(self, startswith='', endswith=''):
for tarinfo in self.target.getmembers():
if (tarinfo.name.startswith(startswith) and
tarinfo.name.endswith(endswith) and
tarinfo.type==tarfile.REGTYPE):
yield tarinfo
def getFileInfo(self, name):
try:
return self.target.getmember(name)
except KeyError:
raise NotFoundError
def getFileInfoName(self, fileinfo):
return fileinfo.name
def readFileInfo(self, fileinfo):
return self.target.extractfile(fileinfo).read()
class BusinessTemplateInfoDir(BusinessTemplateInfoBase):
def getPrefix(self):
return self.target
def findFileInfosByName(self, startswith='', endswith=''):
allfiles = []
def visit(arg, dirname, names):
if '.svn' in dirname:
return
for i in names:
path = os.path.join(self.target, dirname, i)
if os.path.isfile(path):
allfiles.append(path)
os.path.walk(self.target, visit, None)
for i in allfiles:
if i.startswith(startswith) and i.endswith(endswith):
yield i
def getFileInfo(self, name):
if not os.path.isfile(name):
raise NotFoundError
return name
def getFileInfoName(self, fileinfo):
return fileinfo
def readFileInfo(self, fileinfo):
return file(fileinfo).read()
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