ExtFolder.py 4.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
#                    Yoshinori Okuji <yo@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from AccessControl import ClassSecurityInfo

31
from Products.ERP5Type import Permissions, PropertySheet
32 33 34
from Products.ERP5Type.XMLObject import XMLObject
from Products.ExtFile.ExtFile import ExtFile
from Products.ExtFile.ExtImage import ExtImage
35 36
import os
from App.config import getConfiguration
37
from Products.ERP5 import product_path
38
from Shared.DC.ZRDB.TM import TM
39
import shutil
40 41 42

from zLOG import LOG

43 44 45 46 47 48 49 50 51 52
class Deletion(TM):
  """Remove the directory at the end of a transaction.
  """
  def __init__(self, path):
    self.path = path
    self._register()
    
  def _finish(self):
    try:
      LOG('Deletion', 0, 'removing %s' % self.path)
53
      shutil.rmtree(self.path)
54 55 56 57 58 59
    except OSError:
      pass

  def _abort(self):
    pass

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
class ExtFolder( XMLObject ):
    """
       ExtFolder stores sub-objects as ExtFile or ExtImage.
    """

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      )

    # CMF Type Definition
    meta_type='ERP5 External Folder'
    portal_type='External Folder'    
    add_permission = Permissions.AddPortalContent

    # Declarative security
    security = ClassSecurityInfo()
    security.declareObjectProtected(Permissions.AccessContentsInformation)

    security.declarePrivate('PUT_factory')
    def PUT_factory( self, name, typ, body ):
      """Return an ExtFile or ExtImage object.
      """
      major, minor = typ.split('/', 1)
      if major == 'image':
        return ExtImage(name)
      return ExtFile(name)

90 91 92 93
    def _getRepositoryPath(self):
      """Return the path in the filesystem.
      """
      instance_home = getConfiguration().instancehome
94 95
      repository = os.path.join(*ExtFile._repository)
      return os.path.sep.join((instance_home, repository, self.getPath()))
96

Alexandre Boeglin's avatar
Alexandre Boeglin committed
97
    security.declareProtected('Manage portal', 'generateRpmHeaderList')
98 99 100 101 102 103 104
    def generateRpmHeaderList(self):
      """Run genhdlist on the directory behind this object.
      """
      status = os.system("/usr/bin/genhdlist -s %s" % self._getRepositoryPath())
      if status != 0:
        raise RuntimeError, "failed in executing genhdlist"

Alexandre Boeglin's avatar
Alexandre Boeglin committed
105
    security.declareProtected('Manage portal', 'generateBt5HeaderList')
106 107 108 109 110 111
    def generateBt5HeaderList(self):
      """Run genbt5list on the directory behind this object.
      """
      status = os.system("%s/bin/genbt5list %s" % (product_path, self._getRepositoryPath()))
      if status != 0:
        raise RuntimeError, "failed in executing genbt5list"
112 113 114 115 116

    def manage_beforeDelete(self, item, container):
      """Called before deleting this object.
      """
      self._v_deletion = Deletion(self._getRepositoryPath())
Alexandre Boeglin's avatar
Alexandre Boeglin committed
117
      XMLObject.manage_beforeDelete(self, item, container)