Commit 12a26ac7 authored by Julien Muchembled's avatar Julien Muchembled

BT: let BusinessTemplateFolder walk inside directory tree recursively

parent 46eb9884
...@@ -300,19 +300,13 @@ def unregisterSkinFolderId(skin_tool, skin_folder_id, skin_selection_list): ...@@ -300,19 +300,13 @@ def unregisterSkinFolderId(skin_tool, skin_folder_id, skin_selection_list):
deleteSkinSelection(skin_tool, skin_selection) deleteSkinSelection(skin_tool, skin_selection)
skin_tool.getPortalObject().changeSkin(None) skin_tool.getPortalObject().changeSkin(None)
class BusinessTemplateArchive: class BusinessTemplateArchive(object):
""" """
This is the base class for all Business Template archives This is the base class for all Business Template archives
""" """
def _initCreation(self, path, **kw): def __init__(self, path, **kw):
self.path = path self.path = path
def __init__(self, creation=0, importing=0, file=None, path=None, **kw):
if creation:
self._initCreation(path=path, **kw)
elif importing:
self._initImport(file=file, path=path, **kw)
def addObject(self, obj, name, path=None, ext='.xml'): def addObject(self, obj, name, path=None, ext='.xml'):
if path: if path:
name = posixpath.join(path, name) name = posixpath.join(path, name)
...@@ -349,39 +343,29 @@ class BusinessTemplateFolder(BusinessTemplateArchive): ...@@ -349,39 +343,29 @@ class BusinessTemplateFolder(BusinessTemplateArchive):
finally: finally:
f.close() f.close()
def _initImport(self, file, path, **kw):
root_path_len = len(os.path.normpath(os.path.join(path, '_'))) - 1
self.root_path_len = root_path_len
d = {}
for f in file:
f = os.path.normpath(f)
klass = f[root_path_len:].split(os.sep, 1)[0]
d.setdefault(klass, []).append(f)
self.file_list_dict = d
def importFiles(self, item): def importFiles(self, item):
""" """
Import file from a local folder Import file from a local folder
""" """
join = os.path.join
path = os.path.normpath(self.path)
class_name = item.__class__.__name__ class_name = item.__class__.__name__
root_path_len = self.root_path_len root = join(path, class_name, '')
prefix_len = root_path_len + len(class_name) + len(os.sep) root_path_len = len(root)
if CACHE_DATABASE_PATH: if CACHE_DATABASE_PATH:
try: try:
cache_database.db = gdbm.open(CACHE_DATABASE_PATH, 'cf') cache_database.db = gdbm.open(CACHE_DATABASE_PATH, 'cf')
except gdbm.error: except gdbm.error:
cache_database.db = gdbm.open(CACHE_DATABASE_PATH, 'nf') cache_database.db = gdbm.open(CACHE_DATABASE_PATH, 'nf')
try: try:
for file_path in self.file_list_dict.get(class_name, ()): for root, dirs, files in os.walk(root):
if os.path.isfile(file_path): for file_name in files:
file = open(file_path, 'rb') file_name = join(root, file_name)
try: with open(file_name, 'rb') as f:
file_name = file_path[prefix_len:] file_name = file_name[root_path_len:]
if '%' in file_name: if '%' in file_name:
file_name = unquote(file_name) file_name = unquote(file_name)
item._importFile(file_name, file) item._importFile(file_name, f)
finally:
file.close()
finally: finally:
if hasattr(cache_database, 'db'): if hasattr(cache_database, 'db'):
cache_database.db.close() cache_database.db.close()
...@@ -392,12 +376,21 @@ class BusinessTemplateTarball(BusinessTemplateArchive): ...@@ -392,12 +376,21 @@ class BusinessTemplateTarball(BusinessTemplateArchive):
Class archiving businnes template into a tarball file Class archiving businnes template into a tarball file
""" """
def _initCreation(self, **kw): def __init__(self, path, creation=0, importing=0, **kw):
BusinessTemplateArchive._initCreation(self, **kw) super(BusinessTemplateTarball, self).__init__(path, **kw)
# init tarfile obj if creation:
self.fobj = StringIO() self.fobj = StringIO()
self.tar = tarfile.open('', 'w:gz', self.fobj) self.tar = tarfile.open('', 'w:gz', self.fobj)
self.time = time.time() self.time = time.time()
elif importing:
self.tar = tarfile.open(path, 'r:gz')
self.item_dict = item_dict = defaultdict(list)
for info in self.tar.getmembers():
if info.isreg():
path = info.name.split('/')
if path[0] == '.':
del path[0]
item_dict[path[1]].append(('/'.join(path[2:]), info))
def _writeFile(self, obj, path): def _writeFile(self, obj, path):
if self.path: if self.path:
...@@ -413,26 +406,14 @@ class BusinessTemplateTarball(BusinessTemplateArchive): ...@@ -413,26 +406,14 @@ class BusinessTemplateTarball(BusinessTemplateArchive):
self.tar.close() self.tar.close()
return self.fobj return self.fobj
def _initImport(self, file, **kw):
self.tar = tarfile.open(file, 'r:gz')
self.item_dict = {}
setdefault = self.item_dict.setdefault
for info in self.tar.getmembers():
if info.isreg():
path = info.name.split('/')
if path[0] == '.':
del path[0]
file_name = '/'.join(path[2:])
if '%' in file_name:
file_name = unquote(file_name)
setdefault(path[1], []).append((file_name, info))
def importFiles(self, item): def importFiles(self, item):
""" """
Import all file from the archive to the site Import all file from the archive to the site
""" """
extractfile = self.tar.extractfile extractfile = self.tar.extractfile
for file_name, info in self.item_dict.get(item.__class__.__name__, ()): for file_name, info in self.item_dict.get(item.__class__.__name__, ()):
if '%' in file_name:
file_name = unquote(file_name)
item._importFile(file_name, extractfile(info)) item._importFile(file_name, extractfile(info))
class TemplateConditionError(Exception): pass class TemplateConditionError(Exception): pass
...@@ -5581,12 +5562,12 @@ Business Template is a set of definitions, such as skins, portal types and categ ...@@ -5581,12 +5562,12 @@ Business Template is a set of definitions, such as skins, portal types and categ
if bta is None: if bta is None:
if local: if local:
# we export into a folder tree # we export into a folder tree
bta = BusinessTemplateFolder(creation=1, path=path) bta = BusinessTemplateFolder(path, creation=1)
else: else:
# We export BT into a tarball file # We export BT into a tarball file
if path is None: if path is None:
path = self.getTitle() path = self.getTitle()
bta = BusinessTemplateTarball(creation=1, path=path) bta = BusinessTemplateTarball(path, creation=1)
# export bt # export bt
for prop in self.propertyMap(): for prop in self.propertyMap():
...@@ -5612,15 +5593,12 @@ Business Template is a set of definitions, such as skins, portal types and categ ...@@ -5612,15 +5593,12 @@ Business Template is a set of definitions, such as skins, portal types and categ
return bta.finishCreation() return bta.finishCreation()
security.declareProtected(Permissions.ManagePortal, 'importFile') security.declareProtected(Permissions.ManagePortal, 'importFile')
def importFile(self, dir = 0, file=None, root_path=None): def importFile(self, path):
""" """
Import all xml files in Business Template Import all xml files in Business Template
""" """
if dir: bta = (BusinessTemplateFolder if os.path.isdir(path) else
bta = BusinessTemplateFolder(importing=1, file=file, path=root_path) BusinessTemplateTarball)(path, importing=1)
else:
bta = BusinessTemplateTarball(importing=1, file=file)
bt_item = bt() bt_item = bt()
bta.importFiles(bt_item) bta.importFiles(bt_item)
prop_dict = {} prop_dict = {}
......
...@@ -314,28 +314,8 @@ class TemplateTool (BaseTool): ...@@ -314,28 +314,8 @@ class TemplateTool (BaseTool):
def _download_local(self, path, bt_id): def _download_local(self, path, bt_id):
"""Download Business Template from local directory or file """Download Business Template from local directory or file
""" """
bt = self.newContent(portal_type='Business Template', id=bt_id) bt = self.newContent(bt_id, 'Business Template')
if os.path.isdir(os.path.normpath(path)): bt.importFile(path)
path = os.path.normpath(path)
def callback(file_list, directory, files):
for excluded_directory in ('CVS', '.svn'):
try:
files.remove(excluded_directory)
except ValueError:
pass
for file in files:
absolute_path = os.path.join(directory, file)
if os.path.isfile(absolute_path):
file_list.append(absolute_path)
file_list = []
os.path.walk(path, callback, file_list)
file_list.sort()
# import bt object
bt.importFile(dir=True, file=file_list, root_path=path)
else:
# this should be a file
bt.importFile(file=path)
return bt return bt
def _download_url(self, url, bt_id): def _download_url(self, url, bt_id):
......
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