swhid.scm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ;;; Disarchive
  2. ;;; Copyright © 2020, 2021 Timothy Sample <samplet@ngyro.com>
  3. ;;;
  4. ;;; This file is part of Disarchive.
  5. ;;;
  6. ;;; Disarchive is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Disarchive is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Disarchive. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (disarchive resolvers swhid)
  19. #:use-module (disarchive config)
  20. #:use-module (disarchive git-hash)
  21. #:use-module (disarchive resolvers)
  22. #:use-module (disarchive utils)
  23. #:use-module (gcrypt base16)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 popen)
  26. #:use-module (srfi srfi-2)
  27. #:export (swhid-resolver))
  28. (define (file-swhid-address filename)
  29. (string-append "swh:1:dir:"
  30. (bytevector->base16-string
  31. (git-hash-directory filename))))
  32. (define (resolve-swhid-address address output)
  33. ;; XXX: This is a hack to avoid a circular reference between Guix
  34. ;; and Disarchive. We could copy in the Software Heritage module or
  35. ;; pull it out of Guix or...?
  36. (and-let* ((module (resolve-module '(guix swh) #:ensure #f))
  37. (vault-fetch (module-ref module 'vault-fetch)))
  38. (match (string-split address #\:)
  39. (("swh" "1" "dir" address)
  40. (let* ((in (vault-fetch address 'directory))
  41. (_ (mkdir-p output))
  42. ;; XXX: This assumes that Gzip can be found from $PATH.
  43. (out (open-pipe* OPEN_WRITE %tar
  44. "-C" output
  45. "--strip-components=1"
  46. "-xzf" "-")))
  47. (dump-port-all in out)
  48. (close-port in)
  49. (close-pipe out)))
  50. (_ (error "Invalid SWHID" address)))))
  51. (define serialize-swhid-address identity)
  52. (define deserialize-swhid-address identity)
  53. (define swhid-resolver
  54. (make-resolver 'swhid file-swhid-address resolve-swhid-address
  55. serialize-swhid-address deserialize-swhid-address))