DAMASK_EICMD/lib/damask/environment.py

52 lines
1.8 KiB
Python
Raw Normal View History

# $Id$
import os,sys,string,re,subprocess,shlex
2011-12-14 01:32:26 +05:30
class Environment():
__slots__ = [ \
'rootRelation',
2014-01-17 07:07:01 +05:30
'options',
2011-12-14 01:32:26 +05:30
]
def __init__(self,rootRelation = '.'):
2014-01-17 02:59:39 +05:30
self.rootRelation = rootRelation
self.options = {}
self.get_options()
2011-12-14 01:32:26 +05:30
def relPath(self,relative = '.'):
return os.path.join(self.rootDir(),relative)
def rootDir(self):
2014-01-24 18:00:27 +05:30
return os.path.normpath(os.path.join(os.path.realpath(__file__),'../../../'))
2014-01-17 02:59:39 +05:30
def get_options(self):
2014-01-24 18:45:01 +05:30
if os.path.isfile(os.path.join(os.getenv('HOME'),'.damask/damask.conf')):
configFile = os.path.join(os.getenv('HOME'),'.damask/damask.conf')
else:
configFile = '/etc/damask.conf'
with open(self.relPath(configFile)) as file:
2014-01-17 02:59:39 +05:30
for line in file:
l = line.strip()
if not (l.startswith('#') or l == ''):
items = l.split('=') + ['','']
if items[1] != '': # nothing specified
self.options[items[0].upper()] = items[1]
2014-01-17 02:59:39 +05:30
def isAvailable(self,software,Nneeded =-1):
licensesNeeded = {'abaqus' :5,
'standard':5,
'explicit':5}
2014-01-17 02:59:39 +05:30
if Nneeded == -1: Nneeded = licensesNeeded[software]
cmd = """ ssh mulicense2 "/Stat_Flexlm | grep 'Users of %s: ' | cut -d' ' -f7,13" """%software
process = subprocess.Popen(shlex.split(cmd),stdout = subprocess.PIPE,stderr = subprocess.PIPE)
licenses = map(int, process.stdout.readline().split())
try:
2014-01-17 02:59:39 +05:30
if licenses[0]-licenses[1] >= Nneeded:
return 0
else:
2014-01-17 02:59:39 +05:30
print(licenses[1] + Nneeded - licenses[0], 'missing licenses for %s'%software)
return licenses[1] + Nneeded - licenses[0]
except IndexError:
print('Could not retrieve license information for %s'%software)
return 127