import.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  4. ;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com>
  5. ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts import)
  22. #:use-module (guix ui)
  23. #:use-module (guix scripts)
  24. #:use-module (guix utils)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (srfi srfi-37)
  29. #:use-module (ice-9 format)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 pretty-print)
  32. #:export (%standard-import-options
  33. guix-import))
  34. ;;;
  35. ;;; Helper.
  36. ;;;
  37. (define (newline-rewriting-port output)
  38. "Return an output port that rewrites strings containing the \\n escape
  39. to an actual newline. This works around the behavior of `pretty-print'
  40. and `write', which output these as \\n instead of actual newlines,
  41. whereas we want the `description' field to contain actual newlines
  42. rather than \\n."
  43. (define (write-string str)
  44. (let loop ((chars (string->list str)))
  45. (match chars
  46. (()
  47. #t)
  48. ((#\\ #\n rest ...)
  49. (newline output)
  50. (loop rest))
  51. ((chr rest ...)
  52. (write-char chr output)
  53. (loop rest)))))
  54. (make-soft-port (vector (cut write-char <>)
  55. write-string
  56. (lambda _ #t) ; flush
  57. #f
  58. (lambda _ #t) ; close
  59. #f)
  60. "w"))
  61. ;;;
  62. ;;; Command line options.
  63. ;;;
  64. (define %standard-import-options '())
  65. ;;;
  66. ;;; Entry point.
  67. ;;;
  68. (define importers '("gnu" "nix" "pypi" "cpan" "hackage" "stackage" "elpa" "gem"
  69. "cran" "crate" "texlive" "json" "opam"))
  70. (define (resolve-importer name)
  71. (let ((module (resolve-interface
  72. `(guix scripts import ,(string->symbol name))))
  73. (proc (string->symbol (string-append "guix-import-" name))))
  74. (module-ref module proc)))
  75. (define (show-help)
  76. (display (G_ "Usage: guix import IMPORTER ARGS ...
  77. Run IMPORTER with ARGS.\n"))
  78. (newline)
  79. (display (G_ "IMPORTER must be one of the importers listed below:\n"))
  80. (newline)
  81. (format #t "~{ ~a~%~}" importers)
  82. (display (G_ "
  83. -h, --help display this help and exit"))
  84. (display (G_ "
  85. -V, --version display version information and exit"))
  86. (newline)
  87. (show-bug-report-information))
  88. (define-command (guix-import . args)
  89. (category packaging)
  90. (synopsis "import a package definition from an external repository")
  91. (match args
  92. (()
  93. (format (current-error-port)
  94. (G_ "guix import: missing importer name~%")))
  95. ((or ("-h") ("--help"))
  96. (show-help)
  97. (exit 0))
  98. ((or ("-V") ("--version"))
  99. (show-version-and-exit "guix import"))
  100. ((importer args ...)
  101. (if (member importer importers)
  102. (let ((print (lambda (expr)
  103. (pretty-print expr (newline-rewriting-port
  104. (current-output-port))))))
  105. (match (apply (resolve-importer importer) args)
  106. ((and expr (or ('package _ ...)
  107. ('let _ ...)))
  108. (print expr))
  109. ((? list? expressions)
  110. (for-each (lambda (expr)
  111. (print expr)
  112. (newline))
  113. expressions))
  114. (x
  115. (leave (G_ "'~a' import failed~%") importer))))
  116. (leave (G_ "~a: invalid importer~%") importer)))))