all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 918a4b1426fd67eb7b7e4e7f036d6acfaa18513c 14248 bytes (raw)
name: scratch/bba-ob-core-async/my-async-tests.el 	 # note: path name is non-authoritative(*)

  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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
 
;;; my-async-tests.el --- Scratch/temporary file: some tests about async -*- lexical-binding: t -*-

;; Copyright (C) 2024 Bruno BARBIER

;; Author: Bruno BARBIER
;; Status: Temporary tests.
;; Compatibility: GNU Emacs 30.0.50
;;
;; This file is NOT part of GNU Emacs.

;; 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 2 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA

(require 'cl-lib)
(require 'org)
(require 'org-elib-async)

(require 'ob-shell)
(require 'ob-python)

;;; Shells
;;

;;;; One shell script

;; Standalone direct asynchronous execution.

(defun my-shell-babel-schedule (lang body _params handle-feedback)
  "Execute the bash script BODY.
Execute the shell script BODY using bash.  Use HANDLE-FEEDBACK to report
the outcome (success or failure)."
  (unless (equal "bash" lang)
    (error "Only for bash"))
  (funcall handle-feedback (list :pending "started"))
  (org-elib-async-process (list "bash") :input body :callback handle-feedback))


;;;; Asynchronous using ob-shell

(defun my-org-babel-shell-how-to-execute (body params)
  "Return how to execute BODY using a POSIX shell.
Return how to execute, as expected by
`org-elib-async-comint-queue--execution'."
  ;; Code mostly extracted from ob-shell, following
  ;; `org-babel-execute:shell' and `org-babel-sh-evaluate'.
  ;; Results are expected to differ from ob-shell as we follow the
  ;; same process for all execution paths: asynchronous or not, with
  ;; session or without.
  (let* ((session (org-babel-sh-initiate-session
		   (cdr (assq :session params))))
	 (stdin (let ((stdin (cdr (assq :stdin params))))
                  (when stdin (org-babel-sh-var-to-string
                               (org-babel-ref-resolve stdin)))))
	 (result-params (cdr (assq :result-params params)))
	 (value-is-exit-status
	  (or (and
	       (equal '("replace") result-params)
	       (not org-babel-shell-results-defaults-to-output))
	      (member "value" result-params)))
	 (cmdline (cdr (assq :cmdline params)))
         (shebang (cdr (assq :shebang params)))
         (full-body (concat
		     (org-babel-expand-body:generic
		      body params (org-babel-variable-assignments:shell params))
		     (when value-is-exit-status "\necho $?")))
         (post-process
          (lambda (r)
            (setq r (org-trim r))
            (org-babel-reassemble-table
             (org-babel-result-cond result-params
               r
               (let ((tmp-file (org-babel-temp-file "sh-")))
                 (with-temp-file tmp-file (insert r))
                 (org-babel-import-elisp-from-file tmp-file)))
             (org-babel-pick-name
              (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
             (org-babel-pick-name
              (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
         comint-buffer
         finally
         to-run)

    (setq comint-buffer
          (if session session
            ;; No session. We create a temporary one and use 'finally' to
            ;; destroy it once we are done.
            ;;
            ;; FIXME: This session code should be refactored and moved into
            ;;        ob-core.
            (let ((s-buf (org-babel-sh-initiate-session
                          (generate-new-buffer-name (format "*ob-shell-no-session*")))))
              (setq finally (lambda ()
                                ;; We cannot delete it immediately as we are called from it.
                                (run-with-idle-timer
                                 0.1 nil
                                 (lambda ()
                                   (when (buffer-live-p s-buf)
                                     (let ((kill-buffer-query-functions nil)
                                           (kill-buffer-hook nil))
                                       (kill-buffer s-buf)))))))
              s-buf)))

    (org-elib-async-comint-queue-init-if-needed comint-buffer)

    (setq to-run
          (cond
           ((or stdin cmdline)	       ; external shell script w/STDIN
            (let ((script-file (org-babel-temp-file "sh-script-"))
	          (stdin-file (org-babel-temp-file "sh-stdin-"))
	          (padline (not (string= "no" (cdr (assq :padline params))))))
	      (with-temp-file script-file
	        (when shebang (insert shebang "\n"))
	        (when padline (insert "\n"))
	        (insert full-body))
	      (set-file-modes script-file #o755)
	      (with-temp-file stdin-file (insert (or stdin "")))
	      (with-temp-buffer
                (with-connection-local-variables
                 (concat
                  (mapconcat #'shell-quote-argument
                             (cons (if shebang (file-local-name script-file)
                                     shell-file-name)
                                   (if shebang (when cmdline (list cmdline))
                                     (list shell-command-switch
                                           (concat (file-local-name script-file)  " " cmdline))))
                             " ")
                  "<" (shell-quote-argument stdin-file))))))
           (session ; session evaluation
            full-body)
           ;; External shell script, with or without a predefined
           ;; shebang.
           ((org-string-nw-p shebang)
            (let ((script-file (org-babel-temp-file "sh-script-"))
	          (padline (not (equal "no" (cdr (assq :padline params))))))
	      (with-temp-file script-file
	        (insert shebang "\n")
	        (when padline (insert "\n"))
	        (insert full-body))
	      (set-file-modes script-file #o755)
              (if (file-remote-p script-file)
                  ;; Run remote script using its local path as COMMAND.
                  ;; The remote execution is ensured by setting
                  ;; correct `default-directory'.
                  (let ((default-directory (file-name-directory script-file)))
                    (file-local-name script-file)
	            script-file ""))))
           (t
            (let ((script-file (org-babel-temp-file "sh-script-")))
	      (with-temp-file script-file
	        (insert full-body))
	      (set-file-modes script-file #o755)
              (mapconcat #'shell-quote-argument
                         (list shell-file-name
                               shell-command-switch
                               (if (file-remote-p script-file)
                                   (file-local-name script-file)
                                 script-file))
                         " ")))))
    ;; TODO: How to handle `value-is-exit-status'?
    (lambda (&rest q)
      (pcase q
        (`(:instrs-to-enter)
         ;; FIXME: This is wrong.
         "export PS1=''; export PS2='';")
        (`(:instrs-to-exit))
        (`(:finally) (when finally (funcall finally)))
        (`(:instr-to-emit-tag ,tag) (format "printf '%s\\n'" tag))
        (`(:post-process ,r) (when post-process (funcall post-process r)))
        (`(:send-instrs-to-session ,code)
         (with-current-buffer comint-buffer
           (when code
             (goto-char (point-max))
             (insert code) (insert "\n")
             (comint-send-input nil t))))
        (`(:get-code) to-run)
        (`(:get-comint-buffer) comint-buffer)
        (_ (error "Unknown query"))))))





;;; Python
;;

;;;; Asynchronous using ob-python

(defun my-org-babel-python-how-to-execute (body params)
  "Return how to execute BODY using python.
Return how to execute, as expected by
`org-elib-async-comint-queue--execution'."
  ;; Code mostly extracted from ob-python, following
  ;; `org-babel-python-evaluate-session'.
  ;; Results are expected to differ from ob-python as we follow the
  ;; same process for all execution paths: asynchronous or not, with
  ;; session or without.
  (let* ((org-babel-python-command
          (or (cdr (assq :python params))
              org-babel-python-command))
         (session-key (org-babel-python-initiate-session
                       (cdr (assq :session params))))
         (graphics-file (and (member "graphics" (assq :result-params params))
                             (org-babel-graphical-output-file params)))
         (result-params (cdr (assq :result-params params)))
         (result-type (cdr (assq :result-type params)))
         (results-file (when (eq 'value result-type)
                         (or graphics-file
                             (org-babel-temp-file "python-"))))
         (return-val (when (eq result-type 'value)
                       (cdr (assq :return params))))
         (full-body
          (concat
           (org-babel-expand-body:generic
            body params
            (org-babel-variable-assignments:python params))
           (when return-val
             (format "\n%s" return-val))))
         (post-process
          (lambda (r)
            (setq r (string-trim r))
            (when (string-prefix-p "Traceback (most recent call last):" r)
              (signal 'user-error (list r)))
            (when (eq 'value result-type)
              (setq r (org-babel-eval-read-file results-file)))
            (org-babel-reassemble-table
             (org-babel-result-cond result-params
               r
               (org-babel-python-table-or-string r))
             (org-babel-pick-name (cdr (assq :colname-names params))
                                  (cdr (assq :colnames params)))
             (org-babel-pick-name (cdr (assq :rowname-names params))
                                  (cdr (assq :rownames params))))))
         (tmp-src-file (org-babel-temp-file "python-"))
         (session-body
          ;; The real code we evaluate in the session.
          (pcase result-type
            (`output
             (format (string-join
                      (list "with open('%s') as f:\n"
                            "    exec(compile(f.read(), f.name, 'exec'))\n"))
                     (org-babel-process-file-name
                      tmp-src-file 'noquote)))
            (`value
             ;; FIXME: In this case, any output is an error.
             (org-babel-python-format-session-value
              tmp-src-file results-file result-params))))
         comint-buffer
         finally)


    (unless session-key
      ;; No session. We create a temporary one and use 'finally' to
      ;; destroy it once we are done.
      ;;
      ;; FIXME: This session code should be refactored and moved into
      ;;        ob-core.
      (setq session-key (org-babel-python-initiate-session
                         ;; We can't use a simple `generate-new-buffer'
                         ;; due to the earmuffs game.
                         (org-babel-python-without-earmuffs
                          (format "*ob-python-no-session-%s*" (org-id-uuid)))))
      (setq finally (lambda ()
                      (when-let ((s-buf
                                  (get-buffer (org-babel-python-with-earmuffs session-key))))
                        ;; We cannot delete it immediately as we are called from it.
                        (run-with-idle-timer
                         0.1 nil
                         (lambda ()
                           (when (buffer-live-p s-buf)
                             (let ((kill-buffer-query-functions nil)
                                   (kill-buffer-hook nil))
                               (kill-buffer s-buf)))))))))

    (setq comint-buffer
          (get-buffer (org-babel-python-with-earmuffs session-key)))
    (org-elib-async-comint-queue-init-if-needed comint-buffer)
    (with-temp-file tmp-src-file
      (insert (if (and graphics-file (eq result-type 'output))
                  (format org-babel-python--output-graphics-wrapper
                          full-body graphics-file)
                full-body)))

    (lambda (&rest q)
      (pcase q
        (`(:instrs-to-enter)
         ;; FIXME: This is wrong.
         "import sys; sys.ps1=''; sys.ps2=''")
        (`(:instrs-to-exit))
        (`(:finally) (when finally (funcall finally)))
        (`(:instr-to-emit-tag ,tag) (format "print ('%s')" tag))
        (`(:post-process ,r) (when post-process (funcall post-process r)))
        (`(:send-instrs-to-session ,code)
         ;; See org-babel-python-send-string
         (with-current-buffer comint-buffer
           (let ((python-shell-buffer-name
                  (org-babel-python-without-earmuffs session-key)))
             (python-shell-send-string (concat code "\n")))))
        (`(:get-code) session-body)
        (`(:get-comint-buffer) comint-buffer)
        (_ (error "Unknown query"))))))


;;; Org babel 'execute-with'.


;;;; Asynchronous
;;
(defun my-org-babel-schedule (lang body params handle-feedback)
  "Schedule the execution of BODY according to PARAMS.
This function is called by `org-babel-execute-src-block'.  Return a
function that waits and returns the result on success, raise on failure."
  (let ((exec (pcase lang
                ("python" (my-org-babel-python-how-to-execute body params))
                ("bash"   (my-org-babel-shell-how-to-execute body params))
                (_ (error "Not handled (yet): %s" lang)))))
    (org-elib-async-comint-queue--push exec :handle-feedback handle-feedback)))


;;;; Synchronous
;;
(defun my-org-babel-execute (lang body params)
  "Execute Python BODY according to PARAMS.
This function is called by `org-babel-execute-src-block'."
  ;; We just start the asynchronous execution, wait for it, and return
  ;; the result (or raise the exception). No custom code, and,
  ;; synchronous and asynchronous should just mix nicely together.
  (funcall (my-org-babel-schedule lang body params nil)))

debug log:

solving 918a4b142 ...
found 918a4b142 in https://yhetil.org/emacs/65d61a35.050a0220.5a5dc.fb44@mx.google.com/

applying [1/1] https://yhetil.org/emacs/65d61a35.050a0220.5a5dc.fb44@mx.google.com/
diff --git a/scratch/bba-ob-core-async/my-async-tests.el b/scratch/bba-ob-core-async/my-async-tests.el
new file mode 100644
index 000000000..918a4b142

Checking patch scratch/bba-ob-core-async/my-async-tests.el...
Applied patch scratch/bba-ob-core-async/my-async-tests.el cleanly.

index at:
100644 918a4b1426fd67eb7b7e4e7f036d6acfaa18513c	scratch/bba-ob-core-async/my-async-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.