48 lines
1.1 KiB
Python
48 lines
1.1 KiB
Python
"""
|
|
This modules provides functions to copy/move the files.
|
|
"""
|
|
|
|
import os
|
|
import logging
|
|
import shutil
|
|
|
|
module_logger = logging.getLogger(__name__)
|
|
|
|
def write_files(db
|
|
, output_basepath
|
|
, input_basepath
|
|
, move
|
|
, dry_run):
|
|
"""
|
|
Write the changes in the file structure on the disk.
|
|
"""
|
|
cursor = db.cursor()
|
|
|
|
result = cursor.execute(
|
|
'''SELECT DIRECTORIES.name AS pathname,
|
|
FILES.name AS filename
|
|
FROM FILES JOIN ASSOCIATIONS ON FILES.rowid=ASSOCIATIONS.file_id
|
|
JOIN DIRECTORIES ON DIRECTORIES.rowid=ASSOCIATIONS.directory_id
|
|
'''
|
|
)
|
|
|
|
for pathname, filename in result:
|
|
src_name = filename
|
|
dst_name = os.path.join(output_basepath, pathname, os.path.basename(filename))
|
|
|
|
if(dry_run):
|
|
module_logger.warning("COPY {} -> {}".format(src_name, dst_name))
|
|
if(move):
|
|
module_logger.warning("RM {}".format(src_name))
|
|
|
|
continue
|
|
|
|
module_logger.info("COPY {} -> {}".format(src_name, dst_name))
|
|
|
|
shutil.copyfile(src_name, dst_name)
|
|
shutil.copystat(src_name, dst_name)
|
|
|
|
if(move):
|
|
module_logger.info("RM {}".format(src_name))
|
|
os.remove(src_name)
|