gget r63 - trunk/gget



Author: johans
Date: Sun Aug 10 09:35:46 2008
New Revision: 63
URL: http://svn.gnome.org/viewvc/gget?rev=63&view=rev

Log:
Fixed cancel.

Modified:
   trunk/gget/Download.py
   trunk/gget/DownloadManager.py
   trunk/gget/metalink.py

Modified: trunk/gget/Download.py
==============================================================================
--- trunk/gget/Download.py	(original)
+++ trunk/gget/Download.py	Sun Aug 10 09:35:46 2008
@@ -90,6 +90,7 @@
 
         self.bit_rate = 0.0
 
+        self.old_status = -1
         self.status = -1
 
         self.mime_type = gnomevfs.get_file_mime_type(self.file_name)
@@ -231,6 +232,7 @@
         return True
 
     def set_status(self, status):
+        self.old_status = self.status
         self.status = status
         Utils.debug_print("Download status for %s changed to: %s (%s)" % (self,
             self.get_status_string(), status))

Modified: trunk/gget/DownloadManager.py
==============================================================================
--- trunk/gget/DownloadManager.py	(original)
+++ trunk/gget/DownloadManager.py	Sun Aug 10 09:35:46 2008
@@ -103,9 +103,18 @@
     def __download_added(self, download_list, download):
         """Called when a new download is added to DownloadList. Starts the
         download if its not already completed."""
+        download.connect("status-changed", self.__status_changed)
+
         if not download.status == Download.COMPLETED:
             self.start_download(download)
 
+    def __status_changed(self, download, status):
+        """Called when the status of a download changes. If a canceled download
+        is resumed we need to start the download again."""
+        if status == Download.DOWNLOADING and \
+           download.old_status == Download.CANCELED:
+            self.start_download(download)
+
     def start_download(self, download):
         """Starts a download in a new thread."""
         Utils.debug_print("Starting download %s" % download)
@@ -131,7 +140,7 @@
                 print "Failed downloading of file %s" % download.uri
 
         except Exception, e:
-            pass
+            print "Exception caught in DownloadManager.__start_download: " + e
 
     def set_proxy(self, protocol, proxy):
         """Sets the proxy to use for the specified protocol."""

Modified: trunk/gget/metalink.py
==============================================================================
--- trunk/gget/metalink.py	(original)
+++ trunk/gget/metalink.py	Sun Aug 10 09:35:46 2008
@@ -197,12 +197,14 @@
 import hashlib
 import xml.parsers.expat
 import time
+import zlib
 import optparse
 import os.path
 import os
 import sha
 import random
 import threading
+import binascii
 import md5
 import logging
 import gettext
@@ -620,7 +622,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 #
 # Filename: $URL: https://metalinks.svn.sourceforge.net/svnroot/metalinks/checker/download.py $
-# Last Updated: $Date: 2008-07-31 00:38:56 +0200 (tor, 31 jul 2008) $
+# Last Updated: $Date: 2008-08-07 02:05:01 +0200 (tor, 07 aug 2008) $
 # Author(s): Neil McNab
 #
 # Description:
@@ -1245,7 +1247,8 @@
     jigdo.parsehandle(datasource)
     datasource.close()
 
-    template = get(path_join(os.path.dirname(src), jigdo.template), path, {"md5": jigdo.template_md5}, force, handlers, segmented)
+    #print path_join(src, jigdo.template)
+    template = get(path_join(src, jigdo.template), path, {"md5": jigdo.template_md5}, force, handlers, segmented)
     if not template:
         print _("Could not download template file!")
         return False
@@ -1257,37 +1260,15 @@
 
     results = []
     results.extend(template)
-    #for filenode in urllist:
-    #    result = download_file_node(filenode, path, force, handlers, segmented)
-    #    if result:
-    #          results.append(result)
+    for filenode in urllist:
+        result = download_file_node(filenode, path, force, handlers, segmented)
+        if result:
+              results.append(result)
     if len(results) == 0:
         return False
 
-    handle = open(template[0], "rb")
-    data = handle.readline()
-    newhandle = open(jigdo.filename, "wb+")
-    decompress = bz2.BZ2Decompressor()
-    bzip = False
-    raw = False
-    while data:
-        if bzip:
-            newdata = decompress.decompress(data)
-            newhandle.write(newdata)
-            data = handle.read(1024)
-        elif raw:
-            newhandle.write(data)
-            data = handle.read(1024)
-        else:
-            if data.startswith("BZIP"):
-                bzip = True
-            if data.startswith("DATA"):
-                raw = True
-            data = handle.readline()
-    handle.close()
-    newhandle.seek(0, 0)
-    
-    newhandle.close()
+    print _("Reconstituting file...")
+    jigdo.mkiso()
     
     return results
 
@@ -2545,6 +2526,9 @@
             self.error = _("socket error")
             self.response = None
             return
