svn-download.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014-2016, 2019, 2021-2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Sree Harsha Totakura <sreeharsha@totakura.in>
  4. ;;; Copyright © 2017, 2019, 2021 Ricardo Wurmus <rekado@elephly.net>
  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 svn-download)
  21. #:use-module (guix records)
  22. #:use-module (guix gexp)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix modules)
  26. #:use-module (guix packages)
  27. #:use-module (guix utils)
  28. #:use-module ((guix build svn) #:prefix build:)
  29. #:use-module ((guix build utils) #:select (mkdir-p))
  30. #:use-module (ice-9 match)
  31. #:use-module (srfi srfi-1)
  32. #:export (svn-reference
  33. svn-reference?
  34. svn-reference-url
  35. svn-reference-revision
  36. svn-reference-recursive?
  37. svn-reference-user-name
  38. svn-reference-password
  39. svn-fetch
  40. download-svn-to-store
  41. svn-multi-reference
  42. svn-multi-reference?
  43. svn-multi-reference-url
  44. svn-multi-reference-revision
  45. svn-multi-reference-locations
  46. svn-multi-reference-recursive?
  47. svn-multi-reference-user-name
  48. svn-multi-reference-password
  49. svn-multi-fetch
  50. download-multi-svn-to-store))
  51. ;;; Commentary:
  52. ;;;
  53. ;;; An <origin> method that fetches a specific revision from a Subversion
  54. ;;; repository. The repository URL and REVISION are specified with a
  55. ;;; <svn-reference> object. REVISION should be specified as a number.
  56. ;;;
  57. ;;; Code:
  58. (define-record-type* <svn-reference>
  59. svn-reference make-svn-reference
  60. svn-reference?
  61. (url svn-reference-url) ; string
  62. (revision svn-reference-revision) ; number
  63. (recursive? svn-reference-recursive? (default #f))
  64. (user-name svn-reference-user-name (default #f))
  65. (password svn-reference-password (default #f)))
  66. (define (subversion-package)
  67. "Return the default Subversion package."
  68. (let ((distro (resolve-interface '(gnu packages version-control))))
  69. (module-ref distro 'subversion)))
  70. (define* (svn-fetch ref hash-algo hash
  71. #:optional name
  72. #:key (system (%current-system)) (guile (default-guile))
  73. (svn (subversion-package)))
  74. "Return a fixed-output derivation that fetches REF, a <svn-reference>
  75. object. The output is expected to have recursive hash HASH of type
  76. HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
  77. (define guile-json
  78. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  79. (define guile-lzlib
  80. (module-ref (resolve-interface '(gnu packages guile)) 'guile-lzlib))
  81. (define guile-gnutls
  82. (module-ref (resolve-interface '(gnu packages tls)) 'guile-gnutls))
  83. (define build
  84. (with-imported-modules
  85. (source-module-closure '((guix build svn)
  86. (guix build download-nar)
  87. (guix build utils)))
  88. (with-extensions (list guile-json guile-gnutls ;for (guix swh)
  89. guile-lzlib)
  90. #~(begin
  91. (use-modules (guix build svn)
  92. (guix build download-nar)
  93. (ice-9 match))
  94. (or (svn-fetch (getenv "svn url")
  95. (string->number (getenv "svn revision"))
  96. #$output
  97. #:svn-command #+(file-append svn "/bin/svn")
  98. #:recursive? (match (getenv "svn recursive?")
  99. ("yes" #t)
  100. (_ #f))
  101. #:user-name (getenv "svn user name")
  102. #:password (getenv "svn password"))
  103. (download-nar #$output))))))
  104. (mlet %store-monad ((guile (package->derivation guile system)))
  105. (gexp->derivation (or name "svn-checkout") build
  106. ;; Use environment variables and a fixed script name so
  107. ;; there's only one script in store for all the
  108. ;; downloads.
  109. #:script-name "svn-download"
  110. #:env-vars
  111. `(("svn url" . ,(svn-reference-url ref))
  112. ("svn revision"
  113. . ,(number->string (svn-reference-revision ref)))
  114. ,@(if (svn-reference-recursive? ref)
  115. `(("svn recursive?" . "yes"))
  116. '())
  117. ,@(if (svn-reference-user-name ref)
  118. `(("svn user name"
  119. . ,(svn-reference-user-name ref)))
  120. '())
  121. ,@(if (svn-reference-password ref)
  122. `(("svn password"
  123. . ,(svn-reference-password ref)))
  124. '()))
  125. #:system system
  126. #:hash-algo hash-algo
  127. #:hash hash
  128. #:recursive? #t
  129. #:guile-for-build guile
  130. #:local-build? #t)))
  131. (define-record-type* <svn-multi-reference>
  132. svn-multi-reference make-svn-multi-reference
  133. svn-multi-reference?
  134. (url svn-multi-reference-url) ; string
  135. (revision svn-multi-reference-revision) ; number
  136. (locations svn-multi-reference-locations) ; list of strings
  137. (recursive? svn-multi-reference-recursive? (default #f))
  138. (user-name svn-multi-reference-user-name (default #f))
  139. (password svn-multi-reference-password (default #f)))
  140. (define* (svn-multi-fetch ref hash-algo hash
  141. #:optional name
  142. #:key (system (%current-system)) (guile (default-guile))
  143. (svn (subversion-package)))
  144. "Return a fixed-output derivation that fetches REF, a <svn-multi-reference>
  145. object. The output is expected to have recursive hash HASH of type
  146. HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
  147. (define guile-json
  148. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  149. (define guile-lzlib
  150. (module-ref (resolve-interface '(gnu packages guile)) 'guile-lzlib))
  151. (define guile-gnutls
  152. (module-ref (resolve-interface '(gnu packages tls)) 'guile-gnutls))
  153. (define build
  154. (with-imported-modules
  155. (source-module-closure '((guix build svn)
  156. (guix build download-nar)
  157. (guix build utils)))
  158. (with-extensions (list guile-json guile-gnutls ;for (guix swh)
  159. guile-lzlib)
  160. #~(begin
  161. (use-modules (guix build svn)
  162. (guix build utils)
  163. (guix build download-nar)
  164. (srfi srfi-1)
  165. (ice-9 match))
  166. (or (every
  167. (lambda (location)
  168. ;; The directory must exist if we are to fetch only a
  169. ;; single file.
  170. (unless (string-suffix? "/" location)
  171. (mkdir-p (string-append #$output "/" (dirname location))))
  172. (svn-fetch (string-append (getenv "svn url") "/" location)
  173. (string->number (getenv "svn revision"))
  174. (if (string-suffix? "/" location)
  175. (string-append #$output "/" location)
  176. (string-append #$output "/" (dirname location)))
  177. #:svn-command #+(file-append svn "/bin/svn")
  178. #:recursive? (match (getenv "svn recursive?")
  179. ("yes" #t)
  180. (_ #f))
  181. #:user-name (getenv "svn user name")
  182. #:password (getenv "svn password")))
  183. (call-with-input-string (getenv "svn locations")
  184. read))
  185. (begin
  186. (when (file-exists? #$output)
  187. (delete-file-recursively #$output))
  188. (download-nar #$output)))))))
  189. (mlet %store-monad ((guile (package->derivation guile system)))
  190. (gexp->derivation (or name "svn-checkout") build
  191. ;; Use environment variables and a fixed script name so
  192. ;; there's only one script in store for all the
  193. ;; downloads.
  194. #:script-name "svn-multi-download"
  195. #:env-vars
  196. `(("svn url" . ,(svn-multi-reference-url ref))
  197. ("svn locations"
  198. . ,(object->string (svn-multi-reference-locations ref)))
  199. ("svn revision"
  200. . ,(number->string (svn-multi-reference-revision ref)))
  201. ,@(if (svn-multi-reference-recursive? ref)
  202. `(("svn recursive?" . "yes"))
  203. '())
  204. ,@(if (svn-multi-reference-user-name ref)
  205. `(("svn user name"
  206. . ,(svn-multi-reference-user-name ref)))
  207. '())
  208. ,@(if (svn-multi-reference-password ref)
  209. `(("svn password"
  210. . ,(svn-multi-reference-password ref)))
  211. '()))
  212. #:leaked-env-vars '("http_proxy" "https_proxy"
  213. "LC_ALL" "LC_MESSAGES" "LANG"
  214. "COLUMNS")
  215. #:system system
  216. #:hash-algo hash-algo
  217. #:hash hash
  218. #:recursive? #t
  219. #:guile-for-build guile
  220. #:local-build? #t)))
  221. (define* (download-svn-to-store store ref
  222. #:optional (name (basename (svn-reference-url ref)))
  223. #:key (log (current-error-port)))
  224. "Download from REF, a <svn-reference> object to STORE. Write progress
  225. reports to LOG."
  226. (call-with-temporary-directory
  227. (lambda (temp)
  228. (let ((result
  229. (parameterize ((current-output-port log))
  230. (build:svn-fetch (svn-reference-url ref)
  231. (svn-reference-revision ref)
  232. (string-append temp "/svn")
  233. #:user-name (svn-reference-user-name ref)
  234. #:password (svn-reference-password ref)))))
  235. (and result
  236. (add-to-store store name #t "sha256"
  237. (string-append temp "/svn")))))))
  238. (define* (download-multi-svn-to-store store ref
  239. #:optional (name (basename (svn-multi-reference-url ref)))
  240. #:key (log (current-error-port)))
  241. "Download from REF, a <svn-multi-reference> object to STORE. Write progress
  242. reports to LOG."
  243. (call-with-temporary-directory
  244. (lambda (temp)
  245. (and (every (lambda (location)
  246. (let ((dir (string-append temp "/" (dirname location))))
  247. (mkdir-p dir))
  248. (parameterize ((current-output-port log))
  249. (build:svn-fetch (string-append (svn-multi-reference-url ref)
  250. "/" location)
  251. (svn-multi-reference-revision ref)
  252. (if (string-suffix? "/" location)
  253. (string-append temp "/" location)
  254. (string-append temp "/" (dirname location)))
  255. #:recursive?
  256. (svn-multi-reference-recursive? ref)
  257. #:user-name (svn-multi-reference-user-name ref)
  258. #:password (svn-multi-reference-password ref))))
  259. (svn-multi-reference-locations ref))
  260. (add-to-store store name #t "sha256" temp)))))
  261. ;;; svn-download.scm ends here