Mirror of GNU Guix
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.

94 lines
3.5 KiB

  1. ;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
  2. ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of Guix.
  5. ;;;
  6. ;;; Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (distro)
  19. #:use-module (guix packages)
  20. #:use-module (ice-9 ftw)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-26)
  23. #:export (find-packages-by-name))
  24. ;;; Commentary:
  25. ;;;
  26. ;;; General utilities for the software distribution---i.e., the modules under
  27. ;;; (distro ...).
  28. ;;;
  29. ;;; Code:
  30. (define _ (cut gettext <> "guix"))
  31. (define %distro-module-directory
  32. ;; Absolute path of the (distro ...) module root.
  33. (string-append (dirname (search-path %load-path "distro.scm"))
  34. "/distro"))
  35. (define (package-files)
  36. "Return the list of files that implement distro modules."
  37. (define prefix-len
  38. (string-length (dirname %distro-module-directory)))
  39. (file-system-fold (const #t) ; enter?
  40. (lambda (path stat result) ; leaf
  41. (if (string-suffix? ".scm" path)
  42. (cons (substring path prefix-len) result)
  43. result))
  44. (lambda (path stat result) ; down
  45. result)
  46. (lambda (path stat result) ; up
  47. result)
  48. (const #f) ; skip
  49. (lambda (path stat errno result)
  50. (format (current-error-port)
  51. (_ "warning: cannot access `~a': ~a")
  52. path (strerror errno))
  53. result)
  54. '()
  55. %distro-module-directory
  56. stat))
  57. (define (package-modules)
  58. "Return the list of modules that provide packages for the distribution."
  59. (define not-slash
  60. (char-set-complement (char-set #\/)))
  61. (filter-map (lambda (path)
  62. (let ((name (map string->symbol
  63. (string-tokenize (string-drop-right path 4)
  64. not-slash))))
  65. (false-if-exception (resolve-interface name))))
  66. (package-files)))
  67. (define* (find-packages-by-name name #:optional version)
  68. "Return the list of packages with the given NAME. If VERSION is not #f,
  69. then only return packages whose version is equal to VERSION."
  70. (define right-package?
  71. (if version
  72. (lambda (p)
  73. (and (package? p)
  74. (string=? (package-name p) name)
  75. (string=? (package-version p) version)))
  76. (lambda (p)
  77. (and (package? p)
  78. (string=? (package-name p) name)))))
  79. (append-map (lambda (module)
  80. (filter right-package?
  81. (module-map (lambda (sym var)
  82. (variable-ref var))
  83. module)))
  84. (package-modules)))