r194 - in /debtorrent/trunk/DebTorrent: BT1/AptListener.py HTTPCache.py launchmanycore.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Wed Aug 1 20:19:02 UTC 2007


Author: camrdale-guest
Date: Wed Aug  1 20:19:02 2007
New Revision: 194

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=194
Log:
Old torrents get removed when new torrents (with the same name) get added.
Added time info to torrent cache so downloading a package from a stopped torrent will cause the latest one to be started.

Modified:
    debtorrent/trunk/DebTorrent/BT1/AptListener.py
    debtorrent/trunk/DebTorrent/HTTPCache.py
    debtorrent/trunk/DebTorrent/launchmanycore.py

Modified: debtorrent/trunk/DebTorrent/BT1/AptListener.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/AptListener.py?rev=194&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/AptListener.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/AptListener.py Wed Aug  1 20:19:02 2007
@@ -535,6 +535,7 @@
         a['numfiles'] = nf
         a['length'] = l
         a['name'] = name
+        a['time'] = self.Cache.get_file_mtime(path)
         def setkey(k, d = response, a = a):
             if d.has_key(k):
                 a[k] = d[k]

Modified: debtorrent/trunk/DebTorrent/HTTPCache.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/HTTPCache.py?rev=194&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/HTTPCache.py (original)
+++ debtorrent/trunk/DebTorrent/HTTPCache.py Wed Aug  1 20:19:02 2007
@@ -287,6 +287,27 @@
 
         return file
 
+    def get_file_mtime(self, path):
+        """Get the modification time of the file in the cache.
+        
+        @type path: C{list} of C{string}
+        @param path: the server and path to check
+        @rtype: C{int}
+        @return: the file's modification time, or 0 if the file is not found
+        
+        """
+        
+        if not path:
+            return 0
+        
+        # Build the file name
+        file = self.get_filename(path)
+
+        try:
+            return int(getmtime(file))
+        except:
+            return 0
+
     def check_mtime(self, http_mtime_string, path = [], file = '', server_mtime_string = ''):
         """Check the modified time of a file in the cache against a server header string.
         

Modified: debtorrent/trunk/DebTorrent/launchmanycore.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/launchmanycore.py?rev=194&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/launchmanycore.py (original)
+++ debtorrent/trunk/DebTorrent/launchmanycore.py Wed Aug  1 20:19:02 2007
@@ -509,23 +509,45 @@
         if save_cache:
             self.configdir.writeTorrent(self.torrent_cache[hash]['metainfo'], hash)
 
+        # Check if the torrent is already running
         if hash in self.torrent_list:
             if not self.config['disable_http_downloader']:
                 for u in new_debmirrors:
                     self.downloads[hash].d.httpdownloader.make_download(u)
         else:
-            c = self.counter
-            self.counter += 1
-            x = ''
-            for i in xrange(3):
-                x = mapbase64[c & 0x3F]+x
-                c >>= 6
-            peer_id = createPeerID(x)
-            d = SingleDownload(self, hash, data['metainfo'], self.config, peer_id)
-            self.torrent_list.append(hash)
-            self.downloads[hash] = d
-            d.start()
-
+            # Stop any running previous versions of the same torrent
+            same_names = []
+            for old_hash in self.torrent_list:
+                if self.torrent_cache[old_hash]['name'] == data['name']:
+                    same_names.append(old_hash)
+            for old_hash in same_names:
+                self.remove(old_hash)
+                
+            self.start(hash)
+
+    def start(self, hash):
+        """Start a cached torrent.
+        
+        @type hash: C{string}
+        @param hash: the info hash of the torrent to start
+        
+        """
+        
+        if hash not in self.torrent_cache:
+            logging.error('Asked to start a torrent that is not in the cache')
+            return
+        
+        c = self.counter
+        self.counter += 1
+        x = ''
+        for i in xrange(3):
+            x = mapbase64[c & 0x3F]+x
+            c >>= 6
+        peer_id = createPeerID(x)
+        d = SingleDownload(self, hash, self.torrent_cache[hash]['metainfo'], self.config, peer_id)
+        self.torrent_list.append(hash)
+        self.downloads[hash] = d
+        d.start()
 
     def saveAs(self, hash, name, saveas, isdir):
         """Determine the location to save the torrent in.
@@ -593,6 +615,7 @@
 
         file = '/'.join(path)
         logging.debug('Trying to find file: '+file)
+        found_torrents = []
             
         # Check each torrent in the cache
         for hash, data in self.torrent_cache.items():
@@ -607,11 +630,35 @@
                 # Check that the file ends with the desired file name
                 if file.endswith('/'.join(f['path'])):
                     logging.debug('Found file in: '+str(binascii.b2a_hex(hash)))
-                    return self.downloads[hash].d, file_num
-                
-        logging.warning('Failed to find file: '+file)
-        return None, None
-
+                    found_torrents.append((hash, file_num))
+        
+        if not found_torrents:
+            logging.warning('Failed to find file: '+file)
+            return None, None
+        
+        # Find a running torrent with the file, (also the newest non-running torrent)
+        newest_mtime = 0
+        newest_torrent = ''
+        for hash, file_num in found_torrents:
+            if hash in self.torrent_list:
+                logging.info('Using torrent: ' + str(binascii.b2a_hex(hash)))
+                return self.downloads[hash].d, file_num
+            else:
+                if self.torrent_cache[hash]['time'] > newest_mtime:
+                    newest_mtime = self.torrent_cache[hash]['time']
+                    newest_torrent = hash
+        
+        # Otherwise start the newest torrent found running
+        if newest_torrent:
+            logging.info('Starting torrent: ' + str(binascii.b2a_hex(hash)))
+            self.start(newest_torrent)
+        else:
+            logging.warning('Could not find the newest torrent, just starting the last one: ' + 
+                            str(binascii.b2a_hex(hash)))
+            self.start(hash)
+
+        logging.info('Using torrent: ' + str(binascii.b2a_hex(hash)))
+        return self.downloads[hash].d, file_num
 
     def hashchecksched(self, hash = None):
         """Schedule a new torrent for hash checking.




More information about the Debtorrent-commits mailing list