+        except Exception, e:
+            self.response = None
+            return
         if len(data) == 0:
             return
 
@@ -3258,7 +3242,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 #
 # Filename: $URL: https://metalinks.svn.sourceforge.net/svnroot/metalinks/checker/xmlutils.py $
-# Last Updated: $Date: 2008-07-31 00:38:56 +0200 (tor, 31 jul 2008) $
+# Last Updated: $Date: 2008-08-07 02:05:01 +0200 (tor, 07 aug 2008) $
 # Author(s): Hampus Wessman, Neil McNab
 #
 # Description:
@@ -3268,7 +3252,6 @@
 
 
 # for jigdo only
-#import ConfigParser
 
 current_version = "1.1.0"
 
@@ -3836,6 +3819,7 @@
         self.template_md5 = ""
         self.filename = ""
         self.mirrordict = {}
+        self.compression_type = None
         Metalink.__init__(self)
         self.p = ParseINI()
 
@@ -3876,7 +3860,7 @@
             if item[0].lower() == "template":
                 self.template = item[1]
             if item[0].lower() == "template-md5sum":
-                self.template_md5 = self.bin2hex(self.base64hash2bin(item[1]))
+                self.template_md5 = binascii.hexlify(self.base64hash2bin(item[1]))
             if item[0].lower() == "filename":
                 self.filename = item[1]
             if item[0].lower() == "shortinfo":
@@ -3887,7 +3871,7 @@
         for item in configobj.items("Parts"):
             base64hash = item[0]
             binaryhash = self.base64hash2bin(base64hash)
-            hexhash = self.bin2hex(binaryhash)
+            hexhash = binascii.hexlify(binaryhash)
             url = item[1]
             parts = url.split(":", 1)
             urls = []
@@ -3912,13 +3896,90 @@
     def base64hash2bin(self, base64hash):
         # need to pad hash out to multiple of both 6 (base 64) and 8 bits (1 byte characters)
         return base64.b64decode(base64hash + "AA", "-_")[:-2]
-    
-    def bin2hex(self, string):
+
+    def temp2iso(self):
+        '''
+        load template into string in memory
+        '''
+        handle = open(os.path.basename(self.template), "rb")
+        
+        # read text comments first, then switch to binary data
+        data = handle.readline()
+        while data.strip() != "":
+            data = handle.readline()
+
+        data = handle.read(1024)
         text = ""
-        for char in string:
-            text += "%.2x" % ord(char)
+
+        #decompress = bz2.BZ2Decompressor()
+        #zdecompress = zlib.decompressobj()
+        bzip = False
+        gzip = False
+        while data:
+            if data.startswith("BZIP"):
+                bzip = True
+                self.compression_type = "BZIP"
+                data = data[16:]
+            if data.startswith("DATA"):
+                gzip = True
+                self.compression_type = "GZIP"
+                #print self.get_size(data[4:10])
+                #print self.get_size(data[10:16])
+                data = data[16:]
+            if data.startswith("DESC"):
+                gzip = False
+                bzip = False
+
+            if bzip or gzip:
+                #newdata = decompress.decompress(data)
+                text += data
+                data = handle.read(1024)
+            else:
+                data = handle.readline()
+        handle.close()
+        #print text
         return text
 
+    def get_size(self, string):
+        total = 0
+        for i in range(len(string)):
+            temp = ord(string[i]) << (8 * i)
+            total += temp
+        return total
+
+    def mkiso(self):
+        text = self.temp2iso()
+
+        found = {}
+        for fileobj in self.files:
+            hexhash = fileobj.get_checksums()["md5"]
+            loc = text.find(binascii.unhexlify(hexhash))
+            if loc != -1:
+                #print "FOUND:", fileobj.filename
+                found[loc] = fileobj.filename
+
+        decompressor = None
+        if self.compression_type == "BZIP":
+            decompressor = bz2.BZ2Decompressor()
+        elif self.compression_type == "GZIP":
+            decompressor = zlib.decompressobj()
+
+        handle = open(self.filename, "wb")
+
+        keys = found.keys()
+        keys.sort()
+        start = 0
+        for loc in keys:
+            #print start, loc, found[loc]
+            #print "Adding %s to image..." % found[loc]
+            #sys.stdout.write(".")
+            lead = decompressor.decompress(text[start:loc])
+            filedata = open(found[loc], "rb").read()
+            handle.write(lead + filedata)
+            start = loc + 16
+
+        handle.close()
+
 class ParseINI(dict):
     '''
     Similiar to what is available in ConfigParser, but case sensitive
@@ -3940,7 +4001,10 @@
             line = fp.readline()
 
     def items(self, section):
-        return self[section]
+        try:
+            return self[section]
+        except KeyError:
+            return []
 xmlutils = Dummy()
 xmlutils.DecompressFile = DecompressFile
 xmlutils.Jigdo = Jigdo



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]