Last active
December 16, 2015 17:09
-
-
Save eloyz/5468339 to your computer and use it in GitHub Desktop.
We store postgres connection strings and sometimes I need to dump the sql from a database. This python files prints out the dump command for me when I pass the postgres connection string. Example: `python pg_dump.py postgres://<the rest of the string>'
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 | |
import re | |
import sys | |
from functools import partial | |
def f(host=u'', x=None): | |
""" | |
Returns string | |
""" | |
un = x.group(1) | |
host = host or x.group(3) | |
db_name = x.group(4) | |
return 'pg_dump -h %s -U %s %s > %s.sql' % (host, un, db_name, db_name) | |
if __name__ == '__main__': | |
""" | |
Get arguments then print pg_dump command | |
""" | |
s = sys.argv[1] # string | |
h = sys.argv[2] # host | |
p = 'postgres://(\w+):(\w+)@(\w+)/(\w+)' | |
f = partial(f, h) | |
c = re.sub(p, f, s) # pattern, fn, string | |
print c # print command | |
try: | |
os.system(c) | |
except (KeyboardInterrupt, SystemExit) as e: | |
# does nothing for now | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment