fix crash on trying to convert *very* old 0.5 config files

This commit is contained in:
Marcos Pinto 2009-01-06 02:42:08 +00:00
commit 3584fa9e41

View file

@ -76,25 +76,29 @@ class OldStateUpgrader:
log.debug("Unable to open 0.5 state file: %s", e) log.debug("Unable to open 0.5 state file: %s", e)
return return
new_state = deluge.core.torrentmanager.TorrentManagerState() if type(state).__name__ == 'list':
for ti, uid in state.torrents.items(): log.debug("0.5 state file is too old to upgrade:")
torrent_path = os.path.join(self.config["config_location"], "torrentfiles", ti.filename) return
try: else:
torrent_info = None new_state = deluge.core.torrentmanager.TorrentManagerState()
log.debug("Attempting to create torrent_info from %s", torrent_path) for ti, uid in state.torrents.items():
_file = open(torrent_path, "rb") torrent_path = os.path.join(self.config["config_location"], "torrentfiles", ti.filename)
torrent_info = lt.torrent_info(lt.bdecode(_file.read())) try:
_file.close() torrent_info = None
except (IOError, RuntimeError), e: log.debug("Attempting to create torrent_info from %s", torrent_path)
log.warning("Unable to open %s: %s", filepath, e) _file = open(torrent_path, "rb")
torrent_info = lt.torrent_info(lt.bdecode(_file.read()))
_file.close()
except (IOError, RuntimeError), e:
log.warning("Unable to open %s: %s", filepath, e)
# Copy the torrent file to the new location # Copy the torrent file to the new location
import shutil import shutil
shutil.copyfile(torrent_path, os.path.join(self.config["state_location"], str(torrent_info.info_hash()) + ".torrent")) shutil.copyfile(torrent_path, os.path.join(self.config["state_location"], str(torrent_info.info_hash()) + ".torrent"))
# Set the file prioritiy property if not already there # Set the file prioritiy property if not already there
if not hasattr(ti, "priorities"): if not hasattr(ti, "priorities"):
ti.priorities = [1] * torrent_info.num_files() ti.priorities = [1] * torrent_info.num_files()
# Create the new TorrentState object # Create the new TorrentState object
new_torrent = deluge.core.torrentmanager.TorrentState( new_torrent = deluge.core.torrentmanager.TorrentState(
@ -112,16 +116,16 @@ class OldStateUpgrader:
# Append the object to the state list # Append the object to the state list
new_state.torrents.append(new_torrent) new_state.torrents.append(new_torrent)
# Now we need to write out the new state file # Now we need to write out the new state file
try: try:
log.debug("Saving torrent state file.") log.debug("Saving torrent state file.")
state_file = open( state_file = open(
os.path.join(self.config["state_location"], "torrents.state"), "wb") os.path.join(self.config["state_location"], "torrents.state"), "wb")
cPickle.dump(new_state, state_file) cPickle.dump(new_state, state_file)
state_file.close() state_file.close()
except IOError, e: except IOError, e:
log.warning("Unable to save state file: %s", e) log.warning("Unable to save state file: %s", e)
return return
# Rename the persistent.state file # Rename the persistent.state file
try: try: