preCICE v3.1.2
Loading...
Searching...
No Matches
updateSourceFiles.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3import collections
4import glob
5import os
6import pathlib
7import subprocess
8import sys
9
10""" Files matching this pattern will be filtered out """
11IGNORE_PATTERNS = ["drivers", "mapping/device"]
12
13""" Configured files, which should be ignored by git, yet installed by CMake"""
14CONFIGURED_PUBLIC = ["${PROJECT_BINARY_DIR}/src/precice/Version.h"]
15
16""" Configured files, which should be ignored by git """
17CONFIGURED_SOURCES = [
18 "${PROJECT_BINARY_DIR}/src/precice/impl/versions.hpp",
19 "${CMAKE_BINARY_DIR}/src/precice/impl/versions.cpp",
20]
21
22
24 ret = subprocess.run(
25 ["git", "ls-files", "--full-name"],
26 stdout=subprocess.PIPE,
27 stderr=subprocess.PIPE,
28 check=False,
29 )
30 if ret.returncode != 0:
31 return None
32 else:
33 return ret.stdout.decode().split()
34
35
37 _, ext = os.path.splitext(name)
38 return ext
39
40
42 sources = os.path.join(root, "src", "sources.cmake")
43 utests = os.path.join(root, "src", "tests.cmake")
44 itests = os.path.join(root, "tests", "tests.cmake")
45 cmakepaths = collections.namedtuple("CMakePaths", "sources utests itests")
46 return cmakepaths(sources, utests, itests)
47
48
50 paths = get_cmake_file_paths(root)
51 return all(map(os.path.exists, paths))
52
53
55 src_dir = os.path.join(root, "src")
56 tests_dir = os.path.join(root, "tests")
57
58 # Find interface headers
59 public = glob.glob(os.path.join(src_dir, "precice", "*.hpp"))
60 public += CONFIGURED_PUBLIC
61 public = [os.path.relpath(p, root) for p in public]
62
63 # Find all test and source cpp files
64 sources, utests = [], []
65 exts = [".cpp", ".c", ".hpp", ".h"]
66 for dir, _, filenames in os.walk(src_dir):
67 if any([elem in dir for elem in IGNORE_PATTERNS]):
68 continue
69 files = [
70 os.path.relpath(os.path.join(dir, name), root)
71 for name in filenames
72 if file_extension(name) in exts
73 ]
74 if "test" in dir:
75 utests += files
76 else:
77 sources += files
78 sources += CONFIGURED_SOURCES
79
80 itests = []
81 for dir, _, filenames in os.walk(tests_dir):
82 if any([elem in dir for elem in IGNORE_PATTERNS]):
83 continue
84 files = [
85 os.path.relpath(os.path.join(dir, name), root)
86 for name in filenames
87 if file_extension(name) in exts
88 ]
89 itests += files
90
91 return sorted(sources), sorted(public), sorted(utests), sorted(itests)
92
93
95 """
96 Extracts the test suite from a path and translates it to the boost test name
97 """
98 dir = pathlib.PurePath(path).parts[1]
99 parts = map(lambda s: s.capitalize(), dir.split("-"))
100 return "".join(parts)
101
102
104 return sorted(set(map(itest_path_to_suite, itests)))
105
106
107SOURCES_BASE = """#
108# This file lists all sources that will be compiles into the precice library
109#
110
111target_sources(preciceCore
112 PRIVATE
113 {}
114 )
115
116#
117# Select headers to install
118#
119
120set_property(TARGET precice PROPERTY PUBLIC_HEADER
121 {}
122 )
123"""
124TESTS_BASE = """#
125# This file lists all tests sources that will be compiled into the test executable
126#
127target_sources(testprecice
128 PRIVATE
129 {}
130 )
131"""
132ITESTS_BASE = """#
133# This file lists all integration test sources and test suites
134#
135target_sources(testprecice
136 PRIVATE
137 {}
138 )
139
140# Contains the list of integration test suites
141set(PRECICE_TEST_SUITES {})
142"""
143
144
145def generate_lib_sources(sources, public):
146 return SOURCES_BASE.format("\n ".join(sources), "\n ".join(public))
147
148
150 return TESTS_BASE.format("\n ".join(utests))
151
152
154 return ITESTS_BASE.format(
155 "\n ".join(itests), " ".join(test_suites_from_files(itests))
156 )
157
158
159def main():
160 root = os.curdir
161 if not is_precice_root(root):
162 print("Current dir {} is not the root of the precice repository!".format(root))
163 return 1
164 sources, public, utests, itests = get_file_lists(root)
165 print(
166 "Detected files:\n sources: {}\n public headers: {}\n unit tests: {}\n integration tests: {}".format(
167 len(sources), len(public), len(utests), len(itests)
168 )
169 )
170
171 gitfiles = get_gitfiles()
172 if gitfiles:
173 not_tracked = list(
174 set(sources + public + utests + itests)
175 - set(gitfiles + CONFIGURED_SOURCES + CONFIGURED_PUBLIC)
176 )
177 if not_tracked:
178 print("The source tree contains files not tracked by git.")
179 print("Please do one of the following with them:")
180 print(" - track them using 'git add'")
181 print(" - add them to IGNORE_PATTERNS in this script")
182 print(" - add them to CONFIGURED_SOURCES in this script!")
183 print("Files:")
184 for file in not_tracked:
185 print(" {}".format(file))
186 print("Verification FAILED")
187 else:
188 print("Verification SUCCEEDED")
189 else:
190 print("Git did not run successfully.")
191 print("Verification SKIPPED")
192
193 print("Generating CMake files")
194 # sources_file, tests_file = get_cmake_file_paths(root)
195 files = get_cmake_file_paths(root)
196 sources_content = generate_lib_sources(sources, public)
197 utests_content = generate_unit_tests(utests)
198 itests_content = generate_integration_tests(itests)
199
200 print("Writing Files")
201 print(" {}".format(files.sources))
202 with open(files.sources, "w") as f:
203 f.write(sources_content)
204
205 print(" {}".format(files.utests))
206 with open(files.utests, "w") as f:
207 f.write(utests_content)
208
209 print(" {}".format(files.itests))
210 with open(files.itests, "w") as f:
211 f.write(itests_content)
212
213 print("done")
214 return 0
215
216
217if __name__ == "__main__":
218 sys.exit(main())
generate_lib_sources(sources, public)