Back to index

plone3  3.1.7
test_export.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 $Id: test_export.py 5626 2006-01-24 02:26:47Z dreamcatcher $
00020 """
00021 
00022 import os, sys
00023 import zipfile
00024 import glob
00025 import re
00026 
00027 if __name__ == '__main__':
00028     execfile(os.path.join(sys.path[0], 'framework.py'))
00029 
00030 # Load fixture
00031 from Testing import ZopeTestCase
00032 from Products.Marshall.tests.base import BaseTest
00033 
00034 # Install our product
00035 ZopeTestCase.installProduct('Marshall')
00036 ZopeTestCase.installProduct('Archetypes')
00037 ZopeTestCase.installProduct('ATContentTypes')
00038 
00039 from Products.CMFCore.utils import getToolByName
00040 from Products.Marshall.registry import Registry, getRegisteredComponents
00041 from Products.Marshall.registry import getComponent
00042 from Products.Marshall.tests import PACKAGE_HOME
00043 tool_id = Registry.id
00044 
00045 def normalize_xml(s):
00046     s = re.sub(r"[ \t]+", " ", s)
00047     return s
00048 
00049 
00050 def import_file(relparts, fname, target, handler):
00051     marshaller = getComponent(handler)
00052     f = open(fname, 'rb+')
00053     content = f.read()
00054     f.close()
00055     curr = parent = target
00056     for p in relparts[:-1]:
00057         curr = parent.restrictedTraverse(p, None)
00058         if curr is None:
00059             parent.invokeFactory('Folder', p)
00060             curr = parent.restrictedTraverse(p)
00061         parent = curr
00062     obj_id = relparts[-1]
00063     obj = parent.restrictedTraverse(obj_id, None)
00064     if obj is None:
00065         parent.invokeFactory('Document', obj_id)
00066         obj = parent.restrictedTraverse(obj_id)
00067     marshaller.demarshall(obj, content)
00068     return
00069 
00070 IGNORE_NAMES = ('CVS', '.svn')
00071 def fromFS(base, target, metadata='atxml', data='primary_field'):
00072     paths = []
00073     ignore = lambda x: filter(None, [x.endswith(n) for n in IGNORE_NAMES])
00074     def import_metadata(relparts, fname, target, handler=metadata):
00075         return import_file(relparts, fname, target, handler)
00076     def import_data(relparts, fname, target, handler=data):
00077         return import_file(relparts, fname, target, handler)
00078     def import_func(arg, dirname, names):
00079         # Remove ignored filenames
00080         [names.remove(n) for n in names if ignore(n)]
00081         names = map(os.path.normcase, names)
00082         for name in names:
00083             fullname = os.path.join(dirname, name)
00084             if not os.path.isfile(fullname):
00085                 continue
00086             fparts = fullname.split(os.sep)
00087             bparts = base.split(os.sep)
00088             relparts = fparts[len(bparts):]
00089             relpath = '/'.join(relparts)
00090             arg.append(relpath)
00091             if '.metadata' in dirname:
00092                 relparts.remove('.metadata')
00093                 import_metadata(relparts, fullname, target)
00094             else:
00095                 import_data(relparts, fullname, target)
00096     os.path.walk(base, import_func, paths)
00097     return paths
00098 
00099 class ExportTest(BaseTest):
00100 
00101     def afterSetUp(self):
00102         super(ExportTest, self).afterSetUp()
00103         self.loginPortalOwner()
00104         self.qi = self.portal.portal_quickinstaller
00105         self.qi.installProduct('Marshall')
00106         self.tool = getToolByName(self.portal, tool_id)
00107 
00108     def test_export(self):
00109         self.portal.invokeFactory('Folder', 'test_data')
00110         self.folder = self.portal.test_data
00111         paths = fromFS(self.base, self.folder)
00112         paths.sort()
00113         obj_paths = filter(lambda x: '.metadata' not in x, paths)
00114         data = self.tool.export(self.folder, obj_paths)
00115         zipf = zipfile.ZipFile(data)
00116         self.assertEquals(zipf.testzip(), None)
00117         zipl = zipf.namelist()
00118         zipl.sort()
00119         self.assertEquals(zipl, paths)
00120 
00121 def test_suite():
00122     import unittest
00123     suite = unittest.TestSuite()
00124     dirs = glob.glob(os.path.join(PACKAGE_HOME, 'export', '*'))
00125     comps = [i['name'] for i in getRegisteredComponents()]
00126     for d in dirs:
00127         prefix = os.path.basename(d)
00128         if prefix not in comps:
00129             continue
00130         k_dict = {'base':d,
00131                   'prefix':prefix}
00132         klass = type('%sExportTest' % prefix,
00133                      (ExportTest,),
00134                      k_dict)
00135         suite.addTest(unittest.makeSuite(klass))
00136     return suite
00137 
00138 if __name__ == '__main__':
00139     framework(descriptions=1, verbosity=1)