Code cleanup for core files

This commit is contained in:
Calum Lind 2014-08-20 15:10:59 +01:00
parent 82f2fc67c2
commit 068cce353a
3 changed files with 62 additions and 109 deletions

View File

@ -1,37 +1,11 @@
# # -*- coding: utf-8 -*-
# authmanager.py
# #
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> # Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
# Copyright (C) 2011 Pedro Algarvio <pedro@algarvio.me> # Copyright (C) 2011 Pedro Algarvio <pedro@algarvio.me>
# #
# Deluge is free software. # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# # the additional special exception to link portions of this program with the OpenSSL library.
# You may redistribute it and/or modify it under the terms of the # See LICENSE for more details.
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with deluge. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
#
# #
import os import os
@ -111,18 +85,18 @@ class AuthManager(component.Component):
self.__load_auth_file() self.__load_auth_file()
def authorize(self, username, password): def authorize(self, username, password):
""" """Authorizes users based on username and password.
Authorizes users based on username and password
:param username: str, username Args:
:param password: str, password username (str): Username
:returns: int, the auth level for this user password (str): Password
:rtype: int
:raises AuthenticationRequired: if aditional details are required to Returns:
authenticate. int: The auth level for this user.
:raises BadLoginError: if the username does not exist or password does
not match. Raises:
AuthenticationRequired: If aditional details are required to authenticate.
BadLoginError: If the username does not exist or password does not match.
""" """
if not username: if not username:
@ -148,9 +122,7 @@ class AuthManager(component.Component):
return username in self.__auth return username in self.__auth
def get_known_accounts(self): def get_known_accounts(self):
""" """Returns a list of known deluge usernames."""
Returns a list of known deluge usernames.
"""
self.__load_auth_file() self.__load_auth_file()
return [account.data() for account in self.__auth.values()] return [account.data() for account in self.__auth.values()]
@ -256,16 +228,11 @@ class AuthManager(component.Component):
if line.startswith("#") or not line: if line.startswith("#") or not line:
# This line is a comment or empty # This line is a comment or empty
continue continue
try: lsplit = line.split(":")
lsplit = line.split(":")
except Exception, e:
log.error("Your auth file is malformed: %s", e)
continue
if len(lsplit) == 2: if len(lsplit) == 2:
username, password = lsplit username, password = lsplit
log.warning("Your auth entry for %s contains no auth level, " log.warning("Your auth entry for %s contains no auth level, "
"using AUTH_LEVEL_DEFAULT(%s)..", username, "using AUTH_LEVEL_DEFAULT(%s)..", username, AUTH_LEVEL_DEFAULT)
AUTH_LEVEL_DEFAULT)
if username == 'localclient': if username == 'localclient':
authlevel = AUTH_LEVEL_ADMIN authlevel = AUTH_LEVEL_ADMIN
else: else:
@ -275,8 +242,7 @@ class AuthManager(component.Component):
elif len(lsplit) == 3: elif len(lsplit) == 3:
username, password, authlevel = lsplit username, password, authlevel = lsplit
else: else:
log.error("Your auth file is malformed: " log.error("Your auth file is malformed: Incorrect number of fields!")
"Incorrect number of fields!")
continue continue
username = username.strip() username = username.strip()
@ -287,8 +253,7 @@ class AuthManager(component.Component):
try: try:
authlevel = AUTH_LEVELS_MAPPING[authlevel] authlevel = AUTH_LEVELS_MAPPING[authlevel]
except KeyError: except KeyError:
log.error("Your auth file is malformed: %r is not a valid auth " log.error("Your auth file is malformed: %r is not a valid auth level", authlevel)
"level" % authlevel)
continue continue
self.__auth[username] = Account(username, password, authlevel) self.__auth[username] = Account(username, password, authlevel)

View File

