Skip to content

Commit d63fe38

Browse files
committed
コマンドライン引数の指定を mysql client に似せる.
1 parent 09c6595 commit d63fe38

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

myprofiler.py

+40-23
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,14 @@
2929
CMD_PROCESSLIST = "show full processlist"
3030

3131

32-
def connect(conf='~/.my.cnf', section='client'):
33-
"""
34-
connect to MySQL from conf file.
35-
"""
36-
parser = SafeConfigParser()
37-
parser.read([os.path.expanduser(conf)])
38-
32+
def connect(cnf):
3933
args = {}
40-
41-
args['host'] = parser.get(section, 'host')
42-
args['user'] = parser.get(section, 'user')
43-
args['passwd'] = parser.get(section, 'password')
44-
args['charset'] = 'utf8'
45-
if parser.has_option(section, 'port'):
46-
args['port'] = int(parser.get(section, 'port'))
34+
args['host'] = cnf.get('host', 'localhost')
35+
args['user'] = cnf.get('user', '')
36+
args['passwd'] = cnf.get('password', '')
37+
args['charset'] = cnf.get('default-character-set', 'utf8')
38+
if 'port' in cnf:
39+
args['port'] = int(get('port'))
4740
return MySQLdb.connect(**args)
4841

4942

@@ -74,21 +67,36 @@ def normalize_query(row):
7467
return row
7568

7669

70+
def read_mycnf(extra_file=None, group_suffix=''):
71+
cnf_files = [os.path.expanduser('~/.my.cnf')]
72+
if extra_file is not None:
73+
if not os.path.isfile(extra_file):
74+
print >>sys.stderr, "[warn]", extra_file, "is not exists."
75+
else:
76+
cnf_files += [extra_file]
77+
78+
parser = SafeConfigParser()
79+
parser.read(cnf_files)
80+
81+
cnf = dict(parser.items('client'))
82+
if group_suffix:
83+
cnf.update(parser.items('client' + group_suffix))
84+
return cnf
85+
86+
7787
def build_option_parser():
7888
parser = OptionParser()
7989
parser.add_option(
8090
'-o', '--out',
81-
help="write raw queries to this file.",
91+
help="Write raw queries to this file.",
8292
)
8393
parser.add_option(
84-
'-c', '--config',
85-
help="read MySQL configuration from. (default: '~/.my.cnf'",
86-
default='~/.my.cnf'
94+
'-e', '--defaults-extra-file', dest='extra_file',
95+
help="Read MySQL configuration from this file additionaly",
8796
)
8897
parser.add_option(
89-
'-s', '--section',
90-
help="read MySQL configuration from this section. (default: '[DEFAULT]')",
91-
default="DEFAULT"
98+
'-s', '--defaults-group-suffix', dest='group_suffix',
99+
help="Read MySQL configuration from this section additionally",
92100
)
93101
parser.add_option(
94102
'-n', '--num-summary', metavar="K",
@@ -100,6 +108,8 @@ def build_option_parser():
100108
help="Interval of executing show processlist [sec] (default: 1.0)",
101109
type="float", default=1.0
102110
)
111+
parser.add_option('-u', '--user')
112+
parser.add_option('-p', '--password')
103113
return parser
104114

105115

@@ -114,12 +124,18 @@ def show_summary(counter, limit, file=sys.stdout):
114124
def main():
115125
parser = build_option_parser()
116126
opts, args = parser.parse_args()
127+
outfile = None
117128

118129
try:
119-
outfile = None
130+
cnf = read_mycnf(opts.extra_file, opts.group_suffix)
131+
if opts.user:
132+
cnf['user'] = opts.user
133+
if opts.password:
134+
cnf['password'] = opts.password
135+
con = connect(cnf)
136+
120137
if opts.out:
121138
outfile = open(opts.out, "w")
122-
con = connect(opts.config, opts.section)
123139
except Exception, e:
124140
parser.error(e)
125141

@@ -140,6 +156,7 @@ def main():
140156
if outfile:
141157
print >>outfile, "\nSummary"
142158
show_summary(counter, opts.num_summary, outfile)
159+
outfile.close()
143160

144161

145162
if __name__ == '__main__':

0 commit comments

Comments
 (0)