2020-08-18 14:34:04 +02:00
|
|
|
import os, re
|
2020-01-19 02:22:33 +01:00
|
|
|
from . import MinetestCheckError, ContentType
|
|
|
|
from .config import parse_conf
|
|
|
|
|
2020-08-18 14:34:04 +02:00
|
|
|
basenamePattern = re.compile("^([a-z0-9_]+)$")
|
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
def get_base_dir(path):
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
raise IOError("Expected dir")
|
|
|
|
|
|
|
|
root, subdirs, files = next(os.walk(path))
|
|
|
|
if len(subdirs) == 1 and len(files) == 0:
|
|
|
|
return get_base_dir(path + "/" + subdirs[0])
|
|
|
|
else:
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def detect_type(path):
|
|
|
|
if os.path.isfile(path + "/game.conf"):
|
|
|
|
return ContentType.GAME
|
|
|
|
elif os.path.isfile(path + "/init.lua"):
|
|
|
|
return ContentType.MOD
|
|
|
|
elif os.path.isfile(path + "/modpack.txt") or \
|
|
|
|
os.path.isfile(path + "/modpack.conf"):
|
|
|
|
return ContentType.MODPACK
|
|
|
|
elif os.path.isdir(path + "/mods"):
|
|
|
|
return ContentType.GAME
|
|
|
|
elif os.path.isfile(path + "/texture_pack.conf"):
|
|
|
|
return ContentType.TXP
|
|
|
|
else:
|
|
|
|
return ContentType.UNKNOWN
|
|
|
|
|
|
|
|
|
2020-08-18 12:43:13 +02:00
|
|
|
def get_csv_line(line):
|
2020-08-18 14:57:56 +02:00
|
|
|
if line is None:
|
|
|
|
return []
|
|
|
|
|
2020-08-18 12:43:13 +02:00
|
|
|
return [x.strip() for x in line.split(",") if x.strip() != ""]
|
|
|
|
|
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
class PackageTreeNode:
|
|
|
|
def __init__(self, baseDir, relative, author=None, repo=None, name=None):
|
|
|
|
self.baseDir = baseDir
|
|
|
|
self.relative = relative
|
|
|
|
self.author = author
|
|
|
|
self.name = name
|
|
|
|
self.repo = repo
|
|
|
|
self.meta = None
|
|
|
|
self.children = []
|
|
|
|
|
|
|
|
# Detect type
|
|
|
|
self.type = detect_type(baseDir)
|
|
|
|
self.read_meta()
|
|
|
|
|
|
|
|
if self.type == ContentType.GAME:
|
|
|
|
if not os.path.isdir(baseDir + "/mods"):
|
2020-12-04 03:23:04 +01:00
|
|
|
raise MinetestCheckError("Game at {} does not have a mods/ folder".format(self.relative))
|
2020-08-18 14:57:56 +02:00
|
|
|
self.add_children_from_mod_dir("mods")
|
2020-08-18 14:34:04 +02:00
|
|
|
elif self.type == ContentType.MOD:
|
|
|
|
if self.name and not basenamePattern.match(self.name):
|
2020-12-04 03:23:04 +01:00
|
|
|
raise MinetestCheckError("Invalid base name for mod {} at {}, names must only contain a-z0-9_." \
|
|
|
|
.format(self.name, self.relative))
|
2020-01-19 02:22:33 +01:00
|
|
|
elif self.type == ContentType.MODPACK:
|
2020-08-18 14:57:56 +02:00
|
|
|
self.add_children_from_mod_dir(None)
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-08-18 14:34:04 +02:00
|
|
|
|
2021-01-16 19:19:51 +01:00
|
|
|
def getMetaFileName(self):
|
2020-06-03 18:46:59 +02:00
|
|
|
if self.type == ContentType.GAME:
|
2021-01-16 19:19:51 +01:00
|
|
|
return "game.conf"
|
2020-06-03 18:46:59 +02:00
|
|
|
elif self.type == ContentType.MOD:
|
2021-01-16 19:19:51 +01:00
|
|
|
return "mod.conf"
|
2020-06-03 18:46:59 +02:00
|
|
|
elif self.type == ContentType.MODPACK:
|
2021-01-16 19:19:51 +01:00
|
|
|
return "modpack.conf"
|
2020-06-03 18:46:59 +02:00
|
|
|
elif self.type == ContentType.TXP:
|
2021-01-16 19:19:51 +01:00
|
|
|
return "texture_pack.conf"
|
2020-06-03 18:46:59 +02:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
|
|
|
|
def read_meta(self):
|
|
|
|
result = {}
|
|
|
|
|
2021-01-21 21:47:44 +01:00
|
|
|
# Read .conf file
|
2021-01-16 19:19:51 +01:00
|
|
|
meta_file_name = self.getMetaFileName()
|
2021-01-21 21:47:44 +01:00
|
|
|
if meta_file_name is not None:
|
|
|
|
meta_file_rel = self.relative + meta_file_name
|
|
|
|
meta_file_path = self.baseDir + "/" + meta_file_name
|
|
|
|
try:
|
|
|
|
with open(meta_file_path or "", "r") as myfile:
|
|
|
|
conf = parse_conf(myfile.read())
|
|
|
|
for key, value in conf.items():
|
|
|
|
result[key] = value
|
|
|
|
except SyntaxError as e:
|
|
|
|
raise MinetestCheckError("Error while reading {}: {}".format(meta_file_rel , e.msg))
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if "release" in result:
|
|
|
|
raise MinetestCheckError("{} should not contain 'release' key, as this is for use by ContentDB only.".format(meta_file_rel))
|
|
|
|
|
2021-01-16 19:19:51 +01:00
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
# description.txt
|
|
|
|
if not "description" in result:
|
|
|
|
try:
|
|
|
|
with open(self.baseDir + "/description.txt", "r") as myfile:
|
|
|
|
result["description"] = myfile.read()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
2020-08-18 14:57:56 +02:00
|
|
|
# Read dependencies
|
2020-08-18 12:43:13 +02:00
|
|
|
if "depends" in result or "optional_depends" in result:
|
2020-08-18 14:57:56 +02:00
|
|
|
result["depends"] = get_csv_line(result.get("depends"))
|
|
|
|
result["optional_depends"] = get_csv_line(result.get("optional_depends"))
|
|
|
|
|
|
|
|
elif os.path.isfile(self.baseDir + "/depends.txt"):
|
|
|
|
pattern = re.compile("^([a-z0-9_]+)\??$")
|
|
|
|
|
|
|
|
with open(self.baseDir + "/depends.txt", "r") as myfile:
|
|
|
|
contents = myfile.read()
|
|
|
|
soft = []
|
|
|
|
hard = []
|
|
|
|
for line in contents.split("\n"):
|
|
|
|
line = line.strip()
|
|
|
|
if pattern.match(line):
|
|
|
|
if line[len(line) - 1] == "?":
|
|
|
|
soft.append( line[:-1])
|
|
|
|
else:
|
|
|
|
hard.append(line)
|
|
|
|
|
|
|
|
result["depends"] = hard
|
|
|
|
result["optional_depends"] = soft
|
2020-08-18 12:43:13 +02:00
|
|
|
|
|
|
|
else:
|
2020-08-18 14:57:56 +02:00
|
|
|
result["depends"] = []
|
|
|
|
result["optional_depends"] = []
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-08-18 14:57:56 +02:00
|
|
|
|
2020-08-18 19:10:42 +02:00
|
|
|
def checkDependencies(deps):
|
|
|
|
for dep in result["depends"]:
|
|
|
|
if not basenamePattern.match(dep):
|
|
|
|
if " " in dep:
|
2020-12-04 03:23:04 +01:00
|
|
|
raise MinetestCheckError("Invalid dependency name '{}' for mod at {}, did you forget a comma?" \
|
2020-08-18 19:10:42 +02:00
|
|
|
.format(dep, self.relative))
|
|
|
|
else:
|
2020-12-04 03:23:04 +01:00
|
|
|
raise MinetestCheckError(
|
|
|
|
"Invalid dependency name '{}' for mod at {}, names must only contain a-z0-9_." \
|
|
|
|
.format(dep, self.relative))
|
2020-08-18 14:57:56 +02:00
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-08-18 19:10:42 +02:00
|
|
|
# Check dependencies
|
|
|
|
checkDependencies(result["depends"])
|
|
|
|
checkDependencies(result["optional_depends"])
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-08-18 01:25:13 +02:00
|
|
|
# Fix games using "name" as "title"
|
|
|
|
if self.type == ContentType.GAME:
|
|
|
|
result["title"] = result["name"]
|
|
|
|
del result["name"]
|
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
# Calculate Title
|
|
|
|
if "name" in result and not "title" in result:
|
|
|
|
result["title"] = result["name"].replace("_", " ").title()
|
|
|
|
|
|
|
|
# Calculate short description
|
|
|
|
if "description" in result:
|
|
|
|
desc = result["description"]
|
|
|
|
idx = desc.find(".") + 1
|
|
|
|
cutIdx = min(len(desc), 200 if idx < 5 else idx)
|
|
|
|
result["short_description"] = desc[:cutIdx]
|
|
|
|
|
|
|
|
if "name" in result:
|
|
|
|
self.name = result["name"]
|
|
|
|
del result["name"]
|
|
|
|
|
|
|
|
self.meta = result
|
|
|
|
|
2020-08-18 14:57:56 +02:00
|
|
|
def add_children_from_mod_dir(self, subdir):
|
|
|
|
dir = self.baseDir
|
|
|
|
relative = self.relative
|
|
|
|
if subdir:
|
|
|
|
dir += "/" + subdir
|
|
|
|
relative += subdir + "/"
|
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
for entry in next(os.walk(dir))[1]:
|
|
|
|
path = os.path.join(dir, entry)
|
|
|
|
if not entry.startswith('.') and os.path.isdir(path):
|
2020-08-18 14:57:56 +02:00
|
|
|
child = PackageTreeNode(path, relative + entry + "/", name=entry)
|
2020-01-19 02:22:33 +01:00
|
|
|
if not child.type.isModLike():
|
2020-12-04 03:23:04 +01:00
|
|
|
raise MinetestCheckError("Expecting mod or modpack, found {} at {} inside {}" \
|
|
|
|
.format(child.type.value, child.relative, self.type.value))
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-08-18 14:34:04 +02:00
|
|
|
if child.name is None:
|
2020-12-04 03:23:04 +01:00
|
|
|
raise MinetestCheckError("Missing base name for mod at {}".format(self.relative))
|
2020-08-18 14:34:04 +02:00
|
|
|
|
2020-01-19 02:22:33 +01:00
|
|
|
self.children.append(child)
|
|
|
|
|
2020-07-12 04:09:01 +02:00
|
|
|
def getModNames(self):
|
|
|
|
return self.fold("name", type=ContentType.MOD)
|
|
|
|
|
|
|
|
# attr: Attribute name
|
|
|
|
# key: Key in attribute
|
|
|
|
# retval: Accumulator
|
|
|
|
# type: Filter to type
|
|
|
|
def fold(self, attr, key=None, retval=None, type=None):
|
|
|
|
if retval is None:
|
|
|
|
retval = set()
|
|
|
|
|
|
|
|
# Iterate through children
|
|
|
|
for child in self.children:
|
|
|
|
child.fold(attr, key, retval, type)
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-07-12 04:09:01 +02:00
|
|
|
# Filter on type
|
|
|
|
if type and type != self.type:
|
|
|
|
return retval
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-07-12 04:09:01 +02:00
|
|
|
# Get attribute
|
2020-01-19 02:22:33 +01:00
|
|
|
at = getattr(self, attr)
|
2020-07-12 04:09:01 +02:00
|
|
|
if not at:
|
|
|
|
return retval
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-07-12 04:09:01 +02:00
|
|
|
# Get value
|
|
|
|
value = at if key is None else at.get(key)
|
2020-01-19 02:22:33 +01:00
|
|
|
if isinstance(value, list):
|
2020-07-12 04:09:01 +02:00
|
|
|
retval |= set(value)
|
|
|
|
elif value:
|
|
|
|
retval.add(value)
|
2020-01-19 02:22:33 +01:00
|
|
|
|
2020-07-12 04:09:01 +02:00
|
|
|
return retval
|
2020-01-19 02:22:33 +01:00
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
return self.meta.get(key)
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
for child in self.children:
|
|
|
|
child.validate()
|