summary refs log tree commit diff
diff options
context:
space:
mode:
authorVivien Kraus <vivien@planete-kraus.eu>2022-01-20 20:11:56 +0100
committerLudovic Courtès <ludo@gnu.org>2022-01-26 17:54:58 +0100
commitbac9f8302c65802a576f733cc48829edb33019d8 (patch)
treefc006b0305b91a330d68d92589ce3f1aab9eb6ad
parent8b7bd459dc7f86cf2a9569c35efb4a5d845d63ed (diff)
downloadguix-bac9f8302c65802a576f733cc48829edb33019d8.tar.gz
import: pypi: Convert hyphens to underscores in PyPI URLs if needed.
* guix/import/pypi.scm (find-project-url): New function.
(make-pypi-sexp): Use find-project-url.
* tests/pypi.scm (foo-json): New procedure.
(test-json-1, test-json-2): Define in terms of it.
("find-project-url, with numpy", "find-project-url, uWSGI"):
("find-project-url, flake8-array-spacing")
("find-project-url, foo/goo"): New tests.

Co-authored-by: Ludovic Courtès <ludo@gnu.org>
-rw-r--r--guix/import/pypi.scm31
-rw-r--r--tests/pypi.scm111
2 files changed, 80 insertions, 62 deletions
diff --git a/guix/import/pypi.scm b/guix/import/pypi.scm
index b4284f5c33..b80f43ac15 100644
--- a/guix/import/pypi.scm
+++ b/guix/import/pypi.scm
@@ -11,6 +11,7 @@
 ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
 ;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
+;;; Copyright © 2022 Vivien Kraus <vivien@planete-kraus.eu>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -41,6 +42,7 @@
   #:use-module (guix memoization)
   #:use-module (guix diagnostics)
   #:use-module (guix i18n)
+  #:use-module ((guix ui) #:select (display-hint))
   #:use-module ((guix build utils)
                 #:select ((package-name->name+version
                            . hyphen-package-name->name+version)
@@ -59,6 +61,7 @@
             specification->requirement-name
             guix-package->pypi-name
             pypi-recursive-import
+            find-project-url
             pypi->guix-package
             %pypi-updater))
 
@@ -418,6 +421,24 @@ return the unaltered list of upstream dependency names."
     (values (map process-requirements dependencies)
             (concatenate dependencies))))
 
