Merge pull request #2496 from lorin/mycnf-quotes

Strip quotes when parsing my.cnf
reviewable/pr18780/r1
Michael DeHaan 12 years ago
commit b31c133739

@ -129,6 +129,37 @@ def db_create(cursor, db, encoding, collation):
res = cursor.execute(query) res = cursor.execute(query)
return True return True
def strip_quotes(s):
""" Remove surrounding single or double quotes
>>> print strip_quotes('hello')
hello
>>> print strip_quotes('"hello"')
hello
>>> print strip_quotes("'hello'")
hello
>>> print strip_quotes("'hello")
'hello
"""
single_quote = "'"
double_quote = '"'
if s.startswith(single_quote) and s.endswith(single_quote):
s = s.strip(single_quote)
elif s.startswith(double_quote) and s.endswith(double_quote):
s = s.strip(double_quote)
return s
def config_get(config, section, option):
""" Calls ConfigParser.get and strips quotes
See: http://dev.mysql.com/doc/refman/5.0/en/option-files.html
"""
return strip_quotes(config.get(section, option))
def load_mycnf(): def load_mycnf():
config = ConfigParser.RawConfigParser() config = ConfigParser.RawConfigParser()
mycnf = os.path.expanduser('~/.my.cnf') mycnf = os.path.expanduser('~/.my.cnf')
@ -141,14 +172,14 @@ def load_mycnf():
# We support two forms of passwords in .my.cnf, both pass= and password=, # We support two forms of passwords in .my.cnf, both pass= and password=,
# as these are both supported by MySQL. # as these are both supported by MySQL.
try: try:
passwd = config.get('client', 'password') passwd = config_get(config, 'client', 'password')
except (ConfigParser.NoOptionError): except (ConfigParser.NoOptionError):
try: try:
passwd = config.get('client', 'pass') passwd = config_get(config, 'client', 'pass')
except (ConfigParser.NoOptionError): except (ConfigParser.NoOptionError):
return False return False
try: try:
creds = dict(user=config.get('client', 'user'),passwd=passwd) creds = dict(user=config_get(config, 'client', 'user'),passwd=passwd)
except (ConfigParser.NoOptionError): except (ConfigParser.NoOptionError):
return False return False
return creds return creds

@ -252,6 +252,38 @@ def privileges_grant(cursor, user,host,db_table,priv):
query = query + " WITH GRANT OPTION" query = query + " WITH GRANT OPTION"
cursor.execute(query) cursor.execute(query)
def strip_quotes(s):
""" Remove surrounding single or double quotes
>>> print strip_quotes('hello')
hello
>>> print strip_quotes('"hello"')
hello
>>> print strip_quotes("'hello'")
hello
>>> print strip_quotes("'hello")
'hello
"""
single_quote = "'"
double_quote = '"'
if s.startswith(single_quote) and s.endswith(single_quote):
s = s.strip(single_quote)
elif s.startswith(double_quote) and s.endswith(double_quote):
s = s.strip(double_quote)
return s
def config_get(config, section, option):
""" Calls ConfigParser.get and strips quotes
See: http://dev.mysql.com/doc/refman/5.0/en/option-files.html
"""
return strip_quotes(config.get(section, option))
def load_mycnf(): def load_mycnf():
config = ConfigParser.RawConfigParser() config = ConfigParser.RawConfigParser()
mycnf = os.path.expanduser('~/.my.cnf') mycnf = os.path.expanduser('~/.my.cnf')
@ -264,16 +296,16 @@ def load_mycnf():
# We support two forms of passwords in .my.cnf, both pass= and password=, # We support two forms of passwords in .my.cnf, both pass= and password=,
# as these are both supported by MySQL. # as these are both supported by MySQL.
try: try:
passwd = config.get('client', 'password') passwd = config_get(config, 'client', 'password')
except (ConfigParser.NoOptionError): except (ConfigParser.NoOptionError):
try: try:
passwd = config.get('client', 'pass') passwd = config_get(config, 'client', 'pass')
except (ConfigParser.NoOptionError): except (ConfigParser.NoOptionError):
return False return False
# If .my.cnf doesn't specify a user, default to user login name # If .my.cnf doesn't specify a user, default to user login name
try: try:
user = config.get('client', 'user') user = config_get(config, 'client', 'user')
except (ConfigParser.NoOptionError): except (ConfigParser.NoOptionError):
user = getpass.getuser() user = getpass.getuser()
creds = dict(user=user,passwd=passwd) creds = dict(user=user,passwd=passwd)

Loading…
Cancel
Save