Trisurf Monte Carlo simulator
Miha
2016-07-15 c372c14d1f552a3219a733394141179c9e800683
commit | author | age
9f5ff5 1 import sys, getopt
SP 2 import tabulate
a3f6b7 3 import subprocess,re
dfa2df 4 import psutil
9f5ff5 5
5f4aea 6 def printHelp():
SP 7     print('Python module tsmgr accept following switches:\n')
8     print('tsmgr [-n process number] [-R] [-h] [-r] [-s] [-c comment text] [-a comment text]\n')
9     print('[-n process number]: number of process for which -s -r -c or -a switch apply. Should be placed before any other switch');
10     print('[-R]               : raw output for -s switch');
11     print('[-r]               : run process');
12     print('[-s]               : process status');
dfa2df 13     print('[-k]               : kill process');
5f4aea 14     print('[-c comment text]  : write new comment for process');
SP 15     print('[-a comment text]  : append additional comment for process');
16     print('[-h]               : print help');
17
18
a3f6b7 19 def getTrisurfVersion():
SP 20     p = subprocess.Popen('trisurf --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
21     lines=p.stdout.readlines()
22     version=re.findall(r'[0-9a-f]{7}(?:-dirty)?', lines[0].decode('ascii'))
23     p.wait()
24     if(len(version)):
25         return version[0]
26     else:
27         return "unknown version"
28
9f5ff5 29 def start(Runs):
SP 30     argv=sys.argv[1:]
31     processno=0
5f4aea 32     raw=False
9f5ff5 33     try:
dfa2df 34         opts, args = getopt.getopt(argv,"Ra:n:hrskc:")
9f5ff5 35     except getopt.GetoptError:
5f4aea 36         printHelp()
9f5ff5 37         sys.exit(2)
SP 38     for opt, arg in opts:
5f4aea 39         if opt == '-R':
SP 40             raw=True
41         elif opt == '-h':
42             printHelp()
9f5ff5 43             sys.exit()
SP 44         elif opt == '-r':
45             if processno:
46                 localRuns=[Runs[processno-1]]
47             else:
48                 localRuns=Runs
49             for run in localRuns:
50                 run.start()
51         elif opt == '-s':
52             report=[]
53             i=1
54             if processno:
55                 localRuns=[Runs[processno-1]]
56             else:
57                 localRuns=Runs
58             for run in localRuns:
59                 line=run.getStatistics()
60                 line.insert(0,i)
61                 report.append(line)
62                 i=i+1
5f4aea 63             if(raw):
SP 64                 print(report)
65             else:
66                 print ("\n\nTrisurf running processes report\n")
67                 print (tabulate.tabulate(report,headers=["Run no.", "Run start time", "ETA", "Status", "PID", "Path", "Comment"], tablefmt='fancy_grid'))
9f5ff5 68         elif opt == '-n':
SP 69             processno=int(arg)
70             if processno<1 or processno>len(Runs) :
71                 processno=0
72         elif opt == '-c':
73             comment = arg
74             if processno:
75                 Runs[processno-1].writeComment(arg)
76         elif opt == '-a':
77             comment = arg
78             if processno:
79                 Runs[processno-1].writeComment("\n"+arg, 'a')
80
dfa2df 81         elif opt == '-k':
SP 82             if processno:
f06659 83                 Runs[processno-1].stop()
9f5ff5 84         else:
5f4aea 85             printHelp()
9f5ff5 86             sys.exit(2)
SP 87
88
89
90
91
92