From 98fb1b40914c6e0355742327db3d4482abab0a0f Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 12 Jun 2021 15:10:49 +0200 Subject: [PATCH] plugin app manager: add a way to create files: a `files` field can now be used to create files on the fly. Its value must be a dict where key are file names, and values are an other dict with file data. For now file data can only use the `content` key. Value of content will be written to the file. If a file already exists, it is not overwritten. --- sat/plugins/plugin_misc_app_manager.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sat/plugins/plugin_misc_app_manager.py b/sat/plugins/plugin_misc_app_manager.py index 009e746e..7015c1a7 100644 --- a/sat/plugins/plugin_misc_app_manager.py +++ b/sat/plugins/plugin_misc_app_manager.py @@ -548,6 +548,24 @@ class AppManager: if prepare: raise exceptions.InternalError('"prepare" should be empty') + async def _doCreateFiles( + self, + app_data: dict, + ) -> None: + dest_path = app_data['_instance_dir_path'] + files = app_data.get('files') + if not files: + return + if not isinstance(files, dict): + raise ValueError('"files" must be a dictionary') + for filename, data in files.items(): + path = dest_path / filename + if path.is_file(): + log.info(f"{path} already exists, skipping") + with path.open("w") as f: + f.write(data.get("content", "")) + log.debug(f"{path} created") + async def startCommon(self, app_data: dict) -> None: """Method running common action when starting a manager @@ -555,3 +573,4 @@ class AppManager: """ log.info(f"starting {app_data['name']!r}") await self._doPrepare(app_data) + await self._doCreateFiles(app_data)