Subject: [PATCH] python-pycryptodome: Build HTML and info documentation and unbundle sphinx-rtd-theme and libtomcrypt

From: Maxime Devos <[email-masked]>
Date: Tue, 13 Jul 2021 14:31:29 +0200

X-Debbugs-CC: slg <[email-masked]>

Hi guix,

These two patches fix <https://issues.guix.gnu.org/49530>.
The dependencies of python-pycryptodome (found with "guix refresh -l")
still build succesfully.

I performed the unbundling in build phases as I'm not sure
what is the proper way to use 'tar' from a snippet, and whether
the unbundling is done in a build phase or an 'origin' snippet
doesn't seem to matter much, as the bundled code is free software.

Greetings,
Maxime.
From e9b497cbb8f04490b6c835c8b5ed9b92d2765781 Mon Sep 17 00:00:00 2001
From: Maxime Devos <[email-masked]>
Date: Mon, 12 Jul 2021 17:52:39 +0200
Subject: [PATCH 1/2] gnu: python-pycryptodome: Unbundle libtomcrypt.

* gnu/packages/python-crypto.scm
  (pycryptodome)[arguments]<#:phases>{replace-libtomcrypt}:
  New phase.
  (pycryptodome)[native-inputs]{tomcrypt-source}: Add source
  code of 'libtomcrypt'.
---
 gnu/packages/python-crypto.scm | 35 ++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/gnu/packages/python-crypto.scm b/gnu/packages/python-crypto.scm
index 733a87cd2f..5eb990b5ba 100644
--- a/gnu/packages/python-crypto.scm
+++ b/gnu/packages/python-crypto.scm
@@ -24,6 +24,7 @@
 ;;; Copyright © 2020 Alexandros Theodotou <[email-masked]>
 ;;; Copyright © 2020 Justus Winter <[email-masked]>
 ;;; Copyright © 2020 Vinicius Monego <[email-masked]>
+;;; Copyright © 2021 Maxime Devos <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -43,6 +44,7 @@
 (define-module (gnu packages python-crypto)
   #:use-module (guix packages)
   #:use-module (guix download)
+  #:use-module (guix gexp)
   #:use-module (guix git-download)
   #:use-module (guix build-system python)
   #:use-module (gnu packages)
@@ -1001,6 +1003,39 @@ protocol (Javascript Object Signing and Encryption).")
         (base32
          "1i4m74f88qj9ci8rpyzrbk2slmsdj5ipmwdkq6qk24byalm203li"))))
     (build-system python-build-system)
