opam.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  4. ;;; Copyright © 2021 Alice Brenon <alice.brenon@ens-lyon.fr>
  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. (define-module (guix scripts import opam)
  21. #:use-module (guix ui)
  22. #:use-module (guix utils)
  23. #:use-module (guix scripts)
  24. #:use-module (guix import opam)
  25. #:use-module (guix scripts import)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-37)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 format)
  31. #:export (guix-import-opam))
  32. ;;;
  33. ;;; Command-line options.
  34. ;;;
  35. (define %default-options
  36. '())
  37. (define (show-help)
  38. (display (G_ "Usage: guix import opam PACKAGE-NAME
  39. Import and convert the opam package for PACKAGE-NAME.\n"))
  40. (display (G_ "
  41. -h, --help display this help and exit"))
  42. (display (G_ "
  43. -r, --recursive import packages recursively"))
  44. (display (G_ "
  45. --repo import packages from this opam repository (name, URL or local path)
  46. can be used more than once"))
  47. (display (G_ "
  48. -V, --version display version information and exit"))
  49. (newline)
  50. (show-bug-report-information))
  51. (define %options
  52. ;; Specification of the command-line options.
  53. (cons* (option '(#\h "help") #f #f
  54. (lambda args
  55. (show-help)
  56. (exit 0)))
  57. (option '(#\V "version") #f #f
  58. (lambda args
  59. (show-version-and-exit "guix import opam")))
  60. (option '(#f "repo") #t #f
  61. (lambda (opt name arg result)
  62. (alist-cons 'repo arg result)))
  63. (option '(#\r "recursive") #f #f
  64. (lambda (opt name arg result)
  65. (alist-cons 'recursive #t result)))
  66. %standard-import-options))
  67. ;;;
  68. ;;; Entry point.
  69. ;;;
  70. (define (guix-import-opam . args)
  71. (define (parse-options)
  72. ;; Return the alist of option values.
  73. (parse-command-line args %options (list %default-options)
  74. #:build-options? #f))
  75. (let* ((opts (parse-options))
  76. (repo (filter-map (match-lambda
  77. (('repo . name) name)
  78. (_ #f)) opts))
  79. (args (filter-map (match-lambda
  80. (('argument . value)
  81. value)
  82. (_ #f))
  83. (reverse opts))))
  84. (match args
  85. ((package-name)
  86. (if (assoc-ref opts 'recursive)
  87. ;; Recursive import
  88. (map (match-lambda
  89. ((and ('package ('name name) . rest) pkg)
  90. `(define-public ,(string->symbol name)
  91. ,pkg))
  92. (_ #f))
  93. (opam-recursive-import package-name #:repo repo))
  94. ;; Single import
  95. (let ((sexp (opam->guix-package package-name #:repo repo)))
  96. (unless sexp
  97. (leave (G_ "failed to download meta-data for package '~a'~%")
  98. package-name))
  99. sexp)))
  100. (()
  101. (leave (G_ "too few arguments~%")))
  102. ((many ...)
  103. (leave (G_ "too many arguments~%"))))))