contentdb/app/tasks/minetestcheck/__init__.py

69 lines
2.0 KiB
Python
Raw Normal View History

2023-06-19 20:32:36 +02:00
# ContentDB
# Copyright (C) 2018-23 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-01-19 02:22:33 +01:00
from enum import Enum
2023-06-19 20:32:36 +02:00
2020-01-19 02:22:33 +01:00
class MinetestCheckError(Exception):
def __init__(self, value):
self.value = value
2023-06-19 20:32:36 +02:00
2020-01-19 02:22:33 +01:00
def __str__(self):
return repr("Error validating package: " + self.value)
2023-06-19 20:32:36 +02:00
2020-01-19 02:22:33 +01:00
class ContentType(Enum):
UNKNOWN = "unknown"
MOD = "mod"
MODPACK = "modpack"
GAME = "game"
TXP = "texture pack"
2023-06-19 20:32:36 +02:00
def is_mod_like(self):
2020-01-19 02:22:33 +01:00
return self == ContentType.MOD or self == ContentType.MODPACK
def validate_same(self, other):
"""
2023-06-18 23:07:46 +02:00
Whether `other` is an acceptable type for this
2020-01-19 02:22:33 +01:00
"""
2020-12-04 03:23:04 +01:00
assert other
2020-01-19 02:22:33 +01:00
if self == ContentType.MOD:
2023-06-19 20:32:36 +02:00
if not other.is_mod_like():
2020-08-18 14:34:04 +02:00
raise MinetestCheckError("Expected a mod or modpack, found " + other.value)
2020-01-19 02:22:33 +01:00
elif self == ContentType.TXP:
if other != ContentType.UNKNOWN and other != ContentType.TXP:
raise MinetestCheckError("expected a " + self.value + ", found a " + other.value)
elif other != self:
2020-08-18 14:34:04 +02:00
raise MinetestCheckError("Expected a " + self.value + ", found a " + other.value)
2020-01-19 02:22:33 +01:00
from .tree import PackageTreeNode, get_base_dir
2023-06-19 20:32:36 +02:00
2024-03-03 02:13:05 +01:00
def build_tree(path, expected_type=None, author=None, repo=None, name=None, strict: bool = True):
2020-01-19 02:22:33 +01:00
path = get_base_dir(path)
2024-03-03 02:13:05 +01:00
root = PackageTreeNode(path, "/", author=author, repo=repo, name=name, strict=strict)
2020-12-04 03:23:04 +01:00
assert root
2020-01-19 02:22:33 +01:00
if expected_type:
expected_type.validate_same(root.type)
return root