+    (arguments
+     `(#:modules ((srfi srfi-26)
+                  (guix build python-build-system)
+                  (guix build utils))
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'unpack 'replace-libtomcrypt
+           (lambda* (#:key native-inputs inputs #:allow-other-keys)
+             (with-directory-excursion "src/libtom"
+               ;; Delete bundled code.
+               (for-each delete-file (find-files "."))
+               ;; Extract tomcrypt source code into "untarred".
+               (mkdir "untarred")
+               (invoke "tar" "xf"
+                       (assoc-ref (or native-inputs inputs) "tomcrypt-source")
+                       "--strip-components=1"
+                       "-Cuntarred")
+               ;; Use source code from "untarred".
+               (rename-file "untarred/src/ciphers/des.c" "tomcrypt_des.c")
+               (for-each (cut install-file <> ".")
+                         (find-files "untarred/src/headers"))
+               (delete-file-recursively "untarred"))))
+         ;; The code bundled in pycryptdome has been modified
+         ;; to make some variables and functions 'static'.
+         (add-after 'replace-libtomcrypt 'make-des-static
+           (lambda _
+             (substitute* (find-files "src/libtom")
+               (("^extern const struct") "static const struct")
+               (("^const struct") "static const struct")
+               (("^int des") "static int des")
+               (("^void des") "static void des")))))))
+    (native-inputs
+     `(("tomcrypt-source" ,(package-source libtomcrypt))))
     (home-page "https://www.pycryptodome.org")
     (synopsis "Low-level cryptographic Python library")
     (description
-- 
2.32.0

From 5e11b738571167dbb5ba59d9cfb3204dd81ca855 Mon Sep 17 00:00:00 2001
From: Maxime Devos <[email-masked]>
Date: Mon, 12 Jul 2021 20:30:06 +0200
Subject: [PATCH 2/2] gnu: python-pycryptodome: Build documentation.

* gnu/packages/python-crypto.scm
  (python-pycryptodome)[outputs]: Add "doc" output.
  (python-pycryptodome)[arguments]<#:phases>{build-documentation}:
  New phase, removing images loaded from the Internet, unbundling
  sphinx-rtd-theme and building HTML and Info documentation.
  (python-pycryptodome)[arguments]<#:phases>{build-documentation}:
  New phase.
---
 gnu/packages/python-crypto.scm | 35 ++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/gnu/packages/python-crypto.scm b/gnu/packages/python-crypto.scm
index 5eb990b5ba..cb3d0f9609 100644
--- a/gnu/packages/python-crypto.scm
+++ b/gnu/packages/python-crypto.scm
@@ -52,6 +52,7 @@
   #:use-module (gnu packages crypto)
   #:use-module (gnu packages kerberos)
   #:use-module (gnu packages libffi)
+  #:use-module (gnu packages sphinx)
   #:use-module (gnu packages multiprecision)
   #:use-module (gnu packages password-utils)
   #:use-module (gnu packages protobuf)
@@ -62,6 +63,7 @@
   #:use-module (gnu packages python-web)
   #:use-module (gnu packages python-xyz)
   #:use-module (gnu packages swig)
+  #:use-module (gnu packages texinfo)
   #:use-module (gnu packages time)
   #:use-module (gnu packages tls)
   #:use-module (gnu packages xml)
@@ -1003,6 +1005,8 @@ protocol (Javascript Object Signing and Encryption).")
         (base32
          "1i4m74f88qj9ci8rpyzrbk2slmsdj5ipmwdkq6qk24byalm203li"))))
     (build-system python-build-system)
+    ;; "doc" has HTML documentation weighing 4.9 MB
+    (outputs '("out" "doc"))
     (arguments
      `(#:modules ((srfi srfi-26)
                   (guix build python-build-system)
@@ -1033,9 +1037,36 @@ protocol (Javascript Object Signing and Encryption).")
                (("^extern const struct") "static const struct")
                (("^const struct") "static const struct")
                (("^int des") "static int des")
-               (("^void des") "static void des")))))))
+               (("^void des") "static void des"))))
+         (add-after 'build 'build-documentation
+           (lambda _
+             ;; Prevent offline documentation from loading
+             ;; images from the Internet.
+             (substitute* "README.rst"
+               (("^(.*)travis-ci.org(.*)\n") "")
+               (("^(.*)ci.appveyor.com(.*)\n") ""))
+             ;; Unbundle sphinx-rtd-theme.
+             (delete-file-recursively "Doc/sphinx_rtd_theme")
+             (invoke "make" "-C" "Doc" "html" "info")))
+         (add-after 'install 'install-documentation
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let* ((doc (string-append (assoc-ref outputs "doc")
+                                        "/share/doc/" ,name "-" ,version))
+                    (html (string-append doc "/html"))
+                    ;; The 'info' manual only weighs 72 KB
+                    (info (string-append (assoc-ref outputs "out")
+                                         "/share/info")))
+               (mkdir-p info)
+               (mkdir-p html)
+               (copy-recursively "Doc/_build/html" html)
+               (copy-recursively "Doc/_build/texinfo" info)
+               (delete-file (string-append info "/Makefile"))
+               (delete-file (string-append info "/PyCryptodome.texi"))))))))
     (native-inputs
-     `(("tomcrypt-source" ,(package-source libtomcrypt))))
+     `(("python-sphinx" ,python-sphinx)
+       ("python-sphinx-rtd-theme" ,python-sphinx-rtd-theme)
+       ("texinfo" ,texinfo)
+       ("tomcrypt-source" ,(package-source libtomcrypt))))
     (home-page "https://www.pycryptodome.org")
     (synopsis "Low-level cryptographic Python library")
     (description
-- 
2.32.0