gnome.scm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2019, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  4. ;;; Copyright © 2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix import gnome)
  21. #:use-module (guix upstream)
  22. #:use-module (guix utils)
  23. #:use-module (guix packages)
  24. #:use-module (guix http-client)
  25. #:use-module (json)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (web uri)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 regex)
  31. #:export (%gnome-updater))
  32. ;;; Commentary:
  33. ;;;
  34. ;;; This package provides not an actual importer but simply an updater for
  35. ;;; GNOME packages. It grabs package meta-data from 'cache.json' files
  36. ;;; available on ftp.gnome.org.
  37. ;;;
  38. ;;; Code:
  39. (define (jsonish->upstream-source name jsonish)
  40. "Return an <upstream-source> object for package NAME, using JSONISH as the
  41. source for metadata."
  42. (match jsonish
  43. ((version . dictionary)
  44. (upstream-source
  45. (package name)
  46. (version version)
  47. (urls (filter-map (lambda (extension)
  48. (match (assoc-ref dictionary extension)
  49. (#f
  50. #f)
  51. ((? string? relative-url)
  52. (string-append "mirror://gnome/sources/"
  53. name "/" relative-url))))
  54. '("tar.lz" "tar.xz" "tar.bz2" "tar.gz")))))))
  55. (define* (import-gnome-release package #:key (version #f))
  56. "Return the latest release of PACKAGE, a GNOME package, or #f if it could
  57. not be determined. Optionally include a VERSION string to fetch a specific
  58. version."
  59. (define %not-dot
  60. (char-set-complement (char-set #\.)))
  61. (define (pre-release-text? text)
  62. (string-match "^(alpha|beta|rc)" text))
  63. (define (release-version? version)
  64. "Predicate to check if VERSION matches the format of a GNOME release
  65. version. A release version can have more than one form, depending on the
  66. GNOME component, but typically it takes the form of a major-minor tuple, where
  67. minor can also be prefixed wih \"alpha\", \"beta\" or \"rc\". For more
  68. information about the GNOME versioning scheme, see:
  69. https://discourse.gnome.org/t/new-gnome-versioning-scheme/4235"
  70. (define components (string-tokenize version %not-dot))
  71. (if (any pre-release-text? components)
  72. #f ;ignore pre-releases
  73. (match components
  74. (((= string->number major) (= string->number minor) . _)
  75. ;; Any other 3+ components versions such as "2.72.2".
  76. (and major minor))
  77. (((= string->number major) . _)
  78. ;; A GNOME version strings like "42.1".
  79. major))))
  80. (define upstream-name
  81. ;; Some packages like "NetworkManager" have camel-case names.
  82. (package-upstream-name package))
  83. (define (find-latest-release releases)
  84. (fold (match-lambda*
  85. (((key . value) result)
  86. (cond ((release-version? key)
  87. (match result
  88. (#f
  89. (cons key value))
  90. ((newest . _)
  91. (if (version>? key newest)
  92. (cons key value)
  93. result))))
  94. (else
  95. result))))
  96. #f
  97. releases))
  98. (define (find-version-release releases version)
  99. (find (match-lambda
  100. ((key . value)
  101. (string=? key version)))
  102. releases))
  103. (guard (c ((http-get-error? c)
  104. (if (= 404 (http-get-error-code c))
  105. #f
  106. (raise c))))
  107. (let* ((port (http-fetch/cached
  108. (string->uri (string-append
  109. "https://ftp.gnome.org/pub/gnome/sources/"
  110. upstream-name "/cache.json"))
  111. ;; ftp.gnome.org supports 'if-Modified-Since', so the local
  112. ;; cache can expire early.
  113. #:ttl (* 60 10)
  114. ;; Hide messages about URL redirects.
  115. #:log-port (%make-void-port "w")))
  116. (json (json->scm port)))
  117. (close-port port)
  118. (match json
  119. (#(4 releases _ ...)
  120. (let* ((releases (assoc-ref releases upstream-name))
  121. (latest (if version
  122. (find-version-release releases version)
  123. (find-latest-release releases))))
  124. (and latest
  125. (jsonish->upstream-source upstream-name latest))))))))
  126. (define %gnome-updater
  127. (upstream-updater
  128. (name 'gnome)
  129. (description "Updater for GNOME packages")
  130. (pred (url-prefix-predicate "mirror://gnome/"))
  131. (import import-gnome-release)))