mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fixes #4454 Make a temporary clean cnf file if unable to parse existing
This commit is contained in:
parent
1d090b5b25
commit
3c4dd618e4
1 changed files with 30 additions and 0 deletions
|
@ -134,6 +134,7 @@ password=n<_665{vS43y
|
||||||
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import getpass
|
import getpass
|
||||||
|
import tempfile
|
||||||
try:
|
try:
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -316,6 +317,32 @@ def config_get(config, section, option):
|
||||||
return strip_quotes(config.get(section, option))
|
return strip_quotes(config.get(section, option))
|
||||||
|
|
||||||
|
|
||||||
|
def _safe_cnf_load(config, path):
|
||||||
|
|
||||||
|
data = {'user':'', 'password':''}
|
||||||
|
|
||||||
|
# read in user/pass
|
||||||
|
f = open(path, 'r')
|
||||||
|
for line in f.readlines():
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith('user='):
|
||||||
|
data['user'] = line.split('=', 1)[1].strip()
|
||||||
|
if line.startswith('password=') or line.startswith('pass='):
|
||||||
|
data['password'] = line.split('=', 1)[1].strip()
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
# write out a new cnf file with only user/pass
|
||||||
|
fh, newpath = tempfile.mkstemp(prefix=path + '.')
|
||||||
|
f = open(newpath, 'wb')
|
||||||
|
f.write('[client]\n')
|
||||||
|
f.write('user=%s\n' % data['user'])
|
||||||
|
f.write('password=%s\n' % data['password'])
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
config.readfp(open(newpath))
|
||||||
|
os.remove(newpath)
|
||||||
|
return config
|
||||||
|
|
||||||
def load_mycnf():
|
def load_mycnf():
|
||||||
config = ConfigParser.RawConfigParser()
|
config = ConfigParser.RawConfigParser()
|
||||||
mycnf = os.path.expanduser('~/.my.cnf')
|
mycnf = os.path.expanduser('~/.my.cnf')
|
||||||
|
@ -325,6 +352,9 @@ def load_mycnf():
|
||||||
config.readfp(open(mycnf))
|
config.readfp(open(mycnf))
|
||||||
except (IOError):
|
except (IOError):
|
||||||
return False
|
return False
|
||||||
|
except:
|
||||||
|
config = _safe_cnf_load(config, 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:
|
||||||
|
|
Loading…
Reference in a new issue