[Debtorrent-commits] r51 - in /debtorrent/trunk: DebTorrent/download_bt1.py btdownloadheadless.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Fri May 18 03:55:42 UTC 2007


Author: camrdale-guest
Date: Fri May 18 03:55:42 2007
New Revision: 51

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=51
Log:
Add ability to parse dpkg status for priorities of files to download (only for btdownloadheadless)

Modified:
    debtorrent/trunk/DebTorrent/download_bt1.py
    debtorrent/trunk/btdownloadheadless.py

Modified: debtorrent/trunk/DebTorrent/download_bt1.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/download_bt1.py?rev=51&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/download_bt1.py (original)
+++ debtorrent/trunk/DebTorrent/download_bt1.py Fri May 18 03:55:42 2007
@@ -28,7 +28,7 @@
 from bencode import bencode, bdecode
 from natpunch import UPnP_test
 from sha import sha
-from os import path, makedirs, listdir
+from os import path, makedirs, listdir, system
 from parseargs import parseargs, formatDefinitions, defaultargs
 from socket import error as socketerror
 from random import seed
@@ -218,14 +218,14 @@
         failed("Couldn't listen - " + str(e))
         return
 
-    response = get_response(config['responsefile'], config['url'], failed)
+    (response, status_priority) = get_response(config['responsefile'], config['url'], failed)
     if not response:
         return
 
     infohash = sha(bencode(response['info'])).digest()
 
     d = BT1Download(statusfunc, finfunc, errorfunc, exchandler, doneflag,
-                    config, response, infohash, myid, rawserver, listen_port)
+                    config, response, infohash, myid, rawserver, listen_port, status_priority)
 
     if not d.saveAs(filefunc):
         return
@@ -283,15 +283,15 @@
     return (argslistheader + formatDefinitions(defaults, cols, presets))
 
 
-def get_response(file, url, errorfunc):
-    response = get_packages(file, url, errorfunc)
+def get_response(file, url, status_to_download, errorfunc):
+    (response, priority) = get_packages(file, url, status_to_download, errorfunc)
     if response:
         try:
             check_message(response)
         except ValueError, e:
             errorfunc("got bad file info - " + str(e))
-            return None
-        return response
+            return (None, None)
+        return (response, priority)
     try:
         if file:
             h = open(file, 'rb')
@@ -302,7 +302,7 @@
                 int(front[1:])
             except:
                 errorfunc(file+' is not a valid responsefile')
-                return None
+                return (None, None)
             try:
                 h.seek(0)
             except:
@@ -316,12 +316,12 @@
                 h = urlopen(url)
             except:
                 errorfunc(url+' bad url')
-                return None
+                return (None, None)
         response = h.read()
     
     except IOError, e:
         errorfunc('problem getting response info - ' + str(e))
-        return None
+        return (None, None)
     try:    
         h.close()
     except:
@@ -335,11 +335,11 @@
         check_message(response)
     except ValueError, e:
         errorfunc("got bad file info - " + str(e))
-        return None
-
-    return response
-
-def get_packages(file, url, errorfunc):
+        return (None, None)
+
+    return (response, None)
+
+def get_packages(file, url, status_to_download, errorfunc):
     encoding = None
 #    if params.has_key('filesystem_encoding'):
 #        encoding = params['filesystem_encoding']
@@ -347,6 +347,39 @@
         encoding = ENCODING
     if not encoding:
         encoding = 'ascii'
+        
+    get_priority = False
+    installed_versions = {}
+    priority = None
+    if status_to_download > 0:
+        try:
+            status = open("/var/lib/dpkg/status")
+    
+            p = [None, None, None]
+            for line in status:
+                line = line.rstrip()
+                if line == "":
+                    if (p[0] and p[1] and p[2]):
+                        if (p[2] == "install ok installed"):
+                            installed_versions[p[0]] = p[1]
+                        p = [None, None, None]
+                if line[:8] == "Package:":
+                    p[0] = line[9:]
+                if line[:8] == "Version:":
+                    p[1] = line[9:]
+                if line[:7] == "Status:":
+                    p[2] = line[8:]
+        except:
+            installed_versions = {}
+
+        try:
+            status.close()
+        except:
+            pass
+        
+        if installed_versions:
+            get_priority = True
+            priority = {}
 
     try:
         if file:
@@ -359,7 +392,7 @@
                 assert line[:8] == "Package:"
             except:
 #                errorfunc(file+' is not a valid Packages file')
-                return None
+                return (None, None)
             try:
                 h.seek(0)
             except:
@@ -386,31 +419,43 @@
                 h = data.split('\n')
             except:
 #                errorfunc(url+' bad url')
-                return None
+                return (None, None)
 
         pieces = []
         lengths = []
         fs = []
         
-        p = [None, None, None]
+        p = [None, None, None, None, None]
         for line in h:
             line = line.rstrip()
             if line == "":
                 if (p[0] and p[1] and p[2]):
