From 1dd078f4f1d3a35b293c305e53a35d6b1e82d739 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Mon, 10 Dec 2012 19:01:05 +0000 Subject: [PATCH] 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. --- deluge/common.py | 28 +++++++++++++++------------- deluge/main.py | 46 +++++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index 63c364d6c..033bbd2ef 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -42,12 +42,18 @@ import subprocess import platform import sys import chardet +import pkg_resources +import gettext +import locale try: import json except ImportError: 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 # has a different api if not hasattr(json, "dumps"): @@ -63,10 +69,6 @@ if not hasattr(json, "dumps"): json.dump = dump json.load = load -import pkg_resources -import gettext -import locale - # Initialize gettext try: if hasattr(locale, "bindtextdomain"): @@ -75,14 +77,11 @@ try: locale.textdomain("deluge") gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"), unicode=True) except Exception, e: - from deluge.log import LOG as log log.error("Unable to initialize gettext/locale!") log.exception(e) import __builtin__ __builtin__.__dict__["_"] = lambda x: x -from deluge.error import * - LT_TORRENT_STATE = { "Queued": 0, "Checking": 1, @@ -102,7 +101,6 @@ LT_TORRENT_STATE = { 7: "Checking Resume Data" } - TORRENT_STATE = [ "Allocating", "Checking", @@ -159,11 +157,15 @@ def get_default_config_dir(filename=None): else: return os.path.join(appDataPath, "deluge") else: - import xdg.BaseDirectory - if filename: - return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename) - else: - return xdg.BaseDirectory.save_config_path("deluge") + from xdg.BaseDirectory import save_config_path + try: + if filename: + return os.path.join(save_config_path("deluge"), filename) + 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(): """ diff --git a/deluge/main.py b/deluge/main.py index d7f3df257..f8953d2a1 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -42,6 +42,8 @@ import os import sys from optparse import OptionParser +from logging import FileHandler +from errno import EEXIST import deluge.log import deluge.common @@ -86,6 +88,14 @@ def start_ui(): # Get the options and args from the OptionParser (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 not os.path.exists(options.config): # Try to create the config folder if it doesn't exist @@ -94,7 +104,7 @@ def start_ui(): except Exception, e: pass 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) else: 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 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() - from deluge.log import LOG as log log.info("Deluge ui %s", version) log.debug("options: %s", options) 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 (options, args) = parser.parse_args() + # Setup the logger if options.quiet: 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 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) # Sets the options.logfile to point to the default location def open_logfile(): if not options.logfile: options.logfile = deluge.configmanager.get_config_dir("deluged.log") + file_handler = FileHandler(options.logfile) + log.addHandler(file_handler) # Writes out a pidfile if necessary def write_pidfile(): @@ -208,15 +221,6 @@ this should be an IP address", metavar="IFACE", # Do not daemonize 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: import hotshot hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile"))