This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
forked from prelz/BLAH
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from brianhlin/sw2628_pbs_pro_config
Fix python loading of blah.config
- Loading branch information
Showing
3 changed files
with
44 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,29 @@ | ||
"""Common functions for BLAH python scripts""" | ||
|
||
import os | ||
import subprocess | ||
from ConfigParser import RawConfigParser | ||
from io import StringIO | ||
|
||
def load_env(config_dir): | ||
"""Load blah.config into the environment""" | ||
load_config_path = os.path.join(config_dir, 'blah_load_config.sh') | ||
command = ['bash', '-c', 'source %s && env' % load_config_path] | ||
try: | ||
config_proc = subprocess.Popen(command, stdout=subprocess.PIPE) | ||
config_out, _ = config_proc.communicate() | ||
class BlahConfigParser(RawConfigParser, object): | ||
|
||
def __init__(self, path='/etc/blah.config'): | ||
# RawConfigParser requires ini-style [section headers] but since | ||
# blah.config is also used as a shell script we need to fake one | ||
self.header = 'blahp' | ||
with open(path) as f: | ||
config = f.read() | ||
vfile = StringIO(u'[%s]\n%s' % (self.header, config)) | ||
|
||
super(BlahConfigParser, self).__init__() | ||
# TODO: readfp() is replaced by read_file() in Python 3.2+ | ||
self.readfp(vfile) | ||
|
||
def items(self): | ||
return super(BlahConfigParser, self).items(self.header) | ||
|
||
def get(self, option): | ||
# ConfigParser happily includes quotes in value strings, which we | ||
# happily allow in /etc/blah.config. This causes failures when joining | ||
# paths, for example. | ||
return super(BlahConfigParser, self).get(self.header, option).strip('"\'') | ||
|
||
for line in config_out.splitlines(): | ||
(key, _, val) = line.partition('=') | ||
os.environ[key] = val | ||
except IOError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters