Subject: [PATCH 1/2] gnu: Add python-cramjam.

From: Troy Figiel <[email-masked]>
Date: Sat, 2 Mar 2024 19:40:21 +0100

* gnu/packages/python-compression.scm (python-cramjam): New variable.
---
 gnu/packages/python-compression.scm | 80 ++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/python-compression.scm b/gnu/packages/python-compression.scm
index 6f249dd3af..691fbd3065 100644
--- a/gnu/packages/python-compression.scm
+++ b/gnu/packages/python-compression.scm
@@ -7,7 +7,7 @@
 ;;; Copyright © 2020 Nicolas Goaziou <[email-masked]>
 ;;; Copyright © 2020, 2022, 2023 Marius Bakke <[email-masked]>
 ;;; Copyright © 2021 Brendan Tildesley <[email-masked]>
-;;; Copyright © 2023 Troy Figiel <[email-masked]>
+;;; Copyright © 2023, 2024 Troy Figiel <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -30,12 +30,14 @@ (define-module (gnu packages python-compression)
   #:use-module (guix packages)
   #:use-module (guix download)
   #:use-module (guix gexp)
+  #:use-module (guix build-system cargo)
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system python)
   #:use-module (guix build-system pyproject)
   #:use-module (gnu packages)
   #:use-module (gnu packages libffi)
   #:use-module (gnu packages compression)
+  #:use-module (gnu packages crates-io)
   #:use-module (gnu packages check)
   #:use-module (gnu packages maths)
   #:use-module (gnu packages pkg-config)
@@ -44,6 +46,7 @@ (define-module (gnu packages python-compression)
   #:use-module (gnu packages python-check)
   #:use-module (gnu packages python-crypto)
   #:use-module (gnu packages python-xyz)
+  #:use-module (gnu packages rust-apps)
   #:use-module (gnu packages sphinx))
 
 (define-public python-multivolumefile
@@ -73,6 +76,81 @@ (define-public python-multivolumefile
 were a single file.")
     (license license:lgpl2.1+)))
 
+(define-public python-cramjam
+  (package
+    (name "python-cramjam")
+    (version "2.7.0")
+    (source
+     (origin
+       (method url-fetch)
+       (uri (pypi-uri "cramjam" version))
+       (sha256
+        (base32 "1b69qlr0q7q3spa7zy55xc1dr5pjgsdavxx8ijhv2j60xqjbg7sp"))))
+    (build-system cargo-build-system)
+    (arguments
+     (list
+      #:imported-modules `(,@%cargo-build-system-modules
+                           ,@%pyproject-build-system-modules)
+      #:modules '((guix build cargo-build-system)
+                  ((guix build pyproject-build-system)
+                   #:prefix py:)
+                  (guix build utils))
+      #:phases #~(modify-phases %standard-phases
+                   ;; We use Maturin to build the project.
+                   (replace 'build
+                     (assoc-ref py:%standard-phases
+                                'build))
+                   ;; Before being able to run Python tests, we need to
+                   ;; install the module and add it to PYTHONPATH.
+                   (delete 'install)
+                   (add-after 'build 'install
+                     (assoc-ref py:%standard-phases
+                                'install))
+                   (add-after 'install 'add-install-to-pythonpath
+                     (assoc-ref py:%standard-phases
+                                'add-install-to-pythonpath))
+                   ;; Finally run the tests. Only Python tests are provided.
+                   (replace 'check
+                     (lambda* (#:key tests? inputs outputs #:allow-other-keys)
+                       (when tests?
+                         ;; Without the CI variable, tests are run in "local"
+                         ;; mode, which sets a deadline for hypothesis.  For a
+                         ;; deterministic build, we need to set CI.
+                         (setenv "CI" "1")
+                         (invoke "pytest" "-vv" "tests")))))
+      #:cargo-inputs `(("rust-brotli" ,rust-brotli-3)
+                       ("rust-bzip2" ,rust-bzip2-0.4)
+                       ("rust-flate2" ,rust-flate2-1)
+                       ("rust-lz4" ,rust-lz4-1)
+                       ("rust-pyo3" ,rust-pyo3-0.18)
+                       ("rust-snap" ,rust-snap-1)
+                       ("rust-zstd" ,rust-zstd-0.11))
+      #:install-source? #f))
+    (native-inputs (list maturin
+                         python-pytest
+                         python-pytest-xdist
+                         python-numpy
+                         python-hypothesis
+                         python-wrapper))
+    (home-page "https://github.com/milesgranger/cramjam")
+    (synopsis "Python bindings to compression algorithms in Rust")
+    (description
+     "This package provides thin Python bindings to compression and
+decomporession algorithms implemented in Rust.  This allows for using
+algorithms such as Snappy without additional system dependencies.  The
+following algorithms are available:
+
+@itemize
+@item Snappy
+@item Brotli
+@item Bzip2
+@item LZ4
+@item Gzip
+@item Deflate
+@item Zstd
+@end itemize")
+    (license license:expat)))
+
 (define-public python-pybcj
   (package
     (name "python-pybcj")
-- 
2.42.0