Add regex checking for dependency names

This commit is contained in:
rubenwardy 2020-08-18 13:57:56 +01:00
parent 0c81d0ae2b
commit 2f208d9239

@ -32,6 +32,9 @@ def detect_type(path):
def get_csv_line(line): def get_csv_line(line):
if line is None:
return []
return [x.strip() for x in line.split(",") if x.strip() != ""] return [x.strip() for x in line.split(",") if x.strip() != ""]
@ -52,13 +55,13 @@ class PackageTreeNode:
if self.type == ContentType.GAME: if self.type == ContentType.GAME:
if not os.path.isdir(baseDir + "/mods"): if not os.path.isdir(baseDir + "/mods"):
raise MinetestCheckError(("Game at {} does not have a mods/ folder").format(self.relative)) raise MinetestCheckError(("Game at {} does not have a mods/ folder").format(self.relative))
self.add_children_from_mod_dir(baseDir + "/mods") self.add_children_from_mod_dir("mods")
elif self.type == ContentType.MOD: elif self.type == ContentType.MOD:
if self.name and not basenamePattern.match(self.name): if self.name and not basenamePattern.match(self.name):
raise MinetestCheckError(("Invalid base name for mod {} at {}, names must only contain a-z0-9_.") \ raise MinetestCheckError(("Invalid base name for mod {} at {}, names must only contain a-z0-9_.") \
.format(self.name, self.relative)) .format(self.name, self.relative))
elif self.type == ContentType.MODPACK: elif self.type == ContentType.MODPACK:
self.add_children_from_mod_dir(baseDir) self.add_children_from_mod_dir(None)
def getMetaFilePath(self): def getMetaFilePath(self):
@ -97,35 +100,44 @@ class PackageTreeNode:
except IOError: except IOError:
pass pass
# depends.txt # Read dependencies
if "depends" in result or "optional_depends" in result: if "depends" in result or "optional_depends" in result:
if "depends" in result: result["depends"] = get_csv_line(result.get("depends"))
result["depends"] = get_csv_line(result["depends"]) result["optional_depends"] = get_csv_line(result.get("optional_depends"))
if "optional_depends" in result: elif os.path.isfile(self.baseDir + "/depends.txt"):
result["optional_depends"] = get_csv_line(result["optional_depends"]) 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
else: else:
try: result["depends"] = []
pattern = re.compile("^([a-z0-9_]+)\??$") result["optional_depends"] = []
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 # Check dependencies
result["optional_depends"] = soft for dep in result["depends"]:
if not basenamePattern.match(dep):
raise MinetestCheckError(("Invalid dependency name '{}' for mod at {}, names must only contain a-z0-9_.") \
.format(dep, self.relative))
except IOError: for dep in result["optional_depends"]:
pass if not basenamePattern.match(dep):
raise MinetestCheckError(("Invalid dependency name '{}' for mod at {}, names must only contain a-z0-9_.") \
.format(dep, self.relative))
# Fix games using "name" as "title" # Fix games using "name" as "title"
@ -150,11 +162,17 @@ class PackageTreeNode:
self.meta = result self.meta = result
def add_children_from_mod_dir(self, dir): def add_children_from_mod_dir(self, subdir):
dir = self.baseDir
relative = self.relative
if subdir:
dir += "/" + subdir
relative += subdir + "/"
for entry in next(os.walk(dir))[1]: for entry in next(os.walk(dir))[1]:
path = os.path.join(dir, entry) path = os.path.join(dir, entry)
if not entry.startswith('.') and os.path.isdir(path): if not entry.startswith('.') and os.path.isdir(path):
child = PackageTreeNode(path, self.relative + entry + "/", name=entry) child = PackageTreeNode(path, relative + entry + "/", name=entry)
if not child.type.isModLike(): if not child.type.isModLike():
raise MinetestCheckError(("Expecting mod or modpack, found {} at {} inside {}") \ raise MinetestCheckError(("Expecting mod or modpack, found {} at {} inside {}") \
.format(child.type.value, child.relative, self.type.value)) .format(child.type.value, child.relative, self.type.value))