From 73ee9cb40f6539e9e70d41f7871ebefe929173a6 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 3 Jul 2016 20:52:23 -0400 Subject: Use "with" instead of direct "open" --- steuermann/run.py | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/steuermann/run.py b/steuermann/run.py index 48386ea..c90a40a 100644 --- a/steuermann/run.py +++ b/steuermann/run.py @@ -206,35 +206,35 @@ class runner(object): raise # open the log file, write initial notes - logfile=open(logfile_name,"w") - logfile.write('%s %s\n'%(datetime.datetime.now(),run)) - logfile.flush() - - # debug - just say the name of the node we would run - - if ( no_run ) : - run = [ 'echo', 'disable run - node=', node.name ] - - # start running the process - if debug : - print("RUN",run) - p = subprocess.Popen( - args=run, - stdout=logfile, - stderr=subprocess.STDOUT, - shell=False, close_fds=True - ) + with open(logfile_name,"w") as logfile: + logfile.write('%s %s\n'%(datetime.datetime.now(),run)) + logfile.flush() + + # debug - just say the name of the node we would run + + if ( no_run ) : + run = [ 'echo', 'disable run - node=', node.name ] + + # start running the process + if debug : + print("RUN",run) + p = subprocess.Popen( + args=run, + stdout=logfile, + stderr=subprocess.STDOUT, + shell=False, close_fds=True + ) - # remember the popen object for the process; remember the open log file - n = struct() - n.proc = p - n.logfile = logfile - n.logfile_name = logfile_name + # remember the popen object for the process; remember the open log file + n = struct() + n.proc = p + n.logfile = logfile + n.logfile_name = logfile_name - # remember the process is running - self.all_procs[node.name] = n + # remember the process is running + self.all_procs[node.name] = n - return 'R' + return 'R' except Exception as e : log_traceback() @@ -262,7 +262,7 @@ class runner(object): # note the termination of the process at the end of the log file logfile = self.all_procs[node_name].logfile - logfile.seek(0,2) # end of file + logfile.seek(0, os.SEEK_END) # end of file logfile.write('\n%s exit=%s\n'%(datetime.datetime.now(),status)) logfile.close() -- cgit