hg-download.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 guile-json
  63. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  64. (define gnutls
  65. (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
  66. (define modules
  67. (delete '(guix config)
  68. (source-module-closure '((guix build hg)
  69. (guix build download-nar)))))
  70. (define build
  71. (with-imported-modules modules
  72. (with-extensions (list guile-json gnutls ;for (guix swh)
  73. guile-zlib)
  74. #~(begin
  75. (use-modules (guix build hg)
  76. (guix build download-nar))
  77. (or (hg-fetch '#$(hg-reference-url ref)
  78. '#$(hg-reference-changeset ref)
  79. #$output
  80. #:hg-command (string-append #+hg "/bin/hg"))
  81. (download-nar #$output))))))
  82. (mlet %store-monad ((guile (package->derivation guile system)))
  83. (gexp->derivation (or name "hg-checkout") build
  84. #:leaked-env-vars '("http_proxy" "https_proxy"
  85. "LC_ALL" "LC_MESSAGES" "LANG"
  86. "COLUMNS")
  87. #:system system
  88. #:local-build? #t ;don't offload repo cloning
  89. #:hash-algo hash-algo
  90. #:hash hash
  91. #:recursive? #t
  92. #:guile-for-build guile)))
  93. (define (hg-file-list directory)
  94. "Evaluates to a list of files contained in the repository at path
  95. @var{directory}"
  96. (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
  97. (files (let loop ((files '()))
  98. (let ((line (read-line port)))
  99. (cond
  100. ((eof-object? line) files)
  101. (else
  102. (loop (cons line files))))))))
  103. (close-pipe port)
  104. (map canonicalize-path files)))
  105. (define (should-select? path-list candidate)
  106. "Returns #t in case that @var{candidate} is a file that is part of the given
  107. @var{path-list}."
  108. (let ((canon-candidate (canonicalize-path candidate)))
  109. (let loop ((xs path-list))
  110. (cond
  111. ((null? xs)
  112. ;; Directories are not part of `hg files', but `local-file' will not
  113. ;; recurse if we don't return #t for directories.
  114. (equal? (array-ref (lstat candidate) 13) 'directory))
  115. ((string-contains candidate (car xs)) #t)
  116. (else (loop (cdr xs)))))))
  117. (define (hg-predicate directory)
  118. "This procedure evaluates to a predicate that reports back whether a given
  119. @var{file} - @var{stat} combination is part of the files tracked by
  120. Mercurial."
  121. (let ((files (hg-file-list directory)))
  122. (lambda (file stat)
  123. (should-select? files file))))
  124. ;;; hg-download.scm ends here