@ -1,36 +1,14 @@
# # -*- coding: utf-8 -*-
# daemon.py
# #
# Copyright (C) 2007-2009 Andrew Resch <andrewresch@gmail.com> # Copyright (C) 2007-2009 Andrew Resch <andrewresch@gmail.com>
# #
# Deluge is free software. # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# # the additional special exception to link portions of this program with the OpenSSL library.
# You may redistribute it and/or modify it under the terms of the # See LICENSE for more details.
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with deluge. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
# #
"""The Deluge daemon"""
import os import os
import logging import logging
from twisted.internet import reactor from twisted.internet import reactor
@ -50,7 +28,7 @@ log = logging.getLogger(__name__)
def check_running_daemon(pid_file): def check_running_daemon(pid_file):
"""Check for another running instance of the daemon using the same pid file""" """Check for another running instance of the daemon using the same pid file."""
if os.path.isfile(pid_file): if os.path.isfile(pid_file):
# Get the PID and the port of the supposedly running daemon # Get the PID and the port of the supposedly running daemon
with open(pid_file) as _file: with open(pid_file) as _file:
@ -61,6 +39,7 @@ def check_running_daemon(pid_file):
pid, port = None, None pid, port = None, None
def process_running(pid): def process_running(pid):
"""Verify if pid is a running process."""
if windows_check(): if windows_check():
from win32process import EnumProcesses from win32process import EnumProcesses
return pid in EnumProcesses() return pid in EnumProcesses()
@ -89,10 +68,19 @@ def check_running_daemon(pid_file):
class Daemon(object): class Daemon(object):
def __init__(self, options=None, args=None, classic=False): """The Deluge Daemon class"""
def __init__(self, listen_interface=None, interface=None, port=None, classic=False):
"""
Args:
listen_interface (str, optional): The IP address to listen to bittorrent connections on.
interface (str, optional): The IP address the daemon will listen for UI connections on.
port (int, optional): The port the daemon will listen for UI connections on.
classic (bool, optional): If True the client is in Classic (Standalone) mode otherwise, if
False, start the daemon as separate process.
"""
log.info("Deluge daemon %s", get_version()) log.info("Deluge daemon %s", get_version())
log.debug("options: %s", options)
log.debug("args: %s", args)
pid_file = get_config_dir("deluged.pid") pid_file = get_config_dir("deluged.pid")
check_running_daemon(pid_file) check_running_daemon(pid_file)
@ -103,26 +91,24 @@ class Daemon(object):
# Catch some Windows specific signals # Catch some Windows specific signals
if windows_check(): if windows_check():
def win_handler(ctrl_type): def win_handler(ctrl_type):
"""Handle the Windows shutdown or close events."""
log.debug("windows handler ctrl_type: %s", ctrl_type) log.debug("windows handler ctrl_type: %s", ctrl_type)
if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT: if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT:
self._shutdown() self._shutdown()
return 1 return 1
SetConsoleCtrlHandler(win_handler) SetConsoleCtrlHandler(win_handler)
listen_interface = None if listen_interface and not is_ip(listen_interface):
if options and options.listen_interface and is_ip(options.listen_interface): listen_interface = None
listen_interface = options.listen_interface
# Start the core as a thread and join it until it's done # Start the core as a thread and join it until it's done
self.core = Core(listen_interface=listen_interface) self.core = Core(listen_interface=listen_interface)
port = self.core.config["daemon_port"] if port is None:
if options and options.port: port = self.core.config["daemon_port"]
port = options.port
interface = None if interface and not is_ip(interface):
if options and options.ui_interface: interface = None
interface = options.ui_interface
self.rpcserver = RPCServer( self.rpcserver = RPCServer(
port=port, port=port,
@ -131,6 +117,8 @@ class Daemon(object):
interface=interface interface=interface
) )
log.debug("Listening to UI on: %s:%s and bittorrent on: %s", interface, port, listen_interface)
# Register the daemon and the core RPCs # Register the daemon and the core RPCs
self.rpcserver.register_object(self.core) self.rpcserver.register_object(self.core)
self.rpcserver.register_object(self) self.rpcserver.register_object(self)
@ -167,22 +155,20 @@ class Daemon(object):
@export() @export()
def get_method_list(self): def get_method_list(self):
""" """Returns a list of the exported methods."""
Returns a list of the exported methods.
"""
return self.rpcserver.get_method_list() return self.rpcserver.get_method_list()
@export(1) @export(1)
def authorized_call(self, rpc): def authorized_call(self, rpc):
""" """Determines if session auth_level is authorized to call RPC.
Returns True if authorized to call rpc.
:param rpc: a rpc, eg, "core.get_torrents_status" Args:
:type rpc: string rpc (str): A RPC, e.g. core.get_torrents_status
Returns:
bool: True if authorized to call RPC, otherwise False.
""" """
if not rpc in self.get_method_list(): if not rpc in self.get_method_list():
return False return False
auth_level = self.rpcserver.get_session_auth_level() return self.rpcserver.get_session_auth_level() >= self.rpcserver.get_rpc_auth_level(rpc)
return auth_level >= self.rpcserver.get_rpc_auth_level(rpc)

View File

@ -251,10 +251,12 @@ this should be an IP address", metavar="IFACE",
options.group = grp.getgrnam(options.group)[2] options.group = grp.getgrnam(options.group)[2]
os.setuid(options.group) os.setuid(options.group)
def run_daemon(options, args): def run_daemon(options):
from deluge.core.daemon import Daemon from deluge.core.daemon import Daemon
try: try:
Daemon(options, args) Daemon(listen_interface=options.listen_interface,
interface=options.ui_interface,
port=options.port)
except Exception as ex: except Exception as ex:
log.exception(ex) log.exception(ex)
sys.exit(1) sys.exit(1)
@ -275,6 +277,6 @@ this should be an IP address", metavar="IFACE",
from twisted.internet import reactor from twisted.internet import reactor
reactor.addSystemEventTrigger("before", "shutdown", save_profile_stats) reactor.addSystemEventTrigger("before", "shutdown", save_profile_stats)
print "Running with profiler..." print "Running with profiler..."
profiler.runcall(run_daemon, options, args) profiler.runcall(run_daemon, options)
else: else:
run_daemon(options, args) run_daemon(options)