forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtctl_go_reference.py
executable file
·806 lines (739 loc) · 35.3 KB
/
vtctl_go_reference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#!/usr/bin/python
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
"""
import json
import os
import optparse
import re
# TODO: Handle angle brackets that appear in command definitions --
# e.g. ChangeSlaveType
# Traverse directory to get list of all files in the directory.
def get_all_files(directory, filenames):
os.chdir(directory)
for path, dirs, files in os.walk('.'):
for filename in files:
filenames[os.path.join(path, filename)] = True
return filenames
# This needs to produce the same anchor ID as the Markdown processor.
def anchor_id(heading):
return heading.lower().replace(' ', '-').replace(',', '')
def write_header(doc, commands):
doc.write('This reference guide explains the commands that the ' +
'<b>vtctl</b> tool supports. **vtctl** is a command-line tool ' +
'used to administer a Vitess cluster, and it allows a human ' +
'or application to easily interact with a Vitess ' +
'implementation.\n\nCommands are listed in the ' +
'following groups:\n\n')
for group in sorted(commands):
group_link = anchor_id(group)
doc.write('* [' + group + '](#' + group_link + ')\n')
doc.write('\n\n')
def write_footer(doc):
doc.write(' </body>\n')
doc.write('</html>\n')
def create_reference_doc(root_directory, commands, arg_definitions):
doc = open(root_directory + '/doc/vtctlReference.md', 'w')
write_header(doc, commands)
not_found_arguments = {}
for group in sorted(commands):
doc.write('## ' + group + '\n\n');
for command in sorted(commands[group]):
if ('definition' in commands[group][command] and
commands[group][command]['definition'] != '' and
re.search(r'HIDDEN', commands[group][command]['definition'])):
print '\n\n****** ' + command + ' is hidden *******\n\n'
continue
command_link = anchor_id(command)
doc.write('* [' + command + '](#' + command_link + ')\n')
doc.write('\n')
for command in sorted(commands[group]):
if ('definition' in commands[group][command] and
commands[group][command]['definition'] != '' and
re.search(r'HIDDEN', commands[group][command]['definition'])):
continue
doc.write('### ' + command + '\n\n');
if ('definition' in commands[group][command] and
commands[group][command]['definition'] != ''):
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('<', '<'))
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('>', '>'))
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('</a>',
'</a>'))
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('<a href',
'<a href'))
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('">', '">'))
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('"<br>',
'<br>'))
commands[group][command]['definition'] = (
commands[group][command]['definition'].replace('\\n', '<br><br>'))
doc.write(commands[group][command]['definition'] + '\n\n')
if ('arguments' in commands[group][command] and
commands[group][command]['arguments']):
doc.write('#### Example\n\n')
arguments = commands[group][command]['arguments'].strip().strip(
'"').replace('<', '<')
arguments = arguments.strip().replace('>', '>')
doc.write('<pre class="command-example">%s %s</pre>\n\n' %
(command, arguments))
if ('argument_list' in commands[group][command] and
'flags' in commands[group][command]['argument_list']):
flag_text = ''
for command_flag in sorted(
commands[group][command]['argument_list']['flags']):
flag = (
commands[group][command]['argument_list']['flags'][command_flag])
flag_text += ('| %s | %s | %s |\n' % (command_flag, flag['type'],
flag['definition']))
if flag_text:
#flag_text = '<a name="' + command + '-flags"></a>\n\n' +
flag_text = ('| Name | Type | Definition |\n' +
'| :-------- | :--------- | :--------- |\n' +
flag_text + '\n')
doc.write('#### Flags\n\n' + flag_text + '\n')
if ('argument_list' in commands[group][command] and
'args' in commands[group][command]['argument_list']):
arg_text = ''
for arg in commands[group][command]['argument_list']['args']:
if 'name' in arg:
arg_name = arg['name']
new_arg_name = arg['name'].replace('<', '').replace('>', '')
if (new_arg_name[0:len(new_arg_name) - 1] in arg_definitions and
re.search(r'\d', new_arg_name[-1])):
arg_name = '<' + new_arg_name[0:len(new_arg_name) - 1] + '>'
arg_name = arg_name.strip().replace('<', 'START_CODE_TAG<')
arg_name = arg_name.strip().replace('>', '>END_CODE_TAG')
arg_text += '* ' + arg_name + ' – '
if 'required' in arg and arg['required']:
arg_text += 'Required.'
else:
arg_text += 'Optional.'
arg_values = None
temp_name = arg['name'].replace('<', '').replace('>', '')
if temp_name in arg_definitions:
arg_text += ' ' + arg_definitions[temp_name]['description']
if 'list_items' in arg_definitions[temp_name]:
arg_values = arg_definitions[temp_name]['list_items']
# Check if the argument name ends in a digit to catch things like
# keyspace1 being used to identify the first in a list of keyspaces.
elif (temp_name[0:len(temp_name) - 1] in arg_definitions and
re.search(r'\d', temp_name[-1])):
arg_length = len(temp_name) - 1
arg_text += (' ' +
arg_definitions[temp_name[0:arg_length]]['description'])
if 'list_items' in arg_definitions[temp_name[0:arg_length]]:
arg_values = (
arg_definitions[temp_name[0:arg_length]]['list_items'])
else:
not_found_arguments[arg['name']] = True
if arg_values:
arg_text += '\n\n'
for arg_value in sorted(arg_values, key=lambda k: k['value']):
arg_text += (' * <code>' + arg_value['value'] + '</code> ' +
'– ' + arg_value['definition'] + '\n')
arg_text += '\n\n'
if 'multiple' in arg:
separation = 'space'
if 'hasComma' in arg:
separation = 'comma'
arg_text += (' To specify multiple values for this argument, ' +
'separate individual values with a ' +
separation + '.')
arg_text += '\n'
if arg_text:
arg_text = arg_text.replace('START_CODE_TAG', '<code>')
arg_text = arg_text.replace('END_CODE_TAG', '</code>')
doc.write('#### Arguments\n\n' +
arg_text +
'\n')
if 'errors' in commands[group][command]:
errors_text = ''
if 'ARG_COUNT' in commands[group][command]['errors']:
error = commands[group][command]['errors']['ARG_COUNT']
message = re.sub(str(command), '<' + command + '>', error['message'])
#print 'message here'
#print message
#message = error['message'].replace(command, '<' + command + '>')
message = message.replace('<', 'START_CODE_TAG<')
message = message.replace('>', '>END_CODE_TAG')
errors_text += '* ' + message + ' '
if (error['exact_count'] and
len(error['exact_count']) == 1 and
error['exact_count'][0] == '1'):
errors_text += ('This error occurs if the command is not called ' +
'with exactly one argument.')
elif error['exact_count'] and len(error['exact_count']) == 1:
errors_text += ('This error occurs if the command is not called ' +
'with exactly ' + error['exact_count'][0] + ' ' +
'arguments.')
elif error['exact_count']:
allowed_error_counts = ' or '.join(error['exact_count'])
errors_text += ('This error occurs if the command is not called ' +
'with exactly ' + allowed_error_counts + ' ' +
'arguments.')
elif error['min_count'] and error['max_count']:
errors_text += ('This error occurs if the command is not called ' +
'with between ' + error['min_count'] + ' and ' +
error['max_count'] + ' arguments.')
elif error['min_count'] and error['min_count'] == '1':
errors_text += ('This error occurs if the command is not called ' +
'with at least one argument.')
elif error['min_count']:
errors_text += ('This error occurs if the command is not called ' +
'with at least ' + error['min_count'] + ' ' +
'arguments.')
elif error['max_count']:
errors_text += ('This error occurs if the command is not called ' +
'with more than ' + error['max_count'] + ' ' +
'arguments.')
errors_text += '\n'
if 'other' in commands[group][command]['errors']:
#print 'other errors'
#print commands[group][command]['errors']
for error in commands[group][command]['errors']['other']:
error = re.sub(str(command), '<' + command + '>', error)
if ('argument_list' in commands[group][command] and
'flags' in commands[group][command]['argument_list']):
for command_flag in sorted(
commands[group][command]['argument_list']['flags']):
error = re.sub(str(command_flag), '<' + command_flag + '>',
error)
error = error.replace('<', 'START_CODE_TAG<')
error = error.replace('>', '>END_CODE_TAG')
errors_text += '* ' + error + '\n'
if errors_text:
errors_text = errors_text.replace('START_CODE_TAG', '<code>')
errors_text = errors_text.replace('END_CODE_TAG', '</code>')
doc.write('#### Errors\n\n' + errors_text)
doc.write('\n\n')
#write_footer(doc)
doc.close()
#print json.dumps(not_found_arguments, sort_keys=True, indent=4)
return
def parse_arg_list(arguments, current_command):
last_char = ''
find_closing_square_bracket = False
has_comma = False
has_multiple = False
is_optional_argument = False
is_required_argument = False
current_argument = ''
new_arg_list = []
arg_count = 0
char_count = 1
for char in arguments:
if (last_char == '' or last_char == ' ') and char == '[':
find_closing_square_bracket = True
elif (last_char == '[' and
find_closing_square_bracket and
char == '<'):
is_optional_argument = True
current_argument += char
elif find_closing_square_bracket:
if char == ']':
if is_optional_argument:
new_arg_list.append({'name': current_argument,
'has_comma': has_comma,
'has_multiple': has_multiple,
'required': False})
arg_count += 1
find_closing_square_bracket = False
current_argument = ''
has_comma = False
has_multiple = False
elif is_optional_argument:
if char == ',':
has_comma = True
elif last_char == '.' and char == '.':
has_multiple = True
elif not has_comma:
current_argument += char
elif char == '<' and (last_char == '' or last_char == ' '):
is_required_argument = True
current_argument += char
elif char == ',':
has_comma = True
if last_char == '>':
new_arg_list[arg_count - 1]['hasComma'] = True
has_comma = False
elif is_required_argument:
current_argument += char
if char == '>' and current_argument:
if char_count == len(arguments[0]):
new_arg_list.append({'name': current_argument,
'required': True})
arg_count += 1
is_required_argument = False
current_argument = ''
else:
next_char = 'x'
if current_command == 'Resolve':
if char_count < len(arguments[0]):
next_char = arguments[0][char_count:char_count + 1]
if next_char and not next_char == '.' and not next_char == ':':
new_arg_list.append({'name': current_argument,
'required': True})
arg_count += 1
is_required_argument = False
current_argument = ''
elif (arg_count > 0 and
current_argument == '' and
last_char == '.' and
'multiple' in new_arg_list[arg_count - 1] and
char == ' '):
current_argument = ''
elif (arg_count > 0 and
current_argument == '' and
last_char == '.' and
char == '.'):
new_arg_list[arg_count - 1]['multiple'] = True
char_count += 1
last_char = char
return new_arg_list
def get_group_name_from_variable(file_path, variable_name):
vtctl_go_file = open(file_path, 'rU')
vtctl_go_data = vtctl_go_file.readlines()
vtctl_go_file.close()
for line in vtctl_go_data:
regex = r'const\s*' + re.escape(variable_name) + r'\s*=\s*\"([^\"]+)\"'
if re.search(regex, line):
return line.split('=')[1].strip().strip('"')
return variable_name
def main(root_directory):
arg_definitions = {}
commands = {}
command_groups = {}
error_counts = {}
functions = {}
# Read the .go files in the /vitess/go/vt/vtctl/ directory
vtctl_dir_path = root_directory + '/go/vt/vtctl/'
go_files = next(os.walk(vtctl_dir_path))[2]
for path in go_files:
if not path.endswith('.go'):
continue
vtctl_go_path = vtctl_dir_path + path
vtctl_go_file = open(vtctl_go_path, 'rU')
vtctl_go_data = vtctl_go_file.readlines()
vtctl_go_file.close()
add_command_syntax = False
get_commands = False
get_argument_definitions = False
get_wrong_arg_count_error = False
get_group_name = False
current_command_argument = ''
current_command_argument_value = ''
current_command = ''
current_function = ''
is_func_init = False
is_flag_section = False
# treat func init() same as var commands
# treat addCommand("Group Name"... same as command {... in vtctl.go group
# Reformat Generic Help command to same format as commands in backup.go
# and reparent.go.
# Add logic to capture command data from those commands.
for line in vtctl_go_data:
# skip comments and empty lines
if line.strip() == '' or line.strip().startswith('//'):
continue
if is_func_init and not is_flag_section and re.search(r'^if .+ {', line.strip()):
is_flag_section = True
elif is_func_init and not is_flag_section and line.strip() == 'servenv.OnRun(func() {':
pass
elif is_func_init and is_flag_section and line.strip() == 'return':
pass
elif is_func_init and is_flag_section and line.strip() == '}':
is_flag_section = False
elif is_func_init and (line.strip() == '}' or line.strip() == '})'):
is_func_init = False
elif get_commands:
# This line precedes a command group's name, e.g. "Tablets" or "Shards."
# Capture the group name on the next line.
if line.strip() == '{':
get_group_name = True
# Capture the name of a command group.
elif get_group_name:
# Regex to identify the group name. Line in code looks like:
# "Tablets", []command{
find_group = re.findall(r'\"([^\"]+)\", \[\]\s*command\s*\{', line)
if find_group:
current_group = find_group[0]
if current_group not in commands:
commands[current_group] = {}
get_group_name = False
# First line of a command in addCommand syntax. This contains the
# name of the group that the command is in. Line in code looks like:
# addCommand{"Shards", command{
elif re.search(r'^addCommand\(', line.strip()):
command_data = re.findall(r'addCommand\s*\(\s*([^\,]+),\s*command\{',
line)
if command_data:
current_group = command_data[0]
current_group_strip_quotes = current_group.strip('"')
if current_group == current_group_strip_quotes:
current_group = get_group_name_from_variable(vtctl_go_path,
current_group)
else:
current_group = current_group_strip_quotes
if current_group not in commands:
commands[current_group] = {}
add_command_syntax = True
elif add_command_syntax and is_func_init:
if not current_command:
current_command = line.strip().strip(',').strip('"')
commands[current_group][current_command] = {
'definition': '',
'argument_list': {
'flags': {},
'args': []
},
'errors': {'other': []}}
command_groups[current_command] = current_group
elif 'function' not in commands[current_group][current_command]:
function = line.strip().strip(',')
commands[current_group][current_command]['function'] = function
functions[function] = current_command
elif 'arguments' not in commands[current_group][current_command]:
arguments = line.strip().strip(',')
commands[current_group][current_command]['arguments'] = arguments
if arguments:
new_arg_list = parse_arg_list(arguments, current_command)
commands[current_group][current_command]['argument_list']['args'] = new_arg_list
else:
definition_list = line.strip().split(' +')
for definition_part in definition_list:
definition = definition_part.strip().strip('})')
definition = definition.replace('}},', '')
definition = definition.replace('"', '')
commands[current_group][current_command]['definition'] += (
definition)
if line.strip().endswith('})'):
current_command = ''
# Command definition ends with line ending in "},".
elif line.strip().endswith('})'):
current_command = ''
add_command_syntax = False
# First line of a command. This contains the command name and the
# function used to process the command. Line in code looks like:
# command{"ScrapTablet", commandScrapTablet,
elif re.search(r'^\{\s*\"[^\"]+\",\s*command[^\,]+\,', line.strip()):
command_data = re.findall(r'\{\s*\"([^\"]+)\",\s*([^\,]+)\,',
line)
if command_data:
# Capture the command name and associate it with its function.
# Create a data structure to contain information about the command
# and its processing rules.
current_command = command_data[0][0]
function = command_data[0][1]
commands[current_group][current_command] = {
'definition': '',
'argument_list': {
'flags': {},
'args': []
},
'errors': {'other': []}}
# Associate the function with the command and vice versa.
# Associate the command with its group and vice versa.
commands[current_group][current_command]['function'] = function
command_groups[current_command] = current_group
functions[function] = current_command
# If code has identified a command name but has not identified
# arguments for that command, capture the next line and store it
# as the command arguments.
elif (current_command and
'arguments' not in commands[current_group][current_command]):
arguments = re.findall(r'\"([^\"]+)\"', line)
if arguments:
commands[current_group][current_command]['arguments'] = arguments[0]
last_char = ''
find_closing_square_bracket = False
has_comma = False
has_multiple = False
is_optional_argument = False
is_required_argument = False
current_argument = ''
new_arg_list = []
arg_count = 0
char_count = 1
for char in arguments[0]:
if (last_char == '' or last_char == ' ') and char == '[':
find_closing_square_bracket = True
elif (last_char == '[' and
find_closing_square_bracket and
char == '<'):
is_optional_argument = True
current_argument += char
elif find_closing_square_bracket:
if char == ']':
if is_optional_argument:
new_arg_list.append({'name': current_argument,
'has_comma': has_comma,
'has_multiple': has_multiple,
'required': False})
arg_count += 1
find_closing_square_bracket = False
current_argument = ''
has_comma = False
has_multiple = False
elif is_optional_argument:
if char == ',':
has_comma = True
elif last_char == '.' and char == '.':
has_multiple = True
elif not has_comma:
current_argument += char
elif char == '<' and (last_char == '' or last_char == ' '):
is_required_argument = True
current_argument += char
elif char == ',':
has_comma = True
if last_char == '>':
new_arg_list[arg_count - 1]['hasComma'] = True
has_comma = False
elif is_required_argument:
current_argument += char
if char == '>' and current_argument:
if char_count == len(arguments[0]):
new_arg_list.append({'name': current_argument,
'required': True})
arg_count += 1
is_required_argument = False
current_argument = ''
else:
next_char = 'x'
if current_command == 'Resolve':
#print len(arguments[0])
#print char_count
if char_count < len(arguments[0]):
next_char = arguments[0][char_count:char_count + 1]
#print next_char
#if char_count < (len(arguments[0]) - 1):
# print current_argument
# next_char = arguments[0][char_count + 1:char_count + 2]
# print next_char
if next_char and not next_char == '.' and not next_char == ':':
new_arg_list.append({'name': current_argument,
'required': True})
arg_count += 1
is_required_argument = False
current_argument = ''
elif (arg_count > 0 and
current_argument == '' and
last_char == '.' and
'multiple' in new_arg_list[arg_count - 1] and
char == ' '):
current_argument = ''
elif (arg_count > 0 and
current_argument == '' and
last_char == '.' and
char == '.'):
new_arg_list[arg_count - 1]['multiple'] = True
#elif (arg_count > 0 and
# current_argument == '' and
# last_char == '.'):
char_count += 1
last_char = char
commands[current_group][current_command]['argument_list']['args'] = (
new_arg_list)
else:
commands[current_group][current_command]['arguments'] = ""
# If code has identified a command and arguments, capture remaining lines
# as the command description. Assume the description ends at the line
# of code ending with "},".
elif current_command:
definition_list = line.rstrip('},').split(' +')
for definition_part in definition_list:
definition = definition_part.strip().strip('"')
#definition = definition.replace('\\n', '<br><br>')
definition = definition.replace('"},', '')
commands[current_group][current_command]['definition'] += definition
if line.strip().endswith('},'):
current_command = ''
# Command definition ends with line ending in "},".
elif line.strip().endswith('},'):
current_command = ''
# Capture information about a function that processes a command.
# Here, identify the function name.
elif re.search(r'^func ([^\)]+)\(', line.strip()):
current_function = ''
function_data = re.findall(r'func ([^\)]+)\(', line.strip())
if function_data:
current_function = function_data[0]
elif current_function:
# Lines that contain this:
# = subFlags....
# generally seem to contain descriptions of flags for the function.
# Capture the content type of the argument, its name, default value,
# and description. Associate these with the command that calls this
# function.
if re.search(r'\=\s*subFlags\.([^\(]+)\(\s*\"([^\"]+)\"\,' +
'([^\,]+)\,\s*\"([^\"]+)\"', line):
argument_data = re.findall(r'\=\s*subFlags\.([^\(]+)' +
'\(\s*\"([^\"]+)\"\,([^\,]+)\,\s*\"([^\"]+)\"', line)
if argument_data and current_function in functions:
[arg_type, arg_name, arg_default, arg_definition] = argument_data[0]
if arg_type == 'Bool':
arg_type = 'Boolean'
if arg_type == 'String' or arg_type == 'int':
arg_type = arg_type.lower()
arg_default = arg_default.strip().strip('"')
fcommand = functions[current_function]
fgroup = command_groups[fcommand]
commands[fgroup][fcommand]['argument_list']['flags'][arg_name] = {
'type': arg_type,
'default': arg_default,
'definition': arg_definition
}
elif re.search(r'\s*subFlags\.Var\(\s*([^\,]+)\,\s*\"([^\"]+)\"\,' +
'\s*\"([^\"]+)\"', line):
#print 'found subFlags.Var'
argument_data = re.findall(r'\s*subFlags\.Var\(\s*([^\,]+)\,' +
'\s*\"([^\"]+)\"\,\s*\"([^\"]+)\"', line)
#print argument_data
if argument_data and current_function in functions:
[arg_type, arg_name, arg_definition] = argument_data[0]
arg_type = 'string' # Var?
fcommand = functions[current_function]
fgroup = command_groups[fcommand]
commands[fgroup][fcommand]['argument_list']['flags'][arg_name] = {
'type': arg_type,
'definition': arg_definition
}
# Capture information for errors that indicate that the command
# was called with the incorrect number of arguments. Use the
# code to determine whether the code is looking for an exact number
# of arguments, a minimum number, a maximum number, etc.
elif re.search(r'if subFlags.NArg', line):
wrong_arg_data = re.findall(
r'subFlags.NArg\(\)\s*([\<\>\!\=]+)\s*(\d+)', line)
error_counts[current_function] = {'min': None,
'max': None,
'exact': []}
if wrong_arg_data:
get_wrong_arg_count_error = True
for wrong_arg_info in wrong_arg_data:
if wrong_arg_info[0] == '!=':
error_counts[current_function]['exact'].append(
wrong_arg_info[1])
elif wrong_arg_info[0] == '<':
error_counts[current_function]['min'] = wrong_arg_info[1]
elif wrong_arg_info[0] == '>':
error_counts[current_function]['max'] = wrong_arg_info[1]
elif wrong_arg_info[0] == '==' and wrong_arg_info[1] == '0':
error_counts[current_function]['min'] = '1'
# Capture data about other errors that the command might yield.
# TODO: Capture other errors from other files, such as
# //depot/google3/third_party/golang/vitess/go/vt/topo/tablet.go
elif get_wrong_arg_count_error and re.search(r'fmt.Errorf', line):
get_wrong_arg_count_error = False
error_data = re.findall(r'fmt\.Errorf\(\"([^\"]+)\"\)', line)
if error_data and current_function in functions:
fcommand = functions[current_function]
fgroup = command_groups[fcommand]
commands[fgroup][fcommand]['errors']['ARG_COUNT'] = {
'exact_count': error_counts[current_function]['exact'],
'min_count': error_counts[current_function]['min'],
'max_count': error_counts[current_function]['max'],
'message': error_data[0]
}
elif line.strip().endswith('}') or line.strip().endswith('{'):
get_wrong_arg_count_error = False
elif current_function in functions and re.search(r'fmt.Errorf', line):
error_data = re.findall(r'fmt\.Errorf\(\"([^\"]+)\"', line)
if error_data:
fcommand = functions[current_function]
fgroup = command_groups[fcommand]
if (error_data[0] not in
commands[fgroup][fcommand]['errors']['other']):
if ('ARG_COUNT' in commands[fgroup][fcommand]['errors'] and
error_data[0] !=
commands[fgroup][fcommand]['errors']['ARG_COUNT']['message']):
commands[fgroup][fcommand]['errors']['other'].append(
error_data[0])
elif 'ARG_COUNT' not in commands[fgroup][fcommand]['errors']:
commands[fgroup][fcommand]['errors']['other'].append(
error_data[0])
# This line indicates that commands are starting. No need to capture
# stuff before here.
elif re.search(r'^var commands =', line.strip()):
get_commands = True
elif re.search(r'^func init\(\)', line.strip()):
get_commands = True
is_func_init = True
if get_argument_definitions:
if line.strip() == '*/':
get_argument_definitions = False
elif line.startswith('-'):
definition_data = re.findall(r'^\-\s*([^\:]+)\:\s*(.*)', line)
if definition_data:
arg_name_array = definition_data[0][0].split('(internal)')
current_command_argument = arg_name_array[0].strip()
arg_definitions[current_command_argument] = {}
if len(arg_name_array) > 1:
arg_definitions[current_command_argument]['internal'] = True
arg_definitions[current_command_argument]['description'] = (
definition_data[0][1].strip())
current_command_argument_value = ''
elif current_command_argument and line.lstrip()[0:2] == '--':
if 'list_items' not in arg_definitions[current_command_argument]:
arg_definitions[current_command_argument]['list_items'] = []
arg_value_data = re.findall(r'^\--\s*([^\:]+)\:\s*(.*)', line.strip())
if arg_value_data:
current_command_argument_value = arg_value_data[0][0]
argument_definition = arg_value_data[0][1]
arg_definitions[current_command_argument]['list_items'].append({
'value': current_command_argument_value,
'definition': argument_definition})
elif current_command_argument_value:
list_length = (len(
arg_definitions[current_command_argument]['list_items']) - 1)
arg_definitions[current_command_argument][
'list_items'][list_length]['definition'] += ' ' + line.strip()
elif line and current_command_argument:
arg_definitions[current_command_argument]['description'] += ' ' + line.strip()
elif re.search(r'^COMMAND ARGUMENT DEFINITIONS', line.strip()):
get_argument_definitions = True
# Handle arguments that have different names but same meaning
new_arg_definitions = {}
modifiers = ['destination', 'new master', 'original', 'parent', 'served',
'source', 'target']
for defined_argument in arg_definitions:
argument_list = defined_argument.split(',')
if len(argument_list) > 1:
for argument_list_item in argument_list:
new_arg_definitions[argument_list_item.strip()] = (
arg_definitions[defined_argument])
for modifier in modifiers:
new_arg_definitions[modifier + ' ' + argument_list_item.strip()] = (
arg_definitions[defined_argument])
else:
new_arg_definitions[defined_argument] = arg_definitions[defined_argument]
for modifier in modifiers:
new_arg_definitions[modifier + ' ' + defined_argument] = (
arg_definitions[defined_argument])
#print json.dumps(new_arg_definitions, sort_keys=True, indent=4)
#print json.dumps(commands["Generic"], sort_keys=True, indent=4)
create_reference_doc(root_directory, commands, new_arg_definitions)
return
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-r', '--root-directory', default='..',
help='root directory for the vitess github tree')
(options, args) = parser.parse_args()
main(options.root_directory)