Commit 60255e9a authored by Devaev Maxim's avatar Devaev Maxim

Refactoring all function modules for using tools

parent b2ad9c4a
......@@ -7,6 +7,7 @@ from settingsd import config
from settingsd import service
from settingsd import shared
from settingsd import logger
from settingsd import tools
##### Private constants #####
......@@ -31,11 +32,6 @@ UNAME_OPTION_HARDWARE_PLATFORM = "--hardware-platform"
UNAME_OPTION_OPERATING_SYSTEM = "--operating-system"
##### Exceptions #####
class SubprocessFailure(Exception) :
pass
##### Private classes #####
class CommonInfo(service.FunctionObject) :
......@@ -100,36 +96,24 @@ class CommonInfo(service.FunctionObject) :
def lsbOption(self, option) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "lsb_release_prog_path"), option)
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return ":".join(proc_stdout.split(":")[1:]).strip()
def unameOption(self, option) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "uname_prog_path"), option)
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return proc_stdout.strip()
###
def execProcess(self, proc_args) :
logger.debug("{mod}: Executing child process \"%s\"" % (proc_args))
proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={ "LC_ALL" : "C" })
(proc_stdout, proc_stderr) = proc.communicate()
logger.debug("{mod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode))
return (proc_stdout, proc_stderr, proc.returncode)
##### Public classes #####
class Service(service.Service) :
......
......@@ -7,6 +7,7 @@ from settingsd import config
from settingsd import service
from settingsd import shared
from settingsd import logger
from settingsd import tools
from settingsd import validators
......@@ -22,11 +23,6 @@ SHUTDOWN_OPTION_HALT = "-h"
SHUTDOWN_OPTION_REBOOT = "-r"
##### Exceptions #####
class SubprocessFailure(Exception) :
pass
##### Private classes #####
class Machine(service.FunctionObject) :
......@@ -35,36 +31,36 @@ class Machine(service.FunctionObject) :
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def shutdown(self) :
proc_args = "%s %s now" % (config.value(SERVICE_NAME, "shutdown_prog_path"), SHUTDOWN_OPTION_HALT)
return self.execProcess(proc_args)[2]
return tools.execProcess(proc_args)[2]
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def reboot(self) :
proc_args = "%s %s now" % (config.value(SERVICE_NAME, "shutdown_prog_path"), SHUTDOWN_OPTION_REBOOT)
return self.execProcess(proc_args)[2]
return tools.execProcess(proc_args)[2]
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def suspend(self) :
return self.execProcess(config.value(SERVICE_NAME, "pm_suspend_prog_path"))[2]
return tools.execProcess(config.value(SERVICE_NAME, "pm_suspend_prog_path"))[2]
@service.functionMethod(POWER_METHODS_NAMESPACE, out_signature="i")
def hibernate(self) :
return self.execProcess(config.value(SERVICE_NAME, "pm_hibernate_prog_path"))[2]
return tools.execProcess(config.value(SERVICE_NAME, "pm_hibernate_prog_path"))[2]
###
@service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, in_signature="i", out_signature="i")
def switchTo(self, level) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "telinit_prog_path"), validators.validRange(str(level), RUNLEVELS))
return self.execProcess(proc_args)[2]
return tools.execProcess(proc_args)[2]
@service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, out_signature="i")
def currentLevel(self) :
proc_args = config.value(SERVICE_NAME, "runlevel_prog_path")
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
level_pairs_list = proc_stdout.strip().split(" ")
if len(level_pairs_list) != 2 or not level_pairs_list[1] in RUNLEVELS :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return int(level_pairs_list[1])
......@@ -72,29 +68,16 @@ class Machine(service.FunctionObject) :
@service.functionMethod(RUNLEVELS_METHODS_NAMESPACE, out_signature="i")
def previousLevel(self) :
proc_args = config.value(SERVICE_NAME, "runlevel_prog_path")
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
level_pairs_list = proc_stdout.strip().split(" ")
if len(level_pairs_list) != 2 or not level_pairs_list[1] in RUNLEVELS+"N" :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return ( int(level_pairs_list[0]) if level_pairs_list[0] in RUNLEVELS else -1 )
### Private ###
def execProcess(self, proc_args) :
logger.debug("{mod}: Executing child process \"%s\"" % (proc_args))
proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={ "LC_ALL" : "C" })
(proc_stdout, proc_stderr) = proc.communicate()
logger.debug("{mod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode))
return (proc_stdout, proc_stderr, proc.returncode)
##### Public classes #####
class Service(service.Service) :
......
......@@ -8,6 +8,7 @@ from settingsd import config
from settingsd import service
from settingsd import shared
from settingsd import logger
from settingsd import tools
##### Private constants #####
......@@ -16,11 +17,6 @@ SERVICE_NAME = "ntp_config"
NTP_METHODS_NAMESPACE = "time.ntp"
##### Exceptions #####
class SubprocessFailure(Exception) :
pass
##### Private classes #####
class NtpConfig(service.FunctionObject) :
......@@ -64,26 +60,13 @@ class NtpConfig(service.FunctionObject) :
@service.functionMethod(NTP_METHODS_NAMESPACE)
def request(self) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "ntpdate_prog_path"), " ".join(self.servers()))
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
### Private ###
def execProcess(self, proc_args) :
logger.debug("{mod}: Executing child process \"%s\"" % (proc_args))
proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={ "LC_ALL" : "C" })
(proc_stdout, proc_stderr) = proc.communicate()
logger.debug("{mod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode))
return (proc_stdout, proc_stderr, proc.returncode)
##### Public classes #####
class Service(service.Service) :
......
......@@ -10,6 +10,8 @@ from settingsd import service
from settingsd import shared
from settingsd import dbus_tools
from settingsd import logger
from settingsd import tools
from settingsd import validators
##### Private constants #####
......@@ -20,14 +22,6 @@ SYSTEM_SERVICE_METHODS_NAMESPACE = "systemService"
RUNLEVELS = "0123456"
##### Exceptions #####
class SubprocessFailure(Exception) :
pass
class IncorrectArgument(Exception) :
pass
##### Private classes #####
class SystemService(service.FunctionObject) :
def __init__(self, system_service_name, object_path, service_object = None) :
......@@ -49,10 +43,10 @@ class SystemService(service.FunctionObject) :
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="s")
def levelsMap(self) :
proc_args = "%s --list %s" % (config.value(SERVICE_NAME, "chkconfig_prog_path"), self.systemServiceName())
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
service_record_list = re.split(r"\s+", proc_stdout.split("\n")[0])
......@@ -78,16 +72,16 @@ class SystemService(service.FunctionObject) :
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i")
def start(self) :
logger.verbose("{mod}: Request to start service \"%s\"" % (self.systemServiceName()))
return self.execProcess("%s start" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2]
return tools.execProcess("%s start" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2]
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i")
def stop(self) :
return self.execProcess("%s stop" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2]
return tools.execProcess("%s stop" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2]
logger.verbose("{mod}: Request to stop service \"%s\"" % (self.systemServiceName()))
@service.functionMethod(SYSTEM_SERVICE_METHODS_NAMESPACE, out_signature="i")
def status(self) :
return self.execProcess("%s status" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2]
return tools.execProcess("%s status" % (os.path.join(config.value(SERVICE_NAME, "initd_dir_path"), self.systemServiceName())))[2]
### Private ###
......@@ -105,39 +99,27 @@ class SystemService(service.FunctionObject) :
proc_args = "%s %s %s %s" % ( config.value(SERVICE_NAME, "chkconfig_prog_path"), ( "--level %s" % (levels) if levels != None else "" ),
self.systemServiceName(), ( "on" if enabled_flag else "off" ) )
(proc_stdout, proc_stderr, proc_returncode) = self.execProcess(proc_args)
(proc_stdout, proc_stderr, proc_returncode) = tools.execProcess(proc_args)
if proc_returncode != 0 :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
raise tools.SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc_returncode ))
return proc_returncode
###
def execProcess(self, proc_args) :
logger.debug("{mod}: Executing child process \"%s\"" % (proc_args))
proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={ "LC_ALL" : "C" })
(proc_stdout, proc_stderr) = proc.communicate()
logger.debug("{mod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode))
return (proc_stdout, proc_stderr, proc.returncode)
###
def validateLevels(self, levels) :
if type(levels).__name__ in ("str", "String") :
if len(levels) == 0 :
levels = None
for level in levels :
if not level in RUNLEVELS :
raise IncorrectArgument("Incorrect item \"%s\" in argument \"%s\"" % (level, levels))
raise validators.ValidatorError("Incorrect item \"%s\" in argument \"%s\"" % (level, levels))
elif type (levels).__name__ == "NoneType" :
pass
else :
raise IncorrectArgument("Incorrect type \"%s\" of argument" % (type(levels).__name__))
raise validators.ValidatorError("Incorrect type \"%s\" of argument" % (type(levels).__name__))
return levels
......@@ -148,17 +130,7 @@ class Service(service.Service) :
def initService(self) :
proc_args = "%s --list" % (config.value(SERVICE_NAME, "chkconfig_prog_path"))
logger.debug("{mod}: Executing child process \"%s\"" % (proc_args))
proc = subprocess.Popen(proc_args, shell=True, bufsize=1024, close_fds=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={ "LC_ALL" : "C" })
(proc_stdout, proc_stderr) = proc.communicate()
logger.debug("{mod}: Child process \"%s\" finished, return_code=%d" % (proc_args, proc.returncode))
if proc.returncode != 0 :
raise SubprocessFailure("Error while execute \"%s\"\nStdout: %s\nStderr: %s\nReturn code: %d" % (
proc_args, proc_stdout.strip(), proc_stderr.strip(), proc.returncode ))
(proc_stdout, proc_sterr, proc_returncode) = tools.execProcess(proc_args)
system_service_count = 0
shared.Functions.addShared(SERVICE_NAME)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment