a52cd95c931f27080b4ed37e7f1bf7c9c376bf72
[jackhill/guix/guix.git] / guix / scripts / import / pypi.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (guix scripts import pypi)
22 #:use-module (guix ui)
23 #:use-module (guix utils)
24 #:use-module (guix scripts)
25 #:use-module (guix import pypi)
26 #:use-module (guix scripts import)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-11)
29 #:use-module (srfi srfi-37)
30 #:use-module (srfi srfi-71)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 format)
33 #:export (guix-import-pypi))
34
35 \f
36 ;;;
37 ;;; Command-line options.
38 ;;;
39
40 (define %default-options
41 '())
42
43 (define (show-help)
44 (display (G_ "Usage: guix import pypi PACKAGE-NAME
45 Import and convert the PyPI package for PACKAGE-NAME.\n"))
46 (display (G_ "
47 -h, --help display this help and exit"))
48 (display (G_ "
49 -r, --recursive import packages recursively"))
50 (display (G_ "
51 -V, --version display version information and exit"))
52 (newline)
53 (show-bug-report-information))
54
55 (define %options
56 ;; Specification of the command-line options.
57 (cons* (option '(#\h "help") #f #f
58 (lambda args
59 (show-help)
60 (exit 0)))
61 (option '(#\V "version") #f #f
62 (lambda args
63 (show-version-and-exit "guix import pypi")))
64 (option '(#\r "recursive") #f #f
65 (lambda (opt name arg result)
66 (alist-cons 'recursive #t result)))
67 %standard-import-options))
68
69 \f
70 ;;;
71 ;;; Entry point.
72 ;;;
73
74 (define (guix-import-pypi . args)
75 (define (parse-options)
76 ;; Return the alist of option values.
77 (parse-command-line args %options (list %default-options)
78 #:build-options? #f))
79
80 (let* ((opts (parse-options))
81 (args (filter-map (match-lambda
82 (('argument . value)
83 value)
84 (_ #f))
85 (reverse opts))))
86 (match args
87 ((spec)
88 (let ((name version (package-name->name+version spec)))
89 (if (assoc-ref opts 'recursive)
90 ;; Recursive import
91 (map (match-lambda
92 ((and ('package ('name name) . rest) pkg)
93 `(define-public ,(string->symbol name)
94 ,pkg))
95 (_ #f))
96 (pypi-recursive-import name version))
97 ;; Single import
98 (let ((sexp (pypi->guix-package name #:version version)))
99 (unless sexp
100 (leave (G_ "failed to download meta-data for package '~a'~%")
101 name))
102 sexp))))
103 (()
104 (leave (G_ "too few arguments~%")))
105 ((many ...)
106 (leave (G_ "too many arguments~%"))))))