launchpad.scm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Arun Isaac <arunisaac@systemreboot.net>
  3. ;;; Copyright © 2021 Matthew James Kraai <kraai@ftbfs.org>
  4. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  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 launchpad)
  21. #:use-module (ice-9 match)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-26)
  24. #:use-module (web uri)
  25. #:use-module ((guix download) #:prefix download:)
  26. #:use-module (guix import json)
  27. #:use-module (guix packages)
  28. #:use-module (guix upstream)
  29. #:use-module (guix utils)
  30. #:export (%launchpad-updater))
  31. (define (find-extension url)
  32. "Return the extension of the archive e.g. '.tar.gz' given a URL, or
  33. false if none is recognized"
  34. (find (lambda (x) (string-suffix? x url))
  35. (list ".orig.tar.gz" ".tar.gz" ".tar.bz2" ".tar.xz"
  36. ".zip" ".tar" ".tgz" ".tbz" ".love")))
  37. (define (updated-launchpad-url old-package new-version)
  38. ;; Return a url for the OLD-PACKAGE with NEW-VERSION. If no source url in
  39. ;; the OLD-PACKAGE is a Launchpad url, then return false.
  40. (define (updated-url url)
  41. (and (string-prefix? "https://launchpad.net/" url)
  42. (let ((ext (or (find-extension url) ""))
  43. (name (package-name old-package))
  44. (version (package-version old-package))
  45. (repo (launchpad-repository url)))
  46. (cond
  47. ((< (length (string-split version #\.)) 2) #f)
  48. ((string=? (string-append "https://launchpad.net/"
  49. repo "/" (version-major+minor version)
  50. "/" version "/+download/" repo "-" version ext)
  51. url)
  52. (string-append "https://launchpad.net/"
  53. repo "/" (version-major+minor new-version)
  54. "/" new-version "/+download/" repo "-" new-version ext))
  55. ((string=? (string-append "https://launchpad.net/"
  56. repo "/" (version-major+minor version)
  57. "/" version "/+download/" repo "_" version ext)
  58. url)
  59. (string-append "https://launchpad.net/"
  60. repo "/" (version-major+minor new-version)
  61. "/" new-version "/+download/" repo "-" new-version ext))
  62. ((string=? (string-append "https://launchpad.net/"
  63. repo "/trunk/" version "/+download/"
  64. repo "-" version ext)
  65. url)
  66. (string-append "https://launchpad.net/"
  67. repo "/trunk/" new-version
  68. "/+download/" repo "-" new-version ext))
  69. ((string=? (string-append "https://launchpad.net/"
  70. repo "/trunk/" version "/+download/"
  71. repo "_" version ext)
  72. url)
  73. (string-append "https://launchpad.net/"
  74. repo "/trunk/" new-version
  75. "/+download/" repo "_" new-version ext))
  76. (#t #f))))) ; Some URLs are not recognised.
  77. (match (package-source old-package)
  78. ((? origin? origin)
  79. (let ((source-uri (origin-uri origin))
  80. (fetch-method (origin-method origin)))
  81. (and (eq? fetch-method download:url-fetch)
  82. (match source-uri
  83. ((? string?)
  84. (updated-url source-uri))
  85. ((source-uri ...)
  86. (any updated-url source-uri))))))
  87. (_ #f)))
  88. (define (launchpad-package? package)
  89. "Return true if PACKAGE is a package from Launchpad, else false."
  90. (->bool (updated-launchpad-url package "1.0.0")))
  91. (define (launchpad-repository url)
  92. "Return a string e.g. linuxdcpp of the name of the repository, from a string
  93. URL of the form
  94. 'https://launchpad.net/linuxdcpp/1.1/1.1.0/+download/linuxdcpp-1.1.0.tar.bz2'"
  95. (match (string-split (uri-path (string->uri url)) #\/)
  96. ((_ repo . rest) repo)))
  97. (define (latest-released-version repository)
  98. "Return a string of the newest released version name given the REPOSITORY,
  99. for example, 'linuxdcpp'. Return #f if there is no releases."
  100. (define (pre-release? x)
  101. ;; Versions containing anything other than digit characters and "." (for
  102. ;; example, "5.1.0-rc1") are assumed to be pre-releases.
  103. (not (string-every (char-set-union (char-set #\.)
  104. char-set:digit)
  105. (assoc-ref x "version"))))
  106. (match (json-fetch
  107. (string-append "https://api.launchpad.net/1.0/"
  108. repository "/releases"))
  109. (#f #f) ;404 or similar
  110. (json
  111. (assoc-ref
  112. (last (remove pre-release? (vector->list (assoc-ref json "entries"))))
  113. "version"))))
  114. (define (latest-release pkg)
  115. "Return an <upstream-source> for the latest release of PKG."
  116. (define (origin-launchpad-uri origin)
  117. (match (origin-uri origin)
  118. ((? string? url) url) ; surely a Launchpad URL
  119. ((urls ...)
  120. (find (cut string-contains <> "launchpad.net") urls))))
  121. (let* ((source-uri (origin-launchpad-uri (package-source pkg)))
  122. (name (package-name pkg))
  123. (repository (launchpad-repository source-uri))
  124. (newest-version (latest-released-version repository)))
  125. (if newest-version
  126. (upstream-source
  127. (package name)
  128. (version newest-version)
  129. (urls (list (updated-launchpad-url pkg newest-version))))
  130. #f))) ; On Launchpad but no proper releases
  131. (define %launchpad-updater
  132. (upstream-updater
  133. (name 'launchpad)
  134. (description "Updater for Launchpad packages")
  135. (pred launchpad-package?)
  136. (latest latest-release)))