diff mbox

[4/5] import: pypi: Compute test requirements when reading requirements files.

Message ID 1468682605-12622-5-git-send-email-tipecaml@gmail.com
State New
Headers show

Commit Message

Cyril Roelandt July 16, 2016, 3:23 p.m. UTC
* guix/import/pypi.scm (guess-requirement-from-source): Read
test-requirements.txt as well as requirements.txt.
* tests/pypi.scm ("pypi->guix-package"): Update accordingly.
---
 guix/import/pypi.scm | 20 +++++++++++++++-----
 tests/pypi.scm       | 11 +++++++++++
 2 files changed, 26 insertions(+), 5 deletions(-)

Comments

Ludovic Courtès July 22, 2016, 9:30 p.m. UTC | #1
Cyril Roelandt <tipecaml@gmail.com> skribis:

> * guix/import/pypi.scm (guess-requirement-from-source): Read
> test-requirements.txt as well as requirements.txt.
> * tests/pypi.scm ("pypi->guix-package"): Update accordingly.

[...]

>            (let* ((req-file (string-append dirname "/requirements.txt"))
> -                 (exit-code (system* "tar" "xf" tarball req-file)))
> +                 (test-req-file (string-append dirname "/test-requirements.txt"))
> +                 (exit-code (system* "tar" "xf" tarball req-file))
> +                 (test-exit-code (system* "tar" "xf" tarball test-req-file)))
>              ;; TODO: support more formats.
> -            (if (zero? exit-code)
> +            (if (or (zero? exit-code)
> +                    (zero? test-exit-code))

This seems to suggest that this could be factorized somehow.  Maybe
unpack once and read the two files at once?

Ludo’.
Cyril Roelandt July 23, 2016, 2:48 p.m. UTC | #2
On 07/22/2016 11:30 PM, Ludovic Courtès wrote:
> This seems to suggest that this could be factorized somehow.  Maybe
> unpack once and read the two files at once?

The problem is that both files might not be there, and unzip will return
a non-zero exit code if any of them is missing, so it seems easier to
just run unzip twice. WDYT?

Cyril.
Ludovic Courtès July 25, 2016, 9:12 p.m. UTC | #3
Cyril Roelandt <tipecaml@gmail.com> skribis:

> On 07/22/2016 11:30 PM, Ludovic Courtès wrote:
>> This seems to suggest that this could be factorized somehow.  Maybe
>> unpack once and read the two files at once?
>
> The problem is that both files might not be there, and unzip will return
> a non-zero exit code if any of them is missing, so it seems easier to
> just run unzip twice. WDYT?

OK, I see.  Then what about adding a procedure like:

  (define (file-from-zip-archive archive file)
    "Return the contents of FILE from ARCHIVE as a string, or #f if FILE
  could not be found in ARCHIVE or extraction failed."
    ;; … invoke unzip in temporary dir, check return code, then:
    (call-with-input-file (string-append tempdir "/" file)
      get-string-all))

That would move the bits about exit codes and all that out of the main
logic.

WDYT?

Thanks,
Ludo’.
diff mbox

Patch

diff --git a/guix/import/pypi.scm b/guix/import/pypi.scm
index f43d7d5..917713d 100644
--- a/guix/import/pypi.scm
+++ b/guix/import/pypi.scm
@@ -210,16 +210,26 @@  cannot determine package dependencies"))
     (let ((dirname (tarball-directory source-url)))
       (if (string? dirname)
           (let* ((req-file (string-append dirname "/requirements.txt"))
-                 (exit-code (system* "tar" "xf" tarball req-file)))
+                 (test-req-file (string-append dirname "/test-requirements.txt"))
+                 (exit-code (system* "tar" "xf" tarball req-file))
+                 (test-exit-code (system* "tar" "xf" tarball test-req-file)))
             ;; TODO: support more formats.
-            (if (zero? exit-code)
+            (if (or (zero? exit-code)
+                    (zero? test-exit-code))
                 (dynamic-wind
                   (const #t)
                   (lambda ()
-                    (list (read-requirements req-file)
-                          '()))
+                    (list (if (zero? exit-code)
+                              (read-requirements req-file)
+                              '())
+                          (if (zero? test-exit-code)
+                              (read-requirements test-req-file)
+                              '())))
                   (lambda ()
-                    (delete-file req-file)
+                    (when (zero? exit-code)
+                          (delete-file req-file))
+                    (when (zero? test-exit-code)
+                          (delete-file test-req-file))
                     (rmdir dirname)))
                 (begin
                   (warning (_ "'tar xf' failed with exit code ~a\n")
diff --git a/tests/pypi.scm b/tests/pypi.scm
index cbf7066..7a38d6e 100644
--- a/tests/pypi.scm
+++ b/tests/pypi.scm
@@ -59,6 +59,10 @@ 
 bar
 baz > 13.37")
 
+(define test-test-requirements
+"test-bar
+test-baz > 42.42")
+
 (define test-metadata
   "{
   \"run_requires\": [
@@ -96,6 +100,9 @@  baz > 13.37")
                  (with-output-to-file "foo-1.0.0/requirements.txt"
                    (lambda ()
                      (display test-requirements)))
+                 (with-output-to-file "foo-1.0.0/test-requirements.txt"
+                   (lambda ()
+                     (display test-test-requirements)))
                  (system* "tar" "czvf" file-name "foo-1.0.0/")
                  (delete-file-recursively "foo-1.0.0")
                  (set! test-source-hash
@@ -118,6 +125,10 @@  baz > 13.37")
           ('quasiquote
            (("python-bar" ('unquote 'python-bar))
             ("python-baz" ('unquote 'python-baz)))))
+         ('native-inputs
+          ('quasiquote
+           (("python-test-bar" ('unquote 'python-test-bar))
+            ("python-test-baz" ('unquote 'python-test-baz)))))
          ('home-page "http://example.com")
          ('synopsis "summary")
          ('description "summary")