aboutsummaryrefslogtreecommitdiff
path: root/app/tasks/minetestcheck/__init__.py
blob: deec46ed7ce00d3ce7f5bf068c2f816c05851696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from enum import Enum

class MinetestCheckError(Exception):
	def __init__(self, value):
		self.value = value
	def __str__(self):
		return repr("Error validating package: " + self.value)

class ContentType(Enum):
	UNKNOWN = "unknown"
	MOD = "mod"
	MODPACK = "modpack"
	GAME = "game"
	TXP = "texture pack"

	def isModLike(self):
		return self == ContentType.MOD or self == ContentType.MODPACK

	def validate_same(self, other):
		"""
		Whether or not `other` is an acceptable type for this
		"""
		assert(other)

		if self == ContentType.MOD:
			if not other.isModLike():
				raise MinetestCheckError("Expected a mod or modpack, found " + other.value)

		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:
			raise MinetestCheckError("Expected a " + self.value + ", found a " + other.value)


from .tree import PackageTreeNode, get_base_dir

def build_tree(path, expected_type=None, author=None, repo=None, name=None):
	path = get_base_dir(path)

	root = PackageTreeNode(path, "/", author=author, repo=repo, name=name)
	assert(root)

	if expected_type:
		expected_type.validate_same(root.type)

	return root