[Sugar-devel] [PATCH 2/3] Avoiding bloat for partial ownloads

Rafael Ortiz rafael at activitycentral.com
Fri May 20 16:35:22 EDT 2011


This patch avoids bloating due to partial downloads on browse activity, 
(this is a minor changed patch  from Sebastian Silva's  one)
---
 downloadmanager.py |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/downloadmanager.py b/downloadmanager.py
index 90d22a5..27ed311 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -38,7 +38,6 @@ from sugar.graphics.alert import Alert, TimeoutAlert
 from sugar.graphics.icon import Icon
 from sugar.graphics import style
 from sugar.activity import activity
-
 # #3903 - this constant can be removed and assumed to be 1 when dbus-python
 # 0.82.3 is the only version used
 import dbus
@@ -68,6 +67,7 @@ def can_quit():
 def remove_all_downloads():
     for download in _active_downloads:
         download.cancelable.cancel(NS_ERROR_FAILURE)
+        download._delete_partial_download()
         if download.dl_jobject is not None:
             download.datastore_deleted_handler.remove()
             datastore.delete(download.dl_jobject.object_id)
@@ -166,8 +166,9 @@ class Download:
             _active_downloads.append(self)
 
         elif state_flags & interfaces.nsIWebProgressListener.STATE_STOP:
-            if NS_FAILED(status): # download cancelled
-                return
+            if NS_FAILED(status):
+                    self._delete_partial_download()  # download cancelled
+                    return
 
             self._stop_alert = Alert()
             self._stop_alert.props.title = _('Download completed')
@@ -244,10 +245,18 @@ class Download:
         preview_data = ''.join(preview_data)
         return dbus.ByteArray(preview_data)
 
+    def _delete_partial_download(self):
+        file_path = self._target_file.path.encode('utf-8', 'replace')
+        if os.path.exists(file_path):
+            os.unlink(file_path)
+        if os.path.exists(file_path + ".part"):
+            os.unlink(file_path + ".part")
+
     def __start_response_cb(self, alert, response_id):
         global _active_downloads
         if response_id is gtk.RESPONSE_CANCEL:
             logging.debug('Download Canceled')
+            self._delete_partial_download()
             self.cancelable.cancel(NS_ERROR_FAILURE)
             try:
                 self.datastore_deleted_handler.remove()
@@ -272,6 +281,7 @@ class Download:
         global _active_downloads
         _active_downloads.remove(self)
 
+        file_path = self._target_file.path.encode('utf-8', 'replace')
         if os.path.isfile(self.dl_jobject.file_path):
             os.remove(self.dl_jobject.file_path)
         self.dl_jobject.destroy()
@@ -343,6 +353,7 @@ class Download:
         global _active_downloads
         if self in _active_downloads:
             # TODO: Use NS_BINDING_ABORTED instead of NS_ERROR_FAILURE.
+            self._delete_partial_download()
             self.cancelable.cancel(NS_ERROR_FAILURE) #NS_BINDING_ABORTED)
             _active_downloads.remove(self)
 
-- 
1.7.4.1



More information about the Sugar-devel mailing list