#!/usr/bin/env python3 import sys import os import shutil def echo0(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) def add_depends(mod_path): mod_dir_name = os.path.basename(mod_path) modpack_conf = os.path.join(mod_path, "modpack.conf") modpack_txt = os.path.join(mod_path, "modpack.txt") found_modpack_file = None if os.path.isfile(modpack_txt): found_modpack_file = "modpack.txt" if os.path.isfile(modpack_conf): found_modpack_file = "modpack.conf" if not os.path.isfile(modpack_txt): # shutil.copy(modpack_conf, modpack_txt) with open(modpack_txt, 'w') as outs: wroteCount = 0 lineN = 0 with open(modpack_conf, 'r') as ins: for rawL in ins: lineN += 1 # Start counting at 1. line = rawL.strip() signI = line.find("=") if signI < 1: continue name = line[:signI].strip() value = line[signI+1:].strip() if name == "description": outs.write(value) wroteCount += 1 pass if wroteCount < 1: outs.write("\n") # ^ The modpack.txt file just needs to exist. print("* created {}/modpack.txt" "".format(mod_dir_name)) else: echo0("{}/modpack.txt already exists for compatibility." "".format(mod_dir_name)) if os.path.isfile(modpack_txt): echo0("{}/{} indicates it is a modpack." "".format(mod_dir_name, found_modpack_file)) for sub in os.listdir(mod_path): if sub.startswith('.'): continue subPath = os.path.join(mod_path, sub) add_depends(subPath) # It must be a modpack, so return after doing subdirectories. return 1 depends_path = os.path.join(mod_path, "depends.txt") description_path = os.path.join(mod_path, "description.txt") if os.path.isfile(depends_path): echo0("WARNING: Writing {} will be skipped since it exists." "".format(depends_path)) return 0 mod_conf = os.path.join(mod_path, "mod.conf") if not os.path.isfile(mod_conf): echo0("WARNING: Writing {} will be skipped since {} does" " not exist." "".format(depends_path, mod_conf)) return 1 optional_depends = None depends = None description = None lineN = 0 with open(mod_conf, 'r') as ins: for rawL in ins: lineN += 1 # Counting numbers start at 1. line = rawL.strip() if len(line) < 1: continue if line.startswith("#"): continue signI = line.find("=") if signI < 0: print("{}:{}: Warning: There is no '='" "".format(mod_conf, lineN)) continue if signI < 1: print("{}:{}: Warning: starts with '='" "".format(mod_conf, lineN)) continue name = line[:signI].strip() value = line[signI+1:].strip() values = [depend.strip() for depend in value.split(",")] if name == "depends": depends = values elif name == "optional_depends": optional_depends = values elif name == "description": description = value print("") print("mod: {}".format(mod_dir_name)) print("* depends: {}".format(depends)) print("* optional_depends: {}".format(optional_depends)) depends_count = 0 if depends is not None: depends_count += len(depends) if optional_depends is not None: depends_count += len(optional_depends) if depends_count > 0: with open(depends_path, 'w') as outs: if depends is not None: for depend in depends: outs.write("{}\n".format(depend)) if optional_depends is not None: for depend in optional_depends: outs.write("{}?\n".format(depend)) print("* wrote {}/depends.txt".format(mod_dir_name)) if description is not None: if os.path.isfile(description_path): print("* INFO: There is already a description.txt so it" " will be left intact.") return 0 with open(description_path, 'w') as outs: outs.write("{}\n".format(description)) print("* wrote {}/description.txt".format(mod_dir_name)) return 0 def main(): parent = os.path.realpath(".") return add_depends(parent) if __name__ == "__main__": sys.exit(main())