gnu-maintenance.scm 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2012, 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  5. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  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 gnu-maintenance)
  22. #:use-module (web uri)
  23. #:use-module (web client)
  24. #:use-module (web response)
  25. #:use-module (sxml simple)
  26. #:use-module (ice-9 regex)
  27. #:use-module (ice-9 match)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-11)
  30. #:use-module (srfi srfi-26)
  31. #:use-module (srfi srfi-34)
  32. #:use-module (rnrs io ports)
  33. #:use-module (system foreign)
  34. #:use-module ((guix http-client) #:hide (open-socket-for-uri))
  35. ;; not required in many cases, so autoloaded to reduce start-up costs.
  36. #:autoload (guix download) (%mirrors)
  37. #:use-module (guix ftp-client)
  38. #:use-module (guix utils)
  39. #:use-module (guix memoization)
  40. #:use-module (guix records)
  41. #:use-module (guix upstream)
  42. #:use-module (guix packages)
  43. #:autoload (zlib) (call-with-gzip-input-port)
  44. #:autoload (htmlprag) (html->sxml) ;from Guile-Lib
  45. #:export (gnu-package-name
  46. gnu-package-mundane-name
  47. gnu-package-copyright-holder
  48. gnu-package-savannah
  49. gnu-package-fsd
  50. gnu-package-language
  51. gnu-package-logo
  52. gnu-package-doc-category
  53. gnu-package-doc-summary
  54. gnu-package-doc-description
  55. gnu-package-doc-urls
  56. gnu-package-download-url
  57. official-gnu-packages
  58. find-package
  59. gnu-package?
  60. uri-mirror-rewrite
  61. release-file?
  62. releases
  63. latest-release
  64. gnu-release-archive-types
  65. gnu-package-name->name+version
  66. %gnu-updater
  67. %gnu-ftp-updater
  68. %savannah-updater
  69. %sourceforge-updater
  70. %xorg-updater
  71. %kernel.org-updater
  72. %generic-html-updater))
  73. ;;; Commentary:
  74. ;;;
  75. ;;; Code for dealing with the maintenance of GNU packages, such as
  76. ;;; auto-updates.
  77. ;;;
  78. ;;; Code:
  79. ;;;
  80. ;;; List of GNU packages.
  81. ;;;
  82. (define %gnumaint-base-url
  83. "https://web.cvs.savannah.gnu.org/viewvc/*checkout*/www/www/prep/gnumaint/")
  84. (define %package-list-url
  85. (string->uri
  86. (string-append %gnumaint-base-url "rec/gnupackages.rec")))
  87. (define %package-description-url
  88. ;; This file contains package descriptions in recutils format.
  89. ;; See <https://lists.gnu.org/archive/html/guix-devel/2013-10/msg00071.html>
  90. ;; and <https://lists.gnu.org/archive/html/guix-devel/2018-06/msg00362.html>.
  91. (string->uri
  92. (string-append %gnumaint-base-url "rec/pkgblurbs.rec")))
  93. (define-record-type* <gnu-package-descriptor>
  94. gnu-package-descriptor
  95. make-gnu-package-descriptor
  96. gnu-package-descriptor?
  97. (name gnu-package-name)
  98. (mundane-name gnu-package-mundane-name)
  99. (copyright-holder gnu-package-copyright-holder)
  100. (savannah gnu-package-savannah)
  101. (fsd gnu-package-fsd)
  102. (language gnu-package-language) ; list of strings
  103. (logo gnu-package-logo)
  104. (doc-category gnu-package-doc-category)
  105. (doc-summary gnu-package-doc-summary)
  106. (doc-description gnu-package-doc-description) ; taken from 'pkgdescr.txt'
  107. (doc-urls gnu-package-doc-urls) ; list of strings
  108. (download-url gnu-package-download-url))
  109. (define* (official-gnu-packages
  110. #:optional (fetch http-fetch/cached))
  111. "Return a list of records, which are GNU packages. Use FETCH,
  112. to fetch the list of GNU packages over HTTP."
  113. (define (read-records port)
  114. ;; Return a list of alists. Each alist contains fields of a GNU
  115. ;; package.
  116. (let loop ((alist (recutils->alist port))
  117. (result '()))
  118. (if (null? alist)
  119. (reverse result)
  120. (loop (recutils->alist port)
  121. ;; Ignore things like "%rec" (info "(recutils) Record
  122. ;; Descriptors").
  123. (if (assoc-ref alist "package")
  124. (cons alist result)
  125. result)))))
  126. (define official-description
  127. (let ((db (read-records (fetch %package-description-url #:text? #t))))
  128. (lambda (name)
  129. ;; Return the description found upstream for package NAME, or #f.
  130. (and=> (find (lambda (alist)
  131. (equal? name (assoc-ref alist "package")))
  132. db)
  133. (lambda (record)
  134. (let ((field (assoc-ref record "blurb")))
  135. ;; The upstream description file uses "redirect PACKAGE" as
  136. ;; a blurb in cases where the description of the two
  137. ;; packages should be considered the same (e.g., GTK+ has
  138. ;; "redirect gnome".) This is usually not acceptable for
  139. ;; us because we prefer to have distinct descriptions in
  140. ;; such cases. Thus, ignore the 'blurb' field when that
  141. ;; happens.
  142. (and field
  143. (not (string-prefix? "redirect " field))
  144. field)))))))
  145. (map (lambda (alist)
  146. (let ((name (assoc-ref alist "package")))
  147. (alist->record `(("description" . ,(official-description name))
  148. ,@alist)
  149. make-gnu-package-descriptor
  150. (list "package" "mundane_name" "copyright_holder"
  151. "savannah" "fsd" "language" "logo"
  152. "doc_category" "doc_summary" "description"
  153. "doc_url"
  154. "download_url")
  155. '("doc_url" "language"))))
  156. (let* ((port (fetch %package-list-url #:text? #t))
  157. (lst (read-records port)))
  158. (close-port port)
  159. lst)))
  160. (define (find-package name)
  161. "Find GNU package called NAME and return it. Return #f if it was not
  162. found."
  163. (find (lambda (package)
  164. (string=? name (gnu-package-name package)))
  165. (official-gnu-packages)))
  166. (define gnu-package?
  167. (let ((official-gnu-packages (memoize official-gnu-packages)))
  168. (mlambdaq (package)
  169. "Return true if PACKAGE is a GNU package. This procedure may access the
  170. network to check in GNU's database."
  171. (define (mirror-type url)
  172. (let ((uri (string->uri url)))
  173. (and (eq? (uri-scheme uri) 'mirror)
  174. (cond
  175. ((member (uri-host uri)
  176. '("gnu" "gnupg" "gcc" "gnome"))
  177. ;; Definitely GNU.
  178. 'gnu)
  179. ((equal? (uri-host uri) "cran")
  180. ;; Possibly GNU: mirror://cran could be either GNU R itself
  181. ;; or a non-GNU package.
  182. #f)
  183. (else
  184. ;; Definitely non-GNU.
  185. 'non-gnu)))))
  186. (define (gnu-home-page? package)
  187. (letrec-syntax ((>> (syntax-rules ()
  188. ((_ value proc)
  189. (and=> value proc))
  190. ((_ value proc rest ...)
  191. (and=> value
  192. (lambda (next)
  193. (>> (proc next) rest ...)))))))
  194. (>> package package-home-page
  195. string->uri uri-host
  196. (lambda (host)
  197. (member host '("www.gnu.org" "gnu.org"))))))
  198. (or (gnu-home-page? package)
  199. (match (package-source package)
  200. ((? origin? origin)
  201. (let ((url (origin-uri origin))
  202. (name (package-upstream-name package)))
  203. (case (and (string? url) (mirror-type url))
  204. ((gnu) #t)
  205. ((non-gnu) #f)
  206. (else
  207. (and (member name (map gnu-package-name (official-gnu-packages)))
  208. #t)))))
  209. (_ #f))))))
  210. ;;;
  211. ;;; Latest FTP release.
  212. ;;;
  213. (define (ftp-server/directory package)
  214. "Return the FTP server and directory where PACKAGE's tarball are stored."
  215. (let ((name (package-upstream-name package)))
  216. (values (or (assoc-ref (package-properties package) 'ftp-server)
  217. "ftp.gnu.org")
  218. (or (assoc-ref (package-properties package) 'ftp-directory)
  219. (string-append "/gnu/" name)))))
  220. (define %tarball-rx
  221. ;; The .zip extensions is notably used for freefont-ttf.
  222. ;; The "-src" pattern is for "TeXmacs-1.0.7.9-src.tar.gz".
  223. ;; The "-gnu[0-9]" pattern is for "icecat-38.4.0-gnu1.tar.bz2".
  224. ;; Accept underscores as in "PKG_1.2.tar.gz" for some non-GNU packages.
  225. ;; Accept 'v' or 'V' prefix as in 'PKG-v2.3.tgz'.
  226. (make-regexp "^([^.]+)[-_][vV]?([0-9]|[^-])+(-(src|[sS]ource|gnu[0-9]))?\\.(tar\\.|tgz|zip$)"))
  227. (define %alpha-tarball-rx
  228. (make-regexp "^.*-.*[0-9](-|~)?(alpha|beta|rc|RC|cvs|svn|git)-?[0-9\\.]*\\.tar\\."))
  229. (define (release-file? project file)
  230. "Return #f if FILE is not a release tarball of PROJECT, otherwise return
  231. true."
  232. (and (not (member (file-extension file)
  233. '("sig" "sign" "asc"
  234. "md5sum" "sha1sum" "sha256sum")))
  235. (and=> (regexp-exec %tarball-rx file)
  236. (lambda (match)
  237. ;; Filter out unrelated files, like `guile-www-1.1.1'.
  238. ;; Case-insensitive for things like "TeXmacs" vs. "texmacs".
  239. ;; The "-src" suffix is for "freefont-src-20120503.tar.gz".
  240. (and=> (match:substring match 1)
  241. (lambda (name)
  242. (or (string-ci=? name project)
  243. (string-ci=? name
  244. (string-append project
  245. "-src")))))))
  246. (not (regexp-exec %alpha-tarball-rx file))
  247. (let ((s (tarball-sans-extension file)))
  248. (regexp-exec %package-name-rx s))))
  249. (define (tarball->version tarball)
  250. "Return the version TARBALL corresponds to. TARBALL is a file name like
  251. \"coreutils-8.23.tar.xz\"."
  252. (let-values (((name version)
  253. (gnu-package-name->name+version
  254. (tarball-sans-extension tarball))))
  255. version))
  256. (define* (releases project
  257. #:key
  258. (server "ftp.gnu.org")
  259. (directory (string-append "/gnu/" project)))
  260. "Return the list of <upstream-release> of PROJECT as a list of release
  261. name/directory pairs."
  262. ;; TODO: Parse something like fencepost.gnu.org:/gd/gnuorg/packages-ftp.
  263. (define conn (ftp-open server))
  264. (let loop ((directories (list directory))
  265. (result '()))
  266. (match directories
  267. (()
  268. (ftp-close conn)
  269. (coalesce-sources result))
  270. ((directory rest ...)
  271. (let* ((files (ftp-list conn directory))
  272. (subdirs (filter-map (match-lambda
  273. ((name 'directory . _) name)
  274. (_ #f))
  275. files)))
  276. (define (file->url file)
  277. (string-append "ftp://" server directory "/" file))
  278. (define (file->source file)
  279. (let ((url (file->url file)))
  280. (upstream-source
  281. (package project)
  282. (version (tarball->version file))
  283. (urls (list url))
  284. (signature-urls (list (string-append url ".sig"))))))
  285. (loop (append (map (cut string-append directory "/" <>)
  286. subdirs)
  287. rest)
  288. (append
  289. ;; Filter out signatures, deltas, and files which
  290. ;; are potentially not releases of PROJECT--e.g.,
  291. ;; in /gnu/guile, filter out guile-oops and
  292. ;; guile-www; in mit-scheme, filter out binaries.
  293. (filter-map (match-lambda
  294. ((file 'file . _)
  295. (and (release-file? project file)
  296. (file->source file)))
  297. (_ #f))
  298. files)
  299. result)))))))
  300. (define* (latest-ftp-release project
  301. #:key
  302. (server "ftp.gnu.org")
  303. (directory (string-append "/gnu/" project))
  304. (file->signature (cut string-append <> ".sig")))
  305. "Return an <upstream-source> for the latest release of PROJECT on SERVER
  306. under DIRECTORY, or #f. Use FTP-OPEN and FTP-CLOSE to open (resp. close) FTP
  307. connections; this can be useful to reuse connections.
  308. FILE->SIGNATURE must be a procedure; it is passed a source file URL and must
  309. return the corresponding signature URL, or #f it signatures are unavailable."
  310. (define (latest a b)
  311. (if (version>? a b) a b))
  312. (define (latest-release a b)
  313. (if (version>? (upstream-source-version a) (upstream-source-version b))
  314. a b))
  315. (define patch-directory-name?
  316. ;; Return #t for patch directory names such as 'bash-4.2-patches'.
  317. (cut string-suffix? "patches" <>))
  318. (define conn (ftp-open server #:timeout 5))
  319. (define (file->url directory file)
  320. (string-append "ftp://" server directory "/" file))
  321. (define (file->source directory file)
  322. (let ((url (file->url directory file)))
  323. (upstream-source
  324. (package project)
  325. (version (tarball->version file))
  326. ;; uri-mirror-rewrite: Don't turn nice mirror:// URIs into ftp://
  327. ;; URLs during "guix refresh -u".
  328. (urls (list (uri-mirror-rewrite url)))
  329. (signature-urls (match (file->signature url)
  330. (#f #f)
  331. (sig (list (uri-mirror-rewrite sig))))))))
  332. (let loop ((directory directory)
  333. (result #f))
  334. (let* ((entries (catch 'ftp-error
  335. (lambda _ (ftp-list conn directory))
  336. (const '())))
  337. ;; Filter out things like /gnupg/patches. Filter out "w32"
  338. ;; directories as found on ftp.gnutls.org.
  339. (subdirs (filter-map (match-lambda
  340. (((? patch-directory-name? dir)
  341. 'directory . _)
  342. #f)
  343. (("w32" 'directory . _)
  344. #f)
  345. (("unstable" 'directory . _)
  346. ;; As seen at ftp.gnupg.org/gcrypt/pinentry.
  347. #f)
  348. ((directory 'directory . _)
  349. directory)
  350. (_ #f))
  351. entries))
  352. ;; Whether or not SUBDIRS is empty, compute the latest releases
  353. ;; for the current directory. This is necessary for packages
  354. ;; such as 'sharutils' that have a sub-directory that contains
  355. ;; only an older release.
  356. (releases (filter-map (match-lambda
  357. ((file 'file . _)
  358. (and (release-file? project file)
  359. (file->source directory file)))
  360. (_ #f))
  361. entries)))
  362. ;; Assume that SUBDIRS correspond to versions, and jump into the
  363. ;; one with the highest version number.
  364. (let* ((release (reduce latest-release #f
  365. (coalesce-sources releases)))
  366. (result (if (and result release)
  367. (latest-release release result)
  368. (or release result)))
  369. (target (reduce latest #f subdirs)))
  370. (if target
  371. (loop (string-append directory "/" target)
  372. result)
  373. (begin
  374. (ftp-close conn)
  375. result))))))
  376. (define* (latest-release package
  377. #:key
  378. (server "ftp.gnu.org")
  379. (directory (string-append "/gnu/" package)))
  380. "Return the <upstream-source> for the latest version of PACKAGE or #f.
  381. PACKAGE must be the canonical name of a GNU package."
  382. (latest-ftp-release package
  383. #:server server
  384. #:directory directory))
  385. (define-syntax-rule (false-if-ftp-error exp)
  386. "Return #f if an FTP error is raise while evaluating EXP; return the result
  387. of EXP otherwise."
  388. (catch 'ftp-error
  389. (lambda ()
  390. exp)
  391. (lambda (key port . rest)
  392. (if (ftp-connection? port)
  393. (ftp-close port)
  394. (close-port port))
  395. #f)))
  396. (define (latest-release* package)
  397. "Like 'latest-release', but (1) take a <package> object, and (2) ignore FTP
  398. errors that might occur when PACKAGE is not actually a GNU package, or not
  399. hosted on ftp.gnu.org, or not under that name (this is the case for
  400. \"emacs-auctex\", for instance.)"
  401. (let-values (((server directory)
  402. (ftp-server/directory package)))
  403. (false-if-ftp-error (latest-release (package-upstream-name package)
  404. #:server server
  405. #:directory directory))))
  406. ;;;
  407. ;;; Latest HTTP release.
  408. ;;;
  409. (define (html-links sxml)
  410. "Return the list of links found in SXML, the SXML tree of an HTML page."
  411. (let loop ((sxml sxml)
  412. (links '()))
  413. (match sxml
  414. (('a ('@ attributes ...) body ...)
  415. (match (assq 'href attributes)
  416. (#f (fold loop links body))
  417. (('href url) (fold loop (cons url links) body))))
  418. ((tag ('@ _ ...) body ...)
  419. (fold loop links body))
  420. ((tag body ...)
  421. (fold loop links body))
  422. (_
  423. links))))
  424. (define* (latest-html-release package
  425. #:key
  426. (base-url "https://kernel.org/pub")
  427. (directory (string-append "/" package))
  428. file->signature)
  429. "Return an <upstream-source> for the latest release of PACKAGE (a string) on
  430. SERVER under DIRECTORY, or #f. BASE-URL should be the URL of an HTML page,
  431. typically a directory listing as found on 'https://kernel.org/pub'.
  432. When FILE->SIGNATURE is omitted or #f, guess the detached signature file name,
  433. if any. Otherwise, FILE->SIGNATURE must be a procedure; it is passed a source
  434. file URL and must return the corresponding signature URL, or #f it signatures
  435. are unavailable."
  436. (let* ((uri (string->uri (if (string-null? directory)
  437. base-url
  438. (string-append base-url directory "/"))))
  439. (port (http-fetch/cached uri #:ttl 3600))
  440. (sxml (html->sxml port))
  441. (links (delete-duplicates (html-links sxml))))
  442. (define (file->signature/guess url)
  443. (let ((base (basename url)))
  444. (any (lambda (link)
  445. (any (lambda (extension)
  446. (and (string=? (string-append base extension)
  447. (basename link))
  448. (string-append url extension)))
  449. '(".asc" ".sig" ".sign")))
  450. links)))
  451. (define (url->release url)
  452. (let* ((base (basename url))
  453. (base-url (string-append base-url directory))
  454. (url (cond ((and=> (string->uri url) uri-scheme) ;full URL?
  455. url)
  456. ;; full URL, except for URI scheme. Reuse the URI
  457. ;; scheme of the document that contains the link.
  458. ((string-prefix? "//" url)
  459. (string-append
  460. (symbol->string (uri-scheme (string->uri base-url)))
  461. ":" url))
  462. ((string-prefix? "/" url) ;absolute path?
  463. (let ((uri (string->uri base-url)))
  464. (uri->string
  465. (build-uri (uri-scheme uri)
  466. #:host (uri-host uri)
  467. #:port (uri-port uri)
  468. #:path url))))
  469. ;; URL is a relative path and BASE-URL may or may not
  470. ;; end in slash.
  471. ((string-suffix? "/" base-url)
  472. (string-append base-url url))
  473. (else
  474. ;; If DIRECTORY is non-empty, assume BASE-URL
  475. ;; denotes a directory; otherwise, assume BASE-URL
  476. ;; denotes a file within a directory, and that URL
  477. ;; is relative to that directory.
  478. (string-append (if (string-null? directory)
  479. (dirname base-url)
  480. base-url)
  481. "/" url)))))
  482. (and (release-file? package base)
  483. (let ((version (tarball->version base)))
  484. (upstream-source
  485. (package package)
  486. (version version)
  487. ;; uri-mirror-rewrite: Don't turn nice mirror:// URIs into ftp://
  488. ;; URLs during "guix refresh -u".
  489. (urls (list (uri-mirror-rewrite url)))
  490. (signature-urls
  491. (and=> ((or file->signature file->signature/guess) url)
  492. (lambda (url) (list (uri-mirror-rewrite url))))))))))
  493. (define candidates
  494. (filter-map url->release links))
  495. (close-port port)
  496. (match candidates
  497. (() #f)
  498. ((first . _)
  499. ;; Select the most recent release and return it.
  500. (reduce (lambda (r1 r2)
  501. (if (version>? (upstream-source-version r1)
  502. (upstream-source-version r2))
  503. r1 r2))
  504. first
  505. (coalesce-sources candidates))))))
  506. ;;;
  507. ;;; Updaters.
  508. ;;;
  509. (define %gnu-file-list-uri
  510. ;; URI of the file list for ftp.gnu.org.
  511. (string->uri "https://ftp.gnu.org/find.txt.gz"))
  512. (define ftp.gnu.org-files
  513. (mlambda ()
  514. "Return the list of files available at ftp.gnu.org."
  515. ;; XXX: Memoize the whole procedure to work around the fact that
  516. ;; 'http-fetch/cached' caches the gzipped version.
  517. (define (trim-leading-components str)
  518. ;; Trim the leading ".", if any, in "./gnu/foo".
  519. (string-trim str (char-set #\.)))
  520. (define (string->lines str)
  521. (string-tokenize str (char-set-complement (char-set #\newline))))
  522. ;; Since https://ftp.gnu.org honors 'If-Modified-Since', the hard-coded
  523. ;; TTL can be relatively short.
  524. (let ((port (http-fetch/cached %gnu-file-list-uri #:ttl (* 15 60))))
  525. (map trim-leading-components
  526. (call-with-gzip-input-port port
  527. (compose string->lines get-string-all))))))
  528. (define (latest-gnu-release package)
  529. "Return the latest release of PACKAGE, a GNU package available via
  530. ftp.gnu.org.
  531. This method does not rely on FTP access at all; instead, it browses the file
  532. list available from %GNU-FILE-LIST-URI over HTTP(S)."
  533. (let-values (((server directory)
  534. (ftp-server/directory package))
  535. ((name)
  536. (package-upstream-name package)))
  537. (let* ((files (ftp.gnu.org-files))
  538. (relevant (filter (lambda (file)
  539. (and (string-prefix? "/gnu" file)
  540. (string-contains file directory)
  541. (release-file? name (basename file))))
  542. files)))
  543. (match (sort relevant (lambda (file1 file2)
  544. (version>? (tarball-sans-extension
  545. (basename file1))
  546. (tarball-sans-extension
  547. (basename file2)))))
  548. ((and tarballs (reference _ ...))
  549. (let* ((version (tarball->version reference))
  550. (tarballs (filter (lambda (file)
  551. (string=? (tarball-sans-extension
  552. (basename file))
  553. (tarball-sans-extension
  554. (basename reference))))
  555. tarballs)))
  556. (upstream-source
  557. (package name)
  558. (version version)
  559. (urls (map (lambda (file)
  560. (string-append "mirror://gnu/"
  561. (string-drop file
  562. (string-length "/gnu/"))))
  563. tarballs))
  564. (signature-urls (map (cut string-append <> ".sig") urls)))))
  565. (()
  566. #f)))))
  567. (define %package-name-rx
  568. ;; Regexp for a package name, e.g., "foo-X.Y". Since TeXmacs uses
  569. ;; "TeXmacs-X.Y-src", the `-src' suffix is allowed.
  570. (make-regexp "^(.*)[-_][vV]?(([0-9]|\\.)+)(-src|\\.src|\\.orig)?"))
  571. (define (gnu-package-name->name+version name+version)
  572. "Return the package name and version number extracted from NAME+VERSION."
  573. (let ((match (regexp-exec %package-name-rx name+version)))
  574. (if (not match)
  575. (values name+version #f)
  576. (values (match:substring match 1) (match:substring match 2)))))
  577. (define gnome-package?
  578. (url-prefix-predicate "mirror://gnome/"))
  579. (define (pure-gnu-package? package)
  580. "Return true if PACKAGE is a non-Emacs and non-GNOME GNU package. This
  581. excludes AucTeX, for instance, whose releases are now uploaded to
  582. elpa.gnu.org, GNU Radio, which has releases at www.gnuradio.org, and all the
  583. GNOME packages; EMMS is included though, because its releases are on gnu.org."
  584. (and (or (not (string-prefix? "emacs-" (package-name package)))
  585. (gnu-hosted? package))
  586. (not (gnome-package? package))
  587. (not (string-prefix? "gnuradio" (package-name package)))
  588. (gnu-package? package)))
  589. (define gnu-hosted?
  590. (url-prefix-predicate "mirror://gnu/"))
  591. (define (uri-mirror-rewrite uri)
  592. "Rewrite URI to a mirror:// URI if possible, or return URI unmodified."
  593. (if (string-prefix? "mirror://" uri)
  594. uri ;nothing to do, it's already a mirror URI
  595. (let loop ((mirrors %mirrors))
  596. (match mirrors
  597. (()
  598. uri)
  599. (((mirror-id mirror-urls ...) rest ...)
  600. (match (find (cut string-prefix? <> uri) mirror-urls)
  601. (#f
  602. (loop rest))
  603. (prefix
  604. (format #f "mirror://~a/~a"
  605. mirror-id
  606. (string-drop uri (string-length prefix))))))))))
  607. (define %savannah-base
  608. ;; One of the Savannah mirrors listed at
  609. ;; <http://download0.savannah.gnu.org/mirmon/savannah/> that serves valid
  610. ;; HTML (unlike <https://download.savannah.nongnu.org/releases>.)
  611. "https://nongnu.freemirror.org/nongnu")
  612. (define (latest-savannah-release package)
  613. "Return the latest release of PACKAGE."
  614. (let* ((uri (string->uri
  615. (match (origin-uri (package-source package))
  616. ((? string? uri) uri)
  617. ((uri mirrors ...) uri))))
  618. (package (package-upstream-name package))
  619. (directory (dirname (uri-path uri))))
  620. ;; Note: We use the default 'file->signature', which adds ".sig", ".asc",
  621. ;; or whichever detached signature naming scheme PACKAGE uses.
  622. (latest-html-release package
  623. #:base-url %savannah-base
  624. #:directory directory)))
  625. (define (latest-sourceforge-release package)
  626. "Return the latest release of PACKAGE."
  627. (define (uri-append uri extension)
  628. ;; Return URI with EXTENSION appended.
  629. (build-uri (uri-scheme uri)
  630. #:host (uri-host uri)
  631. #:path (string-append (uri-path uri) extension)))
  632. (define (valid-uri? uri port)
  633. ;; Return true if URI is reachable.
  634. (false-if-exception
  635. (case (response-code (http-head uri #:port port #:keep-alive? #t))
  636. ((200 302) #t)
  637. (else #f))))
  638. (let* ((name (package-upstream-name package))
  639. (base (string-append "https://sourceforge.net/projects/"
  640. name "/files"))
  641. (url (string-append base "/latest/download"))
  642. (uri (string->uri url))
  643. (port (false-if-exception (open-socket-for-uri uri)))
  644. (response (and port
  645. (http-head uri #:port port #:keep-alive? #t))))
  646. (dynamic-wind
  647. (const #t)
  648. (lambda ()
  649. (and response
  650. (= 302 (response-code response))
  651. (response-location response)
  652. (match (string-tokenize (uri-path (response-location response))
  653. (char-set-complement (char-set #\/)))
  654. ((_ components ...)
  655. (let* ((path (string-join components "/"))
  656. (url (string-append "mirror://sourceforge/" path)))
  657. (and (release-file? name (basename path))
  658. ;; Take the heavy-handed approach of probing 3 additional
  659. ;; URLs. XXX: Would be nicer if this could be avoided.
  660. (let* ((loc (response-location response))
  661. (sig (any (lambda (extension)
  662. (let ((uri (uri-append loc extension)))
  663. (and (valid-uri? uri port)
  664. (string-append url extension))))
  665. '(".asc" ".sig" ".sign"))))
  666. (upstream-source
  667. (package name)
  668. (version (tarball->version (basename path)))
  669. (urls (list url))
  670. (signature-urls (and sig (list sig)))))))))))
  671. (lambda ()
  672. (when port
  673. (close-port port))))))
  674. (define (latest-xorg-release package)
  675. "Return the latest release of PACKAGE."
  676. (let ((uri (string->uri (origin-uri (package-source package)))))
  677. (false-if-ftp-error
  678. (latest-ftp-release
  679. (package-name package)
  680. #:server "ftp.freedesktop.org"
  681. #:directory
  682. (string-append "/pub/xorg/" (dirname (uri-path uri)))))))
  683. (define (latest-kernel.org-release package)
  684. "Return the latest release of PACKAGE, the name of a kernel.org package."
  685. (define %kernel.org-base
  686. ;; This URL and sub-directories thereof are nginx-generated directory
  687. ;; listings suitable for 'latest-html-release'.
  688. "https://mirrors.edge.kernel.org/pub")
  689. (define (file->signature file)
  690. (string-append (file-sans-extension file) ".sign"))
  691. (let* ((uri (string->uri
  692. (match (origin-uri (package-source package))
  693. ((? string? uri) uri)
  694. ((uri mirrors ...) uri))))
  695. (package (package-upstream-name package))
  696. (directory (dirname (uri-path uri))))
  697. (latest-html-release package
  698. #:base-url %kernel.org-base
  699. #:directory directory
  700. #:file->signature file->signature)))
  701. (define html-updatable-package?
  702. ;; Return true if the given package may be handled by the generic HTML
  703. ;; updater.
  704. (let ((hosting-sites '("github.com" "github.io" "gitlab.com"
  705. "notabug.org" "sr.ht"
  706. "gforge.inria.fr" "gitlab.inria.fr"
  707. "ftp.gnu.org" "download.savannah.gnu.org"
  708. "pypi.org" "crates.io" "rubygems.org"
  709. "bioconductor.org")))
  710. (define http-url?
  711. (url-predicate (lambda (url)
  712. (match (string->uri url)
  713. (#f #f)
  714. (uri
  715. (let ((scheme (uri-scheme uri))
  716. (host (uri-host uri)))
  717. (and (memq scheme '(http https))
  718. (not (member host hosting-sites)))))))))
  719. (lambda (package)
  720. (or (assoc-ref (package-properties package) 'release-monitoring-url)
  721. (http-url? package)))))
  722. (define (latest-html-updatable-release package)
  723. "Return the latest release of PACKAGE. Do that by crawling the HTML page of
  724. the directory containing its source tarball."
  725. (let* ((uri (string->uri
  726. (match (origin-uri (package-source package))
  727. ((? string? url) url)
  728. ((url _ ...) url))))
  729. (custom (assoc-ref (package-properties package)
  730. 'release-monitoring-url))
  731. (base (or custom
  732. (string-append (symbol->string (uri-scheme uri))
  733. "://" (uri-host uri))))
  734. (directory (if custom
  735. ""
  736. (dirname (uri-path uri))))
  737. (package (package-upstream-name package)))
  738. (catch #t
  739. (lambda ()
  740. (guard (c ((http-get-error? c) #f))
  741. (latest-html-release package
  742. #:base-url base
  743. #:directory directory)))
  744. (lambda (key . args)
  745. ;; Return false and move on upon connection failures and bogus HTTP
  746. ;; servers.
  747. (unless (memq key '(gnutls-error tls-certificate-error
  748. system-error
  749. bad-header bad-header-component))
  750. (apply throw key args))
  751. #f))))
  752. (define %gnu-updater
  753. ;; This is for everything at ftp.gnu.org.
  754. (upstream-updater
  755. (name 'gnu)
  756. (description "Updater for GNU packages")
  757. (pred gnu-hosted?)
  758. (latest latest-gnu-release)))
  759. (define %gnu-ftp-updater
  760. ;; This is for GNU packages taken from alternate locations, such as
  761. ;; alpha.gnu.org, ftp.gnupg.org, etc. It is obsolescent.
  762. (upstream-updater
  763. (name 'gnu-ftp)
  764. (description "Updater for GNU packages only available via FTP")
  765. (pred (lambda (package)
  766. (and (not (gnu-hosted? package))
  767. (pure-gnu-package? package))))
  768. (latest latest-release*)))
  769. (define %savannah-updater
  770. (upstream-updater
  771. (name 'savannah)
  772. (description "Updater for packages hosted on savannah.gnu.org")
  773. (pred (url-prefix-predicate "mirror://savannah/"))
  774. (latest latest-savannah-release)))
  775. (define %sourceforge-updater
  776. (upstream-updater
  777. (name 'sourceforge)
  778. (description "Updater for packages hosted on sourceforge.net")
  779. (pred (url-prefix-predicate "mirror://sourceforge/"))
  780. (latest latest-sourceforge-release)))
  781. (define %xorg-updater
  782. (upstream-updater
  783. (name 'xorg)
  784. (description "Updater for X.org packages")
  785. (pred (url-prefix-predicate "mirror://xorg/"))
  786. (latest latest-xorg-release)))
  787. (define %kernel.org-updater
  788. (upstream-updater
  789. (name 'kernel.org)
  790. (description "Updater for packages hosted on kernel.org")
  791. (pred (url-prefix-predicate "mirror://kernel.org/"))
  792. (latest latest-kernel.org-release)))
  793. (define %generic-html-updater
  794. (upstream-updater
  795. (name 'generic-html)
  796. (description "Updater that crawls HTML pages.")
  797. (pred html-updatable-package?)
  798. (latest latest-html-updatable-release)))
  799. ;;; gnu-maintenance.scm ends here