-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
88 lines (69 loc) · 2.66 KB
/
utils.py
File metadata and controls
88 lines (69 loc) · 2.66 KB
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
import hashlib
import logging
import os
import sys
from typing import TYPE_CHECKING, Dict, List
from ModrinthClient import ModrinthClient
def get_current_mod_hashes(path: str) -> Dict[str, str]:
"""Get all the mods names in the specified path.
:param path: Path to the mods folder.
:type path: str
:return: Dict with the mod hashes as keys and filename as value.
:rtype: Dict[str, str]
"""
files = os.listdir(path)
hashes = {}
for file in files:
if file.endswith(".jar"):
sha1 = hashlib.sha1() # You can use hashlib.md5() for MD5
with open(os.path.join(path, file), "rb") as f:
while chunk := f.read(8192):
sha1.update(chunk)
hash = sha1.hexdigest()
hashes[hash] = file
logging.debug("Current hashes: %s", hashes)
return hashes
def check_path(path: str) -> None:
"""Check if the specified path exists, if not create it.
:param path: Path to check.
:type path: str
"""
logging.debug("Checking path: %s", path)
if not os.path.exists(path):
os.makedirs(path)
logging.info("Created path: %s", path)
else:
logging.debug("Path exists: %s", path)
def check_version(version: str, modrinthClient: ModrinthClient) -> str:
"""If the version is "latest" get the latest version from Modrinth. Else return the version if it exists or sys.exit().
:param version: The version to check.
:type version: str
:param modrinthClient: The ModrinthClient object.
:type modrinthClient: ModrinthClient
:return: The version
:rtype: str
"""
if version == "latest":
logging.debug("Getting latest version.")
return modrinthClient.latest_release_version()
else:
if modrinthClient.validate_version(version):
logging.debug("Version (%s) is valid.", version)
return version
logging.error("Version (%s) is not valid.", version)
return sys.exit(1)
def put_current_mods_in_folder(path: str, old_version: str) -> None:
"""Move the current mods to the old version folder inside the specified path.
:param path: Path to the mods folder.
:type str: path
:param old_version: The old version of the mods.
:type old_version: str
"""
old_path = os.path.join(path, old_version)
check_path(old_path)
files = os.listdir(path)
for file in files:
if file.endswith(".jar"):
os.rename(os.path.join(path, file), os.path.join(old_path, file))
logging.debug("Moved mod: %s to: %s version folder", file, old_version)
logging.info("Moved current mods to %s version folder.", old_version)