From b4dd90ba2bb93a6d7fe98840bcade12a2b3fdbd9 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Wed, 27 Apr 2016 12:58:50 +0100 Subject: [PATCH] Cleanup code in web and deamon entries to match --- deluge/core/daemon_entry.py | 26 +++++++++++++------------- deluge/ui/web/web.py | 14 ++++++++------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/deluge/core/daemon_entry.py b/deluge/core/daemon_entry.py index fbb7a90c6..1c00586a9 100644 --- a/deluge/core/daemon_entry.py +++ b/deluge/core/daemon_entry.py @@ -13,9 +13,9 @@ import os import sys from logging import FileHandler, getLogger -import deluge.common -import deluge.configmanager -import deluge.error +from deluge.common import run_profiled, windows_check +from deluge.configmanager import get_config_dir +from deluge.error import DaemonRunningError from deluge.ui.baseargparser import BaseArgParser from deluge.ui.util import lang @@ -30,7 +30,7 @@ def add_daemon_options(parser): help=_("IP address to listen for BitTorrent connections")) group.add_argument("-P", "--pidfile", metavar="", action="store", help=_("Pidfile to store the process id")) - if not deluge.common.windows_check(): + if not windows_check(): group.add_argument("-d", "--do-not-daemonize", dest="donot", action="store_true", help=_("Do not daemonize (fork) this process")) group.add_argument("-U", "--user", metavar="", action="store", @@ -62,10 +62,10 @@ def start_daemon(skip_start=False): # Check for any daemons running with this same config from deluge.core.daemon import check_running_daemon - pid_file = deluge.configmanager.get_config_dir("deluged.pid") + pid_file = get_config_dir("deluged.pid") try: check_running_daemon(pid_file) - except deluge.error.DaemonRunningError: + except DaemonRunningError: print("You cannot run multiple daemons with the same config directory set.") print("If you believe this is an error, you can force a start by deleting: %s" % pid_file) sys.exit(1) @@ -74,26 +74,27 @@ def start_daemon(skip_start=False): # If no logfile specified add logging to default location (as well as stdout) if not options.logfile: - options.logfile = deluge.configmanager.get_config_dir("deluged.log") + options.logfile = get_config_dir("deluged.log") file_handler = FileHandler(options.logfile) log.addHandler(file_handler) # If the donot daemonize is set, then we just skip the forking - if not (deluge.common.windows_check() or options.donot): + if not (windows_check() or options.donot): if os.fork(): - # We've forked and this is now the parent process, so die! os._exit(0) os.setsid() # Do second fork if os.fork(): os._exit(0) + # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. + os.chdir(get_config_dir()) # Write pid file before chuid if options.pidfile: with open(options.pidfile, "wb") as _file: - _file.write("%s\n" % os.getpid()) + _file.write("%d\n" % os.getpid()) - if not deluge.common.windows_check(): + if not windows_check(): if options.user: if not options.user.isdigit(): import pwd @@ -124,5 +125,4 @@ def start_daemon(skip_start=False): if options.pidfile: os.remove(options.pidfile) - return deluge.common.run_profiled(run_daemon, options, output_file=options.profile, - do_profile=options.profile) + return run_profiled(run_daemon, options, output_file=options.profile, do_profile=options.profile) diff --git a/deluge/ui/web/web.py b/deluge/ui/web/web.py index 733cef588..3652f5a2a 100644 --- a/deluge/ui/web/web.py +++ b/deluge/ui/web/web.py @@ -81,20 +81,22 @@ class Web(UI): # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. os.chdir(get_config_dir()) + # Write pid file before chuid if self.options.pidfile: - open(self.options.pidfile, "wb").write("%d\n" % os.getpid()) + with open(self.options.pidfile, "wb") as _file: + _file.write("%d\n" % os.getpid()) if not windows_check(): - if self.options.group: - if not self.options.group.isdigit(): - import grp - self.options.group = grp.getgrnam(self.options.group)[2] - os.setuid(self.options.group) if self.options.user: if not self.options.user.isdigit(): import pwd self.options.user = pwd.getpwnam(self.options.user)[2] os.setuid(self.options.user) + if self.options.group: + if not self.options.group.isdigit(): + import grp + self.options.group = grp.getgrnam(self.options.group)[2] + os.setuid(self.options.group) from deluge.ui.web import server self.__server = server.DelugeWeb(options=self.options)