Subject: bug#22010: bytecode timestamps

From: Ricardo Wurmus <[email-masked]>
Date: Tue, 05 Feb 2019 13:25:54 +0100

Leo Famulari <[email-masked]> writes:

> Those differing bytes are the timestamps of the .py sources files that
> correspond to the compiled .pyc / .pyo files. So, the
> python-2.7-source-date-epoch.patch is working for software compiled by
> the patched compiler, but not on python-2.7 itself.
>
> It's related to <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=22533>.

It looks easy to fix this:

--8<---------------cut here---------------start------------->8---
diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index 1c4ea720f..34c01bd50 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -276,6 +276,38 @@ data types.")
                         (search-patches "python2-CVE-2018-14647.patch"
                                         "python2-CVE-2018-1000802.patch")))))))
 
+(define-public python-2-rekado
+  (package
+    (inherit python-2)
+    (name "python2-rekado")
+    (arguments
+     `(#:tests? #f ; XXX
+       ,@(substitute-keyword-arguments (package-arguments python-2)
+           ((#:phases phases)
+            `(modify-phases ,phases
+               (add-after 'remove-tests 'rebuild-bytecode
+                 (lambda* (#:key outputs #:allow-other-keys)
+                   (let ((out (assoc-ref outputs "out")))
+                     ;; Disable hash randomization to ensure the generated .pycs
+                     ;; are reproducible.
+                     (setenv "PYTHONHASHSEED" "0")
+                     (for-each
+                      (lambda (opt)
+                        (format #t "Compiling with optimization level: ~a\n"
+                                (if (null? opt) "none" (car opt)))
+                        (for-each (lambda (file)
+                                    (apply invoke
+                                           `(,(string-append out "/bin/python")
+                                             ,@opt
+                                             "-m" "compileall"
+                                             "-f" ; force rebuild
+                                             ;; Don't build lib2to3, because it contains Python 3 code.
+                                             "-x" "lib2to3/.*"
+                                             ,file)))
+                                  (find-files out "\\.py$")))
+                      (list '() '("-O") '("-OO")))
+                     #t))))))))))
+
 (define-public python2-called-python
   ;; Both 2.x and 3.x used to be called "python".  In commit
   ;; a7714d42de2c3082f3609d1e63c83d703fb39cf9 (March 2018), we renamed
   the
--8<---------------cut here---------------end--------------->8---

This Python variant builds reproducibly (I disabled the tests for speed
and did this in a separate package to avoid grafting shenanigans and
rebuilds).  The phase is virtually the same as the phase of the same
name in the Python 3 package; only difference is the use of
“/bin/python” instead of “/bin/python3”.

I wonder if we can simplify this with (setenv "PYTHONHASHSEED" "0")
before any pyc files are built, but I expect that to be problematic.

~~ Ricardo