hg-download.scm 7.5 KB

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