summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Kost <alezost@gmail.com>2018-07-12 11:33:59 +0300
committerAlex Kost <alezost@gmail.com>2018-07-16 21:37:19 +0300
commit7fecae86eebb0f145be9505673234ead93df27fd (patch)
tree54a1cc6764b2074403077c888734ba097ec52676
downloademacs-build-farm-7fecae86eebb0f145be9505673234ead93df27fd.tar.gz
Initial commit
The code has been copied from Emacs-Guix package and renamed from 'guix-hydra-...' to 'build-farm-...'. New files: * build-farm.el * build-farm-build.el * build-farm-jobset.el * build-farm-utils.el * .gitignore
-rw-r--r--.gitignore3
-rw-r--r--build-farm-build.el407
-rw-r--r--build-farm-jobset.el171
-rw-r--r--build-farm-utils.el157
-rw-r--r--build-farm.el361
5 files changed, 1099 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..669049b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+tmp
+*.elc
+*-autoloads.el
diff --git a/build-farm-build.el b/build-farm-build.el
new file mode 100644
index 0000000..cde8ec1
--- /dev/null
+++ b/build-farm-build.el
@@ -0,0 +1,407 @@
+;;; build-farm-build.el --- Interface for builds -*- lexical-binding: t -*-
+
+;; Copyright © 2015–2018 Alex Kost <alezost@gmail.com>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This file provides an interface for displaying builds of a build farm
+;; in 'list' and 'info' buffers.
+
+;;; Code:
+
+(require 'cl-lib)
+(require 'bui)
+(require 'build-farm)
+(require 'build-farm-utils)
+
+(build-farm-define-entry-type build
+ :search-types '((latest . build-farm-build-latest-api-url)
+ (queue . build-farm-build-queue-api-url))
+ :filters '(build-farm-build-filter-status)
+ :filter-names '((nixname . name)
+ (buildstatus . build-status)
+ (timestamp . time))
+ :filter-boolean-params '(finished busy))
+
+(defcustom build-farm-number-of-builds 64
+ "Default number of builds to display.
+This variable is used by '\\[build-farm-latest-builds]' and'
+'\\[build-farm-queued-builds]' commands. If nil, always prompt
+for the number of builds."
+ :type 'integer
+ :group 'build-farm-build)
+
+(defun build-farm-build-get-display (search-type &rest args)
+ "Search for builds and show results.
+See `build-farm-search-url' for the meaning of SEARCH-TYPE and ARGS."
+ (apply #'bui-list-get-display-entries
+ 'build-farm-build search-type args))
+
+(cl-defun build-farm-build-latest-prompt-args (&key project jobset
+ job system)
+ "Prompt for and return a list of 'latest builds' arguments."
+ (let* ((number (if (or current-prefix-arg
+ (null build-farm-number-of-builds))
+ (read-number "Number of latest builds: "
+ build-farm-number-of-builds)
+ build-farm-number-of-builds))
+ (project (if current-prefix-arg
+ (build-farm-read-project nil project)
+ project))
+ (jobset (if current-prefix-arg
+ (build-farm-read-jobset nil jobset)
+ jobset))
+ (job-or-name (if current-prefix-arg
+ (build-farm-read-job nil job)
+ job))
+ (job (and job-or-name
+ (string-match-p build-farm-job-regexp
+ job-or-name)
+ job-or-name))
+ (system (if (and (not job)
+ (or current-prefix-arg
+ (and job-or-name (not system))))
+ (if job-or-name
+ (build-farm-while-null
+ (build-farm-read-system
+ (concat job-or-name ".") system))
+ (build-farm-read-system nil system))
+ system))
+ (job (or job
+ (and job-or-name
+ (concat job-or-name "." system)))))
+ (list number
+ :project project
+ :jobset jobset
+ :job job
+ :system system)))
+
+(declare-function guix-build-log-find-file "guix-build-log" (file))
+
+(defun build-farm-build-view-log (id)
+ "View build log of a build ID."
+ (require 'guix-build-log)
+ (guix-build-log-find-file (build-farm-build-log-url id)))
+
+
+;;; Defining URLs
+
+(defun build-farm-build-url (id)
+ "Return URL of a build ID."
+ (build-farm-url "build/" (number-to-string id)))
+
+(defun build-farm-build-log-url (id)
+ "Return URL of the log file of a build ID."
+ (concat (build-farm-build-url id) "/log/raw"))
+
+(cl-defun build-farm-build-latest-api-url
+ (number &key project jobset job system)
+ "Return API URL to receive latest NUMBER of builds."
+ (build-farm-api-url "latestbuilds"
+ `(("nr" . ,number)
+ ("project" . ,project)
+ ("jobset" . ,jobset)
+ ("job" . ,job)
+ ("system" . ,system))))
+
+(defun build-farm-build-queue-api-url (number)
+ "Return API URL to receive the NUMBER of queued builds."
+ (build-farm-api-url "queue"
+ `(("nr" . ,number))))
+
+
+;;; Filters for processing raw entries
+
+(defun build-farm-build-filter-status (entry)
+ "Add 'status' parameter to 'hydra-build' ENTRY."
+ (let ((status (if (bui-entry-non-void-value entry 'finished)
+ (build-farm-build-status-number->name
+ (bui-entry-non-void-value entry 'build-status))
+ (if (bui-entry-non-void-value entry 'busy)
+ 'running
+ 'scheduled))))
+ (cons `(status . ,status)
+ entry)))
+
+
+;;; Build status
+
+(defface build-farm-build-status-running
+ '((t :inherit bold))
+ "Face used if a build is not finished."
+ :group 'build-farm-build-faces)
+
+(defface build-farm-build-status-scheduled
+ '((t))
+ "Face used if a build is scheduled."
+ :group 'build-farm-build-faces)
+
+(defface build-farm-build-status-succeeded
+ '((t :inherit success))
+ "Face used if a build succeeded."
+ :group 'build-farm-build-faces)
+
+(defface build-farm-build-status-cancelled
+ '((t :inherit warning))
+ "Face used if a build was cancelled."
+ :group 'build-farm-build-faces)
+
+(defface build-farm-build-status-failed
+ '((t :inherit error))
+ "Face used if a build failed."
+ :group 'build-farm-build-faces)
+
+(defface build-farm-build-status-unknown
+ '((t))
+ "Face used if a build has an unknown status."
+ :group 'build-farm-build-faces)
+
+(defvar build-farm-build-status-alist
+ ;; "buildstatus" numbers can be looked at:
+ ;; <https://github.com/NixOS/hydra/blob/master/src/root/common.tt>.
+ '((0 . succeeded)
+ (1 . failed-build)
+ (2 . failed-dependency)
+ (3 . failed-other)
+ (6 . failed-output)
+ (4 . cancelled))
+ "Alist of build status numbers and status names.
+Status numbers are returned by build farms APIs; names (symbols)
+are used internally by the elisp code of this package.")
+
+(defun build-farm-build-status-number->name (number)
+ "Convert build status NUMBER to a name.
+See `build-farm-build-status-alist'."
+ (bui-assq-value build-farm-build-status-alist number))
+
+(defun build-farm-build-status-string (status)
+ "Return a human readable string for build STATUS."
+ (cl-case status
+ (scheduled
+ (bui-get-string "Scheduled" 'build-farm-build-status-scheduled))
+ (running
+ (bui-get-string "Running" 'build-farm-build-status-running))
+ (succeeded
+ (bui-get-string "Succeeded" 'build-farm-build-status-succeeded))
+ (cancelled
+ (bui-get-string "Cancelled" 'build-farm-build-status-cancelled))
+ (failed-build
+ (build-farm-build-status-fail-string))
+ (failed-dependency
+ (build-farm-build-status-fail-string "dependency"))
+ (failed-other
+ (build-farm-build-status-fail-string "other"))
+ (failed-output
+ (build-farm-build-status-fail-string "with output"))
+ (t
+ (bui-get-string "Unknown" 'build-farm-build-status-unknown))))
+
+(defun build-farm-build-status-fail-string (&optional reason)
+ "Return a string for a failed build using REASON."
+ (let ((base (bui-get-string "Failed" 'build-farm-build-status-failed)))
+ (if reason
+ (concat base " (" reason ")")
+ base)))
+
+(defun build-farm-build-finished? (entry)
+ "Return non-nil, if build ENTRY was finished."
+ (bui-entry-non-void-value entry 'finished))
+
+(defun build-farm-build-running? (entry)
+ "Return non-nil, if build ENTRY is running."
+ (eq (bui-entry-non-void-value entry 'status)
+ 'running))
+
+(defun build-farm-build-scheduled? (entry)
+ "Return non-nil, if build ENTRY is scheduled."
+ (eq (bui-entry-non-void-value entry 'status)
+ 'scheduled))
+
+(defun build-farm-build-succeeded? (entry)
+ "Return non-nil, if build ENTRY succeeded."
+ (eq (bui-entry-non-void-value entry 'status)
+ 'succeeded))
+
+(defun build-farm-build-cancelled? (entry)
+ "Return non-nil, if build ENTRY was cancelled."
+ (eq (bui-entry-non-void-value entry 'status)
+ 'cancelled))
+
+(defun build-farm-build-failed? (entry)
+ "Return non-nil, if build ENTRY failed."
+ (memq (bui-entry-non-void-value entry 'status)
+ '(failed-build failed-dependency failed-other)))
+
+
+;;; Build 'info'
+
+(build-farm-define-interface build info
+ :mode-name "Farm-Build-Info"
+ :buffer-name "*Farm Build Info*"
+ :format '((name nil (simple bui-info-heading))
+ nil
+ build-farm-build-info-insert-url
+ (time format (time))
+ (status format build-farm-build-info-insert-status)
+ (project format (format build-farm-build-project))
+ (jobset format (format build-farm-build-jobset))
+ (job format (format build-farm-build-job))
+ (system format (format build-farm-build-system))
+ (priority format (format))))
+
+(defface build-farm-build-info-project
+ '((t :inherit link))
+ "Face for project names."
+ :group 'build-farm-build-info-faces)
+
+(defface build-farm-build-info-jobset
+ '((t :inherit link))
+ "Face for jobsets."
+ :group 'build-farm-build-info-faces)
+
+(defface build-farm-build-info-job
+ '((t :inherit link))
+ "Face for jobs."
+ :group 'build-farm-build-info-faces)
+
+(defface build-farm-build-info-system
+ '((t :inherit link))
+ "Face for system names."
+ :group 'build-farm-build-info-faces)
+
+(defmacro build-farm-build-define-button (name)
+ "Define `build-farm-build-NAME' button."
+ (let* ((name-str (symbol-name name))
+ (button-name (intern (concat "build-farm-build-" name-str)))
+ (face-name (intern (concat "build-farm-build-info-" name-str)))
+ (keyword (intern (concat ":" name-str))))
+ `(define-button-type ',button-name
+ :supertype 'bui
+ 'face ',face-name
+ 'help-echo ,(format "\
+Show latest builds for this %s (with prefix, prompt for all parameters)"
+ name-str)
+ 'action (lambda (btn)
+ (let ((args (build-farm-build-latest-prompt-args
+ ,keyword (button-label btn))))
+ (apply #'build-farm-build-get-display
+ 'latest args))))))
+
+(build-farm-build-define-button project)
+(build-farm-build-define-button jobset)
+(build-farm-build-define-button job)
+(build-farm-build-define-button system)
+
+(defun build-farm-build-info-insert-url (entry)
+ "Insert URL for the build ENTRY."
+ (bui-insert-button (build-farm-build-url (bui-entry-id entry))
+ 'bui-url)
+ (when (build-farm-build-finished? entry)
+ (bui-insert-indent)
+ (bui-insert-action-button
+ "Build log"
+ (lambda (btn)
+ (build-farm-build-view-log (button-get btn 'id)))
+ "View build log"
+ 'id (bui-entry-id entry)))
+ (bui-newline))
+
+(defun build-farm-build-info-insert-status (status &optional _)
+ "Insert a string with build STATUS."
+ (insert (build-farm-build-status-string status)))
+
+
+;;; Build 'list'
+
+(build-farm-define-interface build list
+ :describe-function 'build-farm-list-describe
+ :mode-name "Farm-Build-List"
+ :buffer-name "*Farm Builds*"
+ :format '((name nil 30 t)
+ (system nil 16 t)
+ (status build-farm-build-list-get-status 20 t)
+ (project nil 10 t)
+ (jobset nil 17 t)
+ (time bui-list-get-time 20 t))
+ :hint 'build-farm-build-list-hint)
+
+(let ((map build-farm-build-list-mode-map))
+ (define-key map (kbd "B") 'build-farm-build-list-latest-builds)
+ (define-key map (kbd "L") 'build-farm-build-list-view-log))
+
+(defvar build-farm-build-list-default-hint
+ '(("\\[build-farm-build-list-latest-builds]")
+ " show latest builds of the current job;\n"
+ ("\\[build-farm-build-list-view-log]") " show build log;\n"))
+
+(defun build-farm-build-list-hint ()
+ "Return hint string for a build-list buffer."
+ (bui-format-hints
+ build-farm-build-list-default-hint
+ (bui-default-hint)))
+
+(defun build-farm-build-list-get-status (status &optional _)
+ "Return a string for build STATUS."
+ (build-farm-build-status-string status))
+
+(defun build-farm-build-list-latest-builds (number &rest args)
+ "Display latest NUMBER of builds of the current job.
+Interactively, prompt for NUMBER. With prefix argument, prompt
+for all ARGS."
+ (interactive
+ (let ((entry (bui-list-current-entry)))
+ (build-farm-build-latest-prompt-args
+ :project (bui-entry-non-void-value entry 'project)
+ :jobset (bui-entry-non-void-value entry 'name)
+ :job (bui-entry-non-void-value entry 'job)
+ :system (bui-entry-non-void-value entry 'system))))
+ (apply #'build-farm-latest-builds number args))
+
+(defun build-farm-build-list-view-log ()
+ "View build log of the current build."
+ (interactive)
+ (build-farm-build-view-log (bui-list-current-id)))
+
+
+;;; Interactive commands
+
+;;;###autoload
+(defun build-farm-latest-builds (number &rest args)
+ "Display latest NUMBER of builds.
+ARGS are the same arguments as for `build-farm-build-latest-api-url'.
+Interactively, use `build-farm-number-of-builds' variable for
+NUMBER. With prefix argument, prompt for it and for the other
+ARGS."
+ (interactive (build-farm-build-latest-prompt-args))
+ (apply #'build-farm-build-get-display
+ 'latest number args))
+
+;;;###autoload
+(defun build-farm-queued-builds (number)
+ "Display the NUMBER of queued builds.
+Interactively, use `build-farm-number-of-builds' variable for
+NUMBER. With prefix argument, prompt for it."
+ (interactive
+ (list (if (or current-prefix-arg
+ (null build-farm-number-of-builds))
+ (read-number "Number of queued builds: "
+ build-farm-number-of-builds)
+ build-farm-number-of-builds)))
+ (build-farm-build-get-display 'queue number))
+
+(provide 'build-farm-build)
+
+;;; build-farm-build.el ends here
diff --git a/build-farm-jobset.el b/build-farm-jobset.el
new file mode 100644
index 0000000..e5ede38
--- /dev/null
+++ b/build-farm-jobset.el
@@ -0,0 +1,171 @@
+;;; build-farm-jobset.el --- Interface for jobsets -*- lexical-binding: t -*-
+
+;; Copyright © 2015–2018 Alex Kost <alezost@gmail.com>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This file provides an interface for displaying jobsets of a build
+;; farm in 'list' and 'info' buffers.
+
+;;; Code:
+
+(require 'cl-lib)
+(require 'bui)
+(require 'build-farm)
+(require 'build-farm-build)
+
+(build-farm-define-entry-type jobset
+ :search-types '((project . build-farm-jobset-api-url))
+ :filters '(build-farm-jobset-filter-id)
+ :filter-names '((nrscheduled . scheduled)
+ (nrsucceeded . succeeded)
+ (nrfailed . failed)
+ (nrtotal . total)))
+
+(defun build-farm-jobset-get-display (search-type &rest args)
+ "Search for jobsets and show results.
+See `build-farm-search-url' for the meaning of SEARCH-TYPE and ARGS."
+ (apply #'bui-list-get-display-entries
+ 'build-farm-jobset search-type args))
+
+
+;;; Defining URLs
+
+(defun build-farm-jobset-url (project jobset)
+ "Return URL of a PROJECT's JOBSET."
+ (build-farm-url "jobset/" project "/" jobset))
+
+(defun build-farm-jobset-api-url (project)
+ "Return API URL for jobsets by PROJECT."
+ (build-farm-api-url "jobsets"
+ `(("project" . ,project))))
+
+
+;;; Filters for processing raw entries
+
+(defun build-farm-jobset-filter-id (entry)
+ "Add 'ID' parameter to 'jobset' ENTRY."
+ (cons `(id . ,(bui-entry-non-void-value entry 'name))
+ entry))
+
+
+;;; Jobset 'info'
+
+(build-farm-define-interface jobset info
+ :mode-name "Farm-Jobset-Info"
+ :buffer-name "*Farm Jobset Info*"
+ :format '((name nil (simple bui-info-heading))
+ nil
+ build-farm-jobset-info-insert-url
+ (project format build-farm-jobset-info-insert-project)
+ (scheduled format (format build-farm-jobset-info-scheduled))
+ (succeeded format (format build-farm-jobset-info-succeeded))
+ (failed format (format build-farm-jobset-info-failed))
+ (total format (format build-farm-jobset-info-total))))
+
+(defface build-farm-jobset-info-scheduled
+ '((t))
+ "Face used for the number of scheduled builds."
+ :group 'build-farm-jobset-info-faces)
+
+(defface build-farm-jobset-info-succeeded
+ '((t :inherit build-farm-build-status-succeeded))
+ "Face used for the number of succeeded builds."
+ :group 'build-farm-jobset-info-faces)
+
+(defface build-farm-jobset-info-failed
+ '((t :inherit build-farm-build-status-failed))
+ "Face used for the number of failed builds."
+ :group 'build-farm-jobset-info-faces)
+
+(defface build-farm-jobset-info-total
+ '((t))
+ "Face used for the total number of builds."
+ :group 'build-farm-jobset-info-faces)
+
+(defun build-farm-jobset-info-insert-project (project entry)
+ "Insert PROJECT button for the jobset ENTRY."
+ (let ((jobset (bui-entry-non-void-value entry 'name)))
+ (bui-insert-button
+ project 'build-farm-build-project
+ 'action (lambda (btn)
+ (let ((args (build-farm-build-latest-prompt-args
+ :project (button-get btn 'project)
+ :jobset (button-get btn 'jobset))))
+ (apply #'build-farm-build-get-display
+ 'latest args)))
+ 'project project
+ 'jobset jobset)))
+
+(defun build-farm-jobset-info-insert-url (entry)
+ "Insert URL for the jobset ENTRY."
+ (bui-insert-button (build-farm-jobset-url
+ (bui-entry-non-void-value entry 'project)
+ (bui-entry-non-void-value entry 'name))
+ 'bui-url)
+ (bui-newline))
+
+
+;;; Jobset 'list'
+
+(build-farm-define-interface jobset list
+ :describe-function 'build-farm-list-describe
+ :mode-name "Farm-Jobset-List"
+ :buffer-name "*Farm Jobsets*"
+ :format '((name nil 25 t)
+ (project nil 10 t)
+ (scheduled nil 12 t)
+ (succeeded nil 12 t)
+ (failed nil 9 t)
+ (total nil 10 t))
+ :hint 'build-farm-jobset-list-hint)
+
+(let ((map build-farm-jobset-list-mode-map))
+ (define-key map (kbd "B") 'build-farm-jobset-list-latest-builds))
+
+(defvar build-farm-jobset-list-default-hint
+ '(("\\[build-farm-jobset-list-latest-builds]")
+ " show latest builds for the current jobset;\n"))
+
+(defun build-farm-jobset-list-hint ()
+ "Return hint string for a jobset-list buffer."
+ (bui-format-hints
+ build-farm-jobset-list-default-hint
+ (bui-default-hint)))
+
+(defun build-farm-jobset-list-latest-builds (number &rest args)
+ "Display latest NUMBER of builds of the current jobset.
+Interactively, prompt for NUMBER. With prefix argument, prompt
+for all ARGS."
+ (interactive
+ (let ((entry (bui-list-current-entry)))
+ (build-farm-build-latest-prompt-args
+ :project (bui-entry-non-void-value entry 'project)
+ :jobset (bui-entry-non-void-value entry 'name))))
+ (apply #'build-farm-latest-builds number args))
+
+
+;;; Interactive commands
+
+;;;###autoload
+(defun build-farm-jobsets (project)
+ "Display jobsets of PROJECT."
+ (interactive (list (build-farm-read-project)))
+ (build-farm-jobset-get-display 'project project))
+
+(provide 'build-farm-jobset)
+
+;;; build-farm-jobset.el ends here
diff --git a/build-farm-utils.el b/build-farm-utils.el
new file mode 100644
index 0000000..d02ce39
--- /dev/null
+++ b/build-farm-utils.el
@@ -0,0 +1,157 @@
+;;; build-farm-utils.el --- General utility functions -*- lexical-binding: t -*-
+
+;; Copyright © 2015–2018 Alex Kost <alezost@gmail.com>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This file provides auxiliary general code for build-farm package.
+
+;;; Code:
+
+(require 'bui)
+
+(defun build-farm-hexify (value)
+ "Convert VALUE to string and hexify it."
+ (url-hexify-string (bui-get-string value)))
+
+(defun build-farm-number->bool (number)
+ "Convert NUMBER to boolean value.
+Return nil, if NUMBER is 0; return t otherwise."
+ (/= 0 number))
+
+(defmacro build-farm-while-null (&rest body)
+ "Evaluate BODY until its result becomes non-nil."
+ (declare (indent 0) (debug t))
+ (let ((result-var (make-symbol "result")))
+ `(let (,result-var)
+ (while (null ,result-var)
+ (setq ,result-var ,@body))
+ ,result-var)))
+
+(defun build-farm-modify (object &rest modifiers)
+ "Apply MODIFIERS to OBJECT.
+OBJECT is passed as an argument to the first function from
+MODIFIERS list, the returned result is passed to the second
+function from the list and so on. Return result of the last
+modifier call."
+ (if (null modifiers)
+ object
+ (apply #'build-farm-modify
+ (funcall (car modifiers) object)
+ (cdr modifiers))))
+
+(defun build-farm-modify-objects (objects &rest modifiers)
+ "Apply MODIFIERS to each object from a list of OBJECTS.
+See `build-farm-modify' for details."
+ (mapcar (lambda (object)
+ (apply #'build-farm-modify object modifiers))
+ objects))
+
+
+;;; Completing readers
+
+(defun build-farm-completing-read (prompt table &optional predicate
+ require-match initial-input
+ hist def inherit-input-method)
+ "Same as `completing-read' but return nil instead of an empty string."
+ (let ((res (completing-read prompt table predicate
+ require-match initial-input
+ hist def inherit-input-method)))
+ (unless (string= "" res) res)))
+
+(defmacro build-farm-define-reader (name read-fun completions prompt
+ &optional require-match default)
+ "Define NAME function to read from minibuffer.
+READ-FUN may be `completing-read', `completing-read-multiple' or
+another function that takes COMPLETIONS, PROMPT, REQUIRE-MATCH, and
+DEFAULT."
+ (declare (indent 1))
+ `(defun ,name (&optional prompt initial-contents)
+ (,read-fun (or prompt ,prompt)
+ ,completions nil ,require-match
+ initial-contents nil ,default)))
+
+(defmacro build-farm-define-readers (&rest args)
+ "Define reader functions.
+
+ARGS should have a form [KEYWORD VALUE] ... The following
+keywords are available:
+
+ - `completions-var' - variable used to get completions.
+
+ - `completions-getter' - function used to get completions.
+
+ - `require-match' - if the match is required (see
+ `completing-read' for details); default is t.
+
+ - `default' - default value.
+
+ - `single-reader', `single-prompt' - name of a function to read
+ a single value, and a prompt for it.
+
+ - `multiple-reader', `multiple-prompt' - name of a function to
+ read multiple values, and a prompt for it.
+
+ - `multiple-separator' - if specified, another
+ `<multiple-reader-name>-string' function returning a string
+ of multiple values separated the specified separator will be
+ defined."
+ (bui-plist-let args
+ ((completions-var :completions-var)
+ (completions-getter :completions-getter)
+ (require-match :require-match t)
+ (default :default)
+ (single-reader :single-reader)
+ (single-prompt :single-prompt)
+ (multiple-reader :multiple-reader)
+ (multiple-prompt :multiple-prompt)
+ (multiple-separator :multiple-separator))
+ (let ((completions
+ (cond ((and completions-var completions-getter)
+ `(or ,completions-var
+ (setq ,completions-var
+ (funcall ',completions-getter))))
+ (completions-var
+ completions-var)
+ (completions-getter
+ `(funcall ',completions-getter)))))
+ `(progn
+ ,(when (and completions-var
+ (not (boundp completions-var)))
+ `(defvar ,completions-var nil))
+
+ ,(when single-reader
+ `(build-farm-define-reader ,single-reader
+ build-farm-completing-read ,completions ,single-prompt
+ ,require-match ,default))
+
+ ,(when multiple-reader
+ `(build-farm-define-reader ,multiple-reader
+ completing-read-multiple ,completions ,multiple-prompt
+ ,require-match ,default))
+
+ ,(when (and multiple-reader multiple-separator)
+ (let ((name (intern (concat (symbol-name multiple-reader)
+ "-string"))))
+ `(defun ,name (&optional prompt initial-contents)
+ (mapconcat #'identity
+ (,multiple-reader prompt initial-contents)
+ ,multiple-separator))))))))
+
+
+(provide 'build-farm-utils)
+
+;;; build-farm-utils.el ends here
diff --git a/build-farm.el b/build-farm.el
new file mode 100644
index 0000000..94d1a65
--- /dev/null
+++ b/build-farm.el
@@ -0,0 +1,361 @@
+;;; build-farm.el --- Interface for Nix and Guix build farms (Hydra and Cuirass) -*- lexical-binding: t -*-
+
+;; Copyright © 2015–2018 Alex Kost <alezost@gmail.com>
+
+;; Author: Alex Kost <alezost@gmail.com>
+;; Version: 0.1
+;; URL: https://gitlab.com/alezost-emacs/build-farm
+;; Keywords: tools
+;; Package-Requires: ((emacs "24.3") (bui "1.1.0"))
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This package provides Emacs interface for Hydra and Cuirass (Nix and
+;; Guix build farms):
+;;
+;; https://hydra.nixos.org (Hydra)
+;; https://hydra.gnu.org (Hydra)
+;; https://berlin.guixsd.org (Cuirass)
+;;
+;; Set `build-farm-url' variable to choose what build farm you wish to
+;; use.
+
+;; The following M-x commands display lists of builds and jobsets:
+;;
+;; - `build-farm-latest-builds'
+;; - `build-farm-queued-builds'
+;; - `build-farm-jobsets'
+;;
+;; You can press RET in such a list to see more info on the current
+;; entry. You can also select several entries in the list (with "m"
+;; key) and press RET to "describe" them.
+
+;;; Code:
+
+(require 'json)
+(require 'bui)
+(require 'build-farm-utils)
+
+(defgroup build-farm nil
+ "Interface for Hydra and Cuirass build farms used by Guix and Nix."
+ :prefix "build-farm-"
+ :group 'external)
+
+(defgroup build-farm-faces nil
+ "Faces for build-farm interfaces."
+ :group 'build-farm
+ :group 'faces)
+
+(defvar build-farm-system-types
+ '("x86_64-linux" "i686-linux" "armhf-linux" "mips64el-linux")
+ "List of supported systems.")
+
+(defvar build-farm-job-regexp
+ (concat ".*\\." (regexp-opt build-farm-system-types) "\\'")
+ "Regexp matching full name of a job (including system).")
+
+(defun build-farm-job-name-specification (name version)
+ "Return job name specification by NAME and VERSION."
+ (concat name "-" version))
+
+(defun build-farm-message (entries search-type &rest _)
+ "Display a message after showing ENTRIES of SEARCH-TYPE."
+ ;; XXX Add more messages maybe.
+ (when (null entries)
+ (if (eq search-type 'fake)
+ (message "The update is impossible due to lack of the build farm API.")
+ (message "The build farm has returned no results."))))
+
+(defun build-farm-list-describe (&rest ids)
+ "Describe 'build-farm' entries with IDS (list of identifiers)."
+ (bui-display-entries
+ (bui-entries-by-ids (bui-current-entries) ids)
+ (bui-current-entry-type) 'info
+ ;; Hydra and Cuirass do not provide an API to receive builds/jobsets
+ ;; by IDs/names, so we use a 'fake' search type.
+ '(fake)
+ 'add))
+
+
+;;; Readers
+
+(defvar build-farm-projects
+ '("gnu" "guix")
+ "List of available projects.")
+
+(build-farm-define-readers
+ :completions-var build-farm-projects
+ :single-reader build-farm-read-project
+ :single-prompt "Project: ")
+
+(build-farm-define-readers
+ :require-match nil
+ :single-reader build-farm-read-jobset
+ :single-prompt "Jobset: ")
+
+(build-farm-define-readers
+ :require-match nil
+ :single-reader build-farm-read-job
+ :single-prompt "Job: ")
+
+(build-farm-define-readers
+ :completions-var build-farm-system-types
+ :single-reader build-farm-read-system
+ :single-prompt "System: ")
+
+
+;;; Defining URLs
+
+(defvar build-farm-urls
+ '("https://hydra.gnu.org"
+ "https://berlin.guixsd.org"
+ "https://hydra.nixos.org")
+ "List of URLs of the available build farms.")
+
+(defcustom build-farm-url (car build-farm-urls)
+ "URL of the default build farm."
+ :type `(choice ,@(mapcar (lambda (url) (list 'const url))
+ build-farm-urls)
+ (string :tag "Other URL"))
+ :group 'build-farm)
+
+(defun build-farm-url (&rest url-parts)
+ "Return build farm URL-PARTS."
+ (apply #'concat build-farm-url "/" url-parts))
+
+(defun build-farm-api-url (type args)
+ "Return URL for receiving data using build farm API.
+TYPE is the name of an allowed method.
+ARGS is alist of (KEY . VALUE) pairs.
+Skip ARG, if VALUE is nil or an empty string."
+ (declare (indent 1))
+ (let* ((fields (mapcar
+ (lambda (arg)
+ (pcase arg
+ (`(,key . ,value)
+ (unless (or (null value)
+ (equal "" value))
+ (concat (build-farm-hexify key) "="
+ (build-farm-hexify value))))
+ (_ (error "Wrong argument '%s'" arg))))
+ args))
+ (fields (mapconcat #'identity (delq nil fields) "&")))
+ (build-farm-url "api/" type "?" fields)))
+
+
+;;; Receiving data from build farm
+
+(defun build-farm-receive-data (url)
+ "Return output received from URL and processed with `json-read'."
+ (with-temp-buffer
+ (url-insert-file-contents url)
+ (goto-char (point-min))
+ (let ((json-key-type 'symbol)
+ (json-array-type 'list)
+ (json-object-type 'alist))
+ (json-read))))
+
+(defun build-farm-get-entries (entry-type search-type &rest args)
+ "Receive ENTRY-TYPE entries from build farm.
+See `build-farm-search-url' for the meaning of SEARCH-TYPE and ARGS."
+ (unless (eq search-type 'fake)
+ (let* ((url (apply #'build-farm-search-url
+ entry-type search-type args))
+ (raw-entries (build-farm-receive-data url))
+ (entries (apply #'build-farm-modify-objects
+ raw-entries
+ (build-farm-filters entry-type))))
+ entries)))
+
+
+;;; Filters for processing raw entries
+
+(defun build-farm-filter-names (entry name-alist)
+ "Replace names of ENTRY parameters using NAME-ALIST.
+Each element of NAME-ALIST is (OLD-NAME . NEW-NAME) pair."
+ (mapcar (lambda (param)
+ (pcase param
+ (`(,name . ,val)
+ (let ((new-name (bui-assq-value name-alist name)))
+ (if new-name
+ (cons new-name val)
+ param)))))
+ entry))
+
+(defun build-farm-filter-boolean (entry params)
+ "Convert number PARAMS (0/1) of ENTRY to boolean values (nil/t)."
+ (mapcar (lambda (param)
+ (pcase param
+ (`(,name . ,val)
+ (if (memq name params)
+ (cons name (build-farm-number->bool val))
+ param))))
+ entry))
+
+
+;;; Wrappers for defined variables
+
+(defun build-farm-symbol (&rest symbols)
+ "Return `build-farm-...' symbol.
+Where '...' is made from SYMBOLS."
+ (apply #'bui-make-symbol 'build-farm symbols))
+
+(defun build-farm-symbol-value (entry-type symbol)
+ "Return SYMBOL's value for ENTRY-TYPE."
+ (symbol-value (build-farm-symbol entry-type symbol)))
+
+(defun build-farm-search-url (entry-type search-type &rest args)
+ "Return URL to receive ENTRY-TYPE entries from build farm.
+SEARCH-TYPE is one of the types defined by `build-farm-define-entry-type'.
+ARGS are passed to the according URL function."
+ (apply (bui-assq-value (build-farm-symbol-value
+ entry-type 'search-types)
+ search-type)
+ args))
+
+(defun build-farm-filters (entry-type)
+ "Return a list of filters for ENTRY-TYPE."
+ (build-farm-symbol-value entry-type 'filters))
+
+
+;;; Interface definers
+
+(defmacro build-farm-define-entry-type (entry-type &rest args)
+ "Define general code for ENTRY-TYPE.
+Remaining arguments (ARGS) should have a form [KEYWORD VALUE] ...
+
+Required keywords:
+
+ - `:search-types' - default value of the generated
+ `build-farm-ENTRY-TYPE-search-types' variable.
+
+Optional keywords:
+
+ - `:filters' - default value of the generated
+ `build-farm-ENTRY-TYPE-filters' variable.
+
+ - `:filter-names' - if specified, a generated
+ `build-farm-ENTRY-TYPE-filter-names' function for filtering
+ these names will be added to `build-farm-ENTRY-TYPE-filters'
+ variable.
+
+ - `:filter-boolean-params' - if specified, a generated
+ `build-farm-ENTRY-TYPE-filter-boolean' function for filtering
+ these names will be added to `build-farm-ENTRY-TYPE-filters'
+ variable.
+
+The rest keyword arguments are passed to
+`bui-define-entry-type' macro."
+ (declare (indent 1))
+ (let* ((entry-type-str (symbol-name entry-type))
+ (full-entry-type (build-farm-symbol entry-type))
+ (prefix (concat "build-farm-" entry-type-str))
+ (search-types-var (intern (concat prefix "-search-types")))
+ (filters-var (intern (concat prefix "-filters")))
+ (get-fun (intern (concat prefix "-get-entries"))))
+ (bui-plist-let args
+ ((search-types-val :search-types)
+ (filters-val :filters)
+ (filter-names-val :filter-names)
+ (filter-bool-val :filter-boolean-params))
+ `(progn
+ (defvar ,search-types-var ,search-types-val
+ ,(format "\
+Alist of search types and according URL functions.
+Functions are used to define URL to receive '%s' entries."
+ entry-type-str))
+
+ (defvar ,filters-var ,filters-val
+ ,(format "\
+List of filters for '%s' parameters.
+Each filter is a function that should take an entry as a single
+argument, and should also return an entry."
+ entry-type-str))
+
+ ,(when filter-bool-val
+ (let ((filter-bool-var (intern (concat prefix
+ "-filter-boolean-params")))
+ (filter-bool-fun (intern (concat prefix
+ "-filter-boolean"))))
+ `(progn
+ (defvar ,filter-bool-var ,filter-bool-val
+ ,(format "\
+List of '%s' parameters that should be transformed to boolean values."
+ entry-type-str))
+
+ (defun ,filter-bool-fun (entry)
+ ,(format "\
+Run `build-farm-filter-boolean' with `%S' variable."
+ filter-bool-var)
+ (build-farm-filter-boolean entry ,filter-bool-var))
+
+ (setq ,filters-var
+ (cons ',filter-bool-fun ,filters-var)))))
+
+ ;; Do not move this clause up!: name filtering should be
+ ;; performed before any other filtering, so this filter should
+ ;; be consed after the boolean filter.
+ ,(when filter-names-val
+ (let* ((filter-names-var (intern (concat prefix
+ "-filter-names")))
+ (filter-names-fun filter-names-var))
+ `(progn
+ (defvar ,filter-names-var ,filter-names-val
+ ,(format "\
+Alist of '%s' parameter names returned by the build farm API and
+names used internally by the elisp code of this package."
+ entry-type-str))
+
+ (defun ,filter-names-fun (entry)
+ ,(format "\
+Run `build-farm-filter-names' with `%S' variable."
+ filter-names-var)
+ (build-farm-filter-names entry ,filter-names-var))
+
+ (setq ,filters-var
+ (cons ',filter-names-fun ,filters-var)))))
+
+ (defun ,get-fun (search-type &rest args)
+ ,(format "\
+Receive '%s' entries.
+See `build-farm-get-entries' for details."
+ entry-type-str)
+ (apply #'build-farm-get-entries
+ ',entry-type search-type args))
+
+ (bui-define-groups ,full-entry-type
+ :parent-group build-farm
+ :parent-faces-group build-farm-faces)
+
+ (bui-define-entry-type ,full-entry-type
+ :message-function 'build-farm-message
+ ,@%foreign-args)))))
+
+(defmacro build-farm-define-interface (entry-type buffer-type &rest args)
+ "Define BUFFER-TYPE interface for displaying ENTRY-TYPE entries.
+
+This macro should be called after calling
+`build-farm-define-entry-type' with the same ENTRY-TYPE.
+
+ARGS are passed to `bui-define-interface' macro."
+ (declare (indent 2))
+ `(bui-define-interface ,(build-farm-symbol entry-type) ,buffer-type
+ :get-entries-function ',(build-farm-symbol entry-type 'get-entries)
+ ,@args))
+
+(provide 'build-farm)
+
+;;; build-farm.el ends here