From decb3bc8de23e0586a4eb067fda74b0091e65393 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Sun, 17 Feb 2013 07:59:03 +0100 Subject: Improved writepid and pidof functions. --- source/pyAggr3g470r | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'source') diff --git a/source/pyAggr3g470r b/source/pyAggr3g470r index 3ea48933..a231a181 100755 --- a/source/pyAggr3g470r +++ b/source/pyAggr3g470r @@ -54,11 +54,9 @@ def writepid(processname = None, pid = None): """ pidpath = os.path.join(PATH, "var", processname + ".pid") if processname is not None and pid is not None: - f = open (pidpath, "w") - f.write(str(pid)) - f.close() - return True - else: + with open(pidpath, "w") as f: + f.write(str(pid)) + return True return False def checkpid (servicename = None): @@ -71,11 +69,9 @@ def checkpid (servicename = None): def pidof(processname = None): pidpath = os.path.join(PATH, "var", processname + ".pid") if processname is not None and os.path.exists(pidpath): - f = open (pidpath) - pid = f.read() - f.close() - return pid - else: + with open(pidpath) as f: + pid = f.read() + return pid return False def rmpid (processname = None): @@ -93,7 +89,7 @@ def start(python_command): print(SERVICE + " is starting with pid: " + pidof(processname=SERVICE)) else: print(SERVICE + " could not be started (pid exists)") - retval=False + retval = False def stop(): print("Stopping pyAggr3g470r...") -- cgit