hg-download.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  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 hg-download)
  21. #:use-module (guix gexp)
  22. #:use-module (guix store)
  23. #:use-module (guix monads)
  24. #:use-module (guix records)
  25. #:use-module (guix modules)
  26. #:use-module (guix packages)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (srfi srfi-35)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 popen)
  31. #:use-module (ice-9 rdelim)
  32. #:export (hg-reference
  33. hg-reference?
  34. hg-reference-url
  35. hg-reference-changeset
  36. hg-predicate
  37. hg-fetch
  38. hg-version
  39. hg-file-name))
  40. ;;; Commentary:
  41. ;;;
  42. ;;; An <origin> method that fetches a specific changeset from a Mercurial
  43. ;;; repository. The repository URL and changeset ID are specified with a
  44. ;;; <hg-reference> object.
  45. ;;;
  46. ;;; Code:
  47. (define-record-type* <hg-reference>
  48. hg-reference make-hg-reference
  49. hg-reference?
  50. (url hg-reference-url)
  51. (changeset hg-reference-changeset))
  52. (define (hg-package)
  53. "Return the default Mercurial package."
  54. (let ((distro (resolve-interface '(gnu packages version-control))))
  55. (module-ref distro 'mercurial)))
  56. (define* (hg-fetch ref hash-algo hash
  57. #:optional name
  58. #:key (system (%current-system)) (guile (default-guile))
  59. (hg (hg-package)))
  60. "Return a fixed-output derivation that fetches REF, a <hg-reference>
  61. object. The output is expected to have recursive hash HASH of type
  62. HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
  63. (define inputs
  64. ;; The 'swh-download' procedure requires tar and gzip.
  65. `(("gzip" ,(module-ref (resolve-interface '(gnu packages compression))
  66. 'gzip))
  67. ("tar" ,(module-ref (resolve-interface '(gnu packages base))
  68. 'tar))))
  69. (define guile-lzlib
  70. (module-ref (resolve-interface '(gnu packages guile)) 'guile-lzlib))
  71. (define guile-json
  72. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  73. (define gnutls
  74. (module-ref (resolve-interface '(gnu packages tls)) 'guile-gnutls))
  75. (define modules
  76. (delete '(guix config)
  77. (source-module-closure '((guix build hg)
  78. (guix build download-nar)
  79. (guix swh)))))
  80. (define build
  81. (with-imported-modules modules
  82. (with-extensions (list guile-json gnutls ;for (guix swh)
  83. guile-lzlib)
  84. #~(begin
  85. (use-modules (guix build hg)
  86. (guix build utils) ;for `set-path-environment-variable'
  87. (guix build download-nar)
  88. (guix swh)
  89. (ice-9 match))
  90. (set-path-environment-variable "PATH" '("bin")
  91. (match '#+inputs
  92. (((names dirs outputs ...) ...)
  93. dirs)))
  94. (setvbuf (current-output-port) 'line)
  95. (setvbuf (current-error-port) 'line)
  96. (or (hg-fetch '#$(hg-reference-url ref)
  97. '#$(hg-reference-changeset ref)
  98. #$output
  99. #:hg-command (string-append #+hg "/bin/hg"))
  100. (download-nar #$output)
  101. ;; As a last resort, attempt to download from Software Heritage.
  102. ;; Disable X.509 certificate verification to avoid depending
  103. ;; on nss-certs--we're authenticating the checkout anyway.
  104. (parameterize ((%verify-swh-certificate? #f))
  105. (format (current-error-port)
  106. "Trying to download from Software Heritage...~%")
  107. (swh-download #$(hg-reference-url ref)
  108. #$(hg-reference-changeset ref)
  109. #$output)))))))
  110. (mlet %store-monad ((guile (package->derivation guile system)))
  111. (gexp->derivation (or name "hg-checkout") build
  112. #:leaked-env-vars '("http_proxy" "https_proxy"
  113. "LC_ALL" "LC_MESSAGES" "LANG"
  114. "COLUMNS")
  115. #:system system
  116. #:local-build? #t ;don't offload repo cloning
  117. #:hash-algo hash-algo
  118. #:hash hash
  119. #:recursive? #t
  120. #:guile-for-build guile)))
  121. (define (hg-version version revision changeset)
  122. "Return the version string for packages using hg-download."
  123. ;; hg-version is almost exclusively executed while modules are being loaded.
  124. ;; This makes any errors hide their backtrace. Avoid the mysterious error
  125. ;; "Value out of range 0 to N: 7" when the commit ID is too short, which
  126. ;; can happen, for example, when the user swapped the revision and commit
  127. ;; arguments by mistake.
  128. (when (< (string-length changeset) 7)
  129. (raise
  130. (condition
  131. (&message (message "hg-version: changeset ID unexpectedly short")))))
  132. (string-append version "-" revision "." (string-take changeset 7)))
  133. (define (hg-file-name name version)
  134. "Return the file-name for packages using hg-download."
  135. (string-append name "-" version "-checkout"))
  136. (define (hg-file-list directory)
  137. "Evaluates to a list of files contained in the repository at path
  138. @var{directory}"
  139. (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
  140. (files (let loop ((files '()))
  141. (let ((line (read-line port)))
  142. (cond
  143. ((eof-object? line) files)
  144. (else
  145. (loop (cons line files))))))))
  146. (close-pipe port)
  147. (map canonicalize-path files)))
  148. (define (should-select? path-list candidate)
  149. "Returns #t in case that @var{candidate} is a file that is part of the given
  150. @var{path-list}."
  151. (let ((canon-candidate (canonicalize-path candidate)))
  152. (let loop ((xs path-list))
  153. (cond
  154. ((null? xs)
  155. ;; Directories are not part of `hg files', but `local-file' will not
  156. ;; recurse if we don't return #t for directories.
  157. (equal? (array-ref (lstat candidate) 13) 'directory))
  158. ((string-contains candidate (car xs)) #t)
  159. (else (loop (cdr xs)))))))
  160. (define (hg-predicate directory)
  161. "This procedure evaluates to a predicate that reports back whether a given
  162. @var{file} - @var{stat} combination is part of the files tracked by
  163. Mercurial."
  164. (let ((files (hg-file-list directory)))
  165. (lambda (file stat)
  166. (should-select? files file))))
  167. ;;; hg-download.scm ends here