File: genfiles.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 (80 lines) | stat: -rw-r--r-- 2,370 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Some of the test lint would get removed by my editor we have a little script to
help generate the file.
"""

import io
import os
import re
import logging

logger = logging.getLogger(__name__)


def make_expect_lint():
  thisdir = os.path.dirname(os.path.realpath(__file__))
  base_override = os.getenv("PYBUILD_TEST_BASE_OVERRIDE")
  if base_override is not None:
    thisdir = thisdir.replace(os.environ['PWD'], base_override)

  # strip test annotations from the test file make the documentation a little
  # cleaner
  with io.open(os.path.join(thisdir, "lint_tests.cmake"), newline='') as infile:
    content = infile.read()
  content = re.sub(
      "^# ((test)|(expect)): .*\n", "", content, flags=re.MULTILINE)
  with io.open(
      os.path.join(thisdir, "expect_lint.cmake"), "w", newline='') as outfile:
    outfile.write(content)


def rewrite_lint_tests():
  thisdir = os.path.dirname(os.path.realpath(__file__))
  base_override = os.getenv("PYBUILD_TEST_BASE_OVERRIDE")
  if base_override is not None:
    thisdir = thisdir.replace(os.environ['PWD'], base_override)

  # re-process the test file to re-introduce lint that is likely to have been
  # removed by an editor
  with io.open(os.path.join(thisdir, "lint_tests.cmake"), newline='') as infile:
    content = infile.read()

  lines = content.split("\n")
  have_changes = False

  # Need to remove newline
  if content[-1] == "\n":
    logger.info("Removing final newline")
    have_changes = True

  outlines = []
  for line in lines:
    if line.rstrip() == "# This line has the wrong line endings":
      if not line.endswith("\r"):
        logger.info("Adding carriage return to line ending test")
        line = line.rstrip() + "\r"
        have_changes = True

    if line.rstrip() == "# This line has trailing whitespace":
      if not line.endswith(" "):
        logger.info("Adding whitespace to trailing whitespace test")
        line = line.rstrip() + "   "
        have_changes = True
    outlines.append(line)

  if have_changes:
    while outlines and not outlines[-1].strip():
      outlines.pop(-1)
    with io.open(os.path.join(
        thisdir, "lint_tests.cmake"), "w", newline='') as outfile:
      outfile.write("\n".join(outlines))


def genfiles():
  rewrite_lint_tests()
  make_expect_lint()


if __name__ == "__main__":
  logging.basicConfig(level=logging.INFO)
  genfiles()