LootDistributionGenerator/01_process_dump_data.py
2022-03-11 17:10:34 +01:00

72 lines
2.0 KiB
Python

import sys
import os
import shutil
import json
import py7zr
import yaml
from tqdm import tqdm
import subprocess
from pathlib import Path
with open('config/config.yaml', 'r') as fin:
config = yaml.load(fin, Loader=yaml.FullLoader)
exe_7z = config["tools"]["7z_exe"]
src = Path(config["archives"]["source_folder"])
dst = Path(config["archives"]["target_folder"])
archive_complete = Path(config["archives"]["backup_file"])
used = {
dst / config["archives"]["loot_filename"]: lambda x: x.startswith("resp.client.location.getLocalloot"),
dst / config["archives"]["bot_filename"]: lambda x: x.startswith("resp.client.game.bot.generate"),
}
input_files = set(os.listdir(src))
for archive_dst, filt in used.items():
mode = None
if os.path.exists(archive_dst):
mode = 'a'
with py7zr.SevenZipFile(archive_dst, 'r') as archive:
archive_files = set(archive.getnames())
else:
mode = 'w'
archive_files = []
# filter out every file which is already in the archive
files = input_files.difference(archive_files)
# get only those files which fit the recycle filters
print(f"Number of files which are not found in the archive: {len(files)}")
files = [
fi for fi in files
if filt(fi)
]
print(f"Of those according to filter add {len(files)} files to {archive_dst}")
# temporary copy to destination folder to add them via 7z subprocess
if len(files) > 0:
for file in tqdm(files):
shutil.copy(src / file, dst / file)
cmd = f"{exe_7z} a -t7z {archive_dst} *.json"
os.chdir(dst)
subprocess.call(cmd)
for file in tqdm(files):
os.remove(dst / file)
# add all files to backup archive (if they are not inside already)
cmd = f"{exe_7z} a -up1q1r2x1y1z1w1 -t7z {archive_complete} *.json"
os.chdir(src)
subprocess.call(cmd)
# delete raw data from source folder
files = os.listdir(src)
for file in tqdm(files):
os.remove(src / file)