From 9154b3cc571c2461010573f84b91fcf84830cf5a Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@fe.uni-lj.si>
Date: Wed, 16 Mar 2016 08:18:50 +0000
Subject: [PATCH] Improved reporting. Added per process ID possibilities

---
 python/trisurf/trisurf.py |   88 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 78 insertions(+), 10 deletions(-)

diff --git a/python/trisurf/trisurf.py b/python/trisurf/trisurf.py
index bf0809e..30c06bd 100644
--- a/python/trisurf/trisurf.py
+++ b/python/trisurf/trisurf.py
@@ -9,6 +9,10 @@
 from itertools import islice
 import mmap
 import shlex
+import psutil
+import time
+import datetime
+
 '''
 This is a trisurf instance manager written in python
 
@@ -20,7 +24,29 @@
 
 '''
 
+class FileContent:
+	def __init__(self,filename):
+		self.filename=filename
+		self.data=""
+		self.readfile()
 
+	def readfile(self):
+		try:
+			with open (self.filename, "r") as myfile:
+				self.data=myfile.read().replace('\n', '')
+		except:
+			pass
+
+
+	def writefile(self, data, mode='w'):
+		with open (self.filename, mode) as myfile:
+			myfile.write(data)
+
+	def getText(self):
+		return self.data
+
+	def __str__(self):
+		return self.getText()
 
 class Tape:
 	'''Has all the info on the tape'''
@@ -101,11 +127,8 @@
 		self.path=path
 		self.filename=filename
 		self.fullname=os.path.join(path,filename)
-		self.read()
+		self.fileOK=self.read()
 		return
-
-	def __str__(self):
-		return(str(self.fullname))
 
 	def exists(self):
 		if(os.path.isfile(self.fullname)):
@@ -140,14 +163,20 @@
 							n2=fields[1]
 						i=i+1
 			except:
-				print("Cannot read statistics file in "+self.fullname+"\n")
+				#print("Cannot read statistics file in "+self.fullname+"\n")
 				return(False)
 		else:
-			print("File "+self.fullname+" does not exists.\n")
+			#print("File "+self.fullname+" does not exists.\n")
 			return(False)
 
 		self.dT=(int(epoch2)-int(epoch1))/(int(n2)-int(n1))
+		self.last=n2
+		self.startDate=epoch1
 		return(True)
+
+	def __str__(self):
+		return(str(self.fullname))
+
 
 
 class Runner:
@@ -180,14 +209,34 @@
 		self.tape=Tape()
 		self.tape.setTape(tapetxt.text)
 
+	def getPID(self):
+		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
+		self.Dir.makeifnotexist()
+		try:
+			fp = open(os.path.join(self.Dir.fullpath(),'.lock'))
+		except IOError as e:
+			return 0 #file probably does not exist. e==2??
+		pid=fp.readline()
+		fp.close()
+		return pid
+
 	def getStatus(self):
-		return 0
+		pid=self.getPID()
+		if(pid==0):
+			return 0
+		if(psutil.pid_exists(int(pid))):
+			if psutil.Process(int(pid)).name=="trisurf":
+				return 1
+			else:
+				return 0
+		else:
+			return 0
 
 	def start(self):
 		if(self.getStatus()==0):
 			self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
 			self.Dir.makeifnotexist()
-			self.Dir.goto()
+#			self.Dir.goto()
 			print("Starting trisurf-ng executable at "+self.Dir.fullpath()+"\n")
 		else:
 			print("Process already running. Not starting\n")
@@ -212,8 +261,27 @@
 		return
 
 	def getStatistics(self, statfile="statistics.csv"):
-		self.statistics=Statistics("", statfile) # we are already in the running directory, so local path is needed!
-		return
+		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
+		self.statistics=Statistics(self.Dir.fullpath(), statfile)
+		self.Comment=FileContent(os.path.join(self.Dir.fullpath(),".comment"))
+		pid=self.getPID();
+		if(self.getStatus()):
+			statustxt="Running"
+		else:
+			statustxt="Stopped"
+			pid=""
+
+		if(self.statistics.fileOK):
+#			report=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate)))+"\t"+str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000)+" ETA\t"+"STATUS"
+			report=[time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(self.statistics.startDate))),str(datetime.timedelta(microseconds=(int(self.tape.config['iterations'])-int(self.statistics.last))*self.statistics.dT)*1000), statustxt, pid, str(self.Dir.fullpath()), self.Comment.getText()]
+		else:
+			report=["N/A","N/A\t",statustxt, pid, str(self.Dir.fullpath()), self.Comment.getText()]
+		return report
+
+	def writeComment(self, data):
+		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
+		self.Comment=FileContent(os.path.join(self.Dir.fullpath(),".comment"))
+		self.Comment.writefile(data,mode='w')
 
 	def __str__(self):
 		if(self.getStatus()==0):

--
Gitblit v1.9.3