diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-11-26 17:52:32 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-11-27 08:47:53 +0100 |
commit | 742a39962f1ba9c0e42f387394768c13793144c0 (patch) | |
tree | dcf7ec182145671b01ca11d7ff65386de344b745 | |
parent | 37ff1a731a8c5e6d7b0370ad261739786b552c31 (diff) |
snippets_translate: Streamline writing files
The code created all example directories first and extracted the
snippets to .cpp files which were later moved to .py files to
overwrite the snippets in the repository (back then).
Write to the Python file directly and create the directory only when
needed. This removes a lot of empty directories.
Pick-to: 6.2
Task-number: PYSIDE-1721
Change-Id: I38e695db4aae386be18cd3b85708f684bfa228d5
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r-- | tools/snippets_translate/main.py | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py index d687ef6d5..792328933 100644 --- a/tools/snippets_translate/main.py +++ b/tools/snippets_translate/main.py @@ -277,7 +277,18 @@ def translate_file(file_path, final_path, debug, write): if write: # Open the final file - with open(str(final_path), "w") as out_f: + target_file = final_path.with_suffix(".py") + + # Directory where the file will be placed, if it does not exists + # we create it. The option 'parents=True' will create the parents + # directories if they don't exist, and if some of them exists, + # the option 'exist_ok=True' will ignore them. + if not target_file.parent.is_dir(): + if not opt_quiet: + log.info(f"Creating directories for {target_file.parent}") + target_file.parent.mkdir(parents=True, exist_ok=True) + + with target_file.open("w") as out_f: out_f.write(license_header) out_f.write("\n\n") @@ -285,8 +296,6 @@ def translate_file(file_path, final_path, debug, write): out_f.write(s) out_f.write("\n") - # Rename to .py - written_file = shutil.move(str(final_path), str(final_path.with_suffix(".py"))) if not opt_quiet: log.info(f"Written: {written_file}") else: @@ -329,15 +338,6 @@ def copy_file(file_path, py_path, category, category_path, write=False, debug=Fa else: log.info(f"{status_msg:10s} {final_path}") - # Directory where the file will be placed, if it does not exists - # we create it. The option 'parents=True' will create the parents - # directories if they don't exist, and if some of them exists, - # the option 'exist_ok=True' will ignore them. - if write and not final_path.parent.is_dir(): - if not opt_quiet: - log.info(f"Creating directories for {final_path.parent}") - final_path.parent.mkdir(parents=True, exist_ok=True) - # Change .cpp to .py # TODO: # - What do we do with .h in case both .cpp and .h exists with |