Ok, I now have code that will turn a folder full od modded DF installs into a much smaller folder full of mods conforming with the format at the top of the thread. That is, 'LNP/Mods/$mod' contains only files with 'readme' or 'config' in the name, and a raw folder containing those files that are not identical to vanilla.
For Accelerated Modest Mod (40.08), that shrinks it from 10MB to 2MB. Not bad for such naive code!
import os
import shutil
import filecmp
# path from script to mods folder
mods_folder = 'LNP/Mods/'
mod_folders_list = []
for mod in os.listdir(mods_folder):
if mod.startswith('df_'):
# there must be only one folder beginning 'df_', the basis of comparison
vanilla_folder = mods_folder + mod
vanilla_raw_folder = vanilla_folder + '/raw/'
else:
mod_folders_list.append(mod)
def simplify_mod_and_df_folders():
for mod in os.listdir(mods_folder):
files_removed = 0
folders_removed = 0
for item in os.listdir(mods_folder + mod):
# delete anything top-level not containing string 'raw', 'readme', or 'config'
if item == 'raw':
pass
elif 'readme' in item.lower():
pass
elif 'config' in item.lower():
pass
elif os.path.isfile(mods_folder + mod + '/' + item):
os.remove(mods_folder + mod + '/' + item)
files_removed += 1
else:
shutil.rmtree(mods_folder + mod + '/' + item)
folders_removed += 1
if not files_removed + folders_removed == 0:
print(mod, 'folder simplified! (removed', files_removed, 'files and', folders_removed, 'folders)')
def remove_vanilla_files_from_mod_raws():
for mod in mod_folders_list:
mod_raw_folder = mods_folder + mod + '/raw/'
files_removed = 0
for file_tuple in os.walk(mod_raw_folder):
for item in file_tuple[2]:
item_path_str = os.path.join(file_tuple[0], item).replace('\\', '/').replace(mod_raw_folder, '')
if os.path.isfile(vanilla_raw_folder + item_path_str): # if the file exists in the vanilla raws
if filecmp.cmp(mod_raw_folder + item_path_str, vanilla_raw_folder + item_path_str): # and it's the same file
os.remove(mod_raw_folder + item_path_str)
files_removed += 1
if files_removed > 0:
print('\n' + mod + ':\n removed', files_removed, 'files identical to vanilla raws')
def make_raws_with_mods():
print('What mods do you want to load?')
for mod in mod_folders_list:
print(' ', mod_folders_list.index(mod), mod)
mods_to_load = input('Enter the indicies in load order seperated by spaces.\n ')
# gives ordered list with indicies as strings
mods_to_load = mods_to_load.split(' ')
print('Just kidding, this doesn\'t do anything yet!')
simple_folders_q = input('Do you want to simplify mod folders? (y/n)\n ')
if simple_folders_q == 'y':
simplify_mod_and_df_folders()
print(' Done!\n')
else:
print('Not simplifying mod folders\n')
remove_files_q = input('Do you want to remove files from mods which are identical to vanilla raws? (y/n)\n ')
print(remove_files_q)
if remove_files_q == 'y':
remove_vanilla_files_from_mod_raws()
print(' Done!\n')
else:
print('Not removing vanilla files\n')
make_raws_with_mods()