Fix #1963 : Unhandled OSError if permission issue creating default config dir

The fix now logs the error and exits. To log the error also required moving
the logger setup code to before any default_config_dir call.
This commit is contained in:
Calum Lind 2012-12-10 19:01:05 +00:00
parent 0045ec0cf1
commit 1dd078f4f1
2 changed files with 40 additions and 34 deletions

View File

@ -42,12 +42,18 @@ import subprocess
import platform import platform
import sys import sys
import chardet import chardet
import pkg_resources
import gettext
import locale
try: try:
import json import json
except ImportError: except ImportError:
import simplejson as json import simplejson as json
from deluge.error import *
from deluge.log import LOG as log
# Do a little hack here just in case the user has json-py installed since it # Do a little hack here just in case the user has json-py installed since it
# has a different api # has a different api
if not hasattr(json, "dumps"): if not hasattr(json, "dumps"):
@ -63,10 +69,6 @@ if not hasattr(json, "dumps"):
json.dump = dump json.dump = dump
json.load = load json.load = load
import pkg_resources
import gettext
import locale
# Initialize gettext # Initialize gettext
try: try:
if hasattr(locale, "bindtextdomain"): if hasattr(locale, "bindtextdomain"):
@ -75,14 +77,11 @@ try:
locale.textdomain("deluge") locale.textdomain("deluge")
gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"), unicode=True) gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"), unicode=True)
except Exception, e: except Exception, e:
from deluge.log import LOG as log
log.error("Unable to initialize gettext/locale!") log.error("Unable to initialize gettext/locale!")
log.exception(e) log.exception(e)
import __builtin__ import __builtin__
__builtin__.__dict__["_"] = lambda x: x __builtin__.__dict__["_"] = lambda x: x
from deluge.error import *
LT_TORRENT_STATE = { LT_TORRENT_STATE = {
"Queued": 0, "Queued": 0,
"Checking": 1, "Checking": 1,
@ -102,7 +101,6 @@ LT_TORRENT_STATE = {
7: "Checking Resume Data" 7: "Checking Resume Data"
} }
TORRENT_STATE = [ TORRENT_STATE = [
"Allocating", "Allocating",
"Checking", "Checking",
@ -159,11 +157,15 @@ def get_default_config_dir(filename=None):
else: else:
return os.path.join(appDataPath, "deluge") return os.path.join(appDataPath, "deluge")
else: else:
import xdg.BaseDirectory from xdg.BaseDirectory import save_config_path
if filename: try:
return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename) if filename:
else: return os.path.join(save_config_path("deluge"), filename)
return xdg.BaseDirectory.save_config_path("deluge") else:
return save_config_path("deluge")
except OSError, e:
log.error("Unable to use default config directory, exiting... (%s)", e)
sys.exit(1)
def get_default_download_dir(): def get_default_download_dir():
""" """

View File

@ -42,6 +42,8 @@
import os import os
import sys import sys
from optparse import OptionParser from optparse import OptionParser
from logging import FileHandler
from errno import EEXIST
import deluge.log import deluge.log
import deluge.common import deluge.common
@ -86,6 +88,14 @@ def start_ui():
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
# Setup the logger
if options.quiet:
options.loglevel = "none"
if options.loglevel:
options.loglevel = options.loglevel.lower()
deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
from deluge.log import LOG as log
if options.config: if options.config:
if not os.path.exists(options.config): if not os.path.exists(options.config):
# Try to create the config folder if it doesn't exist # Try to create the config folder if it doesn't exist
@ -94,7 +104,7 @@ def start_ui():
except Exception, e: except Exception, e:
pass pass
elif not os.path.isdir(options.config): elif not os.path.isdir(options.config):
print "Config option needs to be a directory!" log.error("Config option needs to be a directory!")
sys.exit(1) sys.exit(1)
else: else:
if not os.path.exists(deluge.common.get_default_config_dir()): if not os.path.exists(deluge.common.get_default_config_dir()):
@ -110,18 +120,8 @@ def start_ui():
print "The default UI has been changed to", options.default_ui print "The default UI has been changed to", options.default_ui
sys.exit(0) sys.exit(0)
if options.quiet:
options.loglevel = "none"
if options.loglevel:
options.loglevel = options.loglevel.lower()
# Setup the logger
deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
version = deluge.common.get_version() version = deluge.common.get_version()
from deluge.log import LOG as log
log.info("Deluge ui %s", version) log.info("Deluge ui %s", version)
log.debug("options: %s", options) log.debug("options: %s", options)
log.debug("args: %s", args) log.debug("args: %s", args)
@ -170,18 +170,31 @@ this should be an IP address", metavar="IFACE",
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
# Setup the logger
if options.quiet: if options.quiet:
options.loglevel = "none" options.loglevel = "none"
if options.logfile:
# Try to create the logfile's directory if it doesn't exist
try:
os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
except OSError, e:
if e.errno != EEXIST:
print "There was an error creating the log directory, exiting... (%s)" % e
sys.exit(1)
deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
from deluge.log import LOG as log
if options.config: if options.config:
if not deluge.configmanager.set_config_dir(options.config): if not deluge.configmanager.set_config_dir(options.config):
print "There was an error setting the config dir! Exiting.." log.error("There was an error setting the config directory! Exiting...")
sys.exit(1) sys.exit(1)
# Sets the options.logfile to point to the default location # Sets the options.logfile to point to the default location
def open_logfile(): def open_logfile():
if not options.logfile: if not options.logfile:
options.logfile = deluge.configmanager.get_config_dir("deluged.log") options.logfile = deluge.configmanager.get_config_dir("deluged.log")
file_handler = FileHandler(options.logfile)
log.addHandler(file_handler)
# Writes out a pidfile if necessary # Writes out a pidfile if necessary
def write_pidfile(): def write_pidfile():
@ -208,15 +221,6 @@ this should be an IP address", metavar="IFACE",
# Do not daemonize # Do not daemonize
write_pidfile() write_pidfile()
# Setup the logger
try:
# Try to make the logfile's directory if it doesn't exist
os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
except:
pass
deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
from deluge.log import LOG as log
if options.profile: if options.profile:
import hotshot import hotshot
hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile")) hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile"))