gnu-maintenance.scm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. (make-regexp "^([^.]+)[-_]([0-9]|[^-])+(-(src|[sS]ource|gnu[0-9]))?\\.(tar\\.|zip$)"))
  222. (define %alpha-tarball-rx
  223. (make-regexp "^.*-.*[0-9](-|~)?(alpha|beta|rc|RC|cvs|svn|git)-?[0-9\\.]*\\.tar\\."))
  224. (define (release-file? project file)
  225. "Return #f if FILE is not a release tarball of PROJECT, otherwise return
  226. true."
  227. (and (not (member (file-extension file)
  228. '("sig" "sign" "asc"
  229. "md5sum" "sha1sum" "sha256sum")))
  230. (and=> (regexp-exec %tarball-rx file)
  231. (lambda (match)
  232. ;; Filter out unrelated files, like `guile-www-1.1.1'.
  233. ;; Case-insensitive for things like "TeXmacs" vs. "texmacs".
  234. ;; The "-src" suffix is for "freefont-src-20120503.tar.gz".
  235. (and=> (match:substring match 1)
  236. (lambda (name)
  237. (or (string-ci=? name project)
  238. (string-ci=? name
  239. (string-append project
  240. "-src")))))))
  241. (not (regexp-exec %alpha-tarball-rx file))
  242. (let ((s (tarball-sans-extension file)))
  243. (regexp-exec %package-name-rx s))))
  244. (define (tarball->version tarball)
  245. "Return the version TARBALL corresponds to. TARBALL is a file name like
  246. \"coreutils-8.23.tar.xz\"."
  247. (let-values (((name version)
  248. (gnu-package-name->name+version
  249. (tarball-sans-extension tarball))))
  250. version))
  251. (define* (releases project
  252. #:key
  253. (server "ftp.gnu.org")
  254. (directory (string-append "/gnu/" project)))
  255. "Return the list of <upstream-release> of PROJECT as a list of release
  256. name/directory pairs."
  257. ;; TODO: Parse something like fencepost.gnu.org:/gd/gnuorg/packages-ftp.
  258. (define conn (ftp-open server))
  259. (let loop ((directories (list directory))
  260. (result '()))
  261. (match directories
  262. (()
  263. (ftp-close conn)
  264. (coalesce-sources result))
  265. ((directory rest ...)
  266. (let* ((files (ftp-list conn directory))
  267. (subdirs (filter-map (match-lambda
  268. ((name 'directory . _) name)
  269. (_ #f))
  270. files)))
  271. (define (file->url file)
  272. (string-append "ftp://" server directory "/" file))
  273. (define (file->source file)
  274. (let ((url (file->url file)))
  275. (upstream-source
  276. (package project)
  277. (version (tarball->version file))
  278. (urls (list url))
  279. (signature-urls (list (string-append url ".sig"))))))
  280. (loop (append (map (cut string-append directory "/" <>)
  281. subdirs)
  282. rest)
  283. (append
  284. ;; Filter out signatures, deltas, and files which
  285. ;; are potentially not releases of PROJECT--e.g.,
  286. ;; in /gnu/guile, filter out guile-oops and
  287. ;; guile-www; in mit-scheme, filter out binaries.
  288. (filter-map (match-lambda
  289. ((file 'file . _)
  290. (and (release-file? project file)
  291. (file->source file)))
  292. (_ #f))
  293. files)
  294. result)))))))
  295. (define* (latest-ftp-release project
  296. #:key
  297. (server "ftp.gnu.org")
  298. (directory (string-append "/gnu/" project))
  299. (file->signature (cut string-append <> ".sig")))
  300. "Return an <upstream-source> for the latest release of PROJECT on SERVER
  301. under DIRECTORY, or #f. Use FTP-OPEN and FTP-CLOSE to open (resp. close) FTP
  302. connections; this can be useful to reuse connections.
  303. FILE->SIGNATURE must be a procedure; it is passed a source file URL and must
  304. return the corresponding signature URL, or #f it signatures are unavailable."
  305. (define (latest a b)
  306. (if (version>? a b) a b))
  307. (define (latest-release a b)
  308. (if (version>? (upstream-source-version a) (upstream-source-version b))
  309. a b))
  310. (define patch-directory-name?
  311. ;; Return #t for patch directory names such as 'bash-4.2-patches'.
  312. (cut string-suffix? "patches" <>))
  313. (define conn (ftp-open server #:timeout 5))
  314. (define (file->url directory file)
  315. (string-append "ftp://" server directory "/" file))
  316. (define (file->source directory file)
  317. (let ((url (file->url directory file)))
  318. (upstream-source
  319. (package project)
  320. (version (tarball->version file))
  321. (urls (list url))
  322. (signature-urls (match (file->signature url)
  323. (#f #f)
  324. (sig (list sig)))))))
  325. (let loop ((directory directory)
  326. (result #f))
  327. (let* ((entries (catch 'ftp-error
  328. (lambda _ (ftp-list conn directory))
  329. (const '())))
  330. ;; Filter out things like /gnupg/patches. Filter out "w32"
  331. ;; directories as found on ftp.gnutls.org.
  332. (subdirs (filter-map (match-lambda
  333. (((? patch-directory-name? dir)
  334. 'directory . _)
  335. #f)
  336. (("w32" 'directory . _)
  337. #f)
  338. (("unstable" 'directory . _)
  339. ;; As seen at ftp.gnupg.org/gcrypt/pinentry.
  340. #f)
  341. ((directory 'directory . _)
  342. directory)
  343. (_ #f))
  344. entries))
  345. ;; Whether or not SUBDIRS is empty, compute the latest releases
  346. ;; for the current directory. This is necessary for packages
  347. ;; such as 'sharutils' that have a sub-directory that contains
  348. ;; only an older release.
  349. (releases (filter-map (match-lambda
  350. ((file 'file . _)
  351. (and (release-file? project file)
  352. (file->source directory file)))
  353. (_ #f))
  354. entries)))
  355. ;; Assume that SUBDIRS correspond to versions, and jump into the
  356. ;; one with the highest version number.
  357. (let* ((release (reduce latest-release #f
  358. (coalesce-sources releases)))
  359. (result (if (and result release)
  360. (latest-release release result)
  361. (or release result)))
  362. (target (reduce latest #f subdirs)))
  363. (if target
  364. (loop (string-append directory "/" target)
  365. result)
  366. (begin
  367. (ftp-close conn)
  368. result))))))
  369. (define* (latest-release package
  370. #:key
  371. (server "ftp.gnu.org")
  372. (directory (string-append "/gnu/" package)))
  373. "Return the <upstream-source> for the latest version of PACKAGE or #f.
  374. PACKAGE must be the canonical name of a GNU package."
  375. (latest-ftp-release package
  376. #:server server
  377. #:directory directory))
  378. (define-syntax-rule (false-if-ftp-error exp)
  379. "Return #f if an FTP error is raise while evaluating EXP; return the result
  380. of EXP otherwise."
  381. (catch 'ftp-error
  382. (lambda ()
  383. exp)
  384. (lambda (key port . rest)
  385. (if (ftp-connection? port)
  386. (ftp-close port)
  387. (close-port port))
  388. #f)))
  389. (define (latest-release* package)
  390. "Like 'latest-release', but (1) take a <package> object, and (2) ignore FTP
  391. errors that might occur when PACKAGE is not actually a GNU package, or not
  392. hosted on ftp.gnu.org, or not under that name (this is the case for
  393. \"emacs-auctex\", for instance.)"
  394. (let-values (((server directory)
  395. (ftp-server/directory package)))
  396. (false-if-ftp-error (latest-release (package-upstream-name package)
  397. #:server server
  398. #:directory directory))))
  399. ;;;
  400. ;;; Latest HTTP release.
  401. ;;;
  402. (define (html-links sxml)
  403. "Return the list of links found in SXML, the SXML tree of an HTML page."
  404. (let loop ((sxml sxml)
  405. (links '()))
  406. (match sxml
  407. (('a ('@ attributes ...) body ...)
  408. (match (assq 'href attributes)
  409. (#f (fold loop links body))
  410. (('href url) (fold loop (cons url links) body))))
  411. ((tag ('@ _ ...) body ...)
  412. (fold loop links body))
  413. ((tag body ...)
  414. (fold loop links body))
  415. (_
  416. links))))
  417. (define* (latest-html-release package
  418. #:key
  419. (base-url "https://kernel.org/pub")
  420. (directory (string-append "/" package))
  421. file->signature)
  422. "Return an <upstream-source> for the latest release of PACKAGE (a string) on
  423. SERVER under DIRECTORY, or #f. BASE-URL should be the URL of an HTML page,
  424. typically a directory listing as found on 'https://kernel.org/pub'.
  425. When FILE->SIGNATURE is omitted or #f, guess the detached signature file name,
  426. if any. Otherwise, FILE->SIGNATURE must be a procedure; it is passed a source
  427. file URL and must return the corresponding signature URL, or #f it signatures
  428. are unavailable."
  429. (let* ((uri (string->uri (if (string-null? directory)
  430. base-url
  431. (string-append base-url directory "/"))))
  432. (port (http-fetch/cached uri #:ttl 3600))
  433. (sxml (html->sxml port))
  434. (links (delete-duplicates (html-links sxml))))
  435. (define (file->signature/guess url)
  436. (let ((base (basename url)))
  437. (any (lambda (link)
  438. (any (lambda (extension)
  439. (and (string=? (string-append base extension)
  440. (basename link))
  441. (string-append url extension)))
  442. '(".asc" ".sig" ".sign")))
  443. links)))
  444. (define (url->release url)
  445. (let* ((base (basename url))
  446. (url (if (string=? base url)
  447. (string-append base-url directory "/" url)
  448. url)))
  449. (and (release-file? package base)
  450. (let ((version (tarball->version base)))
  451. (upstream-source
  452. (package package)
  453. (version version)
  454. (urls (list url))
  455. (signature-urls
  456. (list ((or file->signature file->signature/guess) url))))))))
  457. (define candidates
  458. (filter-map url->release links))
  459. (close-port port)
  460. (match candidates
  461. (() #f)
  462. ((first . _)
  463. ;; Select the most recent release and return it.
  464. (reduce (lambda (r1 r2)
  465. (if (version>? (upstream-source-version r1)
  466. (upstream-source-version r2))
  467. r1 r2))
  468. first
  469. (coalesce-sources candidates))))))
  470. ;;;
  471. ;;; Updaters.
  472. ;;;
  473. (define %gnu-file-list-uri
  474. ;; URI of the file list for ftp.gnu.org.
  475. (string->uri "https://ftp.gnu.org/find.txt.gz"))
  476. (define ftp.gnu.org-files
  477. (mlambda ()
  478. "Return the list of files available at ftp.gnu.org."
  479. ;; XXX: Memoize the whole procedure to work around the fact that
  480. ;; 'http-fetch/cached' caches the gzipped version.
  481. (define (trim-leading-components str)
  482. ;; Trim the leading ".", if any, in "./gnu/foo".
  483. (string-trim str (char-set #\.)))
  484. (define (string->lines str)
  485. (string-tokenize str (char-set-complement (char-set #\newline))))
  486. ;; Since https://ftp.gnu.org honors 'If-Modified-Since', the hard-coded
  487. ;; TTL can be relatively short.
  488. (let ((port (http-fetch/cached %gnu-file-list-uri #:ttl (* 15 60))))
  489. (map trim-leading-components
  490. (call-with-gzip-input-port port
  491. (compose string->lines get-string-all))))))
  492. (define (latest-gnu-release package)
  493. "Return the latest release of PACKAGE, a GNU package available via
  494. ftp.gnu.org.
  495. This method does not rely on FTP access at all; instead, it browses the file
  496. list available from %GNU-FILE-LIST-URI over HTTP(S)."
  497. (let-values (((server directory)
  498. (ftp-server/directory package))
  499. ((name)
  500. (package-upstream-name package)))
  501. (let* ((files (ftp.gnu.org-files))
  502. (relevant (filter (lambda (file)
  503. (and (string-prefix? "/gnu" file)
  504. (string-contains file directory)
  505. (release-file? name (basename file))))
  506. files)))
  507. (match (sort relevant (lambda (file1 file2)
  508. (version>? (tarball-sans-extension
  509. (basename file1))
  510. (tarball-sans-extension
  511. (basename file2)))))
  512. ((and tarballs (reference _ ...))
  513. (let* ((version (tarball->version reference))
  514. (tarballs (filter (lambda (file)
  515. (string=? (tarball-sans-extension
  516. (basename file))
  517. (tarball-sans-extension
  518. (basename reference))))
  519. tarballs)))
  520. (upstream-source
  521. (package name)
  522. (version version)
  523. (urls (map (lambda (file)
  524. (string-append "mirror://gnu/"
  525. (string-drop file
  526. (string-length "/gnu/"))))
  527. tarballs))
  528. (signature-urls (map (cut string-append <> ".sig") urls)))))
  529. (()
  530. #f)))))
  531. (define %package-name-rx
  532. ;; Regexp for a package name, e.g., "foo-X.Y". Since TeXmacs uses
  533. ;; "TeXmacs-X.Y-src", the `-src' suffix is allowed.
  534. (make-regexp "^(.*)[-_](([0-9]|\\.)+)(-src|\\.src|\\.orig)?"))
  535. (define (gnu-package-name->name+version name+version)
  536. "Return the package name and version number extracted from NAME+VERSION."
  537. (let ((match (regexp-exec %package-name-rx name+version)))
  538. (if (not match)
  539. (values name+version #f)
  540. (values (match:substring match 1) (match:substring match 2)))))
  541. (define gnome-package?
  542. (url-prefix-predicate "mirror://gnome/"))
  543. (define (pure-gnu-package? package)
  544. "Return true if PACKAGE is a non-Emacs and non-GNOME GNU package. This
  545. excludes AucTeX, for instance, whose releases are now uploaded to
  546. elpa.gnu.org, GNU Radio, which has releases at www.gnuradio.org, and all the
  547. GNOME packages; EMMS is included though, because its releases are on gnu.org."
  548. (and (or (not (string-prefix? "emacs-" (package-name package)))
  549. (gnu-hosted? package))
  550. (not (gnome-package? package))
  551. (not (string-prefix? "gnuradio" (package-name package)))
  552. (gnu-package? package)))
  553. (define gnu-hosted?
  554. (url-prefix-predicate "mirror://gnu/"))
  555. (define (url-prefix-rewrite old new)
  556. "Return a one-argument procedure that rewrites URL prefix OLD to NEW."
  557. (lambda (url)
  558. (if (and url (string-prefix? old url))
  559. (string-append new (string-drop url (string-length old)))
  560. url)))
  561. (define (adjusted-upstream-source source rewrite-url)
  562. "Rewrite URLs in SOURCE by apply REWRITE-URL to each of them."
  563. (upstream-source
  564. (inherit source)
  565. (urls (map rewrite-url (upstream-source-urls source)))
  566. (signature-urls (and=> (upstream-source-signature-urls source)
  567. (lambda (urls)
  568. (map rewrite-url urls))))))
  569. (define %savannah-base
  570. ;; One of the Savannah mirrors listed at
  571. ;; <http://download0.savannah.gnu.org/mirmon/savannah/> that serves valid
  572. ;; HTML (unlike <https://download.savannah.nongnu.org/releases>.)
  573. "https://nongnu.freemirror.org/nongnu")
  574. (define (latest-savannah-release package)
  575. "Return the latest release of PACKAGE."
  576. (let* ((uri (string->uri
  577. (match (origin-uri (package-source package))
  578. ((? string? uri) uri)
  579. ((uri mirrors ...) uri))))
  580. (package (package-upstream-name package))
  581. (directory (dirname (uri-path uri)))
  582. (rewrite (url-prefix-rewrite %savannah-base
  583. "mirror://savannah")))
  584. ;; Note: We use the default 'file->signature', which adds ".sig", ".asc",
  585. ;; or whichever detached signature naming scheme PACKAGE uses.
  586. (and=> (latest-html-release package
  587. #:base-url %savannah-base
  588. #:directory directory)
  589. (cut adjusted-upstream-source <> rewrite))))
  590. (define (latest-sourceforge-release package)
  591. "Return the latest release of PACKAGE."
  592. (define (uri-append uri extension)
  593. ;; Return URI with EXTENSION appended.
  594. (build-uri (uri-scheme uri)
  595. #:host (uri-host uri)
  596. #:path (string-append (uri-path uri) extension)))
  597. (define (valid-uri? uri port)
  598. ;; Return true if URI is reachable.
  599. (false-if-exception
  600. (case (response-code (http-head uri #:port port #:keep-alive? #t))
  601. ((200 302) #t)
  602. (else #f))))
  603. (let* ((name (package-upstream-name package))
  604. (base (string-append "https://sourceforge.net/projects/"
  605. name "/files"))
  606. (url (string-append base "/latest/download"))
  607. (uri (string->uri url))
  608. (port (false-if-exception (open-socket-for-uri uri)))
  609. (response (and port
  610. (http-head uri #:port port #:keep-alive? #t))))
  611. (dynamic-wind
  612. (const #t)
  613. (lambda ()
  614. (and response
  615. (= 302 (response-code response))
  616. (response-location response)
  617. (match (string-tokenize (uri-path (response-location response))
  618. (char-set-complement (char-set #\/)))
  619. ((_ components ...)
  620. (let* ((path (string-join components "/"))
  621. (url (string-append "mirror://sourceforge/" path)))
  622. (and (release-file? name (basename path))
  623. ;; Take the heavy-handed approach of probing 3 additional
  624. ;; URLs. XXX: Would be nicer if this could be avoided.
  625. (let* ((loc (response-location response))
  626. (sig (any (lambda (extension)
  627. (let ((uri (uri-append loc extension)))
  628. (and (valid-uri? uri port)
  629. (string-append url extension))))
  630. '(".asc" ".sig" ".sign"))))
  631. (upstream-source
  632. (package name)
  633. (version (tarball->version (basename path)))
  634. (urls (list url))
  635. (signature-urls (and sig (list sig)))))))))))
  636. (lambda ()
  637. (when port
  638. (close-port port))))))
  639. (define (latest-xorg-release package)
  640. "Return the latest release of PACKAGE."
  641. (let ((uri (string->uri (origin-uri (package-source package)))))
  642. (false-if-ftp-error
  643. (latest-ftp-release
  644. (package-name package)
  645. #:server "ftp.freedesktop.org"
  646. #:directory
  647. (string-append "/pub/xorg/" (dirname (uri-path uri)))))))
  648. (define (latest-kernel.org-release package)
  649. "Return the latest release of PACKAGE, the name of a kernel.org package."
  650. (define %kernel.org-base
  651. ;; This URL and sub-directories thereof are nginx-generated directory
  652. ;; listings suitable for 'latest-html-release'.
  653. "https://mirrors.edge.kernel.org/pub")
  654. (define (file->signature file)
  655. (string-append (file-sans-extension file) ".sign"))
  656. (let* ((uri (string->uri
  657. (match (origin-uri (package-source package))
  658. ((? string? uri) uri)
  659. ((uri mirrors ...) uri))))
  660. (package (package-upstream-name package))
  661. (directory (dirname (uri-path uri)))
  662. (rewrite (url-prefix-rewrite %kernel.org-base
  663. "mirror://kernel.org")))
  664. (and=> (latest-html-release package
  665. #:base-url %kernel.org-base
  666. #:directory directory
  667. #:file->signature file->signature)
  668. (cut adjusted-upstream-source <> rewrite))))
  669. (define html-updatable-package?
  670. ;; Return true if the given package may be handled by the generic HTML
  671. ;; updater.
  672. (let ((hosting-sites '("github.com" "github.io" "gitlab.com"
  673. "notabug.org" "sr.ht"
  674. "gforge.inria.fr" "gitlab.inria.fr"
  675. "ftp.gnu.org" "download.savannah.gnu.org"
  676. "pypi.org" "crates.io" "rubygems.org"
  677. "bioconductor.org")))
  678. (define http-url?
  679. (url-predicate (lambda (url)
  680. (match (string->uri url)
  681. (#f #f)
  682. (uri
  683. (let ((scheme (uri-scheme uri))
  684. (host (uri-host uri)))
  685. (and (memq scheme '(http https))
  686. (not (member host hosting-sites)))))))))
  687. (lambda (package)
  688. (or (assoc-ref (package-properties package) 'release-monitoring-url)
  689. (http-url? package)))))
  690. (define (latest-html-updatable-release package)
  691. "Return the latest release of PACKAGE. Do that by crawling the HTML page of
  692. the directory containing its source tarball."
  693. (let* ((uri (string->uri
  694. (match (origin-uri (package-source package))
  695. ((? string? url) url)
  696. ((url _ ...) url))))
  697. (custom (assoc-ref (package-properties package)
  698. 'release-monitoring-url))
  699. (base (or custom
  700. (string-append (symbol->string (uri-scheme uri))
  701. "://" (uri-host uri))))
  702. (directory (if custom
  703. ""
  704. (dirname (uri-path uri))))
  705. (package (package-upstream-name package)))
  706. (catch #t
  707. (lambda ()
  708. (guard (c ((http-get-error? c) #f))
  709. (latest-html-release package
  710. #:base-url base
  711. #:directory directory)))
  712. (lambda (key . args)
  713. ;; Return false and move on upon connection failures and bogus HTTP
  714. ;; servers.
  715. (unless (memq key '(gnutls-error tls-certificate-error
  716. system-error
  717. bad-header bad-header-component))
  718. (apply throw key args))
  719. #f))))
  720. (define %gnu-updater
  721. ;; This is for everything at ftp.gnu.org.
  722. (upstream-updater
  723. (name 'gnu)
  724. (description "Updater for GNU packages")
  725. (pred gnu-hosted?)
  726. (latest latest-gnu-release)))
  727. (define %gnu-ftp-updater
  728. ;; This is for GNU packages taken from alternate locations, such as
  729. ;; alpha.gnu.org, ftp.gnupg.org, etc. It is obsolescent.
  730. (upstream-updater
  731. (name 'gnu-ftp)
  732. (description "Updater for GNU packages only available via FTP")
  733. (pred (lambda (package)
  734. (and (not (gnu-hosted? package))
  735. (pure-gnu-package? package))))
  736. (latest latest-release*)))
  737. (define %savannah-updater
  738. (upstream-updater
  739. (name 'savannah)
  740. (description "Updater for packages hosted on savannah.gnu.org")
  741. (pred (url-prefix-predicate "mirror://savannah/"))
  742. (latest latest-savannah-release)))
  743. (define %sourceforge-updater
  744. (upstream-updater
  745. (name 'sourceforge)
  746. (description "Updater for packages hosted on sourceforge.net")
  747. (pred (url-prefix-predicate "mirror://sourceforge/"))
  748. (latest latest-sourceforge-release)))
  749. (define %xorg-updater
  750. (upstream-updater
  751. (name 'xorg)
  752. (description "Updater for X.org packages")
  753. (pred (url-prefix-predicate "mirror://xorg/"))
  754. (latest latest-xorg-release)))
  755. (define %kernel.org-updater
  756. (upstream-updater
  757. (name 'kernel.org)
  758. (description "Updater for packages hosted on kernel.org")
  759. (pred (url-prefix-predicate "mirror://kernel.org/"))
  760. (latest latest-kernel.org-release)))
  761. (define %generic-html-updater
  762. (upstream-updater
  763. (name 'generic-html)
  764. (description "Updater that crawls HTML pages.")
  765. (pred html-updatable-package?)
  766. (latest latest-html-updatable-release)))
  767. ;;; gnu-maintenance.scm ends here