From aec76ca07d7cb39d6149fc5c7befa2226f1479ec Mon Sep 17 00:00:00 2001 From: Crabman77 Date: Thu, 7 Jul 2022 00:02:12 +0200 Subject: [PATCH] add python script to prepare all mod.conf --- prepare_conf.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100755 prepare_conf.py diff --git a/prepare_conf.py b/prepare_conf.py new file mode 100755 index 0000000..20590bc --- /dev/null +++ b/prepare_conf.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- + +import sys, os + +ignore = [".git", "locale", "models", "sounds", "textures", "src"] + + +""" +lancer le script depuis un dossier "mods", il va creer le fichier "prepared_modconf" en dehors de mods(à coté) puis remplir le mod.conf automatiquement +et le mettre dedans avec le path(du modpack si c'est un modpack), le nom du mod "modpack-mod-mod.conf" + +""" + + +class Mod_conf: + def __init__(self): + self.name = "" + self.description = "" + self.depends = [] + self.optional_depends = [] + + def set_name(self, txt): + self.name = txt.strip() + + def set_depends(self, ltxt): + for txt in ltxt.split(","): + txt = txt.strip() + if len(txt) > 1 and not txt in self.depends: + self.depends.append(txt) + + def set_optional_depends(self, ltxt): + for txt in ltxt.split(","): + txt = txt.strip() + if len(txt) > 1 and not txt in self.optional_depends: + self.optional_depends.append(txt) + + def add_depends(self, txt): + if len(txt) > 1 and txt not in self.depends: + self.depends.append(txt) + + def add_optional_depends(self, txt): + if len(txt) > 1 and txt not in self.optional_depends: + self.optional_depends.append(txt) + + def set_description(self, txt): + self.description = txt + + def get_name(self): + return(self.name) + + def get_depends(self): + return(', '.join(self.depends)) + + def get_optional_depends(self): + return(', '.join(self.optional_depends)) + + def get_description(self): + return(self.description) + + def get_formated(self): + data = "name = %s\ndepends = %s\noptional_depends = %s\ndescription = %s" % (self.get_name(), self.get_depends(), self.get_optional_depends(), self.get_description()) + return(data) + + +def prepare(mod_name, prepared_path): + conf = Mod_conf() + modname = os.path.basename(mod_name) + #print(modname) + file_conf = os.path.join(mod_name, "mod.conf") + file_depends = os.path.join(mod_name, "depends.txt") + file_description = os.path.join(mod_name, "description.txt") + if os.path.exists(file_conf): + return + if os.path.exists(file_depends): + file_conf = "%s-mod.conf" % (mod_name.replace('/', '-')) + #print(file_conf) + #print("file_depends: %s" % file_depends) + #print("## exist") + data_depends = [] + data_description = "" + with open(file_depends) as f: + data_depends = f.readlines() + for v in data_depends: + v = v.strip() + if len(v) > 2: + if v.endswith('?'): + conf.add_optional_depends(v[:-1]) + else: + conf.add_depends(v) + conf.set_name(modname) + if os.path.exists(file_description): + with open(file_description) as f: + data_description = f.read() + + + with open(os.path.join(prepared_path, file_conf) , "w") as f: + f.write(conf.get_formated()) + f.write('\n\n\n\n### INFO DE VERIF ###\n# depends.txt\n') + for line in data_depends: + f.write(line) + f.write('\n### description.txt\n') + f.write(data_description) + + +def check(): + path = os.getcwd() + print("path %s" % path) + prepared_path = os.path.join(os.path.dirname(path),"prepared_modconf") + mod_path = os.path.basename(path) + + if mod_path != "mods": + print("Doit être lancer en etant dans un dossier 'mods'!") + sys.exit(1) + if not os.path.exists(prepared_path): + try: + os.mkdir(prepared_path) + except OSError as error: + print(error) + sys.exit(1) + print("Recherche de fichiers depends et description pour convertir en mod.conf.") + + mods = os.listdir(path) + for mod_name in mods: + if os.path.isdir(mod_name) and mod_name not in ignore: + if os.path.exists( os.path.join(mod_name, "modpack.txt")): #it's a modpack + #print(" it's a modpack %s" % mod_name) + modspack = os.listdir(mod_name) + for modpack_name in modspack: + if os.path.isdir(os.path.join(mod_name, modpack_name)) and modpack_name not in ignore: + #print("#### %s ####" % modpack_name) + prepare( os.path.join(mod_name, modpack_name), prepared_path) + else: + prepare(mod_name, prepared_path) + print("les fichiers mod.conf à faire si il y en a, sont dans %s" % prepared_path) + +if __name__ == '__main__': + check()