1 ;;; guix-hydra.el --- Common code for interacting with Hydra -*- lexical-binding: t -*-
3 ;; Copyright © 2015 Alex Kost <alezost@gmail.com>
5 ;; This file is part of GNU Guix.
7 ;; GNU Guix is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Guix is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;; This file provides some general code for 'list'/'info' interfaces for
23 ;; Hydra (Guix build farm).
28 (require 'guix-buffer
)
31 (require 'guix-help-vars
)
33 (guix-define-groups hydra
)
35 (defvar guix-hydra-job-regexp
36 (concat ".*\\." (regexp-opt guix-help-system-types
) "\\'")
37 "Regexp matching a full name of Hydra job (including system).")
39 (defun guix-hydra-job-name-specification (name version
)
40 "Return Hydra's job name specification by NAME and VERSION."
41 (concat name
"-" version
))
43 (defun guix-hydra-message (entries search-type
&rest _
)
44 "Display a message after showing Hydra ENTRIES."
45 ;; XXX Add more messages maybe.
47 (if (eq search-type
'fake
)
48 (message "The update is impossible due to lack of Hydra API.")
49 (message "Hydra has returned no results."))))
51 (defun guix-hydra-list-describe (ids)
52 "Describe 'hydra' entries with IDS (list of identifiers)."
53 (guix-buffer-display-entries
54 (guix-entries-by-ids ids
(guix-buffer-current-entries))
55 'info
(guix-buffer-current-entry-type)
56 ;; Hydra does not provide an API to receive builds/jobsets by
57 ;; IDs/names, so we use a 'fake' search type.
64 (defvar guix-hydra-projects
66 "List of available Hydra projects.")
69 :completions-var guix-hydra-projects
70 :single-reader guix-hydra-read-project
71 :single-prompt
"Project: ")
74 :single-reader guix-hydra-read-jobset
75 :single-prompt
"Jobset: ")
78 :single-reader guix-hydra-read-job
79 :single-prompt
"Job: ")
82 :completions-var guix-help-system-types
83 :single-reader guix-hydra-read-system
84 :single-prompt
"System: ")
89 (defvar guix-hydra-url
"http://hydra.gnu.org"
90 "URL of the Hydra build farm.")
92 (defun guix-hydra-url (&rest url-parts
)
94 (apply #'concat guix-hydra-url
"/" url-parts
))
96 (defun guix-hydra-api-url (type args
)
97 "Return URL for receiving data using Hydra API.
98 TYPE is the name of an allowed method.
99 ARGS is alist of (KEY . VALUE) pairs.
100 Skip ARG, if VALUE is nil or an empty string."
102 (let* ((fields (mapcar
106 (unless (or (null value
)
108 (concat (guix-hexify key
) "="
109 (guix-hexify value
))))
110 (_ (error "Wrong argument '%s'" arg
))))
112 (fields (mapconcat #'identity
(delq nil fields
) "&")))
113 (guix-hydra-url "api/" type
"?" fields
)))
116 ;;; Receiving data from Hydra
118 (defun guix-hydra-receive-data (url)
119 "Return output received from URL and processed with `json-read'."
121 (url-insert-file-contents url
)
122 (goto-char (point-min))
123 (let ((json-key-type 'symbol
)
124 (json-array-type 'list
)
125 (json-object-type 'alist
))
128 (defun guix-hydra-get-entries (entry-type search-type
&rest args
)
129 "Receive ENTRY-TYPE entries from Hydra.
130 SEARCH-TYPE is one of the types defined by `guix-hydra-define-interface'."
131 (unless (eq search-type
'fake
)
132 (let* ((url (apply #'guix-hydra-search-url
133 entry-type search-type args
))
134 (raw-entries (guix-hydra-receive-data url
))
135 (entries (guix-hydra-filter-entries
137 (guix-hydra-filters entry-type
))))
141 ;;; Filters for processing raw entries
143 (defun guix-hydra-filter-entries (entries filters
)
144 "Filter ENTRIES using FILTERS.
145 Call `guix-modify' on each entry from ENTRIES."
146 (mapcar (lambda (entry)
147 (guix-modify entry filters
))
150 (defun guix-hydra-filter-names (entry name-alist
)
151 "Replace names of ENTRY parameters using NAME-ALIST.
152 Each element of NAME-ALIST is (OLD-NAME . NEW-NAME) pair."
153 (mapcar (lambda (param)
156 (let ((new-name (guix-assq-value name-alist name
)))
162 (defun guix-hydra-filter-boolean (entry params
)
163 "Convert number PARAMS (0/1) of ENTRY to boolean values (nil/t)."
164 (mapcar (lambda (param)
167 (if (memq name params
)
168 (cons name
(guix-number->bool val
))
173 ;;; Wrappers for defined variables
175 (defvar guix-hydra-entry-type-data nil
176 "Alist with hydra entry type data.
177 This alist is filled by `guix-hydra-define-entry-type' macro.")
179 (defun guix-hydra-entry-type-value (entry-type symbol
)
180 "Return SYMBOL's value for ENTRY-TYPE from `guix-hydra'."
181 (symbol-value (guix-assq-value guix-hydra-entry-type-data
184 (defun guix-hydra-search-url (entry-type search-type
&rest args
)
185 "Return URL to receive ENTRY-TYPE entries from Hydra."
186 (apply (guix-assq-value (guix-hydra-entry-type-value
187 entry-type
'search-types
)
191 (defun guix-hydra-filters (entry-type)
192 "Return a list of filters for ENTRY-TYPE."
193 (guix-hydra-entry-type-value entry-type
'filters
))
196 ;;; Interface definers
198 (defmacro guix-hydra-define-entry-type
(entry-type &rest args
)
199 "Define general code for ENTRY-TYPE.
200 Remaining arguments (ARGS) should have a form [KEYWORD VALUE] ...
204 - `:search-types' - default value of the generated
205 `guix-ENTRY-TYPE-search-types' variable.
209 - `:filters' - default value of the generated
210 `guix-ENTRY-TYPE-filters' variable.
212 - `:filter-names' - if specified, a generated
213 `guix-ENTRY-TYPE-filter-names' function for filtering these
214 names will be added to `guix-ENTRY-TYPE-filters' variable.
216 - `:filter-boolean-params' - if specified, a generated
217 `guix-ENTRY-TYPE-filter-boolean' function for filtering these
218 names will be added to `guix-ENTRY-TYPE-filters' variable.
220 The rest keyword arguments are passed to
221 `guix-define-entry-type' macro."
223 (let* ((entry-type-str (symbol-name entry-type
))
224 (prefix (concat "guix-" entry-type-str
))
225 (search-types-var (intern (concat prefix
"-search-types")))
226 (filters-var (intern (concat prefix
"-filters")))
227 (get-fun (intern (concat prefix
"-get-entries"))))
228 (guix-keyword-args-let args
229 ((search-types-val :search-types
)
230 (filters-val :filters
)
231 (filter-names-val :filter-names
)
232 (filter-bool-val :filter-boolean-params
))
234 (defvar ,search-types-var
,search-types-val
236 Alist of search types and according URL functions.
237 Functions are used to define URL to receive '%s' entries."
240 (defvar ,filters-var
,filters-val
242 List of filters for '%s' parameters.
243 Each filter is a function that should take an entry as a single
244 argument, and should also return an entry."
247 ,(when filter-bool-val
248 (let ((filter-bool-var (intern (concat prefix
249 "-filter-boolean-params")))
250 (filter-bool-fun (intern (concat prefix
251 "-filter-boolean"))))
253 (defvar ,filter-bool-var
,filter-bool-val
255 List of '%s' parameters that should be transformed to boolean values."
258 (defun ,filter-bool-fun
(entry)
260 Run `guix-hydra-filter-boolean' with `%S' variable."
262 (guix-hydra-filter-boolean entry
,filter-bool-var
))
265 (cons ',filter-bool-fun
,filters-var
)))))
267 ;; Do not move this clause up!: name filtering should be
268 ;; performed before any other filtering, so this filter should
269 ;; be consed after the boolean filter.
270 ,(when filter-names-val
271 (let* ((filter-names-var (intern (concat prefix
273 (filter-names-fun filter-names-var
))
275 (defvar ,filter-names-var
,filter-names-val
277 Alist of '%s' parameter names returned by Hydra API and names
278 used internally by the elisp code of this package."
281 (defun ,filter-names-fun
(entry)
283 Run `guix-hydra-filter-names' with `%S' variable."
285 (guix-hydra-filter-names entry
,filter-names-var
))
288 (cons ',filter-names-fun
,filters-var
)))))
290 (defun ,get-fun
(search-type &rest args
)
292 Receive '%s' entries.
293 See `guix-hydra-get-entries' for details."
295 (apply #'guix-hydra-get-entries
296 ',entry-type search-type args
))
299 '((search-types .
,search-types-var
)
300 (filters .
,filters-var
))
301 'guix-hydra-entry-type-data
',entry-type
)
303 (guix-define-entry-type ,entry-type
304 :parent-group guix-hydra
305 :parent-faces-group guix-hydra-faces
308 (defmacro guix-hydra-define-interface
(buffer-type entry-type
&rest args
)
309 "Define BUFFER-TYPE interface for displaying ENTRY-TYPE entries.
311 This macro should be called after calling
312 `guix-hydra-define-entry-type' with the same ENTRY-TYPE.
314 ARGS are passed to `guix-BUFFER-TYPE-define-interface' macro."
316 (let* ((entry-type-str (symbol-name entry-type
))
317 (buffer-type-str (symbol-name buffer-type
))
318 (get-fun (intern (concat "guix-" entry-type-str
320 (definer (intern (concat "guix-" buffer-type-str
321 "-define-interface"))))
322 `(,definer
,entry-type
323 :get-entries-function
',get-fun
324 :message-function
'guix-hydra-message
327 (defmacro guix-hydra-info-define-interface
(entry-type &rest args
)
328 "Define 'info' interface for displaying ENTRY-TYPE entries.
329 See `guix-hydra-define-interface'."
331 `(guix-hydra-define-interface info
,entry-type
334 (defmacro guix-hydra-list-define-interface
(entry-type &rest args
)
335 "Define 'list' interface for displaying ENTRY-TYPE entries.
336 Remaining arguments (ARGS) should have a form [KEYWORD VALUE] ...
340 - `:describe-function' - default value of the generated
341 `guix-ENTRY-TYPE-list-describe-function' variable (if not
342 specified, use `guix-hydra-list-describe').
344 The rest keyword arguments are passed to
345 `guix-hydra-define-interface' macro."
347 (guix-keyword-args-let args
348 ((describe-val :describe-function
))
349 `(guix-hydra-define-interface list
,entry-type
350 :describe-function
,(or describe-val
''guix-hydra-list-describe
)
354 (defvar guix-hydra-font-lock-keywords
356 `((,(rx "(" (group (or "guix-hydra-define-entry-type"
357 "guix-hydra-define-interface"
358 "guix-hydra-info-define-interface"
359 "guix-hydra-list-define-interface"))
363 (font-lock-add-keywords 'emacs-lisp-mode guix-hydra-font-lock-keywords
)
365 (provide 'guix-hydra
)
367 ;;; guix-hydra.el ends here