import.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2020, 2021 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" "pypi" "cpan" "hackage" "stackage" "egg" "elpa"
  69. "gem" "go" "cran" "crate" "texlive" "json" "opam"
  70. "minetest"))
  71. (define (resolve-importer name)
  72. (let ((module (resolve-interface
  73. `(guix scripts import ,(string->symbol name))))
  74. (proc (string->symbol (string-append "guix-import-" name))))
  75. (module-ref module proc)))
  76. (define (show-help)
  77. (display (G_ "Usage: guix import IMPORTER ARGS ...
  78. Run IMPORTER with ARGS.\n"))
  79. (newline)
  80. (display (G_ "IMPORTER must be one of the importers listed below:\n"))
  81. (newline)
  82. (format #t "~{ ~a~%~}" importers)
  83. (display (G_ "
  84. -h, --help display this help and exit"))
  85. (display (G_ "
  86. -V, --version display version information and exit"))
  87. (newline)
  88. (show-bug-report-information))
  89. (define-command (guix-import . args)
  90. (category packaging)
  91. (synopsis "import a package definition from an external repository")
  92. (match args
  93. (()
  94. (format (current-error-port)
  95. (G_ "guix import: missing importer name~%")))
  96. ((or ("-h") ("--help"))
  97. (show-help)
  98. (exit 0))
  99. ((or ("-V") ("--version"))
  100. (show-version-and-exit "guix import"))
  101. ((importer args ...)
  102. (if (member importer importers)
  103. (let ((print (lambda (expr)
  104. (pretty-print expr (newline-rewriting-port
  105. (current-output-port))))))
  106. (match (apply (resolve-importer importer) args)
  107. ((and expr (or ('package _ ...)
  108. ('let _ ...)
  109. ('define-public _ ...)))
  110. (print expr))
  111. ((? list? expressions)
  112. (for-each (lambda (expr)
  113. (print expr)
  114. (newline))
  115. expressions))
  116. (x
  117. (leave (G_ "'~a' import failed~%") importer))))
  118. (leave (G_ "~a: invalid importer~%") importer)))))