Created
March 18, 2015 14:27
-
-
Save Duologic/74000d450bbb55e7c138 to your computer and use it in GitHub Desktop.
Load Cabot env-file into Python
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
import os, pickle | |
settings_dir = os.path.dirname(__file__) | |
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir)) | |
def readenvfile(filename): | |
if not os.path.exists(filename): | |
raise IOError("file {} not found".format(filename)) | |
command = 'export $(cat {}|grep -v -e "^#"|xargs)'.format(filename) | |
dump = '/usr/bin/python -c "import os,pickle;print pickle.dumps(os.environ)"' | |
penv = os.popen('%s && %s' %(command,dump)) | |
env = pickle.loads(penv.read()) | |
os.environ = env | |
from django.core.exceptions import ImproperlyConfigured | |
try: | |
filename = "{}/{}".format(PROJECT_ROOT, os.environ.get('CABOT_CONFIG')) | |
print filename | |
readenvfile(filename) | |
except IOError: | |
raise ImproperlyConfigured('No configuration file present.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment