Skip to content

Instantly share code, notes, and snippets.

@alaniwi
Last active October 16, 2017 10:06
Show Gist options
  • Save alaniwi/0b777fa37e9bf7824eeaa6b9e40adfd5 to your computer and use it in GitHub Desktop.
Save alaniwi/0b777fa37e9bf7824eeaa6b9e40adfd5 to your computer and use it in GitHub Desktop.
A convenience script to run psql with some variables set based on the esgf.properties
#!/usr/bin/env python
import os
import re
def read_properties(file="/esg/config/esgf.properties"):
"returns the esgf.properties as a dictionary"
d = {}
with open(file) as f:
for line in f.readlines():
m = re.match("([^#=]+)\s*=\s*(.*)$", line)
if m:
d[m.group(1)] = m.group(2)
return d
def set_pg_env_vars():
"set certain env vars based on values in the esgf.properties file"
mappings = {
"db.database": "PGDATABASE",
"db.host": "PGHOST",
"db.port": "PGPORT",
"db.user": "PGUSER"
}
for key, val in read_properties().items():
if key in mappings:
os.environ[mappings[key]] = val
if __name__ == '__main__':
set_pg_env_vars()
os.system("psql")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment