summaryrefslogtreecommitdiff
path: root/meta
diff options
context:
space:
mode:
authorTrial97 <alexandru.tripon97@gmail.com>2025-04-16 16:19:17 +0300
committerTrial97 <alexandru.tripon97@gmail.com>2026-03-18 18:40:13 +0200
commitcc909c32545188e1168fc725c652ff86f74f97a2 (patch)
tree573dda0738133943e3ef4f70988bd3ad4bd5de21 /meta
parenta4ca73c04d9f9becee73c75acbe6c9bf15986c45 (diff)
downloadProject-Tick-cc909c32545188e1168fc725c652ff86f74f97a2.tar.gz
Project-Tick-cc909c32545188e1168fc725c652ff86f74f97a2.zip
chore:add type annotations
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
Diffstat (limited to 'meta')
-rw-r--r--meta/common/__init__.py14
-rwxr-xr-xmeta/run/index.py17
-rwxr-xr-xmeta/run/update_fabric.py8
-rwxr-xr-xmeta/run/update_forge.py10
-rw-r--r--meta/run/update_neoforge.py6
-rwxr-xr-xmeta/run/update_quilt.py8
6 files changed, 21 insertions, 42 deletions
diff --git a/meta/common/__init__.py b/meta/common/__init__.py
index 8e42933942..1f358cdbd7 100644
--- a/meta/common/__init__.py
+++ b/meta/common/__init__.py
@@ -4,7 +4,7 @@ import datetime
import hashlib
import sys
from urllib.parse import urlparse
-from typing import Any, Optional
+from typing import Any, Optional, Callable
import requests
from cachecontrol import CacheControl # type: ignore
@@ -90,7 +90,7 @@ def default_session():
return sess
-def remove_files(file_paths):
+def remove_files(file_paths: list[str]) -> None:
for file_path in file_paths:
try:
if os.path.isfile(file_path):
@@ -103,7 +103,9 @@ def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
-def filehash(filename, hashtype, blocksize=65536):
+def file_hash(
+ filename: str, hashtype: Callable[[], "hashlib._Hash"], blocksize: int = 65536
+) -> str:
hashtype = hashtype()
with open(filename, "rb") as f:
for block in iter(lambda: f.read(blocksize), b""):
@@ -111,12 +113,12 @@ def filehash(filename, hashtype, blocksize=65536):
return hashtype.hexdigest()
-def get_file_sha1_from_file(file_name, sha1_file):
+def get_file_sha1_from_file(file_name: str, sha1_file: str) -> Optional[str]:
if os.path.isfile(sha1_file):
with open(sha1_file, "r") as file:
return file.read()
- new_sha1 = filehash(file_name, hashlib.sha1)
+ new_sha1 = file_hash(file_name, hashlib.sha1)
with open(sha1_file, "w") as file:
file.write(new_sha1)
- return
+ return None
diff --git a/meta/run/index.py b/meta/run/index.py
index 23dc2336ea..22abea8457 100755
--- a/meta/run/index.py
+++ b/meta/run/index.py
@@ -2,7 +2,9 @@ import hashlib
import os
from operator import attrgetter
-from meta.common import launcher_path
+from meta.common import launcher_path, file_hash
+
+
from meta.model import MetaVersion, MetaPackage
from meta.model.index import (
MetaPackageIndex,
@@ -14,15 +16,6 @@ from meta.model.index import (
LAUNCHER_DIR = launcher_path()
-# take the hash type (like hashlib.md5) and filename, return hex string of hash
-def hash_file(hash_fn, file_name):
- hash_instance = hash_fn()
- with open(file_name, "rb") as f:
- for chunk in iter(lambda: f.read(4096), b""):
- hash_instance.update(chunk)
- return hash_instance.hexdigest()
-
-
# ignore these files when indexing versions
ignore = {"index.json", "package.json", ".git", ".github"}
@@ -50,7 +43,7 @@ for package in sorted(os.listdir(LAUNCHER_DIR)):
continue
# parse and hash the version file
filepath = LAUNCHER_DIR + "/%s/%s" % (package, filename)
- filehash = hash_file(hashlib.sha256, filepath)
+ filehash = file_hash(filepath, hashlib.sha256)
versionFile = MetaVersion.parse_file(filepath)
is_recommended = versionFile.version in recommendedVersions
@@ -71,7 +64,7 @@ for package in sorted(os.listdir(LAUNCHER_DIR)):
# insert entry into the package index
packageEntry = MetaPackageIndexEntry(
- uid=package, name=sharedData.name, sha256=hash_file(hashlib.sha256, outFilePath)
+ uid=package, name=sharedData.name, sha256=file_hash(outFilePath, hashlib.sha256)
)
packages.packages.append(packageEntry)
diff --git a/meta/run/update_fabric.py b/meta/run/update_fabric.py
index e0c1d819c9..d19540b5e4 100755
--- a/meta/run/update_fabric.py
+++ b/meta/run/update_fabric.py
@@ -30,14 +30,6 @@ ensure_upstream_dir(META_DIR)
sess = default_session()
-def filehash(filename, hashtype, blocksize=65536):
- h = hashtype()
- with open(filename, "rb") as f:
- for block in iter(lambda: f.read(blocksize), b""):
- h.update(block)
- return h.hexdigest()
-
-
def get_maven_url(maven_key, server, ext):
parts = maven_key.split(":", 3)
maven_ver_url = (
diff --git a/meta/run/update_forge.py b/meta/run/update_forge.py
index 30c701fb32..7ec9e45023 100755
--- a/meta/run/update_forge.py
+++ b/meta/run/update_forge.py
@@ -21,7 +21,7 @@ from meta.common import (
default_session,
remove_files,
eprint,
- filehash,
+ file_hash,
get_file_sha1_from_file,
)
from meta.common.forge import (
@@ -376,8 +376,8 @@ def main():
# installer info v1
if not os.path.isfile(installer_info_path):
installer_info = InstallerInfo()
- installer_info.sha1hash = filehash(jar_path, hashlib.sha1)
- installer_info.sha256hash = filehash(jar_path, hashlib.sha256)
+ installer_info.sha1hash = file_hash(jar_path, hashlib.sha1)
+ installer_info.sha256hash = file_hash(jar_path, hashlib.sha256)
installer_info.size = os.path.getsize(jar_path)
installer_info.write(installer_info_path)
else:
@@ -400,8 +400,8 @@ def main():
tstamp = tstamp_new
legacy_info = ForgeLegacyInfo()
legacy_info.release_time = tstamp
- legacy_info.sha1 = filehash(jar_path, hashlib.sha1)
- legacy_info.sha256 = filehash(jar_path, hashlib.sha256)
+ legacy_info.sha1 = file_hash(jar_path, hashlib.sha1)
+ legacy_info.sha256 = file_hash(jar_path, hashlib.sha256)
legacy_info.size = os.path.getsize(jar_path)
legacy_info_list.number[key] = legacy_info
diff --git a/meta/run/update_neoforge.py b/meta/run/update_neoforge.py
index d9b7cc0f64..186edb253c 100644
--- a/meta/run/update_neoforge.py
+++ b/meta/run/update_neoforge.py
@@ -22,7 +22,7 @@ from meta.common import (
default_session,
remove_files,
eprint,
- filehash,
+ file_hash,
get_file_sha1_from_file,
)
from meta.common.http import download_binary_file
@@ -305,8 +305,8 @@ def main():
# installer info v1
if not os.path.isfile(installer_info_path):
installer_info = InstallerInfo()
- installer_info.sha1hash = filehash(jar_path, hashlib.sha1)
- installer_info.sha256hash = filehash(jar_path, hashlib.sha256)
+ installer_info.sha1hash = file_hash(jar_path, hashlib.sha1)
+ installer_info.sha256hash = file_hash(jar_path, hashlib.sha256)
installer_info.size = os.path.getsize(jar_path)
installer_info.write(installer_info_path)
diff --git a/meta/run/update_quilt.py b/meta/run/update_quilt.py
index 5f3c40caa8..2f3160265d 100755
--- a/meta/run/update_quilt.py
+++ b/meta/run/update_quilt.py
@@ -22,14 +22,6 @@ ensure_upstream_dir(META_DIR)
sess = default_session()
-def filehash(filename, hashtype, blocksize=65536):
- h = hashtype()
- with open(filename, "rb") as f:
- for block in iter(lambda: f.read(blocksize), b""):
- h.update(block)
- return h.hexdigest()
-
-
def get_maven_url(maven_key, server, ext):
parts = maven_key.split(":", 3)
maven_ver_url = (