Subject: [bug#48785] [PATCH v2 3/3] gnu: Add python-lightgbm.

From: Vinicius Monego <[email-masked]>
Date: Wed, 30 Jun 2021 20:09:19 +0000

* gnu/packages/machine-learning.scm (python-lightgbm): New variable.
---
Replace patch changes with code in 'preparations phase and added phase 'install-liblightgbm

 gnu/packages/machine-learning.scm | 59 +++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm
index 04d1fa0c4d..79bf0bbbfd 100644
--- a/gnu/packages/machine-learning.scm
+++ b/gnu/packages/machine-learning.scm
@@ -1109,6 +1109,65 @@ the following advantages:
 @end itemize\n")
     (license license:expat)))
 
+(define-public python-lightgbm
+  (package
+    (inherit lightgbm)
+    (name "python-lightgbm")
+    (source (package-source lightgbm))
+    (build-system python-build-system)
+    (arguments
+     `(#:configure-flags (list "--precompile")
+       #:phases
+       (modify-phases %standard-phases
+         (add-after 'unpack 'preparations
+           (lambda _
+             (setenv "HOME" "/tmp") ;to write a log file
+             ;; Move Python files to source root to silence some warnings.
+             (rename-file "python-package/setup.py" "setup.py")
+             (rename-file "python-package/README.rst" "README.rst")
+             (rename-file "python-package/lightgbm" "lightgbm")
+             ;; Install version file to read the library's version.
+             (with-output-to-file "lightgbm/VERSION.txt"
+               (lambda ()
+                 (display ,(package-version lightgbm))))
+             ;; Skip building the shared library.
+             (substitute* "setup.py"
+               (("'install_lib': CustomInstallLib,") ""))))
+         (add-after 'install 'install-liblightgbm
+           (lambda* (#:key inputs outputs #:allow-other-keys)
+             (let* ((out (assoc-ref outputs "out"))
+                    (pylib (string-append out "/lib/python"
+                                          ,(version-major+minor
+                                            (package-version python))
+                                          "/site-packages"))
+                    (lgbdir (string-append pylib "/lightgbm"))
+                    (lib-lightgbm (string-append (assoc-ref inputs "lightgbm")
+                                                 "/lib/lib_lightgbm.so")))
+               ;; This symlink is necessary for tests.
+               (symlink lib-lightgbm "lightgbm/lib_lightgbm.so")
+               ;; And this one for runtime.
+               (mkdir-p (string-append lgbdir "/lib"))
+               (symlink lib-lightgbm (string-append lgbdir
+                                                    "/lib_lightgbm.so")))))
+         (replace 'check
+           (lambda* (#:key inputs outputs tests? #:allow-other-keys)
+             (when tests?
+               (add-installed-pythonpath inputs outputs)
+               (invoke "python" "-m" "pytest"
+                       "tests/python_package_test")))))))
+    (native-inputs
+     `(("python-psutil" ,python-psutil)
+       ("python-pytest" ,python-pytest)
+       ("python-wheel" ,python-wheel)))
+    (inputs
+     `(("lightgbm" ,lightgbm)))
+    (propagated-inputs
+     `(("python-numpy" ,python-numpy)
+       ("python-pandas" ,python-pandas)
+       ("python-scipy" ,python-scipy)
+       ("python-scikit-learn" ,python-scikit-learn)))
+    (synopsis "Python frontend for LightGBM")))
+
 (define-public vowpal-wabbit
   ;; Language bindings not included.
   (package
-- 
2.32.0