import os import shutil import tempfile import hashlib import time import zipfile # https://packs.steamwar.de/hello.txt current_dir = os.getcwd() def getVersions(): directories = [name for name in os.listdir(current_dir) if os.path.isdir(os.path.join(current_dir, name)) and not name.startswith(".")] packVersions = [] for s in directories: try: num = int(s) packVersions.append(num) except ValueError: pass packVersions.sort() return packVersions def buildPack(dir, version): current_path = os.path.join(current_dir, str(version)) copyFiles(current_path, dir) zip_folder(dir, current_path + ".zip") # shutil.make_archive(current_path, "zip", dir) current_path = current_path + ".zip" timestamp = time.mktime(time.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')) os.utime(current_path, (timestamp, timestamp)) sha1 = sha1_of_file(current_path) os.rename(current_path, os.path.join(current_dir, str(version) + "_" + sha1 + ".zip")) def copyFiles(src, dst): for root, dirs, files in os.walk(src): # Create corresponding path in destination rel_path = os.path.relpath(root, src) dest_path = os.path.join(dst, rel_path) os.makedirs(dest_path, exist_ok=True) for file in files: src_file = os.path.join(root, file) dst_file = os.path.join(dest_path, file) shutil.copy2(src_file, dst_file) # Overwrites if exists def zip_folder(folder_path, zip_file_path): with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(folder_path): for file in files: abs_path = os.path.join(root, file) rel_path = os.path.relpath(abs_path, folder_path) zipf.write(abs_path, rel_path) def sha1_of_file(filepath): sha1 = hashlib.sha1() with open(filepath, 'rb') as f: while chunk := f.read(8192): # read in 8KB chunks sha1.update(chunk) return sha1.hexdigest() if __name__ == "__main__": versions=getVersions() with tempfile.TemporaryDirectory() as tmpdirname: for version in versions: buildPack(tmpdirname, version)