Skip to content
This repository was archived by the owner on Oct 10, 2019. It is now read-only.

Commit 87286a7

Browse files
committed
Properly load blah.config (SOFTWARE-2628)
Previous method didn't work because the options in blah.config weren't ever exported so they never made it ot the environment
1 parent 48485cd commit 87286a7

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/scripts/blah.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
"""Common functions for BLAH python scripts"""
22

33
import os
4-
import subprocess
4+
from ConfigParser import RawConfigParser
5+
from io import StringIO
56

6-
def load_env(config_dir):
7-
"""Load blah.config into the environment"""
8-
load_config_path = os.path.join(config_dir, 'blah_load_config.sh')
9-
command = ['bash', '-c', 'source %s && env' % load_config_path]
10-
try:
11-
config_proc = subprocess.Popen(command, stdout=subprocess.PIPE)
12-
config_out, _ = config_proc.communicate()
7+
class BlahConfigParser(RawConfigParser, object):
8+
9+
def __init__(self, path='/etc/blah.config'):
10+
# RawConfigParser requires ini-style [section headers] but since
11+
# blah.config is also used as a shell script we need to fake one
12+
self.header = 'blahp'
13+
with open(path) as f:
14+
config = f.read()
15+
vfile = StringIO(u'[%s]\n%s' % (self.header, config))
16+
17+
super(BlahConfigParser, self).__init__()
18+
# TODO: readfp() is replaced by read_file() in Python 3.2+
19+
self.readfp(vfile)
20+
21+
def items(self):
22+
return super(BlahConfigParser, self).items(self.header)
23+
24+
def get(self, option):
25+
return super(BlahConfigParser, self).get(self.header, option)
1326

14-
for line in config_out.splitlines():
15-
(key, _, val) = line.partition('=')
16-
os.environ[key] = val
17-
except IOError:
18-
pass

src/scripts/pbs_status.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def qstat(jobid=""):
233233
starttime = time.time()
234234
log("Starting qstat.")
235235
command = (qstat_bin, '-f')
236-
if os.environ.get('pbs_pro').lower() != 'yes':
236+
if config.get('pbs_pro').lower() != 'yes':
237237
command += ('-1',) # -1 conflicts with -f in PBS Pro
238238
if jobid:
239239
command += (jobid,)
@@ -358,7 +358,7 @@ def get_qstat_location():
358358
if _qstat_location_cache != None:
359359
return _qstat_location_cache
360360
try:
361-
cmd = os.path.join(os.environ['pbs_binpath'], 'qstat')
361+
cmd = os.path.join(config.get('pbs_binpath'), 'qstat')
362362
except KeyError:
363363
cmd = 'which qstat'
364364
child_stdout = os.popen(cmd)
@@ -527,7 +527,8 @@ def main():
527527
jobid = jobid_arg.split("/")[-1].split(".")[0]
528528

529529
config_dir = os.path.dirname(os.path.abspath(__file__))
530-
blah.load_env(config_dir)
530+
global config
531+
config = blah.BlahConfigParser()
531532

532533
log("Checking cache for jobid %s" % jobid)
533534
cache_contents = None

src/scripts/slurm_status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def get_slurm_location(program):
336336
if _slurm_location_cache != None:
337337
return os.path.join(_slurm_location_cache, program)
338338
try:
339-
cmd = os.path.join(os.environ['slurm_binpath'], program)
339+
cmd = os.path.join(config.get('slurm_binpath'), program)
340340
except KeyError:
341341
cmd = 'which %s' % program
342342
child_stdout = os.popen(cmd)

0 commit comments

Comments
 (0)