summaryrefslogtreecommitdiff
path: root/rtemsspec
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2023-11-21 11:13:15 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2023-11-21 11:15:24 +0100
commit3fc5156844f626c33be7931deb4ef73dba13275b (patch)
tree134090690a324cd9ffb9c752a7a3b7716a4b22c8 /rtemsspec
parenta4d045b0a8d6548be58b3b8abb3509429758c89d (diff)
util: Add copy_file()
Add logging to copy_files().
Diffstat (limited to 'rtemsspec')
-rw-r--r--rtemsspec/tests/test_util.py26
-rw-r--r--rtemsspec/util.py19
2 files changed, 33 insertions, 12 deletions
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: