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 f47c9186bf
commit 7c2725acdc
2 changed files with 45 additions and 50 deletions

View File

@ -37,17 +37,23 @@
"""Common functions for various parts of Deluge to use.""" """Common functions for various parts of Deluge to use."""
import os import os
import sys
import time import time
import subprocess import subprocess
import platform import platform
import chardet import chardet
import logging import logging
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 *
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# 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
@ -65,13 +71,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
import sys
from deluge.error import *
LT_TORRENT_STATE = { LT_TORRENT_STATE = {
"Queued": 0, "Queued": 0,
"Checking": 1, "Checking": 1,
@ -91,7 +90,6 @@ LT_TORRENT_STATE = {
7: "Checking Resume Data" 7: "Checking Resume Data"
} }
TORRENT_STATE = [ TORRENT_STATE = [
"Allocating", "Allocating",
"Checking", "Checking",
@ -135,24 +133,26 @@ def get_default_config_dir(filename=None):
:rtype: string :rtype: string
""" """
if windows_check(): if windows_check():
appDataPath = os.environ.get("APPDATA") def save_config_path(resource):
if not appDataPath: appDataPath = os.environ.get("APPDATA")
import _winreg if not appDataPath:
hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") import _winreg
appDataReg = _winreg.QueryValueEx(hkey, "AppData") hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders")
appDataPath = appDataReg[0] appDataReg = _winreg.QueryValueEx(hkey, "AppData")
_winreg.CloseKey(hkey) appDataPath = appDataReg[0]
if filename: _winreg.CloseKey(hkey)
return os.path.join(appDataPath, "deluge", filename) return os.path.join(appDataPath, resource)
else:
return os.path.join(appDataPath, "deluge")
else: else:
import xdg.BaseDirectory from xdg.BaseDirectory import save_config_path
if filename: if not filename:
return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename) filename = ''
else: try:
return xdg.BaseDirectory.save_config_path("deluge") return os.path.join(save_config_path("deluge"), filename)
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

@ -43,8 +43,10 @@
import os import os
import sys import sys
from optparse import OptionParser from optparse import OptionParser
from logging import FileHandler, getLogger
from errno import EEXIST
import deluge.log from deluge.log import setupLogger
import deluge.error import deluge.error
def version_callback(option, opt_str, value, parser): def version_callback(option, opt_str, value, parser):
@ -88,19 +90,16 @@ def start_ui():
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args(deluge.common.unicode_argv()[1:]) (options, args) = parser.parse_args(deluge.common.unicode_argv()[1:])
# Setup the logger
if options.quiet: if options.quiet:
options.loglevel = "none" options.loglevel = "none"
if options.loglevel: if options.loglevel:
options.loglevel = options.loglevel.lower() options.loglevel = options.loglevel.lower()
logfile_mode = 'w' logfile_mode = 'w'
if options.rotate_logs: if options.rotate_logs:
logfile_mode = 'a' logfile_mode = 'a'
setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode)
# Setup the logger log = getLogger(__name__)
deluge.log.setupLogger(level=options.loglevel, filename=options.logfile,
filemode=logfile_mode)
if options.config: if options.config:
if not os.path.exists(options.config): if not os.path.exists(options.config):
@ -110,7 +109,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()):
@ -129,8 +128,6 @@ def start_ui():
version = deluge.common.get_version() version = deluge.common.get_version()
import logging
log = logging.getLogger(__name__)
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)
@ -188,27 +185,35 @@ 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)
logfile_mode = 'w' logfile_mode = 'w'
if options.rotate_logs: if options.rotate_logs:
logfile_mode = 'a' logfile_mode = 'a'
setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode)
# Setup the logger log = getLogger(__name__)
deluge.log.setupLogger(level=options.loglevel, filename=options.logfile,
filemode=logfile_mode)
import deluge.configmanager import deluge.configmanager
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():
@ -242,16 +247,6 @@ this should be an IP address", metavar="IFACE",
open_logfile() open_logfile()
# 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
import logging
log = logging.getLogger(__name__)
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"))