From 9f5ff50a40f78ecdf25ec6bcd4e5490152db81d6 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Fri, 13 May 2016 07:56:43 +0000
Subject: [PATCH] Changed the way tsmgr is called

---
 python/trisurf/trisurf.py |   59 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/python/trisurf/trisurf.py b/python/trisurf/trisurf.py
index 34c5ca8..9018555 100644
--- a/python/trisurf/trisurf.py
+++ b/python/trisurf/trisurf.py
@@ -12,7 +12,7 @@
 import psutil
 import time
 import datetime
-
+import subprocess
 
 # Process status
 TS_NOLOCK=0 # lock file does not exist
@@ -142,7 +142,9 @@
 		'''Method makeifnotexist() creates directory if it does not exist.'''
 		if(self.exists()==0):
 			self.make()
-		return
+			return True
+		else:
+			return False
 
 	def remove(self):
 		'''Method remove() removes directory recursively. WARNING! No questions asked.'''
@@ -246,11 +248,13 @@
 	'''
 	Class Runner consists of a single running or terminated instance of the trisurf. It manages starting, stopping, verifying the running process and printing the reports of the configured instances.
 	'''
-	def __init__(self, subdir='run0', tape='', snapshot=''):
+	def __init__(self, subdir='run0', tape=None, snapshot=None, runArgs=[]):
 		self.subdir=subdir
-		if(tape!=''):
+		self.runArgs=runArgs
+		self.fromSnapshot=False
+		if(tape!=None):
 			self.initFromTape(tape)
-		if(snapshot!=''):
+		if(snapshot!=None):
 			self.initFromSnapshot(snapshot)
 		return
 
@@ -258,6 +262,7 @@
 	def initFromTape(self, tape):
 		self.tape=Tape()
 		self.tape.readTape(tape)
+		self.tapeFile=tape
 
 	def initFromSnapshot(self, snapshotfile):
 		try:
@@ -265,16 +270,17 @@
 		except:
 			print("Error reading snapshot file")
 			exit(1)
-
+		self.fromSnapshot=True
+		self.snapshotFile=snapshotfile
 		root = tree.getroot()
 		tapetxt=root.find('tape')
 		version=root.find('trisurfversion')
 		self.tape=Tape()
 		self.tape.setTape(tapetxt.text)
-
+		
 	def getPID(self):
 		self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
-		self.Dir.makeifnotexist()
+		#self.Dir.makeifnotexist()
 		try:
 			fp = open(os.path.join(self.Dir.fullpath(),'.lock'))
 		except IOError as e:
@@ -289,8 +295,8 @@
 			return TS_NOLOCK
 		if(psutil.pid_exists(int(pid))):
 			proc= psutil.Process(int(pid))
-			if proc.name=="trisurf":
-				if proc.status=="stopped":
+			if proc.name()=="trisurf":
+				if proc.status()=="stopped":
 					return TS_STOPPED
 				else:
 					return TS_RUNNING
@@ -302,9 +308,36 @@
 	def start(self):
 		if(self.getStatus()==0):
 			self.Dir=Directory(maindir=self.maindir,simdir=self.subdir)
-			self.Dir.makeifnotexist()
-#			self.Dir.goto()
+#Symlinks tape file to the directory or create tape file from snapshot in the direcory...
+			if(self.Dir.makeifnotexist()):
+				if(self.fromSnapshot==False):
+					try:
+						os.symlink(os.path.abspath(self.tapeFile), self.Dir.fullpath()+"/tape")
+					except:
+						print("Error while symlinking "+os.path.abspath(self.tapeFile)+" to "+self.Dir.fullpath()+"/tape")
+						exit(1)
+				else:
+					try:
+						with open (os.path.join(self.Dir.fullpath(),"tape"), "w") as myfile:
+							myfile.write("#This is automatically generated tape file from snapshot\n")
+							myfile.write(str(self.tape))
+					except:
+						print("Error -- cannot make tapefile  "+ os.path.join(self.Dir.fullpath(),"tape")+" from the snapshot in the running directory")
+						exit(1)
+					try:
+						os.symlink(os.path.abspath(self.snapshotFile), os.path.join(self.Dir.fullpath(),self.snapshotFile))
+					except:
+						print("Error while symlinking "+os.path.abspath(self.snapshotFile)+" to "+os.path.join(self.Dir.fullpath(),self.snapshotFile))
+	
+			cwd=Directory(maindir=os.getcwd())
+			self.Dir.goto()
 			print("Starting trisurf-ng executable at "+self.Dir.fullpath()+"\n")
+			if(self.fromSnapshot==True):
+				params=["trisurf", "--restore-from-vtk",self.snapshotFile]+self.runArgs
+			else:
+				params=["trisurf"]+self.runArgs
+			subprocess.Popen (params, stdout=subprocess.DEVNULL)
+			cwd.goto()
 		else:
 			print("Process already running. Not starting\n")
 		return
@@ -342,7 +375,7 @@
 			statustxt="Running"
 
 		if(self.statistics.fileOK):
-			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()]
+			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)*1000000), 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

--
Gitblit v1.9.3