Hi,

I'm trying to update ob-awk.el to allow for code to be executed on remote 
machines. This doesn't seem to work at present:

#+begin_src awk :dir /ssh:gpsc:data/rubus 
  END { print "I ran!"}
#+end_src

: awk: fatal: cannot open source file `/ssh:gpsc:/home/tys000/awk-oQ0Av9' for 
reading: No such file : or directory
: [ Babel evaluation exited with code 2 ]

Digging through the code, particularly ~org-babel-execute:awk~ in the file 
~ob-awk.el~, I think the issue is the code chunk is stored as a temporary file 
on the remote machine, and then we try to run awk on the remote machine with 
the path to the code file *relative to my local machine*. i.e.,

    awk -f /ssh:gpsc:/home/tys000/awk-qSxjHH

I don't think Awk can process paths in this format in any case.

I've come up with a solution. Instead of saving the code to a temporary file, 
we could just pass the code chunk as a string to awk. In the body of 
~org-babel-execute:awk~ the awk command is constructed like this:

  ...
  (list org-babel-awk-command
        "-f" code-file cmd-line)
  ...

Where code-file is the path (a string) to the temporary file with the code in 
it. 

If I replace this with:

    ....
    (list org-babel-awk-command
          ;;"-f" code-file 
          cmd-line (shell-quote-argument full-body)
    ...

The command we ask to run on the remote machine is:

    awk  END\ \{\ print\ \"I\ ran\!\"\} 

My toy Awk program works as expected:

#+begin_src awk :dir /ssh:gpsc:data/rubus 
  END { print "I ran!"}
#+end_src

#+RESULTS:
: I ran!

After some limited testing, this seems to work. Does that sound like a good 
approach?

If anyone wants to try this out I'd appreciate tests. The full function is 
below.

- tyler

(defun org-babel-execute:awk (body params)
  "Execute a block of Awk code BODY with org-babel.
PARAMS is a plist of src block parameters .
This function is called by `org-babel-execute-src-block'."
  (let* ((result-params (cdr (assq :result-params params)))
         (cmd-line (cdr (assq :cmd-line params)))
         (in-file (cdr (assq :in-file params)))
         (full-body (org-babel-expand-body:awk body params))
         (code-file (let ((file (org-babel-temp-file "awk-")))
                      (with-temp-file file (insert full-body)) file))
         (stdin (let ((stdin (cdr (assq :stdin params))))
                  (when stdin
                    (let ((tmp (org-babel-temp-file "awk-stdin-"))
                          (res (org-babel-ref-resolve stdin)))
                      (with-temp-file tmp
                        (insert (org-babel-awk-var-to-awk res)))
                      tmp))))
         (cmd (mapconcat #'identity
                         (append
                          (list org-babel-awk-command
                                ;;"-f" code-file 
                                cmd-line (shell-quote-argument full-body))
                          (mapcar (lambda (pair)
                                    (format "-v %s='%s'"
                                            (car pair)
                                            (org-babel-awk-var-to-awk
                                             (cdr pair))))
                                  (org-babel--get-vars params))
                          (list in-file))
                         " ")))
    (org-babel-reassemble-table
     (let ((results
            (cond
             (stdin (with-temp-buffer
                      (call-process-shell-command cmd stdin (current-buffer))
                      (buffer-string)))
             (t (org-babel-eval cmd "")))))
       (when results
         (org-babel-result-cond result-params
           results
           (let ((tmp (org-babel-temp-file "awk-results-")))
             (with-temp-file tmp (insert results))
             (org-babel-import-elisp-from-file tmp)))))
     (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))))))

-- 
plantarum.ca

Reply via email to