Remove multiple uses of '.&&.' with conjoin
[ganeti-local] / autotools / build-bash-completion
index c0295cc..511f521 100755 (executable)
@@ -27,6 +27,7 @@
 # [C0103] Invalid name build-bash-completion
 
 import os
+import os.path
 import re
 import itertools
 import optparse
@@ -641,11 +642,12 @@ def HaskellOptToOptParse(opts, kind):
   opts = opts.split(",")
   if kind == "none":
     return cli.cli_option(*opts, action="store_true")
-  elif kind in ["file", "string", "host", "dir"]:
-    return cli.cli_option(*opts, type="string")
-  elif kind == "numeric":
-    # FIXME: float values here
+  elif kind in ["file", "string", "host", "dir", "inetaddr"]:
     return cli.cli_option(*opts, type="string")
+  elif kind == "integer":
+    return cli.cli_option(*opts, type="int")
+  elif kind == "float":
+    return cli.cli_option(*opts, type="float")
   elif kind == "onegroup":
     return cli.cli_option(*opts, type="string",
                            completion_suggest=cli.OPT_COMPL_ONE_NODEGROUP)
@@ -655,13 +657,56 @@ def HaskellOptToOptParse(opts, kind):
   elif kind == "manyinstances":
     # FIXME: no support for many instances
     return cli.cli_option(*opts, type="string")
-  elif kind.startswith("choices "):
-    choices = kind[len("choices "):].split(",")
+  elif kind.startswith("choices="):
+    choices = kind[len("choices="):].split(",")
     return cli.cli_option(*opts, type="choice", choices=choices)
   else:
     # FIXME: there are many other currently unused completion types,
     # should be added on an as-needed basis
-    raise Exception("Unhnadled option kind '%s'" % kind)
+    raise Exception("Unhandled option kind '%s'" % kind)
+
+
+#: serialised kind to arg type
+_ARG_MAP = {
+  "choices": cli.ArgChoice,
+  "command": cli.ArgCommand,
+  "file": cli.ArgFile,
+  "host": cli.ArgHost,
+  "jobid": cli.ArgJobId,
+  "onegroup": cli.ArgGroup,
+  "oneinstance": cli.ArgInstance,
+  "onenode": cli.ArgNode,
+  "oneos": cli.ArgOs,
+  "string": cli.ArgUnknown,
+  "suggests": cli.ArgSuggest,
+  }
+
+
+def HaskellArgToCliArg(kind, min_cnt, max_cnt):
+  """Converts a Haskell options to Python _Argument.
+
+  @type kind: string
+  @param kind: type generated by Common.hs/argComplToText; needs to be
+      kept in sync
+
+  """
+  min_cnt = int(min_cnt)
+  if max_cnt == "none":
+    max_cnt = None
+  else:
+    max_cnt = int(max_cnt)
+  # pylint: disable=W0142
+  # since we pass **kwargs
+  kwargs = {"min": min_cnt, "max": max_cnt}
+
+  if kind.startswith("choices=") or kind.startswith("suggest="):
+    (kind, choices) = kind.split("=", 1)
+    kwargs["choices"] = choices.split(",")
+
+  if kind not in _ARG_MAP:
+    raise Exception("Unhandled argument kind '%s'" % kind)
+  else:
+    return _ARG_MAP[kind](**kwargs)
 
 
 def WriteHaskellCompletion(sw, script, htools=True, debug=True):
@@ -677,21 +722,31 @@ def WriteHaskellCompletion(sw, script, htools=True, debug=True):
     script_name = script
     func_name = "htools_%s" % script
   else:
-    # note: this is not yet used (daemons)
-    cmd = script
+    cmd = "./" + script
     env = {}
-    script_name = script
-    func_name = script
+    script_name = os.path.basename(script)
+    func_name = script_name
+  func_name = func_name.replace("-", "_")
   output = utils.RunCmd([cmd, "--help-completion"], env=env, cwd=".").output
   cli_opts = []
+  args = []
   for line in output.splitlines():
-    v = line.split(None, 1)
-    if len(v) != 2:
-      raise Exception("Invalid help completion output from %s: %s" %
-                      (script, v))
-    (opts, kind) = v
-    cli_opts.append(HaskellOptToOptParse(opts, kind))
-  WriteCompletion(sw, script_name, func_name, debug, opts=cli_opts, args=[])
+    v = line.split(None)
+    exc = lambda msg: Exception("Invalid %s output from %s: %s" %
+                                (msg, script, v))
+    if len(v) < 2:
+      raise exc("help completion")
+    if v[0].startswith("-"):
+      if len(v) != 2:
+        raise exc("option format")
+      (opts, kind) = v
+      cli_opts.append(HaskellOptToOptParse(opts, kind))
+    else:
+      if len(v) != 3:
+        raise exc("argument format")
+      (kind, min_cnt, max_cnt) = v
+      args.append(HaskellArgToCliArg(kind, min_cnt, max_cnt))
+  WriteCompletion(sw, script_name, func_name, debug, opts=cli_opts, args=args)
 
 
 def main():
@@ -729,12 +784,21 @@ def main():
                   not options.compact,
                   opts=burnin.OPTIONS, args=burnin.ARGUMENTS)
 
+  # ganeti-cleaner
+  WriteHaskellCompletion(sw, "daemons/ganeti-cleaner", htools=False,
+                         debug=not options.compact)
+
   # htools, if enabled
   if _autoconf.HTOOLS:
     for script in _autoconf.HTOOLS_PROGS:
       WriteHaskellCompletion(sw, script, htools=True,
                              debug=not options.compact)
 
+  # ganeti-confd, if enabled
+  if _autoconf.ENABLE_CONFD:
+    WriteHaskellCompletion(sw, "htools/ganeti-confd", htools=False,
+                           debug=not options.compact)
+
   # Reset extglob to original value
   sw.Write("[[ -n \"$gnt_shopt_extglob\" ]] && $gnt_shopt_extglob")
   sw.Write("unset gnt_shopt_extglob")