mt_convert_conf/prepare_conf.py

213 lines
6.7 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Lancer le script depuis un dossier "mods", ou utiliser [-p --path] pour indiquer le dossier.
Utiliser [-m, --mod] si c'est pour vérifier un mod en particulier.
Utiliser [-c, --check] pour ajouter aussi le contenu brut de depends.txt et description.txt dans le mod.conf pour pouvoir vérifier et compléter le fichier mod.conf
Sans [-m, --mod] il va créer le fichier "prepared_modconf" en dehors de mods(à coté) puis remplir les mod.conf automatiquement pour tout les mod qui n'en n'ont pas et
les mettres dedans avec le nom(du modpack si c'est un modpack), le nom du mod, formaté ainsi "modpack-modname-mod.conf".
"""
import sys, os
import argparse
VERBOSE = False
CHECK = False
ignore = ["locale", "models", "sounds", "textures", "src"]
def my_print(data):
if VERBOSE:
print(data)
sys.stdout.flush()
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, write_inside=False):
conf = Mod_conf()
modname = os.path.basename(mod_name)
my_print("Prepare check mod dir: %s, mod name: %s" % (mod_name, 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):
if write_inside:
print("le fichier mod.conf %s existe déjà!" % file_conf)
return (0)
if os.path.exists(file_depends):
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()
if write_inside:
new_file_conf = "mod.conf"
else:
new_file_conf = "%s-mod.conf" % (mod_name.replace('/', '-'))
with open(os.path.join(prepared_path, new_file_conf) , "w") as f:
f.write(conf.get_formated())
if CHECK:
f.write('\n\n\n\n### INFO VERIFICATION ###\n# depends.txt\n')
for line in data_depends:
f.write(line)
f.write('\n### description.txt\n')
f.write(data_description)
my_print("Ajouté: %s." % new_file_conf)
return (1)
else:
print("%s: Ce dossier n'a pas de fichier depends.txt, ce n'est pas un mod!" % modname)
return (0)
def check_global(path):
total = 0
my_print("path %s" % path)
os.chdir(path)
prepared_path = os.path.join(os.path.dirname(path),"prepared_modconf")
mod_path = os.path.basename(path)
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)
#print(mods)
for mod_name in mods:
path_mod = os.path.join(path, mod_name)
if os.path.isdir(mod_name) and not mod_name.startswith(".") and mod_name not in ignore:
if os.path.exists( os.path.join(mod_name, "modpack.txt")) or os.path.exists( os.path.join(mod_name, "modpack.conf")): #it's a modpack
my_print("C'est un 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 not modpack_name.startswith(".") and modpack_name not in ignore:
total += prepare( os.path.join(mod_name, modpack_name), prepared_path)
else:
total += prepare(mod_name, prepared_path)
if total > 0:
print("%s fichier(s) mod.conf préparé(s), dans %s." % (total, prepared_path) )
else:
print("Aucun mod.conf fait.")
def check_mod(path):
os.chdir(path)
if os.path.exists(os.path.join(path, "modpack.txt")) or os.path.exists(os.path.join(path, "modpack.conf")): #it's a modpack
my_print("C'est un modpack: %s" % path)
modspack = os.listdir(path)
for modpack_name in modspack:
if os.path.isdir(os.path.join(path, modpack_name)) and not modpack_name.startswith(".") and modpack_name not in ignore:
prepared_path = os.path.join(path, modpack_name)
prepare( prepared_path, prepared_path, True)
else:
prepare(path, path, True)
if __name__ == '__main__':
path = os.getcwd()
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="Affiche plus de messages",
action="store_true")
parser.add_argument("-p", "--path", help="dossier de recherche")
parser.add_argument("-m", "--mod", help="cherche dans un dossier de mod seul et ajoute le mod.conf dedans",
action="store_true")
parser.add_argument("-c", "--check", help="ajoute les infos de verification(contenu de depends.txt et description.txt) pour controler/completer le fichier manuellement",
action="store_true")
args = parser.parse_args()
if args.verbose:
VERBOSE = True
if args.check:
CHECK = True
if args.path:
if os.path.exists(args.path) and os.path.isdir(args.path):
path = args.path
else:
print("Erreur: '%s' n'est pas un dossier ou n'existe pas." % args.path)
sys.exit(1)
if args.mod:
print("Check un dossier mod seulement dans: %s." % path)
check_mod(path)
else:
mod_path = os.path.basename(path)
if mod_path != "mods":
if args.path:
print("path doit être un dossier 'mods' ou utiliser [-m, --mod] pour un mod specific!")
else:
print("Doit être lancer en etant dans un dossier 'mods' ou utiliser [-m, --mod] pour un mod specifique!")
sys.exit(1)
print("Check depuis le repertoire mod %s" % path)
check_global(path)