+(define (find-project-url name pypi-url)
+  "Try different project name substitution until the result is found in
+pypi-uri.  Downcase is required for \"uWSGI\", and
+underscores are required for flake8-array-spacing."
+  (or (find (cut string-contains pypi-url <>)
+            (list name
+                  (string-downcase name)
+                  (string-replace-substring name "-" "_")))
+      (begin
+        (warning
+         (G_ "project name ~a does not appear verbatim in the PyPI URI~%")
+         name)
+        (display-hint
+         (format #f (G_ "The PyPI URI is: @url{~a}.  You should review the
+pypi-uri declaration in the generated package. You may need to replace ~s with
+a substring of the PyPI URI that identifies the package.")  pypi-url name))
+name)))
+
 (define (make-pypi-sexp name version source-url wheel-url home-page synopsis
                         description license)
   "Return the `package' s-expression for a python package with the given NAME,
@@ -446,15 +467,7 @@ VERSION, SOURCE-URL, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
                     (origin
                       (method url-fetch)
                       (uri (pypi-uri
-                             ;; PyPI URL are case sensitive, but sometimes
-                             ;; a project named using mixed case has a URL
-                             ;; using lower case, so we must work around this
-                             ;; inconsistency.  For actual examples, compare
-                             ;; the URLs of the "Deprecated" and "uWSGI" PyPI
-                             ;; packages.
-                             ,(if (string-contains source-url name)
-                                  name
-                                  (string-downcase name))
+                             ,(find-project-url name source-url)
                              version
                              ;; Some packages have been released as `.zip`
                              ;; instead of the more common `.tar.gz`. For
diff --git a/tests/pypi.scm b/tests/pypi.scm
index 1ea5f02643..47a276ae58 100644
--- a/tests/pypi.scm
+++ b/tests/pypi.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
 ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
+;;; Copyright © 2022 Vivien Kraus <vivien@planete-kraus.eu>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -23,68 +24,48 @@
   #:use-module (guix import pypi)
   #:use-module (guix base32)
   #:use-module (guix memoization)
+  #:use-module (guix utils)
   #:use-module (gcrypt hash)
   #:use-module (guix tests)
   #:use-module (guix build-system python)
   #:use-module ((guix build utils) #:select (delete-file-recursively which mkdir-p))
+  #:use-module ((guix diagnostics) #:select (guix-warning-port))
+  #:use-module (json)
+  #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-64)
-  #:use-module (ice-9 match))
+  #:use-module (ice-9 match)
+  #:use-module (ice-9 optargs))
+
+(define* (foo-json #:key (name "foo") (name-in-url #f))
+  "Create a JSON description of an example pypi package, named @var{name},
+optionally using a different @var{name in its URL}."
+  (scm->json-string
+   `((info
+      . ((version . "1.0.0")
+         (name . ,name)
+         (license . "GNU LGPL")
+         (summary . "summary")
+         (home_page . "http://example.com")
+         (classifiers . #())
+         (download_url . "")))
+     (urls . #())
+     (releases
+      . ((1.0.0
+          . #(((url . ,(format #f "https://example.com/~a-1.0.0.egg"
+                               (or name-in-url name)))
+               (packagetype . "bdist_egg"))
+              ((url . ,(format #f "https://example.com/~a-1.0.0.tar.gz"
+                               (or name-in-url name)))
+               (packagetype . "sdist"))
+              ((url . ,(format #f "https://example.com/~a-1.0.0-py2.py3-none-any.whl"
+                               (or name-in-url name)))
+               (packagetype . "bdist_wheel")))))))))
 
 (define test-json-1
-  "{
-  \"info\": {
-    \"version\": \"1.0.0\",
-    \"name\": \"foo\",
-    \"license\": \"GNU LGPL\",
-    \"summary\": \"summary\",
-    \"home_page\": \"http://example.com\",
-    \"classifiers\": [],
-    \"download_url\": \"\"
-  },
-  \"urls\": [],
-  \"releases\": {
-    \"1.0.0\": [
-      {
-        \"url\": \"https://example.com/foo-1.0.0.egg\",
-        \"packagetype\": \"bdist_egg\"
-      }, {
-        \"url\": \"https://example.com/foo-1.0.0.tar.gz\",
-        \"packagetype\": \"sdist\"
-      }, {
-        \"url\": \"https://example.com/foo-1.0.0-py2.py3-none-any.whl\",
-        \"packagetype\": \"bdist_wheel\"
-      }
-    ]
-  }
-}")
+  (foo-json))
 
 (define test-json-2
-  "{
-  \"info\": {
-    \"version\": \"1.0.0\",
-    \"name\": \"foo-99\",
-    \"license\": \"GNU LGPL\",
-    \"summary\": \"summary\",
-    \"home_page\": \"http://example.com\",
-    \"classifiers\": [],
-    \"download_url\": \"\"
-  },
-  \"urls\": [],
-  \"releases\": {
-    \"1.0.0\": [
-      {
-        \"url\": \"https://example.com/foo-99-1.0.0.egg\",
-        \"packagetype\": \"bdist_egg\"
-      }, {
-        \"url\": \"https://example.com/foo-99-1.0.0.tar.gz\",
-        \"packagetype\": \"sdist\"
-      }, {
-        \"url\": \"https://example.com/foo-99-1.0.0-py2.py3-none-any.whl\",
-        \"packagetype\": \"bdist_wheel\"
-      }
-    ]
-  }
-}")
+  (foo-json #:name "foo-99"))
 
 (define test-source-hash
   "")
@@ -211,6 +192,30 @@ Requires-Dist: pytest (>=3.1.0); extra == 'testing'
          call-with-input-string)
         (parse-wheel-metadata test-metadata-with-extras-jedi)))
 
+(test-equal "find-project-url, with numpy"
+  "numpy"
+  (find-project-url
+   "numpy"
+   "https://files.pythonhosted.org/packages/0a/c8/a62767a6b374a0dfb02d2a0456e5f56a372cdd1689dbc6ffb6bf1ddedbc0/numpy-1.22.1.zip"))
+
+(test-equal "find-project-url, uWSGI"
+  "uwsgi"
+  (find-project-url
+   "uWSGI"
+   "https://files.pythonhosted.org/packages/24/fd/93851e4a076719199868d4c918cc93a52742e68370188c1c570a6e42a54f/uwsgi-2.0.20.tar.gz"))
+
+(test-equal "find-project-url, flake8-array-spacing"
+  "flake8_array_spacing"
+  (find-project-url
+   "flake8-array-spacing"
+   "https://files.pythonhosted.org/packages/a4/21/ff29b901128b681b7de7a2787b3aeb3e1f3cba4a8c0cffa9712cbff016bc/flake8_array_spacing-0.2.0.tar.gz"))
+
+(test-equal "find-project-url, foo/goo"
+  "foo"
+  (find-project-url
+   "foo"
+   "https://files.pythonhosted.org/packages/f0/f00/goo-0.0.0.tar.gz"))
+
 (test-assert "pypi->guix-package, no wheel"
   ;; Replace network resources with sample data.
     (mock ((guix import utils) url-fetch