Skip to content
Snippets Groups Projects
Commit 6e6f124b authored by Oleksii Kutuzov's avatar Oleksii Kutuzov
Browse files

package_release: Add a way to copy the whole directory

Item should end with "/" to be treated as a directory
parent ab19eb61
No related branches found
No related tags found
1 merge request!388package_release: Add a way to copy the whole directory
Pipeline #118311 passed with stage
in 18 minutes and 49 seconds
...@@ -26,7 +26,20 @@ def generate_md5sums_file(input_files: list[str], output_file: str): ...@@ -26,7 +26,20 @@ def generate_md5sums_file(input_files: list[str], output_file: str):
# This might should be used somewhere before, # This might should be used somewhere before,
# but the previous state was not to fail when a file in # but the previous state was not to fail when a file in
# the list did not exist, like in copy files # 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) output_dir = os.path.dirname(output_file)
if output_dir: if output_dir:
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
...@@ -37,19 +50,29 @@ def generate_md5sums_file(input_files: list[str], output_file: str): ...@@ -37,19 +50,29 @@ def generate_md5sums_file(input_files: list[str], output_file: str):
def copy_files(files: list[str], target_dir: 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 Copy given files or directories 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. hierarchies on the input directories will be preserved. Only plain files are copied.
""" """
if target_dir is None: if target_dir is None:
return return
os.makedirs(target_dir, exist_ok=True) os.makedirs(target_dir, exist_ok=True)
for source_file in files: for source in files:
if os.path.exists(source_file): # Check if the source ends with "/"
target_file = os.path.join(target_dir, os.path.basename(source_file)) if source.endswith("/"):
print(f"Copy: {source_file} -> {target_file}") # If it does, copy the whole directory recursively
shutil.copyfile(source_file, target_file, follow_symlinks=True) 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: 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(): def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment