2007-09-06 22:24:44 +02:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
"""Sugar bundle file handler"""
|
|
|
|
|
|
|
|
import os
|
2008-04-19 11:48:57 +02:00
|
|
|
import logging
|
2008-08-14 08:49:45 +02:00
|
|
|
import shutil
|
2007-09-06 22:24:44 +02:00
|
|
|
import StringIO
|
|
|
|
import zipfile
|
|
|
|
|
2008-04-19 11:48:57 +02:00
|
|
|
class AlreadyInstalledException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NotInstalledException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class InvalidPathException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ZipExtractException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class RegistrationException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class MalformedBundleException(Exception):
|
|
|
|
pass
|
2007-09-06 22:24:44 +02:00
|
|
|
|
|
|
|
class Bundle:
|
|
|
|
"""A Sugar activity, content module, etc.
|
|
|
|
|
|
|
|
The bundle itself may be either a zip file or a directory
|
|
|
|
hierarchy, with metadata about the bundle stored various files
|
|
|
|
inside it.
|
|
|
|
|
|
|
|
This is an abstract base class. See ActivityBundle and
|
|
|
|
ContentBundle for more details on those bundle types.
|
|
|
|
"""
|
2008-04-19 11:48:57 +02:00
|
|
|
|
|
|
|
_zipped_extension = None
|
|
|
|
_unzipped_extension = None
|
|
|
|
|
2007-09-06 22:24:44 +02:00
|
|
|
def __init__(self, path):
|
|
|
|
self._path = path
|
2008-04-19 11:48:57 +02:00
|
|
|
self._zip_root_dir = None
|
2007-09-06 22:24:44 +02:00
|
|
|
|
|
|
|
if os.path.isdir(self._path):
|
2008-08-14 08:49:45 +02:00
|
|
|
self._zip_file = None
|
2007-09-06 22:24:44 +02:00
|
|
|
else:
|
2008-08-14 08:49:45 +02:00
|
|
|
self._zip_file = zipfile.ZipFile(self._path)
|
2007-09-06 22:24:44 +02:00
|
|
|
self._check_zip_bundle()
|
|
|
|
|
|
|
|
# manifest = self._get_file(self._infodir + '/contents')
|
|
|
|
# if manifest is None:
|
|
|
|
# raise MalformedBundleException('No manifest file')
|
|
|
|
#
|
|
|
|
# signature = self._get_file(self._infodir + '/contents.sig')
|
|
|
|
# if signature is None:
|
|
|
|
# raise MalformedBundleException('No signature file')
|
|
|
|
|
2008-08-14 08:49:45 +02:00
|
|
|
def __del__(self):
|
|
|
|
if self._zip_file is not None:
|
|
|
|
self._zip_file.close()
|
|
|
|
|
2007-09-06 22:24:44 +02:00
|
|
|
def _check_zip_bundle(self):
|
2008-08-14 08:49:45 +02:00
|
|
|
file_names = self._zip_file.namelist()
|
2007-09-06 22:24:44 +02:00
|
|
|
if len(file_names) == 0:
|
|
|
|
raise MalformedBundleException('Empty zip file')
|
2007-10-01 17:48:46 +02:00
|
|
|
|
|
|
|
if file_names[0] == 'mimetype':
|
|
|
|
del file_names[0]
|
|
|
|
|
2007-09-06 22:24:44 +02:00
|
|
|
self._zip_root_dir = file_names[0].split('/')[0]
|
2008-09-26 06:47:35 +02:00
|
|
|
if self._zip_root_dir.startswith('.'):
|
|
|
|
raise MalformedBundleException(
|
|
|
|
'root directory starts with .')
|
2007-09-06 22:24:44 +02:00
|
|
|
if self._unzipped_extension is not None:
|
2008-04-24 16:23:53 +02:00
|
|
|
(name_, ext) = os.path.splitext(self._zip_root_dir)
|
2007-09-06 22:24:44 +02:00
|
|
|
if ext != self._unzipped_extension:
|
|
|
|
raise MalformedBundleException(
|
|
|
|
'All files in the bundle must be inside a single ' +
|
|
|
|
'directory whose name ends with "%s"' %
|
|
|
|
self._unzipped_extension)
|
|
|
|
|
|
|
|
for file_name in file_names:
|
|
|
|
if not file_name.startswith(self._zip_root_dir):
|
|
|
|
raise MalformedBundleException(
|
|
|
|
'All files in the bundle must be inside a single ' +
|
|
|
|
'top-level directory')
|
|
|
|
|
2008-06-13 12:37:45 +02:00
|
|
|
def get_file(self, filename):
|
2008-04-19 11:48:57 +02:00
|
|
|
f = None
|
2007-09-06 22:24:44 +02:00
|
|
|
|
2008-08-14 08:49:45 +02:00
|
|
|
if self._zip_file is None:
|
2007-09-06 22:24:44 +02:00
|
|
|
path = os.path.join(self._path, filename)
|
2008-06-13 12:37:45 +02:00
|
|
|
try:
|
|
|
|
f = open(path,"rb")
|
|
|
|
except IOError:
|
|
|
|
return None
|
2007-09-06 22:24:44 +02:00
|
|
|
else:
|
|
|
|
path = os.path.join(self._zip_root_dir, filename)
|
|
|
|
try:
|
2008-08-14 08:49:45 +02:00
|
|
|
data = self._zip_file.read(path)
|
2008-04-19 11:48:57 +02:00
|
|
|
f = StringIO.StringIO(data)
|
2007-09-06 22:24:44 +02:00
|
|
|
except KeyError:
|
2008-04-24 16:23:53 +02:00
|
|
|
logging.debug('%s not found.' % filename)
|
2007-09-06 22:24:44 +02:00
|
|
|
|
2008-04-19 11:48:57 +02:00
|
|
|
return f
|
2008-06-13 12:37:45 +02:00
|
|
|
|
|
|
|
def is_file(self, filename):
|
2008-08-14 08:49:45 +02:00
|
|
|
if self._zip_file is None:
|
2008-06-13 12:37:45 +02:00
|
|
|
path = os.path.join(self._path, filename)
|
|
|
|
return os.path.isfile(path)
|
|
|
|
else:
|
|
|
|
path = os.path.join(self._zip_root_dir, filename)
|
|
|
|
try:
|
2008-08-14 08:49:45 +02:00
|
|
|
self._zip_file.getinfo(path)
|
2008-06-13 12:37:45 +02:00
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2008-07-10 21:58:12 +02:00
|
|
|
|
|
|
|
def is_dir(self, filename):
|
2008-08-14 08:49:45 +02:00
|
|
|
if self._zip_file is None:
|
2008-07-10 21:58:12 +02:00
|
|
|
path = os.path.join(self._path, filename)
|
|
|
|
return os.path.isdir(path)
|
|
|
|
else:
|
|
|
|
path = os.path.join(self._zip_root_dir, filename, "")
|
2008-08-14 08:49:45 +02:00
|
|
|
for f in self._zip_file.namelist():
|
2008-07-10 21:58:12 +02:00
|
|
|
if f.startswith(path):
|
|
|
|
return True
|
|
|
|
return False
|
2007-09-06 22:24:44 +02:00
|
|
|
|
|
|
|
def get_path(self):
|
|
|
|
"""Get the bundle path."""
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
def _unzip(self, install_dir):
|
2008-08-14 08:49:45 +02:00
|
|
|
if self._zip_file is None:
|
2007-09-06 22:24:44 +02:00
|
|
|
raise AlreadyInstalledException
|
|
|
|
|
|
|
|
if not os.path.isdir(install_dir):
|
2007-11-28 19:42:51 +01:00
|
|
|
os.mkdir(install_dir, 0775)
|
2007-09-06 22:24:44 +02:00
|
|
|
|
|
|
|
# zipfile provides API that in theory would let us do this
|
|
|
|
# correctly by hand, but handling all the oddities of
|
|
|
|
# Windows/UNIX mappings, extension attributes, deprecated
|
|
|
|
# features, etc makes it impractical.
|
|
|
|
# FIXME: use manifest
|
2007-11-02 18:37:57 +01:00
|
|
|
if os.spawnlp(os.P_WAIT, 'unzip', 'unzip', '-o', self._path,
|
2007-10-01 17:48:46 +02:00
|
|
|
'-x', 'mimetype', '-d', install_dir):
|
2008-08-14 08:49:45 +02:00
|
|
|
# clean up install dir after failure
|
2008-09-26 06:47:35 +02:00
|
|
|
shutil.rmtree(os.path.join(install_dir, self._zip_root_dir),
|
|
|
|
ignore_errors=True)
|
2008-08-14 08:49:45 +02:00
|
|
|
# indicate failure.
|
2007-09-06 22:24:44 +02:00
|
|
|
raise ZipExtractException
|
|
|
|
|
|
|
|
def _zip(self, bundle_path):
|
2008-08-14 08:49:45 +02:00
|
|
|
if self._zip_file is not None:
|
2007-09-06 22:24:44 +02:00
|
|
|
raise NotInstalledException
|
|
|
|
|
2008-04-19 11:48:57 +02:00
|
|
|
raise NotImplementedError
|
2007-09-06 22:24:44 +02:00
|
|
|
|
2007-10-04 20:58:43 +02:00
|
|
|
def _uninstall(self, install_path):
|
|
|
|
if not os.path.isdir(install_path):
|
|
|
|
raise InvalidPathException
|
|
|
|
if self._unzipped_extension is not None:
|
2008-04-24 16:23:53 +02:00
|
|
|
(name_, ext) = os.path.splitext(install_path)
|
2007-10-04 20:58:43 +02:00
|
|
|
if ext != self._unzipped_extension:
|
2007-09-06 22:24:44 +02:00
|
|
|
raise InvalidPathException
|
2007-10-04 20:58:43 +02:00
|
|
|
|
|
|
|
for root, dirs, files in os.walk(install_path, topdown=False):
|
|
|
|
for name in files:
|
|
|
|
os.remove(os.path.join(root, name))
|
|
|
|
for name in dirs:
|
|
|
|
os.rmdir(os.path.join(root, name))
|
|
|
|
os.rmdir(install_path)
|