root/CPS3/legacy/CPSIO/trunk/BaseImporter.py

Revision 29207, 3.8 kB (checked in by madarche, 4 years ago)

- Made file checking stronger and error messages associated with it clearer.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # (C) Copyright 2004 Nuxeo SARL <http://nuxeo.com>
2 # Author: Emmanuel Pietriga <ep@nuxeo.com>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 # 02111-1307, USA.
17 #
18 # $Id$
19
20 import os, random
21
22 import Acquisition
23 from Acquisition import aq_base
24 from Globals import InitializeClass
25 from AccessControl.Permission import Permission
26
27 from Products.CMFCore.utils import UniqueObject
28 from Products.CMFCore.permissions import ManagePortal
29 from AccessControl import ClassSecurityInfo
30
31 from elementtree.ElementTree import ElementTree, Element, SubElement
32
33 from zipfile import ZipFile
34 from zipfile import is_zipfile
35 import re      # for Perl-style regular expression operations
36 import shutil
37
38 from types import ListType
39
40 from zLOG import LOG, DEBUG, INFO, WARNING, ERROR
41
42 MAIN_NAMESPACE_URI = 'http://www.nuxeo.com/2004/06/'
43 from Products.CPSIO.IOBase import IOBase
44
45 class BaseImporter(UniqueObject, Acquisition.Explicit, IOBase):
46
47     options_template = ''
48     options_table = []
49
50     security = ClassSecurityInfo()
51     security.declareObjectPublic()
52
53     security.declareProtected(ManagePortal, 'setOptions')
54     def setOptions(self, file_name, options=[]):
55         """Set import options
56
57         The zip archive has to be given in the 'import' directory.
58         """
59         file_name = file_name.strip()
60         file_path = os.path.join(INSTANCE_HOME, 'import', file_name)
61
62         if not os.path.isfile(file_path):
63             err = ("File %s does not exist. "
64                    "Check that your file is in the Zope import directory."
65                    % file_path)
66             LOG('BaseImporter.setOptions', WARNING, err)
67             raise ValueError(err)
68
69         if not is_zipfile(file_path):
70             err = "File %s is not a ZIP archive." % file_path
71             LOG('BaseImporter.setOptions', WARNING, err)
72             raise ValueError(err)
73
74         self.unzipArchive(file_path)
75
76         self.dir_name = re.sub('.zip', '', file_name)
77         self.dir_path = os.path.join(CLIENT_HOME, self.dir_name)
78         self.options = options
79
80         self.file_path = os.path.join(CLIENT_HOME, self.dir_name, 'index')
81
82
83     security.declareProtected(ManagePortal, 'finalize')
84     def finalize(self):
85         shutil.rmtree(self.dir_path)
86
87
88     security.declareProtected(ManagePortal, 'unzipArchive')
89     def unzipArchive(self, file_path):
90         """The archive is unpacked in the var directory."""
91         self.log("Unziping archive file_path = %s" % file_path)
92         LOG('BaseImporter.unzipArchive', DEBUG, "file_path = %s" % file_path)
93         # Uncompress zipfile into a flat structure
94         archive_file = ZipFile(file_path)
95         for entry_name in archive_file.namelist():
96             LOG('BaseImporter.unzipArchive', DEBUG,
97                 "entry_name = %s" % entry_name)
98             entry_file_path = os.path.join(CLIENT_HOME, entry_name)
99             LOG('BaseImporter.unzipArchive', DEBUG,
100                 "entry_file_path = %s" % entry_file_path)
101             try:
102                 os.makedirs(os.path.dirname(entry_file_path))
103             except OSError:
104                 pass
105             fstr = archive_file.read(entry_name)
106             f = open(entry_file_path, 'w')
107             f.write(fstr)
108             f.close()
109             del fstr
110         archive_file.close()
111
112 InitializeClass(BaseImporter)
113
Note: See TracBrowser for help on using the browser.