Skip to content

Instantly share code, notes, and snippets.

@eloyz
Last active December 16, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eloyz/5468339 to your computer and use it in GitHub Desktop.
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>'
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