File: generate_missing_parsers.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (59 lines) | stat: -rw-r--r-- 1,281 bytes parent folder | download | duplicates (4)
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
"""
Generate stubs for missing parsers
"""

import argparse
import io
import os
import subprocess
import sys

from cmakelang import parse

TEMPLATE = '''

def parse_{0}(ctx, tokens, breakstack):
  """
  ::

    {0}

  :see: https://cmake.org/cmake/help/latest/command/{0}.html
  """
'''


def main():
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument("outfile", nargs="?", default="-")
  args = parser.parse_args()

  missing_commands = []
  proc = subprocess.Popen(
      ["cmake", "--help-command-list"],
      stdout=subprocess.PIPE)

  parse_db = parse.funs.get_parse_db()

  with proc.stdout as infile:
    for line in infile:
      command = line.strip().decode("utf-8")
      if command not in parse_db and not command.startswith("end"):
        missing_commands.append(command)
  proc.wait()

  outfile_path = args.outfile
  if outfile_path == "-":
    outfile_path = os.dup(sys.stdout.fileno())

  with io.open(outfile_path, "w", encoding="utf-8") as outfile:
    for command in missing_commands:
      outfile.write(TEMPLATE.format(command))

    outfile.write("\n\ndef populate_db(parse_db):\n")
    for command in missing_commands:
      outfile.write('  parse_db["{0}"] = parse_{0}\n'.format(command))


if __name__ == "__main__":
  main()