kde.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  3. ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2019 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 kde)
  21. #:use-module (guix http-client)
  22. #:use-module (guix memoization)
  23. #:use-module (guix gnu-maintenance)
  24. #:use-module (guix packages)
  25. #:use-module (guix upstream)
  26. #:use-module (guix utils)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 rdelim)
  29. #:use-module (ice-9 regex)
  30. #:use-module (srfi srfi-11)
  31. #:use-module (web uri)
  32. #:export (%kde-updater))
  33. ;;; Commentary:
  34. ;;;
  35. ;;; This package provides not an actual importer but simply an updater for
  36. ;;; KDE packages. It grabs available files from the 'ls-lR.bz2' file
  37. ;;; available on download.kde.org.
  38. ;;;
  39. ;;; Code:
  40. (define (tarball->version tarball)
  41. "Return the version TARBALL corresponds to. TARBALL is a file name like
  42. \"coreutils-8.23.tar.xz\"."
  43. (let-values (((name version)
  44. (gnu-package-name->name+version
  45. (tarball-sans-extension tarball))))
  46. version))
  47. (define %kde-file-list-uri
  48. ;; URI of the file list (ls -lR format) for download.kde.org.
  49. (string->uri "https://download.kde.org/ls-lR.bz2"))
  50. (define (download.kde.org-files)
  51. ;;"Return the list of files available at download.kde.org."
  52. (define (ls-lR-line->filename path line)
  53. ;; Remove mode, blocks, user, group, size, date, time and one space,
  54. ;; then prepend PATH
  55. (regexp-substitute
  56. #f (string-match "^(\\S+\\s+){6}\\S+\\s" line) path 'post))
  57. (define (canonicalize path)
  58. (let* ((path (if (string-prefix? "/srv/archives/ftp/" path)
  59. (string-drop path (string-length "/srv/archives/ftp"))
  60. path))
  61. (path (if (string-suffix? ":" path)
  62. (string-drop-right path 1)
  63. path))
  64. (path (if (not (string-suffix? "/" path))
  65. (string-append path "/")
  66. path)))
  67. path))
  68. (define (write-cache input cache)
  69. "Read bzipped ls-lR from INPUT, and write it as a list of file paths to
  70. CACHE."
  71. (call-with-decompressed-port 'bzip2 input
  72. (lambda (input)
  73. (let loop_dirs ((files '()))
  74. ;; process a new directory block
  75. (let ((path (read-line input)))
  76. (if
  77. (or (eof-object? path) (string= path ""))
  78. (write (reverse files) cache)
  79. (let loop_entries ((path (canonicalize path))
  80. (files files))
  81. ;; process entries within the directory block
  82. (let ((line (read-line input)))
  83. (cond
  84. ((eof-object? line)
  85. (write (reverse files) cache))
  86. ((string-prefix? "-" line)
  87. ;; this is a file entry: prepend to FILES, then re-enter
  88. ;; the loop for remaining entries
  89. (loop_entries path
  90. (cons (ls-lR-line->filename path line) files)
  91. ))
  92. ((not (string= line ""))
  93. ;; this is a non-file entry: ignore it, just re-enter the
  94. ;; loop for remaining entries
  95. (loop_entries path files))
  96. ;; empty line: directory block end, re-enter the outer
  97. ;; loop for the next block
  98. (#t (loop_dirs files)))))))))))
  99. (define (cache-miss uri)
  100. (format (current-error-port) "fetching ~a...~%" (uri->string uri)))
  101. (let* ((port (http-fetch/cached %kde-file-list-uri
  102. #:ttl 3600
  103. #:write-cache write-cache
  104. #:cache-miss cache-miss))
  105. (files (read port)))
  106. (close-port port)
  107. files))
  108. (define (uri->kde-path-pattern uri)
  109. "Build a regexp from the package's URI suitable for matching the package
  110. path version-agnostic.
  111. Example:
  112. Input:
  113. mirror://kde//stable/frameworks/5.55/portingAids/kross-5.55.0.zip
  114. Output:
  115. //stable/frameworks/[^/]+/portingAids/
  116. "
  117. (define version-regexp
  118. ;; regexp for matching versions as used in the ld-lR file
  119. (make-regexp
  120. (string-join '("^([0-9]+\\.)+[0-9]+-?" ;; 5.12.90, 4.2.0-preview
  121. "^[0-9]+$" ;; 20031002
  122. ".*-([0-9]+\\.)+[0-9]+$") ;; kdepim-4.6.1
  123. "|")))
  124. (define (version->pattern part)
  125. ;; If a path element might be a version, replace it by a catch-all part
  126. (if (regexp-exec version-regexp part)
  127. "[^/]+"
  128. part))
  129. (let* ((path (uri-path uri))
  130. (directory-parts (string-split (dirname path) #\/)))
  131. (make-regexp
  132. (string-append
  133. (string-join (map version->pattern directory-parts) "/")
  134. "/"))))
  135. (define (latest-kde-release package)
  136. "Return the latest release of PACKAGE, a KDE package, or #f if it could
  137. not be determined."
  138. (let* ((uri (string->uri (origin-uri (package-source package))))
  139. (path-rx (uri->kde-path-pattern uri))
  140. (name (package-upstream-name package))
  141. (files (download.kde.org-files))
  142. (relevant (filter (lambda (file)
  143. (and (regexp-exec path-rx file)
  144. (release-file? name (basename file))))
  145. files)))
  146. (match (sort relevant (lambda (file1 file2)
  147. (version>? (tarball-sans-extension
  148. (basename file1))
  149. (tarball-sans-extension
  150. (basename file2)))))
  151. ((and tarballs (reference _ ...))
  152. (let* ((version (tarball->version reference))
  153. (tarballs (filter (lambda (file)
  154. (string=? (tarball-sans-extension
  155. (basename file))
  156. (tarball-sans-extension
  157. (basename reference))))
  158. tarballs)))
  159. (upstream-source
  160. (package name)
  161. (version version)
  162. (urls (map (lambda (file)
  163. (string-append "mirror://kde/" file))
  164. tarballs)))))
  165. (()
  166. #f))))
  167. (define %kde-updater
  168. (upstream-updater
  169. (name 'kde)
  170. (description "Updater for KDE packages")
  171. (pred (url-prefix-predicate "mirror://kde/"))
  172. (latest latest-kde-release)))