Hash :
4c543356
Author :
Date :
2015-07-23T18:42:29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
;;
;; adams - Remote system administration tools
;;
;; Copyright 2013,2014 Thomas de Grivel <thomas@lowh.net>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(in-package :adams)
;; Resource metaclass
(defmethod closer-mop:finalize-inheritance :after ((rc resource-class))
(setf (slot-value rc 'probes)
(compute-probes rc)))
(defmethod slot-unbound (metaclass (rc resource-class) (slot-name (eql 'probes)))
(closer-mop:finalize-inheritance rc)
(slot-value rc 'probes))
;; Resource
(defmethod print-object ((r resource) stream)
(print-unreadable-object (r stream :type t :identity (not *print-pretty*))
(format stream "~S ~D spec ~D probed" (resource-id r)
(/ (length (specified-properties r)) 2)
(/ (length (probed-properties r)) 2))))
(defun make-resource (type id &rest initargs &key &allow-other-keys)
(apply #'make-instance type :id id initargs))
(defun resource-type (resource)
(class-name (class-of resource)))
(defmethod resource-probes-properties ((res resource))
(let ((properties))
(dolist (probe (probes-of res))
(dolist (property (probe-properties probe))
(pushnew property properties)))
(sort properties #'string<)))
#+nil
(resource-probes-properties (resource 'file "/"))
(defun probe-all-properties (res)
(dolist (p (resource-probes-properties res))
(get-probed res p))
(probed-properties res))
#+nil
(probe-all-properties (resource 'file "/"))
(defun pprint-plist (plist &optional (stream *standard-output*))
(pprint-logical-block (stream plist)
(iter (for* (k v) in plist)
(for first-line-p initially t then nil)
(unless first-line-p
(pprint-newline :mandatory stream))
(write k :stream stream)
(write-char #\Space stream)
(write v :stream stream))))
#+nil
(pprint-plist '(:a "aaa" :b "foo" :xyz "bar"))
(defmethod describe-probed% ((res resource) (out (eql :form)))
(let* ((props (probe-all-properties res))
(sorted-keys (sort (iter (for* (k v) in props)
(collect k))
#'string<))
(sorted-props (iter (for key in sorted-keys)
(collect key)
(collect (get-property key props)))))
`(resource ',(class-name (class-of res))
,(resource-id res)
,@sorted-props)))
(defmethod describe-probed% ((res resource) (out null))
(with-output-to-string (str)
(describe-probed res str)))
(defmethod describe-probed% ((res resource) (out (eql t)))
(describe-probed res *standard-output*))
(defmethod describe-probed% ((res resource) (out stream))
(let ((form (describe-probed res :form))
(*print-case* :downcase))
(fresh-line out)
(pprint-logical-block (out form :prefix "(" :suffix ")")
(write (first form) :stream out)
(write-char #\Space out)
(write (second form) :stream out)
(write-char #\Space out)
(write (third form) :stream out)
(pprint-indent :block 1 out)
(pprint-newline :mandatory out)
(pprint-plist (cdddr form) out))))
(defun describe-probed (resource &optional (output t))
(describe-probed% resource output))
#+nil
(describe-probed (resource 'mount "/rd") t)