iyzs...@member.fsf.org (宋文武) writes:

>> I plan to implement option 1 by adding a "sitecustomize.py" (better
>> than modify "site.py") into the python packages, and modify
>> "search-path-specification" to use "GUIX_PYTHON_X_Y_SITE_PACKAGES".
>
> Patch coming:
>
> [patch with typo...]
>
>
> This targets 'core-updates' and will rebuild the world, I can't afford
> to test it...

Updated with typo fixed:

>From d9c273c0ee8c5e87b12b37a325c649f8df808af3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AE=8B=E6=96=87=E6=AD=A6?= <iyzs...@member.fsf.org>
Date: Sat, 17 Mar 2018 18:46:55 +0800
Subject: [PATCH] gnu: python-2.7, python-3.6: Honor
 'GUIX_PYTHON_X_Y_SITE_PACKAGES'.

This replace the use of 'PYTHONPATH' as search path specification, as
suggested by Hartmut Goebel <h.goe...@crazy-compilers.com>.  See
<https://lists.gnu.org/archive/html/guix-devel/2018-03/msg00178.html> for
details.

* gnu/packages/python.scm (python-guix-search-path-specification)
(python-guix-sitecustomize.py): New procedures.
(python-2.7, python-3.6):
[native-search-paths]: Use 'python-guix-search-path-specification'.
[arguments]: Add 'install-sitecustomize.py' phase.
---
 gnu/packages/python.scm | 67 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 58 insertions(+), 9 deletions(-)

diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index 6639e6c9e..2ce8db710 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -136,6 +136,41 @@
   #:use-module (guix build-system trivial)
   #:use-module (srfi srfi-1))
 
+(define (python-guix-search-path-specification version)
+  "Return the search path specification for python VERSION."
+  (let* ((major.minor (version-major+minor version))
+         (variable    (string-append
+                       "GUIX_PYTHON_"
+                       (string-replace-substring major.minor "." "_")
+                       "_SITE_PACKAGES"))
+         (files       (list (string-append
+                             "lib/python" major.minor "/site-packages"))))
+    (search-path-specification
+     (variable variable)
+     (files files))))
+
+(define (python-guix-sitecustomize.py version)
+  "Return the content of @file{sitecustomize.py} for python VERSION."
+  (let* ((major.minor (version-major+minor version))
+         (variable    (string-append
+                       "GUIX_PYTHON_"
+                       (string-replace-substring major.minor "." "_")
+                       "_SITE_PACKAGES")))
+    (format #f "# Append module search paths for guix packages to sys.path.
+import os
+import site
+
+SITE_PACKAGES = os.environ.get('~a')
+
+if SITE_PACKAGES is None:
+    SITE_PACKAGES = []
+else:
+    SITE_PACKAGES = SITE_PACKAGES.split(os.pathsep)
+
+for i in SITE_PACKAGES:
+    site.addsitedir(i)
+" variable)))
+
 (define-public python-2.7
   (package
     (name "python2")
@@ -304,6 +339,16 @@
                                      "/site-packages")))
                        (install-file tkinter.so target)
                        (delete-file tkinter.so)))))
+                #t)))
+          (add-after 'install 'install-sitecustomize.py
+            (lambda* (#:keys outputs #:allow-other-keys)
+              (let* ((out (assoc-ref outputs "out"))
+                     (sitedir (car (find-files out "^site-packages$"
+                                               #:directories #t))))
+                (with-output-to-file
+                    (string-append sitedir "/sitecustomize.py")
+                  (lambda ()
+                    (display ,(python-guix-sitecustomize.py version))))
                 #t))))))
     (inputs
      `(("bzip2" ,bzip2)
@@ -318,9 +363,7 @@
     (native-inputs
      `(("pkg-config" ,pkg-config)))
     (native-search-paths
-     (list (search-path-specification
-            (variable "PYTHONPATH")
-            (files '("lib/python2.7/site-packages")))))
+     (list (python-guix-search-path-specification version)))
     (home-page "https://www.python.org";)
     (synopsis "High-level, dynamically-typed programming language")
     (description
@@ -427,13 +470,19 @@ data types.")
                                          "-x" "(lib2to3|test/bad.*)"
                                          ,file)))
                               (find-files out "\\.py$")))
-                  (list '() '("-O") '("-OO"))))))))))
+                  (list '() '("-O") '("-OO"))))))
+           (replace 'install-sitecustomize.py
+             (lambda* (#:keys outputs #:allow-other-keys)
+               (let* ((out (assoc-ref outputs "out"))
+                      (sitedir (car (find-files out "^site-packages$"
+                                                #:directories #t))))
+                 (with-output-to-file
+                     (string-append sitedir "/sitecustomize.py")
+                   (lambda ()
+                     (display ,(python-guix-sitecustomize.py version))))
+                 #t)))))))
     (native-search-paths
-     (list (search-path-specification
-            (variable "PYTHONPATH")
-            (files (list (string-append "lib/python"
-                                        (version-major+minor version)
-                                        "/site-packages"))))))))
+     (list (python-guix-search-path-specification version)))))
 
 ;; Current 3.x version.
 (define-public python-3 python-3.6)
-- 
2.13.3

Reply via email to