Subject: [PATCH 3/3] gnu: trytond: Ensure all modules are found.

From: Hartmut Goebel <[email-masked]>
Date: Fri, 3 Dec 2021 14:41:04 +0100

If the python package matching tryton is not installed alongside to trytond,
PYTHONPATH ist not set (or pointing to a wring version) and trytond (and the
trytond tools) would not find the trytond modules.

This change introduces a new native-search-path GUIX_TRYTOND_MODULES_PATH.
Trytond gets patched to honor this environment variable.

Thanks to Maxim Cournoyer, who's code for guix-pythonpath-search-path
was the template for guix-trytonpath-search-path.

* gnu/packages/patches/trytond-add-guix_trytond_path.patch: New file.
* gnu/local.mk: (dist_patch_DATA): Add it.
* gnu/packages/tryton.scm (trytond): Use it.
  [native-search-paths]: New element.
  (guix-trytonpath-search-path): New function.
---
 gnu/local.mk                                  |  1 +
 .../trytond-add-guix_trytond_path.patch       | 26 +++++++++++++++++++
 gnu/packages/tryton.scm                       | 19 +++++++++++++-
 3 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 gnu/packages/patches/trytond-add-guix_trytond_path.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index a8b9bcac21..cac9079325 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1817,6 +1817,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/transcode-ffmpeg.patch	\
   %D%/packages/patches/transmission-honor-localedir.patch	\
   %D%/packages/patches/trytond-add-egg-modules-to-path.patch	\
+  %D%/packages/patches/trytond-add-guix_trytond_path.patch	\
   %D%/packages/patches/ttf2eot-cstddef.patch			\
   %D%/packages/patches/tup-unbundle-dependencies.patch		\
   %D%/packages/patches/tuxpaint-stamps-path.patch		\
diff --git a/gnu/packages/patches/trytond-add-guix_trytond_path.patch b/gnu/packages/patches/trytond-add-guix_trytond_path.patch
new file mode 100644
index 0000000000..7dc136ec46
--- /dev/null
+++ b/gnu/packages/patches/trytond-add-guix_trytond_path.patch
@@ -0,0 +1,26 @@
+Make trytond add all paths listed in GUIX_TRYTOND_MODULES_PATH to sys.path.
+
+*** a/trytond/modules/__init__.py	1970-01-01 01:00:01.000000000 +0100
+--- b/trytond/modules/__init__.py	2021-12-02 22:17:28.014612267 +0100
+***************
+*** 31,36 ****
+--- 31,48 ----
+  
+  EGG_MODULES = {}
+  
++ def __extend_python_path__():
++     tryton_python_path = os.environ.get("GUIX_TRYTOND_MODULES_PATH")
++     if tryton_python_path:
++         paths = [path[:-16]  # remove "/trytond/modules"
++                  for path in tryton_python_path.split(os.pathsep)]
++         sys.path.extend(paths)
++         # ensure new paths are in the pkg_resources WorkingSet
++         import pkg_resources
++         list(map(pkg_resources.working_set.add_entry, paths))
++ 
++ __extend_python_path__()
++ 
+  
+  def update_egg_modules():
+      global EGG_MODULES
+l
diff --git a/gnu/packages/tryton.scm b/gnu/packages/tryton.scm
index 212d6593ad..f8cd2b4c9d 100644
--- a/gnu/packages/tryton.scm
+++ b/gnu/packages/tryton.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2020 Vinicius Monego <[email-masked]>
 ;;; Copyright © 2021 Maxime Devos <[email-masked]>
 ;;; Copyright © 2021 Hartmut Goebel <[email-masked]>
+;;; Copyright © 2021 Maxim Cournoyer <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -39,6 +40,19 @@
   #:use-module (guix utils)
   #:use-module (guix build-system python))
 
+(define (guix-trytonpath-search-path version)
+  "Generate a GUIX_TRYTOND_MODULES_PATH search path specification, using
+VERSION.
+
+Do not use PYTHHONPATH not avoid interfering with any different Python package
+installed in the same environments.  Collecting only paths actually containing
+/tryton/modules reduces the number of paths."
+  (search-path-specification (variable "GUIX_TRYTOND_MODULES_PATH")
+                             (files (list (string-append
+                                           "lib/python"
+                                           (version-major+minor version)
+                                           "/site-packages/trytond/modules")))))
+
 (define-public trytond
   (package
     (name "trytond")
@@ -49,7 +63,8 @@
        (uri (pypi-uri "trytond" version))
        (sha256
         (base32 "1jp5cadqpwkcnml8r1hj6aak5kc8an2d5ai62p96x77nn0dp3ny4"))
-       (patches (search-patches "trytond-add-egg-modules-to-path.patch"))))
+       (patches (search-patches "trytond-add-egg-modules-to-path.patch"
+                                "trytond-add-guix_trytond_path.patch"))))
     (build-system python-build-system)
     (propagated-inputs
      `(("python-dateutil" ,python-dateutil)
@@ -66,6 +81,8 @@
     (native-inputs
      `(("python-mock" ,python-mock)
        ("python-pillow" ,python-pillow)))
+    (native-search-paths
+     (list (guix-trytonpath-search-path (package-version python))))
     (arguments
      `(#:phases
        (modify-phases %standard-phases
-- 
2.30.2