guix-hydra.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ;;; guix-hydra.el --- Common code for interacting with Hydra -*- lexical-binding: t -*-
  2. ;; Copyright © 2015–2017 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides some general code for 'list'/'info' interfaces for
  18. ;; Hydra (Guix build farm).
  19. ;;; Code:
  20. (require 'json)
  21. (require 'bui)
  22. (require 'guix nil t)
  23. (require 'guix-utils)
  24. (require 'guix-help-vars)
  25. (guix-define-groups hydra)
  26. (defvar guix-hydra-job-regexp
  27. (concat ".*\\." (regexp-opt guix-help-system-types) "\\'")
  28. "Regexp matching a full name of Hydra job (including system).")
  29. (defun guix-hydra-job-name-specification (name version)
  30. "Return Hydra's job name specification by NAME and VERSION."
  31. (concat name "-" version))
  32. (defun guix-hydra-message (entries search-type &rest _)
  33. "Display a message after showing Hydra ENTRIES."
  34. ;; XXX Add more messages maybe.
  35. (when (null entries)
  36. (if (eq search-type 'fake)
  37. (message "The update is impossible due to lack of Hydra API.")
  38. (message "Hydra has returned no results."))))
  39. (defun guix-hydra-list-describe (&rest ids)
  40. "Describe 'hydra' entries with IDS (list of identifiers)."
  41. (bui-display-entries
  42. (bui-entries-by-ids (bui-current-entries) ids)
  43. (bui-current-entry-type) 'info
  44. ;; Hydra does not provide an API to receive builds/jobsets by
  45. ;; IDs/names, so we use a 'fake' search type.
  46. '(fake)
  47. 'add))
  48. ;;; Readers
  49. (defvar guix-hydra-projects
  50. '("gnu" "guix")
  51. "List of available Hydra projects.")
  52. (guix-define-readers
  53. :completions-var guix-hydra-projects
  54. :single-reader guix-hydra-read-project
  55. :single-prompt "Project: ")
  56. (guix-define-readers
  57. :require-match nil
  58. :single-reader guix-hydra-read-jobset
  59. :single-prompt "Jobset: ")
  60. (guix-define-readers
  61. :require-match nil
  62. :single-reader guix-hydra-read-job
  63. :single-prompt "Job: ")
  64. (guix-define-readers
  65. :completions-var guix-help-system-types
  66. :single-reader guix-hydra-read-system
  67. :single-prompt "System: ")
  68. ;;; Defining URLs
  69. (defvar guix-hydra-url "http://hydra.gnu.org"
  70. "URL of the Hydra build farm.")
  71. (defun guix-hydra-url (&rest url-parts)
  72. "Return Hydra URL."
  73. (apply #'concat guix-hydra-url "/" url-parts))
  74. (defun guix-hydra-api-url (type args)
  75. "Return URL for receiving data using Hydra API.
  76. TYPE is the name of an allowed method.
  77. ARGS is alist of (KEY . VALUE) pairs.
  78. Skip ARG, if VALUE is nil or an empty string."
  79. (declare (indent 1))
  80. (let* ((fields (mapcar
  81. (lambda (arg)
  82. (pcase arg
  83. (`(,key . ,value)
  84. (unless (or (null value)
  85. (equal "" value))
  86. (concat (guix-hexify key) "="
  87. (guix-hexify value))))
  88. (_ (error "Wrong argument '%s'" arg))))
  89. args))
  90. (fields (mapconcat #'identity (delq nil fields) "&")))
  91. (guix-hydra-url "api/" type "?" fields)))
  92. ;;; Receiving data from Hydra
  93. (defun guix-hydra-receive-data (url)
  94. "Return output received from URL and processed with `json-read'."
  95. (with-temp-buffer
  96. (url-insert-file-contents url)
  97. (goto-char (point-min))
  98. (let ((json-key-type 'symbol)
  99. (json-array-type 'list)
  100. (json-object-type 'alist))
  101. (json-read))))
  102. (defun guix-hydra-get-entries (entry-type search-type &rest args)
  103. "Receive ENTRY-TYPE entries from Hydra.
  104. SEARCH-TYPE is one of the types defined by `guix-hydra-define-interface'."
  105. (unless (eq search-type 'fake)
  106. (let* ((url (apply #'guix-hydra-search-url
  107. entry-type search-type args))
  108. (raw-entries (guix-hydra-receive-data url))
  109. (entries (apply #'guix-modify-objects
  110. raw-entries
  111. (guix-hydra-filters entry-type))))
  112. entries)))
  113. ;;; Filters for processing raw entries
  114. (defun guix-hydra-filter-names (entry name-alist)
  115. "Replace names of ENTRY parameters using NAME-ALIST.
  116. Each element of NAME-ALIST is (OLD-NAME . NEW-NAME) pair."
  117. (mapcar (lambda (param)
  118. (pcase param
  119. (`(,name . ,val)
  120. (let ((new-name (bui-assq-value name-alist name)))
  121. (if new-name
  122. (cons new-name val)
  123. param)))))
  124. entry))
  125. (defun guix-hydra-filter-boolean (entry params)
  126. "Convert number PARAMS (0/1) of ENTRY to boolean values (nil/t)."
  127. (mapcar (lambda (param)
  128. (pcase param
  129. (`(,name . ,val)
  130. (if (memq name params)
  131. (cons name (guix-number->bool val))
  132. param))))
  133. entry))
  134. ;;; Wrappers for defined variables
  135. (defun guix-hydra-symbol (&rest symbols)
  136. "Return `guix-SYMBOLS-...' symbol."
  137. (apply #'guix-make-symbol 'hydra symbols))
  138. (defun guix-hydra-symbol-value (entry-type symbol)
  139. "Return SYMBOL's value for ENTRY-TYPE."
  140. (symbol-value (guix-hydra-symbol entry-type symbol)))
  141. (defun guix-hydra-search-url (entry-type search-type &rest args)
  142. "Return URL to receive ENTRY-TYPE entries from Hydra."
  143. (apply (bui-assq-value (guix-hydra-symbol-value
  144. entry-type 'search-types)
  145. search-type)
  146. args))
  147. (defun guix-hydra-filters (entry-type)
  148. "Return a list of filters for ENTRY-TYPE."
  149. (guix-hydra-symbol-value entry-type 'filters))
  150. ;;; Interface definers
  151. (defmacro guix-hydra-define-entry-type (entry-type &rest args)
  152. "Define general code for ENTRY-TYPE.
  153. Remaining arguments (ARGS) should have a form [KEYWORD VALUE] ...
  154. Required keywords:
  155. - `:search-types' - default value of the generated
  156. `guix-hydra-ENTRY-TYPE-search-types' variable.
  157. Optional keywords:
  158. - `:filters' - default value of the generated
  159. `guix-hydra-ENTRY-TYPE-filters' variable.
  160. - `:filter-names' - if specified, a generated
  161. `guix-hydra-ENTRY-TYPE-filter-names' function for filtering
  162. these names will be added to `guix-hydra-ENTRY-TYPE-filters'
  163. variable.
  164. - `:filter-boolean-params' - if specified, a generated
  165. `guix-hydra-ENTRY-TYPE-filter-boolean' function for filtering
  166. these names will be added to `guix-hydra-ENTRY-TYPE-filters'
  167. variable.
  168. The rest keyword arguments are passed to
  169. `bui-define-entry-type' macro."
  170. (declare (indent 1))
  171. (let* ((entry-type-str (symbol-name entry-type))
  172. (full-entry-type (guix-hydra-symbol entry-type))
  173. (prefix (concat "guix-hydra-" entry-type-str))
  174. (search-types-var (intern (concat prefix "-search-types")))
  175. (filters-var (intern (concat prefix "-filters")))
  176. (get-fun (intern (concat prefix "-get-entries"))))
  177. (bui-plist-let args
  178. ((search-types-val :search-types)
  179. (filters-val :filters)
  180. (filter-names-val :filter-names)
  181. (filter-bool-val :filter-boolean-params))
  182. `(progn
  183. (defvar ,search-types-var ,search-types-val
  184. ,(format "\
  185. Alist of search types and according URL functions.
  186. Functions are used to define URL to receive '%s' entries."
  187. entry-type-str))
  188. (defvar ,filters-var ,filters-val
  189. ,(format "\
  190. List of filters for '%s' parameters.
  191. Each filter is a function that should take an entry as a single
  192. argument, and should also return an entry."
  193. entry-type-str))
  194. ,(when filter-bool-val
  195. (let ((filter-bool-var (intern (concat prefix
  196. "-filter-boolean-params")))
  197. (filter-bool-fun (intern (concat prefix
  198. "-filter-boolean"))))
  199. `(progn
  200. (defvar ,filter-bool-var ,filter-bool-val
  201. ,(format "\
  202. List of '%s' parameters that should be transformed to boolean values."
  203. entry-type-str))
  204. (defun ,filter-bool-fun (entry)
  205. ,(format "\
  206. Run `guix-hydra-filter-boolean' with `%S' variable."
  207. filter-bool-var)
  208. (guix-hydra-filter-boolean entry ,filter-bool-var))
  209. (setq ,filters-var
  210. (cons ',filter-bool-fun ,filters-var)))))
  211. ;; Do not move this clause up!: name filtering should be
  212. ;; performed before any other filtering, so this filter should
  213. ;; be consed after the boolean filter.
  214. ,(when filter-names-val
  215. (let* ((filter-names-var (intern (concat prefix
  216. "-filter-names")))
  217. (filter-names-fun filter-names-var))
  218. `(progn
  219. (defvar ,filter-names-var ,filter-names-val
  220. ,(format "\
  221. Alist of '%s' parameter names returned by Hydra API and names
  222. used internally by the elisp code of this package."
  223. entry-type-str))
  224. (defun ,filter-names-fun (entry)
  225. ,(format "\
  226. Run `guix-hydra-filter-names' with `%S' variable."
  227. filter-names-var)
  228. (guix-hydra-filter-names entry ,filter-names-var))
  229. (setq ,filters-var
  230. (cons ',filter-names-fun ,filters-var)))))
  231. (defun ,get-fun (search-type &rest args)
  232. ,(format "\
  233. Receive '%s' entries.
  234. See `guix-hydra-get-entries' for details."
  235. entry-type-str)
  236. (apply #'guix-hydra-get-entries
  237. ',entry-type search-type args))
  238. (bui-define-groups ,full-entry-type
  239. :parent-group guix-hydra
  240. :parent-faces-group guix-hydra-faces)
  241. (bui-define-entry-type ,full-entry-type
  242. :message-function 'guix-hydra-message
  243. ,@%foreign-args)))))
  244. (defmacro guix-hydra-define-interface (entry-type buffer-type &rest args)
  245. "Define BUFFER-TYPE interface for displaying ENTRY-TYPE hydra entries.
  246. This macro should be called after calling
  247. `guix-hydra-define-entry-type' with the same ENTRY-TYPE.
  248. ARGS are passed to `bui-define-interface' macro."
  249. (declare (indent 2))
  250. `(bui-define-interface ,(guix-hydra-symbol entry-type) ,buffer-type
  251. :get-entries-function ',(guix-hydra-symbol entry-type 'get-entries)
  252. ,@args))
  253. (defvar guix-hydra-font-lock-keywords
  254. (eval-when-compile
  255. `((,(rx "(" (group (or "guix-hydra-define-entry-type"
  256. "guix-hydra-define-interface"))
  257. symbol-end)
  258. . 1))))
  259. (font-lock-add-keywords 'emacs-lisp-mode guix-hydra-font-lock-keywords)
  260. (provide 'guix-hydra)
  261. ;;; guix-hydra.el ends here