hg-download.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix hg-download)
  20. #:use-module (guix gexp)
  21. #:use-module (guix store)
  22. #:use-module (guix monads)
  23. #:use-module (guix records)
  24. #:use-module (guix modules)
  25. #:use-module (guix packages)
  26. #:autoload (guix build-system gnu) (standard-packages)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 popen)
  29. #:use-module (ice-9 rdelim)
  30. #:export (hg-reference
  31. hg-reference?
  32. hg-reference-url
  33. hg-reference-changeset
  34. hg-reference-recursive?
  35. hg-predicate
  36. hg-fetch))
  37. ;;; Commentary:
  38. ;;;
  39. ;;; An <origin> method that fetches a specific changeset from a Mercurial
  40. ;;; repository. The repository URL and changeset ID are specified with a
  41. ;;; <hg-reference> object.
  42. ;;;
  43. ;;; Code:
  44. (define-record-type* <hg-reference>
  45. hg-reference make-hg-reference
  46. hg-reference?
  47. (url hg-reference-url)
  48. (changeset hg-reference-changeset))
  49. (define (hg-package)
  50. "Return the default Mercurial package."
  51. (let ((distro (resolve-interface '(gnu packages version-control))))
  52. (module-ref distro 'mercurial)))
  53. (define* (hg-fetch ref hash-algo hash
  54. #:optional name
  55. #:key (system (%current-system)) (guile (default-guile))
  56. (hg (hg-package)))
  57. "Return a fixed-output derivation that fetches REF, a <hg-reference>
  58. object. The output is expected to have recursive hash HASH of type
  59. HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
  60. (define guile-zlib
  61. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  62. (define modules
  63. (delete '(guix config)
  64. (source-module-closure '((guix build hg)
  65. (guix build download-nar)))))
  66. (define build
  67. (with-imported-modules modules
  68. (with-extensions (list guile-zlib)
  69. #~(begin
  70. (use-modules (guix build hg)
  71. (guix build download-nar))
  72. (or (hg-fetch '#$(hg-reference-url ref)
  73. '#$(hg-reference-changeset ref)
  74. #$output
  75. #:hg-command (string-append #+hg "/bin/hg"))
  76. (download-nar #$output))))))
  77. (mlet %store-monad ((guile (package->derivation guile system)))
  78. (gexp->derivation (or name "hg-checkout") build
  79. #:leaked-env-vars '("http_proxy" "https_proxy"
  80. "LC_ALL" "LC_MESSAGES" "LANG"
  81. "COLUMNS")
  82. #:system system
  83. #:local-build? #t ;don't offload repo cloning
  84. #:hash-algo hash-algo
  85. #:hash hash
  86. #:recursive? #t
  87. #:guile-for-build guile)))
  88. (define (hg-file-list directory)
  89. "Evaluates to a list of files contained in the repository at path
  90. @var{directory}"
  91. (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
  92. (files (let loop ((files '()))
  93. (let ((line (read-line port)))
  94. (cond
  95. ((eof-object? line) files)
  96. (else
  97. (loop (cons line files))))))))
  98. (close-pipe port)
  99. (map canonicalize-path files)))
  100. (define (should-select? path-list candidate)
  101. "Returns #t in case that @var{candidate} is a file that is part of the given
  102. @var{path-list}."
  103. (let ((canon-candidate (canonicalize-path candidate)))
  104. (let loop ((xs path-list))
  105. (cond
  106. ((null? xs)
  107. ;; Directories are not part of `hg files', but `local-file' will not
  108. ;; recurse if we don't return #t for directories.
  109. (equal? (array-ref (lstat candidate) 13) 'directory))
  110. ((string-contains candidate (car xs)) #t)
  111. (else (loop (cdr xs)))))))
  112. (define (hg-predicate directory)
  113. "This procedure evaluates to a predicate that reports back whether a given
  114. @var{file} - @var{stat} combination is part of the files tracked by
  115. Mercurial."
  116. (let ((files (hg-file-list directory)))
  117. (lambda (file stat)
  118. (should-select? files file))))
  119. ;;; hg-download.scm ends here