29 |
29 |
import os
|
30 |
30 |
import re
|
31 |
31 |
import itertools
|
|
32 |
import optparse
|
32 |
33 |
from cStringIO import StringIO
|
33 |
34 |
|
34 |
35 |
from ganeti import constants
|
... | ... | |
46 |
47 |
_OPT_NAME_RE = re.compile(r"^-[a-zA-Z0-9]|--[a-z][-a-z0-9]+$")
|
47 |
48 |
|
48 |
49 |
|
49 |
|
def WritePreamble(sw):
|
|
50 |
def WritePreamble(sw, support_debug):
|
50 |
51 |
"""Writes the script preamble.
|
51 |
52 |
|
52 |
53 |
Helper functions should be written here.
|
... | ... | |
55 |
56 |
sw.Write("# This script is automatically generated at build time.")
|
56 |
57 |
sw.Write("# Do not modify manually.")
|
57 |
58 |
|
58 |
|
sw.Write("_gnt_log() {")
|
59 |
|
sw.IncIndent()
|
60 |
|
try:
|
61 |
|
sw.Write("if [[ -n \"$GANETI_COMPL_LOG\" ]]; then")
|
|
59 |
if support_debug:
|
|
60 |
sw.Write("_gnt_log() {")
|
62 |
61 |
sw.IncIndent()
|
63 |
62 |
try:
|
64 |
|
sw.Write("{")
|
|
63 |
sw.Write("if [[ -n \"$GANETI_COMPL_LOG\" ]]; then")
|
65 |
64 |
sw.IncIndent()
|
66 |
65 |
try:
|
67 |
|
sw.Write("echo ---")
|
68 |
|
sw.Write("echo \"$@\"")
|
69 |
|
sw.Write("echo")
|
|
66 |
sw.Write("{")
|
|
67 |
sw.IncIndent()
|
|
68 |
try:
|
|
69 |
sw.Write("echo ---")
|
|
70 |
sw.Write("echo \"$@\"")
|
|
71 |
sw.Write("echo")
|
|
72 |
finally:
|
|
73 |
sw.DecIndent()
|
|
74 |
sw.Write("} >> $GANETI_COMPL_LOG")
|
70 |
75 |
finally:
|
71 |
76 |
sw.DecIndent()
|
72 |
|
sw.Write("} >> $GANETI_COMPL_LOG")
|
|
77 |
sw.Write("fi")
|
73 |
78 |
finally:
|
74 |
79 |
sw.DecIndent()
|
75 |
|
sw.Write("fi")
|
76 |
|
finally:
|
77 |
|
sw.DecIndent()
|
78 |
|
sw.Write("}")
|
|
80 |
sw.Write("}")
|
79 |
81 |
|
80 |
82 |
sw.Write("_ganeti_nodes() {")
|
81 |
83 |
sw.IncIndent()
|
... | ... | |
212 |
214 |
sw.DecIndent()
|
213 |
215 |
sw.Write("fi")
|
214 |
216 |
|
215 |
|
sw.Write("_gnt_log optcur=\"'$optcur'\"")
|
|
217 |
if support_debug:
|
|
218 |
sw.Write("_gnt_log optcur=\"'$optcur'\"")
|
216 |
219 |
|
217 |
220 |
sw.Write("return 1")
|
218 |
221 |
finally:
|
... | ... | |
225 |
228 |
sw.IncIndent()
|
226 |
229 |
try:
|
227 |
230 |
sw.Write("""COMPREPLY=( $(compgen "$@") )""")
|
228 |
|
sw.Write("_gnt_log COMPREPLY=\"${COMPREPLY[@]}\"")
|
|
231 |
if support_debug:
|
|
232 |
sw.Write("_gnt_log COMPREPLY=\"${COMPREPLY[@]}\"")
|
229 |
233 |
finally:
|
230 |
234 |
sw.DecIndent()
|
231 |
235 |
sw.Write("}")
|
... | ... | |
240 |
244 |
"""Command completion writer class.
|
241 |
245 |
|
242 |
246 |
"""
|
243 |
|
def __init__(self, arg_offset, opts, args):
|
|
247 |
def __init__(self, arg_offset, opts, args, support_debug):
|
244 |
248 |
self.arg_offset = arg_offset
|
245 |
249 |
self.opts = opts
|
246 |
250 |
self.args = args
|
|
251 |
self.support_debug = support_debug
|
247 |
252 |
|
248 |
253 |
for opt in opts:
|
249 |
254 |
# While documented, these variables aren't seen as public attributes by
|
... | ... | |
365 |
370 |
sw.DecIndent()
|
366 |
371 |
sw.Write("fi")
|
367 |
372 |
|
368 |
|
sw.Write("_gnt_log pfx=\"'$pfx'\" curvalue=\"'$curvalue'\""
|
369 |
|
" node1=\"'$node1'\"")
|
|
373 |
if self.support_debug:
|
|
374 |
sw.Write("_gnt_log pfx=\"'$pfx'\" curvalue=\"'$curvalue'\""
|
|
375 |
" node1=\"'$node1'\"")
|
370 |
376 |
|
371 |
377 |
sw.Write("for i in $(_ganeti_nodes); do")
|
372 |
378 |
sw.IncIndent()
|
... | ... | |
505 |
511 |
self._CompleteArguments(sw)
|
506 |
512 |
|
507 |
513 |
|
508 |
|
def WriteCompletion(sw, scriptname, funcname,
|
|
514 |
def WriteCompletion(sw, scriptname, funcname, support_debug,
|
509 |
515 |
commands=None,
|
510 |
516 |
opts=None, args=None):
|
511 |
517 |
"""Writes the completion code for one command.
|
... | ... | |
527 |
533 |
' cur="${COMP_WORDS[COMP_CWORD]}"'
|
528 |
534 |
' prev="${COMP_WORDS[COMP_CWORD-1]}"')
|
529 |
535 |
|
530 |
|
sw.Write("_gnt_log cur=\"$cur\" prev=\"$prev\"")
|
531 |
|
sw.Write("[[ -n \"$GANETI_COMPL_LOG\" ]] &&"
|
532 |
|
" _gnt_log \"$(set | grep ^COMP_)\"")
|
|
536 |
if support_debug:
|
|
537 |
sw.Write("_gnt_log cur=\"$cur\" prev=\"$prev\"")
|
|
538 |
sw.Write("[[ -n \"$GANETI_COMPL_LOG\" ]] &&"
|
|
539 |
" _gnt_log \"$(set | grep ^COMP_)\"")
|
533 |
540 |
|
534 |
541 |
sw.Write("COMPREPLY=()")
|
535 |
542 |
|
536 |
543 |
if opts is not None and args is not None:
|
537 |
544 |
assert not commands
|
538 |
|
CompletionWriter(0, opts, args).WriteTo(sw)
|
|
545 |
CompletionWriter(0, opts, args, support_debug).WriteTo(sw)
|
539 |
546 |
|
540 |
547 |
else:
|
541 |
548 |
sw.Write("""if [[ "$COMP_CWORD" == 1 ]]; then""")
|
... | ... | |
565 |
572 |
sw.Write("%s)", "|".join(map(utils.ShellQuote, sorted(cmds))))
|
566 |
573 |
sw.IncIndent()
|
567 |
574 |
try:
|
568 |
|
CompletionWriter(1, optdef, argdef).WriteTo(sw)
|
|
575 |
CompletionWriter(1, optdef, argdef, support_debug).WriteTo(sw)
|
569 |
576 |
finally:
|
570 |
577 |
sw.DecIndent()
|
571 |
578 |
sw.Write(";;")
|
... | ... | |
638 |
645 |
|
639 |
646 |
|
640 |
647 |
def main():
|
|
648 |
parser = optparse.OptionParser(usage="%prog [--compact]")
|
|
649 |
parser.add_option("--compact", action="store_true",
|
|
650 |
help=("Don't indent output and don't include debugging"
|
|
651 |
" facilities"))
|
|
652 |
|
|
653 |
options, args = parser.parse_args()
|
|
654 |
if args:
|
|
655 |
parser.error("Wrong number of arguments")
|
|
656 |
|
641 |
657 |
buf = StringIO()
|
642 |
|
sw = utils.ShellWriter(buf)
|
|
658 |
sw = utils.ShellWriter(buf, indent=not options.compact)
|
643 |
659 |
|
644 |
|
WritePreamble(sw)
|
|
660 |
WritePreamble(sw, not options.compact)
|
645 |
661 |
|
646 |
662 |
# gnt-* scripts
|
647 |
663 |
for scriptname in _autoconf.GNT_SCRIPTS:
|
648 |
664 |
filename = "scripts/%s" % scriptname
|
649 |
665 |
|
650 |
|
WriteCompletion(sw, scriptname,
|
651 |
|
GetFunctionName(scriptname),
|
|
666 |
WriteCompletion(sw, scriptname, GetFunctionName(scriptname),
|
|
667 |
not options.compact,
|
652 |
668 |
commands=GetCommands(filename,
|
653 |
669 |
build.LoadModule(filename)))
|
654 |
670 |
|
655 |
671 |
# Burnin script
|
656 |
672 |
burnin = build.LoadModule("tools/burnin")
|
657 |
673 |
WriteCompletion(sw, "%s/burnin" % pathutils.TOOLSDIR, "_ganeti_burnin",
|
|
674 |
not options.compact,
|
658 |
675 |
opts=burnin.OPTIONS, args=burnin.ARGUMENTS)
|
659 |
676 |
|
660 |
677 |
print buf.getvalue()
|