diff --git a/deluge/common.py b/deluge/common.py index eaaa3b161..b68c1c776 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -37,17 +37,23 @@ """Common functions for various parts of Deluge to use.""" import os +import sys import time import subprocess import platform import chardet import logging +import pkg_resources +import gettext +import locale try: import json except ImportError: import simplejson as json +from deluge.error import * + log = logging.getLogger(__name__) # 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.load = load -import pkg_resources -import gettext -import locale -import sys - -from deluge.error import * - LT_TORRENT_STATE = { "Queued": 0, "Checking": 1, @@ -91,7 +90,6 @@ LT_TORRENT_STATE = { 7: "Checking Resume Data" } - TORRENT_STATE = [ "Allocating", "Checking", @@ -135,24 +133,26 @@ def get_default_config_dir(filename=None): :rtype: string """ + if windows_check(): - appDataPath = os.environ.get("APPDATA") - if not appDataPath: - import _winreg - hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") - appDataReg = _winreg.QueryValueEx(hkey, "AppData") - appDataPath = appDataReg[0] - _winreg.CloseKey(hkey) - if filename: - return os.path.join(appDataPath, "deluge", filename) - else: - return os.path.join(appDataPath, "deluge") + def save_config_path(resource): + appDataPath = os.environ.get("APPDATA") + if not appDataPath: + import _winreg + hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") + appDataReg = _winreg.QueryValueEx(hkey, "AppData") + appDataPath = appDataReg[0] + _winreg.CloseKey(hkey) + return os.path.join(appDataPath, resource) 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 + if not filename: + filename = '' + try: + 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(): """ diff --git a/deluge/main.py b/deluge/main.py index 422b661fe..a1525df1c 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -43,8 +43,10 @@ import os import sys from optparse import OptionParser +from logging import FileHandler, getLogger +from errno import EEXIST -import deluge.log +from deluge.log import setupLogger import deluge.error def version_callback(option, opt_str, value, parser): @@ -88,19 +90,16 @@ def start_ui(): # Get the options and args from the OptionParser (options, args) = parser.parse_args(deluge.common.unicode_argv()[1:]) + # Setup the logger if options.quiet: options.loglevel = "none" - if options.loglevel: options.loglevel = options.loglevel.lower() - logfile_mode = 'w' if options.rotate_logs: logfile_mode = 'a' - - # Setup the logger - deluge.log.setupLogger(level=options.loglevel, filename=options.logfile, - filemode=logfile_mode) + setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode) + log = getLogger(__name__) if options.config: if not os.path.exists(options.config): @@ -110,7 +109,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()): @@ -129,8 +128,6 @@ def start_ui(): version = deluge.common.get_version() - import logging - log = logging.getLogger(__name__) log.info("Deluge ui %s", version) log.debug("options: %s", options) 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 (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) logfile_mode = 'w' if options.rotate_logs: logfile_mode = 'a' - - # Setup the logger - deluge.log.setupLogger(level=options.loglevel, filename=options.logfile, - filemode=logfile_mode) + setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode) + log = getLogger(__name__) import deluge.configmanager 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(): @@ -242,16 +247,6 @@ this should be an IP address", metavar="IFACE", 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: import hotshot hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile"))