git: f0b69e29e340 - main - sysutils/py-magika: New port: Tool to determine the content type of a file with deep learning
Yuri Victorovich <[email protected]>
| Newsgroups | gmane.os.freebsd.devel.cvs.ports |
|---|---|
| Message-ID | <6a756890.3181e.3df8d87d__12866.9230079808$1786079456$gmane$org@gitrepo.freebsd.org> |
The branch main has been updated by yuri: URL: https://cgit.FreeBSD.org/ports/commit/?id=f0b69e29e340f7e9e22c9a4c6b75f362a4c147cf commit f0b69e29e340f7e9e22c9a4c6b75f362a4c147cf Author: Yuri Victorovich <[email protected]> AuthorDate: 2026-08-07 03:06:36 +0000 Commit: Yuri Victorovich <[email protected]> CommitDate: 2026-08-07 05:09:29 +0000 sysutils/py-magika: New port: Tool to determine the content type of a file with deep learning --- sysutils/Makefile | 1 + sysutils/py-magika/Makefile | 42 +++++++++ sysutils/py-magika/distinfo | 3 + sysutils/py-magika/files/example.py | 45 ++++++++++ sysutils/py-magika/files/patch-pyproject.toml | 118 ++++++++++++++++++++++++++ sysutils/py-magika/pkg-descr | 3 + 6 files changed, 212 insertions(+) diff --git a/sysutils/Makefile b/sysutils/Makefile index 18d751484b28..cdb95a400545 100644 --- a/sysutils/Makefile +++ b/sysutils/Makefile @@ -1104,6 +1104,7 @@ SUBDIR += py-kubernetes SUBDIR += py-leviathan SUBDIR += py-liquidctl + SUBDIR += py-magika SUBDIR += py-mitogen SUBDIR += py-mqttwarn SUBDIR += py-nagiosplugin diff --git a/sysutils/py-magika/Makefile b/sysutils/py-magika/Makefile new file mode 100644 index 000000000000..0d33d24cfec1 --- /dev/null +++ b/sysutils/py-magika/Makefile @@ -0,0 +1,42 @@ +PORTNAME= magika +DISTVERSIONPREFIX= python-v +DISTVERSION= 1.0.3 +CATEGORIES= sysutils python # machine-learning +PKGNAMEPREFIX= ${PYTHON_PKGNAMEPREFIX} + +MAINTAINER= [email protected] +COMMENT= Tool to determine the content type of a file with deep learning +WWW= https://github.com/google/magika + +LICENSE= APACHE20 +LICENSE_FILE= ${WRKSRC}/../LICENSE + +BUILD_DEPENDS= ${PYTHON_PKGNAMEPREFIX}hatchling>0:devel/py-hatchling@${PY_FLAVOR} +RUN_DEPENDS= ${PYTHON_PKGNAMEPREFIX}click>=8.1.7:devel/py-click@${PY_FLAVOR} \ + ${PYTHON_PKGNAMEPREFIX}onnxruntime>=1.17.0:misc/py-onnxruntime@${PY_FLAVOR} +TEST_DEPENDS= ${PYTHON_PKGNAMEPREFIX}requests>0:www/py-requests@${PY_FLAVOR} \ + ${PYTHON_PKGNAMEPREFIX}tqdm>0:misc/py-tqdm@${PY_FLAVOR} \ + ${PYTHON_PKGNAMEPREFIX}dacite>=1.9.2:devel/py-dacite@${PY_FLAVOR} \ + ${PYTHON_PKGNAMEPREFIX}tomli>=2.0.1:textproc/py-tomli@${PY_FLAVOR} \ + ${PYTHON_PKGNAMEPREFIX}tomli-w>=1.0.0:textproc/py-tomli-w@${PY_FLAVOR} + +USES= python shebangfix +USE_PYTHON= pep517 concurrent autoplist pytest + +SHEBANG_FILES= ${WRKSRC}/src/magika/cli/magika_client.py \ + ${WRKSRC}/src/magika/cli/magika_rust_client_not_found_warning.py + +USE_GITHUB= yes +GH_ACCOUNT= google +GH_PROJECT= magika + +WRKSRC_SUBDIR= python + +NO_ARCH= yes + +TEST_ENV= ${MAKE_ENV} PYTHONPATH=${STAGEDIR}${PYTHONPREFIX_SITELIBDIR}:${PYTHONPREFIX_SITELIBDIR} +TEST_WRKSRC= ${WRKSRC}/tests + +# tests as of 1.0.3: 32 passed, 1 skipped, 1 warning in 12.27s + +.include <bsd.port.mk> diff --git a/sysutils/py-magika/distinfo b/sysutils/py-magika/distinfo new file mode 100644 index 000000000000..c3d879e67746 --- /dev/null +++ b/sysutils/py-magika/distinfo @@ -0,0 +1,3 @@ +TIMESTAMP = 1786057028 +SHA256 (google-magika-python-v1.0.3_GH0.tar.gz) = 8d24d6638f30f4c89e5b71fd2f6dee665e5381215b8f9d1ba88b065e57edaff4 +SIZE (google-magika-python-v1.0.3_GH0.tar.gz) = 84265337 diff --git a/sysutils/py-magika/files/example.py b/sysutils/py-magika/files/example.py new file mode 100644 index 000000000000..5d6fb3d22aa8 --- /dev/null +++ b/sysutils/py-magika/files/example.py @@ -0,0 +1,45 @@ +import argparse +import sys +from pathlib import Path +from magika import Magika + +def main(): + parser = argparse.ArgumentParser( + description="Verify that an uploaded file matches its format criteria." + ) + parser.add_argument( + "image_path", + type=str, + help="The path to the file you want to check." + ) + + args = parser.parse_args() + target_path = Path(args.image_path) + + if not target_path.is_file(): + print(f"Error: File '{target_path}' does not exist.", file=sys.stderr) + sys.exit(1) + + magika = Magika() + result = magika.identify_path(target_path) + + # --- UPDATED FOR NEW MAGIKA API --- + detected_group = result.output.group + detected_label = result.output.label + + # Use getattr to safely handle variance between 0.5.x and 0.6.x versions + confidence = getattr(result, "similarity_score", getattr(result, "score", 0.0)) + + print(f"Inspecting: {target_path.name}") + print(f"Result: {detected_label} ({confidence:.2%})") + + # In your test, a Makefile is categorized under the "text" or "code" group + if detected_group != "image" and detected_label != "makefile": + print(f"❌ Verification failed. Group: {detected_group}, Label: {detected_label}") + sys.exit(1) + + print("✅ File verified successfully.") + +if __name__ == "__main__": + main() + diff --git a/sysutils/py-magika/files/patch-pyproject.toml b/sysutils/py-magika/files/patch-pyproject.toml new file mode 100644 index 000000000000..a5f8214b8738 --- /dev/null +++ b/sysutils/py-magika/files/patch-pyproject.toml @@ -0,0 +1,118 @@ +-- Replace maturin/Rust-based build configuration with hatchling for a pure +-- Python package. The Python package uses onnxruntime directly and does not +-- require the Rust CLI binary. The PyPI sdist already ships this hatchling +-- pyproject.toml, but the GitHub source used by the port references the +-- ../rust/cli/Cargo.toml maturin build, which is unsupported on FreeBSD. +-- This patch mirrors the upstream PyPI sdist configuration. + +--- pyproject.toml.orig 2026-05-04 08:32:03 UTC ++++ pyproject.toml +@@ -2,12 +2,14 @@ authors = [ + name = "magika" + description = "A tool to determine the content type of a file with deep learning" + authors = [ +- {name = "Magika Developers", email = "[email protected]"}, ++ { name = "Magika Developers", email = "[email protected]" }, + ] + readme = "README.md" +-license = {"text" = "Apache-2.0"} + requires-python = ">=3.8" +-keywords = ["content type detection", "machine learning"] ++keywords = [ ++ "content type detection", ++ "machine learning", ++] + classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Console", +@@ -33,22 +35,17 @@ version = "1.0.3" + "Typing :: Typed", + ] + version = "1.0.3" +- + dependencies = [ + "click>=8.1.7", +- +- # Python 3.14+: Min version 1.24.1 + "onnxruntime>=1.24.1 ; python_version >= '3.14'", +- # Python 3.13: Min version 1.21.0 + "onnxruntime>=1.21.0 ; python_version == '3.13'", +- # Python 3.11 - 3.12: Min version 1.17.0 + "onnxruntime>=1.17.0 ; python_version >= '3.11' and python_version < '3.13'", +- # Python 3.10: Support dropped in 1.24.1 (use latest compatible) + "onnxruntime>=1.17.0, <1.24.1 ; python_version == '3.10'", +- # Python 3.9: Support dropped in 1.20.0 + "onnxruntime>=1.17.0, <1.20.0 ; python_version <= '3.9'", + ] + ++[project.license] ++text = "Apache-2.0" + + [project.urls] + Homepage = "https://github.com/google/magika" +@@ -57,6 +54,9 @@ Changelog = "https://github.com/google/magika/blob/mai + Issues = "https://github.com/google/magika/issues" + Changelog = "https://github.com/google/magika/blob/main/python/CHANGELOG.md" + ++[project.scripts] ++magika-python-client = "magika.cli.magika_client:main" ++magika = "magika.cli.magika_rust_client_not_found_warning:main" + + [dependency-groups] + dev = [ +@@ -72,30 +72,36 @@ dev = [ + ] + + [build-system] +-requires = ["maturin>=1.12.2"] +-build-backend = "maturin" ++requires = [ ++ "hatchling", ++] ++build-backend = "hatchling.build" + +-[[tool.uv.index]] +-url = "https://pypi.org/simple" +-default = true ++[tool.uv] ++index = [ ++ { url = "https://pypi.org/simple", default = true }, ++] + +-[tool.maturin] +-python-source = "src" +-module-name = "magika" +-bindings = "bin" +-manifest-path = "../rust/cli/Cargo.toml" +-locked = true +- + [tool.ruff.lint] +-# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +-# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +-# McCabe complexity (`C901`) by default. +-select = ["D", "E4", "E7", "E9", "F", "I001"] +-ignore = ["D105"] ++select = [ ++ "D", ++ "E4", ++ "E7", ++ "E9", ++ "F", ++ "I001", ++] ++ignore = [ ++ "D105", ++] + + [tool.ruff.lint.per-file-ignores] +-"scripts/*" = ["D"] +-"tests/*" = ["D"] ++"scripts/*" = [ ++ "D", ++] ++"tests/*" = [ ++ "D", ++] + + [tool.ruff.lint.pydocstyle] + convention = "google" diff --git a/sysutils/py-magika/pkg-descr b/sysutils/py-magika/pkg-descr new file mode 100644 index 000000000000..db21beaa8aa0 --- /dev/null +++ b/sysutils/py-magika/pkg-descr @@ -0,0 +1,3 @@ +Magika is a tool to determine the content type of a file with deep learning. +It can be used as a Python module or command-line tool to identify the type +of files based on their contents.