You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
3.4 KiB

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