summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@qt.io>2020-07-08 10:32:03 +0200
committerJoerg Bornemann <joerg.bornemann@qt.io>2020-07-13 10:51:43 +0200
commitac14858e85cfee06c1e19843b92d50e38bc969dd (patch)
treeaade0c471dbfbc2af4d9446d60efcbc800310b55
parent8a0676d5f9b54bc3d291223c3e88e91043a81507 (diff)
configurejson2cmake: Use a context manager for file special handling
One can now write with special_cased_file("base/dir", "foo.txt") as fh: do_something(fh) This makes the code of processJson a bit clearer, and it allows us to easily add more files that support the special handling comments. Change-Id: Ia25d0c0d48df1802c5e2123d05345a88b42a2981 Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
-rwxr-xr-xutil/cmake/configurejson2cmake.py34
1 files changed, 21 insertions, 13 deletions
diff --git a/util/cmake/configurejson2cmake.py b/util/cmake/configurejson2cmake.py
index e2e3890364..fdab8aba1c 100755
--- a/util/cmake/configurejson2cmake.py
+++ b/util/cmake/configurejson2cmake.py
@@ -1354,6 +1354,26 @@ def processSubconfigs(path, ctx, data):
subconfCtx = ctx
processJson(subconfDir, subconfCtx, subconfData)
+class special_cased_file:
+ def __init__(self, base_dir : str, file_name : str):
+ self.base_dir = base_dir
+ self.file_path = posixpath.join(base_dir, file_name)
+ self.gen_file_path = self.file_path + ".gen"
+
+ def __enter__(self):
+ self.file = open(self.gen_file_path, "w")
+ self.sc_handler = SpecialCaseHandler(
+ os.path.abspath(self.file_path),
+ os.path.abspath(self.gen_file_path),
+ os.path.abspath(self.base_dir),
+ debug=False,
+ )
+ return self.file
+
+ def __exit__(self, type, value, trace_back):
+ self.file.close()
+ if self.sc_handler.handle_special_cases():
+ os.replace(self.gen_file_path, self.file_path)
def processJson(path, ctx, data):
ctx["project_dir"] = path
@@ -1362,9 +1382,7 @@ def processJson(path, ctx, data):
ctx = processFiles(ctx, data)
- destination = posixpath.join(path, "configure.cmake")
- generated_file = destination + '.gen'
- with open(generated_file, "w") as cm_fh:
+ with special_cased_file(path, "configure.cmake") as cm_fh:
cm_fh.write("\n\n#### Inputs\n\n")
processInputs(ctx, data, cm_fh)
@@ -1394,16 +1412,6 @@ def processJson(path, ctx, data):
# do this late:
processSubconfigs(path, ctx, data)
- handler = SpecialCaseHandler(
- os.path.abspath(destination),
- os.path.abspath(generated_file),
- os.path.abspath(path),
- debug=False,
- )
- if handler.handle_special_cases():
- os.replace(generated_file, destination)
-
-
def main():
if len(sys.argv) != 2:
print("This scripts needs one directory to process!")