QtBase  v6.3.1
cmakeconversionrate.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
29 
30 from argparse import ArgumentParser
31 
32 import os
33 import re
34 import subprocess
35 import typing
36 
37 
38 def _parse_commandline():
39  parser = ArgumentParser(description="Calculate the conversion rate to cmake.")
40  parser.add_argument("--debug", dest="debug", action="store_true", help="Turn on debug output")
41  parser.add_argument(
42  "source_directory",
43  metavar="<Source Directory>",
44  type=str,
45  help="The Qt module source directory",
46  )
47  parser.add_argument(
48  "binary_directory",
49  metavar="<CMake build direcotry>",
50  type=str,
51  help="The CMake build directory (might be empty)",
52  )
53 
54  return parser.parse_args()
55 
56 
57 def calculate_baseline(source_directory: str, *, debug: bool = False) -> int:
58  if debug:
59  print(f'Scanning "{source_directory}" for qmake-based tests.')
60  result = subprocess.run(
61  '/usr/bin/git grep -E "^\\s*CONFIG\\s*\\+?=.*\\btestcase\\b" | sort -u | wc -l',
62  shell=True,
63  capture_output=True,
64  cwd=source_directory,
65  )
66  return int(result.stdout)
67 
68 
69 def build(source_directory: str, binary_directory: str, *, debug=False) -> None:
70  abs_source = os.path.abspath(source_directory)
71  if not os.path.isdir(binary_directory):
72  os.makedirs(binary_directory)
73  if not os.path.exists(os.path.join(binary_directory, "CMakeCache.txt")):
74 
75  if debug:
76  print(f'Running cmake in "{binary_directory}"')
77  result = subprocess.run(["/usr/bin/cmake", "-GNinja", abs_source], cwd=binary_directory)
78  if debug:
79  print(f"CMake return code: {result.returncode}.")
80 
81  assert result.returncode == 0
82 
83  if debug:
84  print(f'Running ninja in "{binary_directory}".')
85  result = subprocess.run("/usr/bin/ninja", cwd=binary_directory)
86  if debug:
87  print(f"Ninja return code: {result.returncode}.")
88 
89  assert result.returncode == 0
90 
91 
92 def test(binary_directory: str, *, debug=False) -> typing.Tuple[int, int]:
93  if debug:
94  print(f'Running ctest in "{binary_directory}".')
95  result = subprocess.run(
96  '/usr/bin/ctest -j 250 | grep "tests passed, "',
97  shell=True,
98  capture_output=True,
99  cwd=binary_directory,
100  )
101  summary = result.stdout.decode("utf-8").replace("\n", "")
102  if debug:
103  print(f"Test summary: {summary} ({result.returncode}).")
104 
105  matches = re.fullmatch(r"\d+% tests passed, (\d+) tests failed out of (\d+)", summary)
106  if matches:
107  if debug:
108  print(f"Matches: failed {matches.group(1)}, total {matches.group(2)}.")
109  return (int(matches.group(2)), int(matches.group(2)) - int(matches.group(1)))
110 
111  return (0, 0)
112 
113 
114 def main() -> int:
115  args = _parse_commandline()
116 
117  base_line = calculate_baseline(args.source_directory, debug=args.debug)
118  if base_line <= 0:
119  print(f"Could not find the qmake baseline in {args.source_directory}.")
120  return 1
121 
122  if args.debug:
123  print(f"qmake baseline: {base_line} test binaries.")
124 
125  cmake_total = 0
126  cmake_success = 0
127  try:
128  build(args.source_directory, args.binary_directory, debug=args.debug)
129  (cmake_total, cmake_success) = test(args.binary_directory, debug=args.debug)
130  finally:
131  if cmake_total == 0:
132  print("\n\n\nCould not calculate the cmake state.")
133  return 2
134  else:
135  print(f"\n\n\nCMake test conversion rate: {cmake_total/base_line:.2f}.")
136  print(f"CMake test success rate : {cmake_success/base_line:.2f}.")
137  return 0
138 
139 
140 if __name__ == "__main__":
141  main()
None build(str source_directory, str binary_directory, *debug=False)
typing.Tuple[int, int] test(str binary_directory, *debug=False)
int calculate_baseline(str source_directory, *bool debug=False)