changeset 3565:d66a8453b02b

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.
author Goffi <goffi@goffi.org>
date Sat, 12 Jun 2021 15:10:49 +0200 (2021-06-12)
parents 2c9e95796371
children 17b69a111a85
files sat/plugins/plugin_misc_app_manager.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_app_manager.py	Fri Jun 11 17:39:59 2021 +0200
+++ b/sat/plugins/plugin_misc_app_manager.py	Sat Jun 12 15:10:49 2021 +0200
@@ -548,6 +548,24 @@
         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 @@
         """
         log.info(f"starting {app_data['name']!r}")
         await self._doPrepare(app_data)
+        await self._doCreateFiles(app_data)