[rtems-central commit] util: Add copy_file()

Sebastian Huber sebh at rtems.org
Tue Nov 21 13:35:40 UTC 2023


Module:    rtems-central
Branch:    master
Commit:    3fc5156844f626c33be7931deb4ef73dba13275b
Changeset: http://git.rtems.org/rtems-central/commit/?id=3fc5156844f626c33be7931deb4ef73dba13275b

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Nov 21 11:13:15 2023 +0100

util: Add copy_file()

Add logging to copy_files().

---

 rtems_spec_to_x.py           |  2 +-
 rtemsspec/tests/test_util.py | 26 +++++++++++++++++++-------
 rtemsspec/util.py            | 19 ++++++++++++++-----
 3 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/rtems_spec_to_x.py b/rtems_spec_to_x.py
index 77b6ebf6..7ff3d561 100755
--- a/rtems_spec_to_x.py
+++ b/rtems_spec_to_x.py
@@ -39,7 +39,7 @@ def _run_pre_qualified_only_build(config: dict, item_cache: ItemCache) -> None:
     files = rtemsspec.build.gather_files(config, item_cache)
     source_dir = config["source-directory"]
     workspace_dir = config["workspace-directory"]
-    rtemsspec.util.copy_files(source_dir, workspace_dir, files)
+    rtemsspec.util.copy_files(source_dir, workspace_dir, files, "workspace")
     with open(os.path.join(workspace_dir, "config.ini"), "w",
               encoding="utf-8") as config_ini:
         content = string.Template(config["config-ini"]).substitute(config)
diff --git a/rtemsspec/tests/test_util.py b/rtemsspec/tests/test_util.py
index 35d484d0..3eab1641 100644
--- a/rtemsspec/tests/test_util.py
+++ b/rtemsspec/tests/test_util.py
@@ -27,18 +27,30 @@
 import os
 import logging
 
-from rtemsspec.util import copy_files, create_argument_parser, base64_to_hex, \
-    init_logging, load_config, run_command
+from rtemsspec.util import copy_file, copy_files, create_argument_parser, \
+    base64_to_hex, init_logging, load_config, run_command
 from rtemsspec.tests.util import get_and_clear_log
 
 
-def test_copy_files(tmpdir):
+def test_copy_files(caplog, tmpdir):
+    caplog.set_level(logging.INFO)
     src_dir = os.path.dirname(__file__)
-    copy_files(src_dir, tmpdir, [])
+    copy_files(src_dir, tmpdir, [], "uid")
     filename = "config/c/d.yml"
-    assert not os.path.exists(os.path.join(tmpdir, filename))
-    copy_files(src_dir, tmpdir, [filename])
-    assert os.path.exists(os.path.join(tmpdir, filename))
+    dst_dir = os.path.join(tmpdir, "1")
+    assert not os.path.exists(os.path.join(dst_dir, filename))
+    copy_files(src_dir, dst_dir, [filename], "uid")
+    assert os.path.exists(os.path.join(dst_dir, filename))
+    assert get_and_clear_log(caplog) == (
+        f"INFO uid: copy '{src_dir}"
+        f"/config/c/d.yml' to '{dst_dir}/config/c/d.yml'")
+    src_file = os.path.join(src_dir, filename)
+    dst_file = os.path.join(tmpdir, "2", filename)
+    assert not os.path.exists(dst_file)
+    copy_file(src_file, dst_file, "uid")
+    assert os.path.exists(dst_file)
+    assert get_and_clear_log(
+        caplog) == f"INFO uid: copy '{src_file}' to '{dst_file}'"
 
 
 def test_base64_to_hex():
diff --git a/rtemsspec/util.py b/rtemsspec/util.py
index 851a7a25..65db817c 100644
--- a/rtemsspec/util.py
+++ b/rtemsspec/util.py
@@ -41,17 +41,26 @@ def base64_to_hex(data: str) -> str:
     return binascii.hexlify(binary).decode('ascii')
 
 
-def copy_files(src_dir: str, dst_dir: str, files: List[str]) -> None:
+def copy_file(src_file: str, dst_file: str, log_context: str) -> None:
+    """ Copies the source file to the destination file. """
+    os.makedirs(os.path.dirname(dst_file), exist_ok=True)
+    logging.info("%s: copy '%s' to '%s'", log_context, src_file, dst_file)
+    shutil.copy2(src_file, dst_file)
+
+
+def copy_files(src_dir: str, dst_dir: str, files: List[str],
+               log_context: str) -> None:
     """
     Copies a list of files in the source directory to the destination
     directory preserving the directory of the files relative to the source
     directory.
     """
     for a_file in files:
-        src = os.path.join(src_dir, a_file)
-        dst = os.path.join(dst_dir, a_file)
-        os.makedirs(os.path.dirname(dst), exist_ok=True)
-        shutil.copy2(src, dst)
+        src_file = os.path.join(src_dir, a_file)
+        dst_file = os.path.join(dst_dir, a_file)
+        os.makedirs(os.path.dirname(dst_file), exist_ok=True)
+        logging.info("%s: copy '%s' to '%s'", log_context, src_file, dst_file)
+        shutil.copy2(src_file, dst_file)
 
 
 def load_config(config_filename: str) -> Any:



More information about the vc mailing list