[Sugar-devel] [PATCH sugar 2/2] Check for required activity deps before installing

Aleksey Lim alsroot at member.fsf.org
Wed Jan 12 09:32:32 EST 2011


The patch moves all bundle install calls to one function and wraps all known
exceptions to send error notifications.
---
 src/jarabe/journal/journalactivity.py |   11 ++----
 src/jarabe/journal/misc.py            |   53 ++++++++++++++++++++++----------
 src/jarabe/model/bundleregistry.py    |    3 ++
 3 files changed, 43 insertions(+), 24 deletions(-)

diff --git a/src/jarabe/journal/journalactivity.py b/src/jarabe/journal/journalactivity.py
index 52a677e..7b04735 100644
--- a/src/jarabe/journal/journalactivity.py
+++ b/src/jarabe/journal/journalactivity.py
@@ -305,14 +305,11 @@ class JournalActivity(JournalWindow):
                         bundle.get_path())
             return
 
-        try:
-            registry.install(bundle)
-        except (ZipExtractException, RegistrationException):
-            logging.exception('Could not install bundle %s', bundle.get_path())
-            return
+        def install_cb():
+            metadata['bundle_id'] = bundle.get_bundle_id()
+            model.write(metadata)
 
-        metadata['bundle_id'] = bundle.get_bundle_id()
-        model.write(metadata)
+        misc.install_activity(bundle, install_cb)
 
     def search_grab_focus(self):
         search_toolbar = self._main_toolbox.search_toolbar
diff --git a/src/jarabe/journal/misc.py b/src/jarabe/journal/misc.py
index 710cb15..28644d6 100644
--- a/src/jarabe/journal/misc.py
+++ b/src/jarabe/journal/misc.py
@@ -30,8 +30,9 @@ from sugar.graphics.icon import get_icon_file_name
 from sugar.graphics.xocolor import XoColor
 from sugar.graphics.alert import ConfirmationAlert
 from sugar import mime
-from sugar.bundle.activitybundle import ActivityBundle
+from sugar.bundle.activitybundle import ActivityBundle, DependencyException
 from sugar.bundle.bundle import AlreadyInstalledException
+from sugar.bundle.bundle import ZipExtractException, RegistrationException
 from sugar.bundle.contentbundle import ContentBundle
 from sugar import util
 
@@ -40,6 +41,7 @@ from jarabe.model import bundleregistry, shell
 from jarabe.journal.journalentrybundle import JournalEntryBundle
 from jarabe.journal import model
 from jarabe.journal import journalwindow
+from jarabe import frame
 
 def _get_icon_for_mime(mime_type):
     generic_types = mime.get_all_generic_types()
@@ -161,18 +163,7 @@ def resume(metadata, bundle_id=None):
 
         file_path = model.get_file(metadata['uid'])
         bundle = ActivityBundle(file_path)
-        if not registry.is_installed(bundle):
-            logging.debug('Installing activity bundle')
-            try:
-                registry.install(bundle)
-            except AlreadyInstalledException:
-                _downgrade_option_alert(bundle)
-                return
-        else:
-            logging.debug('Upgrading activity bundle')
-            registry.upgrade(bundle)
-
-        _launch_bundle(bundle)
+        install_activity(bundle, lambda: _launch_bundle(bundle))
 
     elif is_content_bundle(metadata) and bundle_id is None:
 
@@ -229,6 +220,33 @@ def resume(metadata, bundle_id=None):
         else:
             activityfactory.create_with_object_id(bundle, object_id)
 
+
+def install_activity(bundle, finish_cb=None):
+    try:
+        registry = bundleregistry.get_registry()
+
+        if not registry.is_installed(bundle):
+            logging.debug('Installing activity bundle')
+            registry.install(bundle)
+        else:
+            logging.debug('Upgrading activity bundle')
+            registry.upgrade(bundle)
+
+        if finish_cb is not None:
+            finish_cb()
+
+    except AlreadyInstalledException:
+        _downgrade_option_alert(bundle, finish_cb)
+    except DependencyException, error:
+        frame.get_view().add_message(
+                summary=_('Cannot install %s activity') % bundle.get_name(),
+                body=str(error))
+    except (ZipExtractException, RegistrationException):
+        frame.get_view().add_message(
+                summary=_('Cannot install activity %s') % bundle.get_name(),
+                body = _('Malformed activity bundle'))
+
+
 def _launch_bundle(bundle):
     registry = bundleregistry.get_registry()
     logging.debug('activityfactory.creating bundle with id %r',
@@ -240,21 +258,22 @@ def _launch_bundle(bundle):
         logging.error('Bundle %r is not installed.',
                     bundle.get_bundle_id())
 
-def _downgrade_option_alert(bundle):
+def _downgrade_option_alert(bundle, finish_cb):
     alert = ConfirmationAlert()
     alert.props.title = _('Older Version Of %s Activity') % (bundle.get_name())
     alert.props.msg = _('Do you want to downgrade to version %s') % \
                        bundle.get_activity_version()
-    alert.connect('response', _downgrade_alert_response_cb, bundle)
+    alert.connect('response', _downgrade_alert_response_cb, bundle, finish_cb)
     journalwindow.get_journal_window().add_alert(alert)
     alert.show()
 
-def _downgrade_alert_response_cb(alert, response_id, bundle):
+def _downgrade_alert_response_cb(alert, response_id, bundle, finish_cb):
     if response_id is gtk.RESPONSE_OK:
         journalwindow.get_journal_window().remove_alert(alert)
         registry = bundleregistry.get_registry()
         registry.install(bundle, force_downgrade=True)
-        _launch_bundle(bundle)
+        if finish_cb is not None:
+            finish_cb()
     elif response_id is gtk.RESPONSE_CANCEL:
         journalwindow.get_journal_window().remove_alert(alert)
 
diff --git a/src/jarabe/model/bundleregistry.py b/src/jarabe/model/bundleregistry.py
index f2c2a52..cd82fc9 100644
--- a/src/jarabe/model/bundleregistry.py
+++ b/src/jarabe/model/bundleregistry.py
@@ -377,6 +377,9 @@ class BundleRegistry(gobject.GObject):
         return False
 
     def install(self, bundle, uid=None, force_downgrade=False):
+        if hasattr(bundle, 'meets_restriction'):
+            bundle.meets_restriction('sugar', config.version)
+
         activities_path = env.get_user_activities_path()
 
         for installed_bundle in self._bundles:
-- 
1.7.3.4



More information about the Sugar-devel mailing list