pylint sugar.bundle

This commit is contained in:
Marco Pesenti Gritti 2008-04-19 11:48:57 +02:00
parent 050e9b54c3
commit 7b485120a3
3 changed files with 62 additions and 35 deletions

View File

@ -217,8 +217,9 @@ class ActivityBundle(Bundle):
return False return False
def install(self): def install(self):
activities_path = env.get_user_activities_path()
act = activity.get_registry().get_activity(self._bundle_id) act = activity.get_registry().get_activity(self._bundle_id)
if act is not None and act.path.startswith(env.get_user_activities_path()): if act is not None and act.path.startswith(activities_path):
raise AlreadyInstalledException raise AlreadyInstalledException
install_dir = env.get_user_activities_path() install_dir = env.get_user_activities_path()
@ -226,7 +227,8 @@ class ActivityBundle(Bundle):
install_path = os.path.join(install_dir, self._zip_root_dir) install_path = os.path.join(install_dir, self._zip_root_dir)
xdg_data_home = os.getenv('XDG_DATA_HOME', os.path.expanduser('~/.local/share')) xdg_data_home = os.getenv('XDG_DATA_HOME',
os.path.expanduser('~/.local/share'))
mime_path = os.path.join(install_path, 'activity', 'mimetypes.xml') mime_path = os.path.join(install_path, 'activity', 'mimetypes.xml')
if os.path.isfile(mime_path): if os.path.isfile(mime_path):
@ -234,7 +236,8 @@ class ActivityBundle(Bundle):
mime_pkg_dir = os.path.join(mime_dir, 'packages') mime_pkg_dir = os.path.join(mime_dir, 'packages')
if not os.path.isdir(mime_pkg_dir): if not os.path.isdir(mime_pkg_dir):
os.makedirs(mime_pkg_dir) os.makedirs(mime_pkg_dir)
installed_mime_path = os.path.join(mime_pkg_dir, '%s.xml' % self._bundle_id) installed_mime_path = os.path.join(mime_pkg_dir,
'%s.xml' % self._bundle_id)
os.symlink(mime_path, installed_mime_path) os.symlink(mime_path, installed_mime_path)
os.spawnlp(os.P_WAIT, 'update-mime-database', os.spawnlp(os.P_WAIT, 'update-mime-database',
'update-mime-database', mime_dir) 'update-mime-database', mime_dir)
@ -272,7 +275,7 @@ class ActivityBundle(Bundle):
act = activity.get_registry().get_activity(self._bundle_id) act = activity.get_registry().get_activity(self._bundle_id)
if not force and act.version != self._activity_version: if not force and act.version != self._activity_version:
logging.warning('Not uninstalling because different bundle present') logging.warning('Not uninstalling, different bundle present')
return return
elif not act.path.startswith(env.get_user_activities_path()): elif not act.path.startswith(env.get_user_activities_path()):
logging.warning('Not uninstalling system activity') logging.warning('Not uninstalling system activity')
@ -281,10 +284,12 @@ class ActivityBundle(Bundle):
install_path = os.path.join(env.get_user_activities_path(), install_path = os.path.join(env.get_user_activities_path(),
self._zip_root_dir) self._zip_root_dir)
xdg_data_home = os.getenv('XDG_DATA_HOME', os.path.expanduser('~/.local/share')) xdg_data_home = os.getenv('XDG_DATA_HOME',
os.path.expanduser('~/.local/share'))
mime_dir = os.path.join(xdg_data_home, 'mime') mime_dir = os.path.join(xdg_data_home, 'mime')
installed_mime_path = os.path.join(mime_dir, 'packages', '%s.xml' % self._bundle_id) installed_mime_path = os.path.join(mime_dir, 'packages',
'%s.xml' % self._bundle_id)
if os.path.exists(installed_mime_path): if os.path.exists(installed_mime_path):
os.remove(installed_mime_path) os.remove(installed_mime_path)
os.spawnlp(os.P_WAIT, 'update-mime-database', os.spawnlp(os.P_WAIT, 'update-mime-database',
@ -294,8 +299,8 @@ class ActivityBundle(Bundle):
if mime_types is not None: if mime_types is not None:
installed_icons_dir = os.path.join(xdg_data_home, installed_icons_dir = os.path.join(xdg_data_home,
'icons/sugar/scalable/mimetypes') 'icons/sugar/scalable/mimetypes')
for file in os.listdir(installed_icons_dir): for f in os.listdir(installed_icons_dir):
path = os.path.join(installed_icons_dir, file) path = os.path.join(installed_icons_dir, f)
if os.path.islink(path) and \ if os.path.islink(path) and \
os.readlink(path).startswith(install_path): os.readlink(path).startswith(install_path):
os.remove(path) os.remove(path)
@ -313,9 +318,11 @@ class ActivityBundle(Bundle):
try: try:
self.uninstall(force=True) self.uninstall(force=True)
except Exception, e: except Exception, e:
logging.warning('Uninstall failed (%s), still trying to install newer bundle', e) logging.warning('Uninstall failed (%s), still trying ' \
'to install newer bundle', e)
else: else:
logging.warning('Unable to uninstall system activity, installing upgraded version in user activities') logging.warning('Unable to uninstall system activity, ' \
'installing upgraded version in user activities')
self.install() self.install()

View File

@ -18,15 +18,27 @@
"""Sugar bundle file handler""" """Sugar bundle file handler"""
import os import os
import logging
import StringIO import StringIO
import zipfile import zipfile
class AlreadyInstalledException(Exception): pass class AlreadyInstalledException(Exception):
class NotInstalledException(Exception): pass pass
class InvalidPathException(Exception): pass
class ZipExtractException(Exception): pass class NotInstalledException(Exception):
class RegistrationException(Exception): pass pass
class MalformedBundleException(Exception): pass
class InvalidPathException(Exception):
pass
class ZipExtractException(Exception):
pass
class RegistrationException(Exception):
pass
class MalformedBundleException(Exception):
pass
class Bundle: class Bundle:
"""A Sugar activity, content module, etc. """A Sugar activity, content module, etc.
@ -38,8 +50,13 @@ class Bundle:
This is an abstract base class. See ActivityBundle and This is an abstract base class. See ActivityBundle and
ContentBundle for more details on those bundle types. ContentBundle for more details on those bundle types.
""" """
_zipped_extension = None
_unzipped_extension = None
def __init__(self, path): def __init__(self, path):
self._path = path self._path = path
self._zip_root_dir = None
if os.path.isdir(self._path): if os.path.isdir(self._path):
self._unpacked = True self._unpacked = True
@ -66,7 +83,7 @@ class Bundle:
self._zip_root_dir = file_names[0].split('/')[0] self._zip_root_dir = file_names[0].split('/')[0]
if self._unzipped_extension is not None: if self._unzipped_extension is not None:
(name, ext) = os.path.splitext(self._zip_root_dir) ext = os.path.splitext(self._zip_root_dir)[0]
if ext != self._unzipped_extension: if ext != self._unzipped_extension:
raise MalformedBundleException( raise MalformedBundleException(
'All files in the bundle must be inside a single ' + 'All files in the bundle must be inside a single ' +
@ -80,24 +97,23 @@ class Bundle:
'top-level directory') 'top-level directory')
def _get_file(self, filename): def _get_file(self, filename):
file = None f = None
if self._unpacked: if self._unpacked:
path = os.path.join(self._path, filename) path = os.path.join(self._path, filename)
if os.path.isfile(path): if os.path.isfile(path):
file = open(path) f = open(path)
else: else:
zip_file = zipfile.ZipFile(self._path) zip_file = zipfile.ZipFile(self._path)
path = os.path.join(self._zip_root_dir, filename) path = os.path.join(self._zip_root_dir, filename)
try: try:
data = zip_file.read(path) data = zip_file.read(path)
file = StringIO.StringIO(data) f = StringIO.StringIO(data)
except KeyError: except KeyError:
# == "file not found" logging.log('%s not found.' % filename)
pass
zip_file.close() zip_file.close()
return file return f
def get_path(self): def get_path(self):
"""Get the bundle path.""" """Get the bundle path."""
@ -123,12 +139,7 @@ class Bundle:
if not self._unpacked: if not self._unpacked:
raise NotInstalledException raise NotInstalledException
# FIXME: use manifest raise NotImplementedError
zip = zipfile.ZipFile(bundle_path, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(self._path):
for name in files:
zip.write(filename, os.path.join(base_dir, filename))
zip.close()
def _uninstall(self, install_path): def _uninstall(self, install_path):
if not os.path.isdir(install_path): if not os.path.isdir(install_path):

View File

@ -39,6 +39,16 @@ class ContentBundle(Bundle):
def __init__(self, path): def __init__(self, path):
Bundle.__init__(self, path) Bundle.__init__(self, path)
self._locale = None
self._l10n = None
self._category = None
self._name = None
self._subcategory = None
self._category_class = None
self._category_icon = None
self._library_version = None
self._bundle_class = None
info_file = self._get_file('library/library.info') info_file = self._get_file('library/library.info')
if info_file is None: if info_file is None:
raise MalformedBundleException('No library.info file') raise MalformedBundleException('No library.info file')
@ -145,9 +155,6 @@ class ContentBundle(Bundle):
def get_category(self): def get_category(self):
return self._category return self._category
def get_category(self):
return self._category
def get_category_icon(self): def get_category_icon(self):
return self._category_icon return self._category_icon
@ -161,9 +168,11 @@ class ContentBundle(Bundle):
return self._bundle_class return self._bundle_class
def _run_indexer(self): def _run_indexer(self):
os.spawnlp(os.P_WAIT, 'python', if os.environ.has_key('XDG_DATA_DIRS'):
'python', for path in os.environ['XDG_DATA_DIRS'].split(':'):
env.get_prefix_path('share/library-common/make_index.py')) indexer = os.path.join(path, 'library-common', 'make_index.py')
if os.path.exists(indexer):
os.spawnlp(os.P_WAIT, 'python', 'python', indexer)
def is_installed(self): def is_installed(self):
if self._unpacked: if self._unpacked: