svn-download.scm 12 KB

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