gnu-maintenance.scm 35 KB

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