Fixed #437 - Startup no longer fails with old persistent.state files. Thanks,

codergeek42.
This commit is contained in:
Alex Dedul 2007-07-15 00:55:53 +00:00
parent f31b079a42
commit 0999349be5
1 changed files with 49 additions and 5 deletions

View File

@ -34,11 +34,14 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
import gettext
import locale
import os
import os.path
from optparse import OptionParser from optparse import OptionParser
import re
import sys import sys
import os, os.path
import gettext, locale
import deluge, deluge.common, deluge.interface
try: try:
import dbus import dbus
dbus_version = getattr(dbus, 'version', (0,0,0)) dbus_version = getattr(dbus, 'version', (0,0,0))
@ -52,6 +55,10 @@ try:
except: dbus_imported = False except: dbus_imported = False
else: dbus_imported = True else: dbus_imported = True
import deluge
import deluge.common
import deluge.core
import deluge.interface
parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.PROGRAM_VERSION) parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.PROGRAM_VERSION)
parser.add_option("--tray", dest="tray", help="start Deluge hidden in system tray", parser.add_option("--tray", dest="tray", help="start Deluge hidden in system tray",
@ -61,14 +68,52 @@ parser.add_option("--url", dest="url", help="Load a torrent from a URL",
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
def fix_old_persistence_state():
pstate_file_path = os.path.join(deluge.common.CONFIG_DIR,
deluge.core.STATE_FILENAME)
## The persistent_state object moved from the deluge.deluge module to the
## deluge.core module from 0.5; so let's edit the user's saved data to
## reflect this.
if os.path.isfile(pstate_file_path):
try:
pstate_fd = open(pstate_file_path, "r")
pstate_data = pstate_fd.read()
pstate_fd.close()
## If the file was empty, then we should remove it so that the
## pickler doesn't not attempt to unpack an empty state.
if len(pstate_data) is 0:
os.remove(pstate_file_path)
print "Empty persistent state data file removed successfully."
## The file exists and contains data, so let's do a regex-based
## find/replace to update the module name.
else:
pstate_old_regex = re.compile("\(ideluge\.deluge$",
re.MULTILINE)
if re.search(pstate_old_regex, pstate_data):
pstate_new_data = re.sub(pstate_old_regex,
"(ideluge.core", pstate_data)
pstate_fd = open(pstate_file_path, "w")
pstate_fd.write(pstate_new_data)
pstate_fd.close()
print "Persistent state data updated successfully."
except OSError, oopsie:
print >> sys.stderr, \
"""There was an error updating the persistent.state file.
If this is an upgrade from an earlier version of Deluge, this may cause bugs
or failures in startup. You may wish to remove it manually. (%s)
Continuing...""" % pstate_file_path
print >> sys.stderr, "The error was: %s." % oopsie
def start_deluge(): def start_deluge():
print "Starting new Deluge session..." print "Starting new Deluge session..."
fix_old_persistence_state()
interface = deluge.interface.DelugeGTK() interface = deluge.interface.DelugeGTK()
add_args(interface) add_args(interface)
interface.start(options.tray) interface.start(options.tray)
def add_args(interface): def add_args(interface):
if options.url: if options.url:
interface.external_add_url(options.url) interface.external_add_url(options.url)
@ -80,7 +125,6 @@ def add_args(interface):
else: else:
print "Error,", arg, " does not seem to be a .torrent file" print "Error,", arg, " does not seem to be a .torrent file"
if dbus_imported: if dbus_imported:
bus = dbus.SessionBus() bus = dbus.SessionBus()