From 7003dd1bf2b330619ead1103b824ac2d6bb0ca7a Mon Sep 17 00:00:00 2001 From: Alex <4lexKidd@iamsander.de> Date: Sun, 25 Jun 2023 21:38:48 +0200 Subject: [PATCH] Add Config + Prepper Func --- config.ini | 7 ++++++ rom_prepper.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 config.ini create mode 100644 rom_prepper.py diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..b738662 --- /dev/null +++ b/config.ini @@ -0,0 +1,7 @@ +[Settings] +source_root_dir = roms_source/ +target_root_dir = roms_prep/ +max_depth = 2 + +[FileExtensions] +extensions = .smc, .sfc \ No newline at end of file diff --git a/rom_prepper.py b/rom_prepper.py new file mode 100644 index 0000000..7378331 --- /dev/null +++ b/rom_prepper.py @@ -0,0 +1,58 @@ +import os +import pathlib +import shutil +import configparser + + +def load_path_config(): + conf = configparser.ConfigParser() + root_dir = pathlib.Path(__file__).parent.absolute() + conf_dir = root_dir / "config.ini" + conf.read(conf_dir) + paths = conf['Paths'] + return paths + + +def replicate_directory_structure(source_path:str, target_path:str, file_extensions, max_depth=None): + for root, dirs, files in os.walk(source_path): + # Get the relative path of the current directory + relative_path = os.path.relpath(root, source_path) + target_dir = os.path.join(target_path, relative_path) + + # Calculate the depth of the current directory + current_depth = relative_path.count(os.sep) + + # Create the corresponding directory in the target path + corr_path = target_dir.split('[')[0] + os.makedirs(corr_path, exist_ok=True) + + # Copy files with the specified extensions to the target directory + for file in files: + if any(file.endswith(ext) for ext in file_extensions): + source_file = os.path.join(root, file) + target_file = os.path.join(corr_path, file) + shutil.copy2(source_file, target_file) + + # Check if the maximum depth has been reached + if max_depth is not None and current_depth >= max_depth: + del dirs[:] # Do not traverse deeper into subdirectories + + +if __name__ == "__main__": + + config_file = 'config.ini' + + # Read settings from the config file + config = configparser.ConfigParser() + config.read(config_file) + source_directory = config.get('Settings', 'source_root_dir') + target_directory = config.get('Settings', 'target_root_dir') + max_depth = config.getint('Settings', 'max_depth') + + # Read file extensions from the config file + file_extensions = [ext.strip() for ext in config.get('FileExtensions', 'extensions').split(',')] + + print('Scan Roms...') + replicate_directory_structure(source_directory, target_directory, file_extensions, max_depth) + print(f'Scan Completed. Prepped Folder Structure saved at {target_directory}') + \ No newline at end of file