r346 - /debtorrent/trunk/DebTorrent/BT1/track.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Thu Jan 24 00:41:59 UTC 2008


Author: camrdale-guest
Date: Thu Jan 24 00:41:59 2008
New Revision: 346

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=346
Log:
Upgrade the old saved tracker state when starting.

Modified:
    debtorrent/trunk/DebTorrent/BT1/track.py

Modified: debtorrent/trunk/DebTorrent/BT1/track.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/track.py?rev=346&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/track.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/track.py Thu Jan 24 00:41:59 2008
@@ -195,7 +195,50 @@
                 if dirkeys.has_key(y[1]): # and each should have a unique info_hash
                     raise ValueError
                 dirkeys[y[1]] = 1
-            
+        elif cname == 'version':
+            if (type(cinfo) != IntType): # the version of the state file
+                raise ValueError
+
+def loadState(configdir):
+    """Load the saved state from a previous run.
+    
+    @type configdir: L{DebTorrent.ConfigDir.ConfigDir}
+    @param configdir: the configuration and cache directory manager
+    @rtype: C{dictionary}
+    @return: the saved state from the previous run
+    
+    """
+    
+    tempstate = configdir.getState()
+    if tempstate:
+        version = tempstate.get('version', 0)
+        # Upgrade from the previous version
+        if version < 1:
+            try:
+                tempstate['stats'] = {}
+                if completed in tempstate:
+                    for hash, num in tempstate['completed']:
+                        tempstate['stats'][hash] = {'completed': num,
+                                                    'downloaded': 0,
+                                                    'uploaded': 0}
+                    del tempstate['completed']
+                tempstate['version'] = 1
+                logger.info('successfully upgraded the previous state file')
+            except:
+                logger.exception('old statefile corrupt; resetting')
+                tempstate = {'version': 1}
+
+        # Test the state to make sure it's not been corrupted
+        try:
+            statefiletemplate(tempstate)
+            logger.info('successfully loaded the previous state file')
+        except:
+            logger.exception('statefile corrupt; resetting')
+            tempstate = {'version': 1}
+    else:
+        tempstate = {'version': 1}
+    
+    return tempstate
 
 alas = 'your file may exist elsewhere in the universe\nbut alas, not here\n'
 
@@ -464,7 +507,6 @@
         self.cached = {}    # format: infohash: [[time1, l1, s1], [time2, l2, s2], ...]
         self.cached_t = {}  # format: infohash: [time, cache]
         self.times = {}
-        self.state = {}
         self.seedcount = {}
         self.torrent_names = {}
 
@@ -483,16 +525,7 @@
             logger.warning('crypto library not installed,' +
                    ' cannot completely verify encrypted peers')
 
-        tempstate = configdir.getState()
-        if tempstate:
-            try:
-                if not tempstate.has_key('peers'):
-                    tempstate = {'peers': tempstate}
-                statefiletemplate(tempstate)
-                self.state = tempstate
-                logger.info('successfully loaded the previous state file')
-            except:
-                logger.warning('statefile corrupt; resetting')
+        self.state = loadState(configdir)
         self.downloads = self.state.setdefault('peers', {})
         self.stats = self.state.setdefault('stats', {})
 




More information about the Debtorrent-commits mailing list