From 6e6f124b6aa90aa6eb41779c2a6ddaa12fa5ac25 Mon Sep 17 00:00:00 2001 From: Oleksii Kutuzov <oleksii.kutuzov@seco.com> Date: Thu, 4 Apr 2024 12:25:32 +0000 Subject: [PATCH] package_release: Add a way to copy the whole directory Item should end with "/" to be treated as a directory --- scripts/package_release.py | 41 +++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/scripts/package_release.py b/scripts/package_release.py index 3f311ebc..1b33414c 100755 --- a/scripts/package_release.py +++ b/scripts/package_release.py @@ -26,7 +26,20 @@ def generate_md5sums_file(input_files: list[str], output_file: str): # This might should be used somewhere before, # but the previous state was not to fail when a file in # the list did not exist, like in copy files - md5sums = {os.path.basename(f): md5(f) for f in input_files if os.path.exists(f)} + md5sums = {} + for item in input_files: + # Check if the item ends with "/" + if item.endswith("/"): + # If it does, recursively calculate MD5 sums for all files in the directory + for root, files in os.walk(item): + for file in files: + file_path = os.path.join(root, file) + md5sums[os.path.relpath(file_path, item)] = md5(file_path) + else: + # Otherwise, calculate MD5 sum for the file + if os.path.exists(item): + md5sums[os.path.basename(item)] = md5(item) + output_dir = os.path.dirname(output_file) if output_dir: os.makedirs(output_dir, exist_ok=True) @@ -37,19 +50,29 @@ def generate_md5sums_file(input_files: list[str], output_file: str): def copy_files(files: list[str], target_dir: str): """ - Copy given files to target_dir. Create target_dir, if it does not exist. Subfolder - hierarchies on the input files will not be preserved, only plain files are copied. + Copy given files or directories to target_dir. Create target_dir, if it does not exist. Subfolder + hierarchies on the input directories will be preserved. Only plain files are copied. """ if target_dir is None: return os.makedirs(target_dir, exist_ok=True) - for source_file in files: - if os.path.exists(source_file): - target_file = os.path.join(target_dir, os.path.basename(source_file)) - print(f"Copy: {source_file} -> {target_file}") - shutil.copyfile(source_file, target_file, follow_symlinks=True) + for source in files: + # Check if the source ends with "/" + if source.endswith("/"): + # If it does, copy the whole directory recursively + old_dir = os.path.abspath(source) + new_dir = os.path.join(target_dir, os.path.basename(source.rstrip("/"))) + print(f"Copy Directory: {old_dir} -> {new_dir}") + shutil.copytree(old_dir, new_dir) else: - print(f"Missing: {source_file}") + # Otherwise, treat it as a file and copy it directly + source_file = os.path.abspath(source) + if os.path.exists(source_file): + target_file = os.path.join(target_dir, os.path.basename(source_file)) + print(f"Copy: {source_file} -> {target_file}") + shutil.copyfile(source_file, target_file, follow_symlinks=True) + else: + print(f"Missing: {source_file}") def main(): -- GitLab