diff options
| -rw-r--r-- | gnu/services/shepherd.scm | 30 | ||||
| -rw-r--r-- | tests/services.scm | 11 |
2 files changed, 30 insertions, 11 deletions
diff --git a/gnu/services/shepherd.scm b/gnu/services/shepherd.scm index a14f51592a6..3cfca8574ea 100644 --- a/gnu/services/shepherd.scm +++ b/gnu/services/shepherd.scm | |||
| @@ -52,6 +52,7 @@ | |||
| 52 | 52 | ||
| 53 | shepherd-service-file | 53 | shepherd-service-file |
| 54 | 54 | ||
| 55 | shepherd-service-lookup-procedure | ||
| 55 | shepherd-service-back-edges)) | 56 | shepherd-service-back-edges)) |
| 56 | 57 | ||
| 57 | ;;; Commentary: | 58 | ;;; Commentary: |
| @@ -249,20 +250,29 @@ stored." | |||
| 249 | 250 | ||
| 250 | (gexp->file "shepherd.conf" config))) | 251 | (gexp->file "shepherd.conf" config))) |
| 251 | 252 | ||
| 253 | (define* (shepherd-service-lookup-procedure services | ||
| 254 | #:optional | ||
| 255 | (provision | ||
| 256 | shepherd-service-provision)) | ||
| 257 | "Return a procedure that, when passed a symbol, return the item among | ||
| 258 | SERVICES that provides this symbol. PROVISION must be a one-argument | ||
| 259 | procedure that takes a service and returns the list of symbols it provides." | ||
| 260 | (let ((services (fold (lambda (service result) | ||
| 261 | (fold (cut vhash-consq <> service <>) | ||
| 262 | result | ||
| 263 | (provision service))) | ||
| 264 | vlist-null | ||
| 265 | services))) | ||
| 266 | (lambda (name) | ||
| 267 | (match (vhash-assq name services) | ||
| 268 | ((_ . service) service) | ||
| 269 | (#f #f))))) | ||
| 270 | |||
| 252 | (define (shepherd-service-back-edges services) | 271 | (define (shepherd-service-back-edges services) |
| 253 | "Return a procedure that, when given a <shepherd-service> from SERVICES, | 272 | "Return a procedure that, when given a <shepherd-service> from SERVICES, |
| 254 | returns the list of <shepherd-service> that depend on it." | 273 | returns the list of <shepherd-service> that depend on it." |
| 255 | (define provision->service | 274 | (define provision->service |
| 256 | (let ((services (fold (lambda (service result) | 275 | (shepherd-service-lookup-procedure services)) |
| 257 | (fold (cut vhash-consq <> service <>) | ||
| 258 | result | ||
| 259 | (shepherd-service-provision service))) | ||
| 260 | vlist-null | ||
| 261 | services))) | ||
| 262 | (lambda (name) | ||
| 263 | (match (vhash-assq name services) | ||
| 264 | ((_ . service) service) | ||
| 265 | (#f #f))))) | ||
| 266 | 276 | ||
| 267 | (define edges | 277 | (define edges |
| 268 | (fold (lambda (service edges) | 278 | (fold (lambda (service edges) |
diff --git a/tests/services.scm b/tests/services.scm index 477a197160c..12745c80061 100644 --- a/tests/services.scm +++ b/tests/services.scm | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org> | 2 | ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; | 3 | ;;; |
| 4 | ;;; This file is part of GNU Guix. | 4 | ;;; This file is part of GNU Guix. |
| 5 | ;;; | 5 | ;;; |
| @@ -105,6 +105,15 @@ | |||
| 105 | (fold-services (list s) #:target-type t1) | 105 | (fold-services (list s) #:target-type t1) |
| 106 | #f))) | 106 | #f))) |
| 107 | 107 | ||
| 108 | (test-assert "shepherd-service-lookup-procedure" | ||
| 109 | (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f))) | ||
| 110 | (s2 (shepherd-service (provision '(s2 s2b)) (start #f))) | ||
| 111 | (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f))) | ||
| 112 | (lookup (shepherd-service-lookup-procedure (list s1 s2 s3)))) | ||
| 113 | (and (eq? (lookup 's1) (lookup 's1b) s1) | ||
| 114 | (eq? (lookup 's2) (lookup 's2b) s2) | ||
| 115 | (eq? (lookup 's3) (lookup 's3b) s3)))) | ||
| 116 | |||
| 108 | (test-assert "shepherd-service-back-edges" | 117 | (test-assert "shepherd-service-back-edges" |
| 109 | (let* ((s1 (shepherd-service (provision '(s1)) (start #f))) | 118 | (let* ((s1 (shepherd-service (provision '(s1)) (start #f))) |
| 110 | (s2 (shepherd-service (provision '(s2)) | 119 | (s2 (shepherd-service (provision '(s2)) |