-                    path = []
+                    file_path = []
+                    temp = p[1]
                     while p[1]:
                         p[1],d = split(p[1])
-                        path.insert(0,d)
-                    fs.append({'length': p[0], 'path': uniconvertl(path, encoding)})
+                        file_path.insert(0,d)
+                    fs.append({'length': p[0], 'path': uniconvertl(file_path, encoding)})
                     lengths.append(p[0])
                     pieces.append(p[2])
-                p = [None, None, None]
+                    if (get_priority and p[3] and installed_versions.has_key(p[3])):
+                        if status_to_download == 2:
+                            priority[path.join(uniconvert(name, encoding), temp)] = 1
+                        elif (status_to_download == 1 and p[4]):
+                            ret = system('dpkg --compare-versions "' + installed_versions[p[3]] + '" lt "' + p[4] + '"')
+                            if ret == 0:
+                                priority[path.join(uniconvert(name, encoding), temp)] = 1
+                p = [None, None, None, None, None]
             if line[:9] == "Filename:":
                 p[1] = line[10:]
             if line[:5] == "Size:":
                 p[0] = long(line[6:])
             if line[:5] == "SHA1:":
                 p[2] = binascii.a2b_hex(line[6:])
+            if line[:8] == "Package:":
+                p[3] = line[9:]
+            if line[:8] == "Version:":
+                p[4] = line[9:]
         
         response = {'info': {'pieces': ''.join(pieces),
             'piece lengths': lengths, 'files': fs },
@@ -419,19 +464,19 @@
     
     except IOError, e:
 #        errorfunc('problem getting Packages info - ' + str(e))
-        return None
+        return (None, None)
     try:    
         h.close()
     except:
         pass
 
-    return response
+    return (response, priority)
 
 
 class BT1Download:    
     def __init__(self, statusfunc, finfunc, errorfunc, excfunc, doneflag,
                  config, response, infohash, id, rawserver, port,
-                 appdataobj = None):
+                 status_priority = None, appdataobj = None):
         self.statusfunc = statusfunc
         self.finfunc = finfunc
         self.errorfunc = errorfunc
@@ -449,6 +494,7 @@
                        for x in xrange(0, len(self.info['pieces']), 20)]
         self.piece_lengths = self.info['piece lengths']
         self.len_pieces = len(self.pieces)
+        self.status_priority = status_priority
         self.argslistheader = argslistheader
         self.unpauseflag = Event()
         self.unpauseflag.set()
@@ -599,6 +645,18 @@
 
         disabled_files = None
         if self.selector_enabled:
+            if self.status_priority is not None:
+                if len(self.status_priority) == 0:
+                    self._failed('Nothing to download')
+                    if self.doneflag.isSet():
+                        return None
+                status_priority = []
+                for file, length in self.files:
+                    status_priority.append(self.status_priority.get(file, -1))
+                if len(status_priority) == len(self.files):
+                    self.status_priority = status_priority
+                else:
+                    self.status_priority = None
             self.priority = self.config['priority']
             if self.priority:
                 try:
@@ -610,7 +668,9 @@
                         assert p <= 2
                 except:
                     self.errorfunc('bad priority list given, ignored')
-                    self.priority = None
+                    self.priority = self.status_priority
+            else:
+                self.priority = self.status_priority
 
             data = self.appdataobj.getTorrentData(self.infohash)
             try:

Modified: debtorrent/trunk/btdownloadheadless.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/btdownloadheadless.py?rev=51&op=diff
==============================================================================
--- debtorrent/trunk/btdownloadheadless.py (original)
+++ debtorrent/trunk/btdownloadheadless.py Fri May 18 03:55:42 2007
@@ -148,6 +148,10 @@
         defaultsToIgnore = ['responsefile', 'url', 'priority']
         configdir.setDefaults(defaults,defaultsToIgnore)
         configdefaults = configdir.loadConfig()
+        defaults.append(('status_to_download', 0,
+        'determines which packages to download based on /var/lib/dpkg/status ' +
+        '(0 = disabled [download all or use --priority], 1 = download updated versions of installed packages, ' +
+         '2 = download all installed packages)'))
         defaults.append(('save_options',0,
          "whether to save the current options as the new default configuration " +
          "(only for btdownloadheadless.py)"))
@@ -188,7 +192,7 @@
                 h.failed()
                 return
 
-        response = get_response(config['responsefile'], config['url'], h.error)
+        (response, status_priority) = get_response(config['responsefile'], config['url'], config['status_to_download'], h.error)
         if not response:
             break
 
@@ -196,7 +200,7 @@
 
         dow = BT1Download(h.display, h.finished, h.error, disp_exception, doneflag,
                         config, response, infohash, myid, rawserver, listen_port,
-                        configdir)
+                        status_priority, configdir)
         
         if not dow.saveAs(h.chooseFile, h.newpath):
             break




More information about the Debtorrent-commits mailing list