cran.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
  3. ;;; Copyright © 2015, 2017, 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  5. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  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 cran)
  22. #:use-module (guix ui)
  23. #:use-module (guix utils)
  24. #:use-module (guix scripts)
  25. #:use-module (guix import cran)
  26. #:use-module (guix import utils)
  27. #:use-module (guix scripts import)
  28. #:use-module (srfi srfi-1)
  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-cran))
  34. ;;;
  35. ;;; Command-line options.
  36. ;;;
  37. (define %default-options
  38. '())
  39. (define (show-help)
  40. (display (G_ "Usage: guix import cran PACKAGE-NAME
  41. Import and convert the CRAN package for PACKAGE-NAME.\n"))
  42. (display (G_ "
  43. -a, --archive=ARCHIVE specify the archive repository"))
  44. (display (G_ "
  45. -h, --help display this help and exit"))
  46. (display (G_ "
  47. -r, --recursive import packages recursively"))
  48. (display (G_ "
  49. -s, --style=STYLE choose output style, either specification or variable"))
  50. (display (G_ "
  51. -p, --license-prefix=PREFIX
  52. add custom prefix to licenses"))
  53. (display (G_ "
  54. -V, --version display version information and exit"))
  55. (newline)
  56. (show-bug-report-information))
  57. (define %options
  58. ;; Specification of the command-line options.
  59. (cons* (option '(#\h "help") #f #f
  60. (lambda args
  61. (show-help)
  62. (exit 0)))
  63. (option '(#\V "version") #f #f
  64. (lambda args
  65. (show-version-and-exit "guix import cran")))
  66. (option '(#\a "archive") #t #f
  67. (lambda (opt name arg result)
  68. (alist-cons 'repo (string->symbol arg)
  69. (alist-delete 'repo result))))
  70. (option '(#\s "style") #t #f
  71. (lambda (opt name arg result)
  72. (alist-cons 'style (string->symbol arg)
  73. (alist-delete 'style result))))
  74. (option '(#\p "license-prefix") #t #f
  75. (lambda (opt name arg result)
  76. (alist-cons 'license-prefix arg
  77. (alist-delete 'license-prefix result))))
  78. (option '(#\r "recursive") #f #f
  79. (lambda (opt name arg result)
  80. (alist-cons 'recursive #t result)))
  81. %standard-import-options))
  82. ;;;
  83. ;;; Entry point.
  84. ;;;
  85. (define (guix-import-cran . args)
  86. (define (parse-options)
  87. ;; Return the alist of option values.
  88. (parse-command-line args %options (list %default-options)
  89. #:build-options? #f))
  90. (let* ((opts (parse-options))
  91. (args (filter-map (match-lambda
  92. (('argument . value)
  93. value)
  94. (_ #f))
  95. (reverse opts)))
  96. (prefix (assoc-ref opts 'license-prefix))
  97. (prefix-proc (if (string? prefix)
  98. (lambda (symbol)
  99. (string->symbol
  100. (string-append prefix (symbol->string symbol))))
  101. identity)))
  102. (parameterize ((%input-style (assoc-ref opts 'style)))
  103. (match args
  104. ((spec)
  105. (let ((name version (package-name->name+version spec)))
  106. (if (assoc-ref opts 'recursive)
  107. ;; Recursive import
  108. (with-error-handling
  109. (map package->definition
  110. (filter identity
  111. (cran-recursive-import name
  112. #:version version
  113. #:repo (or (assoc-ref opts 'repo) 'cran)
  114. #:license-prefix prefix-proc))))
  115. ;; Single import
  116. (let ((sexp (cran->guix-package name
  117. #:version version
  118. #:repo (or (assoc-ref opts 'repo) 'cran)
  119. #:license-prefix prefix-proc)))
  120. (unless sexp
  121. (leave (G_ "failed to download description for package '~a'~%")
  122. name))
  123. sexp))))
  124. (()
  125. (leave (G_ "too few arguments~%")))
  126. ((many ...)
  127. (leave (G_ "too many arguments~%")))))))