This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
util.py
95 lines (77 loc) · 2.57 KB
/
util.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/python
"""
Utils for the other python scripts
"""
import sys
from typing import Any
import subprocess
from packaging.version import Version
def should_show_mod(mod: dict[str, Any]) -> bool:
"""
Checks if mod should be shown.
Parameters:
mod: The mod in question
"""
# Exclude plugins and libraries from being shown
if "flags" in mod and (
"deprecated" in mod["flags"] or
"plugin" in mod["flags"] or
"file" in mod["flags"]
):
return False
# Don't add listings for NSFW mods on the website.
# NSFW needs to be opt-in to be shown,
# mod managers can implement that.
if mod["category"] == "NSFW":
return False
# Only show mods with versions
if mod["versions"] is None or len(mod["versions"]) == 0:
return False
# Don't show mods with only vulnerable versions
only_vulnerable_versions = True
for version in mod["versions"]:
if "flags" not in version:
only_vulnerable_versions = False
else:
if not any(flag.startswith("vulnerability:") for flag in version["flags"]):
only_vulnerable_versions = False
if only_vulnerable_versions:
return False
# Show all mods by default
return True
def map_mod_versions(versions: dict[str, Any], mod_guid: str) -> list[dict[str, Any]]:
"""
Filters unwanted mod versions away, and turns them into a list
Parameters:
versions: The mod's versions
mod_guid: The mod's GUID
"""
versions_list: list[dict[str, Any]] = []
for version_id in versions:
try:
mod_version = versions[version_id]
mod_version["id"] = Version(version_id)
# Skip over listing pre-release versions
if (
"preRelease" in mod_version or
mod_version["id"].is_prerelease or
mod_version["id"].is_devrelease
):
continue
versions_list.append(mod_version)
except Exception as err:
print(
f"Failed to process [{mod_guid}/{version_id}], reason: {err}",
file=sys.stderr
)
return versions_list
def exec_shell(command: str) -> str:
"""
Execute a shell command, and throws an exception if it fails, otherwise return the output.
Parameters:
command: The command to execute in the system shell
"""
[status, output] = subprocess.getstatusoutput(command)
if status != 0:
raise Exception(f"{command} exited with status ${status}, output: {output}")
return output