Browse Source

babel: New function to process file names for use in external processes

	* ob.el (org-babel-process-file-name): New function
	(org-babel-maybe-remote-file): Delete function

	* ob-sql.el (org-babel-execute:sql):
	Use org-babel-process-file-name

	* ob-scheme.el (org-babel-execute:scheme):
	Use org-babel-process-file-name

	* ob-sass.el (org-babel-execute:sass):
	Use org-babel-process-file-name

	* ob-ruby.el (org-babel-ruby-evaluate):
	Use org-babel-process-file-name

	* ob-python.el (org-babel-python-evaluate-external-process):
	Use org-babel-process-file-name
	(org-babel-python-evaluate-session):
	Use org-babel-process-file-name

	* ob-plantuml.el (org-babel-execute:plantuml):
	Use org-babel-process-file-name

	* ob-perl.el (org-babel-perl-evaluate):
	Use org-babel-process-file-name

	* ob-octave.el (org-babel-octave-evaluate-external-process):
	Use org-babel-process-file-name
	(org-babel-octave-evaluate-session):
	Use org-babel-process-file-name,
	don't use org-babel-maybe-remote-file

	* ob-lisp.el (org-babel-execute:lisp):
	Use org-babel-process-file-name

	* ob-ledger.el (org-babel-execute:ledger):
	Use org-babel-process-file-name

	* ob-js.el (org-babel-execute:js):
	Use org-babel-process-file-name

	* ob-haskell.el (org-babel-haskell-export-to-lhs):
	Use org-babel-process-file-name

	* ob-gnuplot.el (org-babel-execute:gnuplot):
	Use org-babel-process-file-name

	* ob-eval.el (org-babel-eval-read-file): Don't use
	org-babel-maybe-remote-file

	* ob-dot.el (org-babel-execute:dot):
	Use org-babel-process-file-name

	* ob-ditaa.el (org-babel-execute:ditaa):
	Use org-babel-process-file-name

	* ob-clojure.el (org-babel-clojure-evaluate-external-process):
	Use org-babel-process-file-name

	* ob-asymptote.el (org-babel-execute:asymptote):
	Use org-babel-process-file-name

	* ob-R.el (org-babel-R-assign-elisp): Don't use
	org-babel-maybe-remote-file, use org-babel-process-file-name
	(org-babel-R-evaluate-external-process):
	Use org-babel-process-file-name
	(org-babel-R-evaluate-session):
	Use org-babel-process-file-name

	* ob-C.el (org-babel-C-execute):
	Use org-babel-process-file-name

In addition to passing the file path through `expand-file-name',
tramp-style remote file names are converted to conventional (local)
file paths. The reason is that, if a tramp file name was in use in
emacs, then the shell command will be executing on the remote machine
in question. Further, by default the file name is passed through
`shell-quote-argument'.
Dan Davison 10 years ago
parent
commit
ec0b3892b9

+ 2 - 2
lisp/ob-C.el

@@ -100,10 +100,10 @@ or `org-babel-execute:c++'."
 		     (cond
 		      ((equal org-babel-c-variant 'c) org-babel-C-compiler)
 		      ((equal org-babel-c-variant 'cpp) org-babel-c++-compiler))
-		     tmp-bin-file
+		     (org-babel-process-file-name tmp-bin-file)
 		     (mapconcat 'identity
 				(if (listp flags) flags (list flags)) " ")
-		     tmp-src-file) ""))))
+		     (org-babel-process-file-name tmp-src-file)) ""))))
     ((lambda (results)
        (org-babel-reassemble-table
 	(if (member "vector" (nth 2 processed-params))

+ 4 - 4
lisp/ob-R.el

@@ -144,11 +144,11 @@ This function is called by `org-babel-execute-src-block'."
       (let ((transition-file (org-babel-temp-file "R-import-")))
         ;; ensure VALUE has an orgtbl structure (depth of at least 2)
         (unless (listp (car value)) (setq value (list value)))
-        (with-temp-file (org-babel-maybe-remote-file transition-file)
+        (with-temp-file transition-file
           (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
           (insert "\n"))
         (format "%s <- read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE)"
-                name transition-file
+                name (org-babel-process-file-name transition-file 'noquote)
 		(if (or (eq (nth 1 value) 'hline) colnames-p) "TRUE" "FALSE")
 		(if rownames-p "1" "NULL")))
     (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
@@ -245,7 +245,7 @@ last statement in BODY, as elisp."
 				   (if row-names-p "NA" "TRUE")
 				 "FALSE")
 			       (format "{function ()\n{\n%s\n}}()" body)
-			       (org-babel-tramp-localname tmp-file)))
+			       (org-babel-process-file-name tmp-file 'noquote)))
        (org-babel-R-process-value-result
 	(org-babel-import-elisp-from-file tmp-file '(16)) column-names-p)))
     (output (org-babel-eval org-babel-R-command body))))
@@ -271,7 +271,7 @@ last statement in BODY, as elisp."
 		(if column-names-p
 		    (if row-names-p "NA" "TRUE")
 		  "FALSE")
-		".Last.value" (org-babel-tramp-localname tmp-file)))
+		".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
        (org-babel-R-process-value-result
 	(org-babel-import-elisp-from-file tmp-file '(16))  column-names-p)))
     (output

+ 9 - 5
lisp/ob-asymptote.el

@@ -74,11 +74,15 @@ This function is called by `org-babel-execute-src-block'."
                      "pdf"))
          (cmdline (cdr (assoc :cmdline params)))
          (in-file (org-babel-temp-file "asymptote-"))
-         (cmd (concat "asy "
-                      (if out-file
-                          (concat "-globalwrite -f " format " -o " out-file)
-                        "-V")
-                      " " cmdline " " in-file)))
+         (cmd
+	  (concat "asy "
+		  (if out-file
+		      (concat
+		       "-globalwrite -f " format
+		       " -o " (org-babel-process-file-name out-file))
+		    "-V")
+		  " " cmdline
+		  " " (org-babel-process-file-name in-file))))
     (with-temp-file in-file
       (insert (org-babel-expand-body:asymptote body params processed-params)))
     (message cmd) (shell-command cmd)

+ 6 - 2
lisp/ob-clojure.el

@@ -262,8 +262,12 @@ repl buffer."
     (case result-type
       (output (org-babel-eval cmd body))
       (value (let* ((tmp-file (org-babel-temp-file "clojure-results-")))
-	       (org-babel-eval cmd (format org-babel-clojure-wrapper-method
-					   body tmp-file tmp-file))
+	       (org-babel-eval
+		cmd
+		(format
+		 org-babel-clojure-wrapper-method
+		 body
+		 (org-babel-process-file-name tmp-file 'noquote) tmp-file))
 	       (org-babel-clojure-table-or-string
 		(org-babel-eval-read-file tmp-file)))))))
 

+ 3 - 1
lisp/ob-ditaa.el

@@ -57,7 +57,9 @@ This function is called by `org-babel-execute-src-block'."
 	 (cmd (concat "java -jar "
 		      (shell-quote-argument
 		       (expand-file-name org-ditaa-jar-path))
-		      " " cmdline " " in-file " " out-file)))
+		      " " cmdline
+		      " " (org-babel-process-file-name in-file)
+		      " " (org-babel-process-file-name out-file))))
     (unless (file-exists-p org-ditaa-jar-path)
       (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
     (with-temp-file in-file (insert body))

+ 5 - 1
lisp/ob-dot.el

@@ -74,7 +74,11 @@ This function is called by `org-babel-execute-src-block'."
 	 (in-file (org-babel-temp-file "dot-")))
     (with-temp-file in-file
       (insert (org-babel-expand-body:dot body params processed-params)))
-    (org-babel-eval (concat cmd " " in-file " " cmdline " -o " out-file) "")
+    (org-babel-eval
+     (concat cmd
+	     " " (org-babel-process-file-name in-file)
+	     " " cmdline
+	     " -o " (org-babel-process-file-name out-file)) "")
     out-file))
 
 (defun org-babel-prep-session:dot (session params)

+ 1 - 2
lisp/ob-eval.el

@@ -60,8 +60,7 @@ STDERR with `org-babel-eval-error-notify'."
 
 (defun org-babel-eval-read-file (file)
   "Return the contents of FILE as a string."
-  (with-temp-buffer (insert-file-contents
-		     (org-babel-maybe-remote-file file))
+  (with-temp-buffer (insert-file-contents file)
 		    (buffer-string)))
 
 (defun org-babel-shell-command-on-region (start end command

+ 4 - 1
lisp/ob-gnuplot.el

@@ -146,7 +146,10 @@ This function is called by `org-babel-execute-src-block'."
               (insert (concat body "\n")))
             (message "gnuplot \"%s\"" script-file)
             (setq output
-                  (shell-command-to-string (format "gnuplot \"%s\"" script-file)))
+                  (shell-command-to-string
+		   (format
+		    "gnuplot \"%s\""
+		    (org-babel-process-file-name script-file))))
             (message output))
         (with-temp-buffer
           (insert (concat body "\n"))

+ 3 - 1
lisp/ob-haskell.el

@@ -182,7 +182,9 @@ constructs (header arguments, no-web syntax etc...) are ignored."
          (tmp-tex-file (concat tmp-file ".tex"))
          (lhs-file (concat base-name ".lhs"))
          (tex-file (concat base-name ".tex"))
-         (command (concat org-babel-haskell-lhs2tex-command " " lhs-file " > " tex-file))
+         (command (concat org-babel-haskell-lhs2tex-command
+			  " " (org-babel-process-file-name lhs-file)
+			  " > " (org-babel-process-file-name tex-file)))
          (preserve-indentp org-src-preserve-indentation)
          indentation)
     ;; escape haskell source-code blocks

+ 3 - 1
lisp/ob-js.el

@@ -100,7 +100,9 @@ This function is called by `org-babel-execute-src-block'"
             (if (string= result-type "value")
                 (format org-babel-js-function-wrapper full-body)
               full-body)))
-         (org-babel-eval (format "%s %s" org-babel-js-cmd script-file) ""))))))
+         (org-babel-eval
+	  (format "%s %s" org-babel-js-cmd
+		  (org-babel-process-file-name script-file)) ""))))))
 
 (defun org-babel-js-read (results)
   "Convert RESULTS into an appropriate elisp value.

+ 7 - 2
lisp/ob-ledger.el

@@ -53,9 +53,14 @@ called by `org-babel-execute-src-block'."
 	(out-file (org-babel-temp-file "ledger-output-"))
 	)
     (with-temp-file in-file (insert body))
-    (message (concat "ledger -f " in-file " " cmdline))
+    (message (concat "ledger"
+		     " -f " (org-babel-process-file-name in-file)
+		     " " cmdline))
     (with-output-to-string
-      (shell-command (concat "ledger -f " in-file " " cmdline " > " out-file)))
+      (shell-command (concat "ledger"
+			     " -f " (org-babel-process-file-name in-file)
+			     " " cmdline
+			     " > " (org-babel-process-file-name out-file))))
     (with-temp-buffer (insert-file-contents out-file) (buffer-string))))
 
 (defun org-babel-prep-session:ledger (session params)

+ 3 - 1
lisp/ob-lisp.el

@@ -81,7 +81,9 @@ This function is called by `org-babel-execute-src-block'"
             (if (string= result-type "value")
                 (format "(print %s)" full-body)
               full-body)))
-         (org-babel-eval (format "%s %s" org-babel-lisp-cmd script-file) ""))))))
+         (org-babel-eval
+	  (format "%s %s" org-babel-lisp-cmd
+		  (org-babel-process-file-name script-file)) ""))))))
 
 ;; This function should be used to assign any variables in params in
 ;; the context of the session environment.

+ 11 - 7
lisp/ob-octave.el

@@ -181,9 +181,10 @@ value of the last statement in BODY, as elisp."
       (value (let ((tmp-file (org-babel-temp-file "results-")))
 	       (org-babel-eval
 		cmd
-		(format org-babel-octave-wrapper-method body tmp-file tmp-file))
-	       (org-babel-octave-import-elisp-from-file
-		(org-babel-maybe-remote-file tmp-file)))))))
+		(format org-babel-octave-wrapper-method body
+			(org-babel-process-file-name tmp-file 'noquote)
+			(org-babel-process-file-name tmp-file 'noquote)))
+	       (org-babel-octave-import-elisp-from-file tmp-file))))))
 
 (defun org-babel-octave-evaluate-session
   (session body result-type &optional matlabp)
@@ -200,11 +201,15 @@ value of the last statement in BODY, as elisp."
 	     (if (and matlabp org-babel-matlab-with-emacs-link)
 		 (concat
 		  (format org-babel-matlab-emacs-link-wrapper-method
-			  body tmp-file tmp-file wait-file) "\n")
+			  body
+			  (org-babel-process-file-name tmp-file 'noquote)
+			  (org-babel-process-file-name tmp-file 'noquote) wait-file) "\n")
 	       (mapconcat
 		#'org-babel-chomp
 		(list (format org-babel-octave-wrapper-method
-			      body tmp-file tmp-file)
+			      body
+			      (org-babel-process-file-name tmp-file 'noquote)
+			      (org-babel-process-file-name tmp-file 'noquote))
 		      org-babel-octave-eoe-indicator) "\n")))))
 	 (raw (if (and matlabp org-babel-matlab-with-emacs-link)
 		  (save-window-excursion
@@ -227,8 +232,7 @@ value of the last statement in BODY, as elisp."
 		  (insert full-body) (comint-send-input nil t)))) results)
     (case result-type
       (value
-       (org-babel-octave-import-elisp-from-file
-	(org-babel-maybe-remote-file tmp-file)))
+       (org-babel-octave-import-elisp-from-file tmp-file))
       (output
        (progn
 	 (setq results

+ 2 - 1
lisp/ob-perl.el

@@ -110,7 +110,8 @@ return the value of the last statement in BODY, as elisp."
     (value (let ((tmp-file (org-babel-temp-file "perl-results-")))
 	     (org-babel-eval
 	      org-babel-perl-command
-	      (format org-babel-perl-wrapper-method body tmp-file))
+	      (format org-babel-perl-wrapper-method body
+		      (org-babel-process-file-name tmp-file 'noquote)))
 	     (org-babel-eval-read-file tmp-file)))))
 
 (provide 'ob-perl)

+ 2 - 4
lisp/ob-plantuml.el

@@ -66,11 +66,9 @@ This function is called by `org-babel-execute-src-block'."
 			(if (string= (file-name-extension out-file) "svg")
 			    "-tsvg" "")
 			" -p " cmdline " < "
-			(shell-quote-argument
-			 (expand-file-name in-file))
+			(org-babel-process-file-name in-file)
 			" > "
-			(shell-quote-argument
-			 (expand-file-name out-file))))))
+			(org-babel-process-file-name out-file)))))
     (unless (file-exists-p org-plantuml-jar-path)
       (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
     (with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))

+ 5 - 3
lisp/ob-python.el

@@ -222,7 +222,7 @@ last statement in BODY, as elisp."
 				(org-remove-indentation
 				 (org-babel-trim body))
 				"[\r\n]") "\n")
-			      tmp-file))
+			      (org-babel-process-file-name tmp-file 'noquote)))
 	     ((lambda (raw)
 		(if (or (member "code" result-params)
 			(member "pp" result-params))
@@ -243,8 +243,10 @@ last statement in BODY, as elisp."
 	   (if pp
 	       (list
 		"import pp"
-		(format "open('%s', 'w').write(pprint.pformat(_))" tmp-file))
-	     (list (format "open('%s', 'w').write(str(_))" tmp-file)))))
+		(format "open('%s', 'w').write(pprint.pformat(_))"
+			(org-babel-process-file-name tmp-file 'noquote)))
+	     (list (format "open('%s', 'w').write(str(_))"
+			   (org-babel-process-file-name tmp-file 'noquote))))))
 	 (input-body (body)
 		     (mapc (lambda (statement) (insert statement) (comint-send-input))
 			   (split-string (org-babel-trim body) "[\r\n]+"))

+ 11 - 8
lisp/ob-ruby.el

@@ -185,12 +185,13 @@ return the value of the last statement in BODY, as elisp."
       ;; external process evaluation
       (case result-type
 	(output (org-babel-eval org-babel-ruby-command body))
-	(value (let ((tmp-file (org-babel-temp-file "ruby-results-")))
-		 (org-babel-eval org-babel-ruby-command
-				 (format (if (member "pp" result-params)
-					     org-babel-ruby-pp-wrapper-method
-					   org-babel-ruby-wrapper-method)
-					 body tmp-file))
+	(value (let ((tmp-file (org-babel-temp-file "ruby-")))
+		 (org-babel-eval
+		  org-babel-ruby-command
+		  (format (if (member "pp" result-params)
+			      org-babel-ruby-pp-wrapper-method
+			    org-babel-ruby-wrapper-method)
+			  body (org-babel-process-file-name tmp-file 'noquote)))
 		 ((lambda (raw)
 		    (if (or (member "code" result-params)
 			    (member "pp" result-params))
@@ -232,10 +233,12 @@ return the value of the last statement in BODY, as elisp."
 	     (append
 	      (list body)
 	      (if (not ppp)
-		  (list (format org-babel-ruby-f-write tmp-file))
+		  (list (format org-babel-ruby-f-write
+				(org-babel-process-file-name tmp-file 'noquote)))
 		(list
 		 "results=_" "require 'pp'" "orig_out = $stdout"
-		 (format org-babel-ruby-pp-f-write tmp-file)))
+		 (format org-babel-ruby-pp-f-write
+			 (org-babel-process-file-name tmp-file 'noquote))))
 	      (list org-babel-ruby-eoe-indicator)))
 	    (comint-send-input nil t))
 	  (org-babel-eval-read-file tmp-file)))))))

+ 3 - 1
lisp/ob-sass.el

@@ -54,7 +54,9 @@ This function is called by `org-babel-execute-src-block'."
          (out-file (or file (org-babel-temp-file "sass-out-")))
          (cmdline (cdr (assoc :cmdline params)))
          (in-file (org-babel-temp-file "sass-in-"))
-         (cmd (concat "sass " (or cmdline "") in-file " " out-file)))
+         (cmd (concat "sass " (or cmdline "")
+		      " " (org-babel-process-file-name in-file)
+		      " " (org-babel-process-file-name out-file))))
     (with-temp-file in-file
       (insert (org-babel-expand-body:sass body params))) (shell-command cmd)
     (or file (with-temp-buffer (insert-file-contents out-file) (buffer-string)))))

+ 2 - 1
lisp/ob-scheme.el

@@ -98,7 +98,8 @@ This function is called by `org-babel-execute-src-block'"
                 (format "(display %s)" full-body)
               full-body)))
          (org-babel-eval
-	  (format "%s %s" org-babel-scheme-cmd script-file) ""))))))
+	  (format "%s %s" org-babel-scheme-cmd
+		  (org-babel-process-file-name script-file)) ""))))))
 
 (defun org-babel-prep-session:scheme (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."

+ 6 - 2
lisp/ob-sql.el

@@ -65,9 +65,13 @@ This function is called by `org-babel-execute-src-block'."
                        (org-babel-temp-file "sql-out-")))
          (command (case (intern engine)
                     ('mysql (format "mysql %s -e \"source %s\" > %s"
-                                    (or cmdline "") in-file out-file))
+                                    (or cmdline "")
+				    (org-babel-process-file-name in-file)
+				    (org-babel-process-file-name out-file)))
 		    ('postgresql (format "psql -A -P footer=off -F \"\t\"  -f %s -o %s %s"
-					 in-file out-file (or cmdline "")))
+				    (org-babel-process-file-name in-file)
+				    (org-babel-process-file-name out-file)
+				    (or cmdline "")))
                     (t (error "no support for the %s sql engine" engine)))))
     (with-temp-file in-file
       (insert (org-babel-expand-body:sql body params)))

+ 12 - 12
lisp/ob.el

@@ -1766,18 +1766,7 @@ Fixes a bug in `tramp-handle-call-process-region'."
     (apply org-babel-call-process-region-original
            start end program delete buffer display args)))
 
-(defun org-babel-maybe-remote-file (file)
-  "Conditionally parse information on a remote connnection.
-If FILE specifies a remote file, then parse the information on
-the remote connection."
-  (if (file-remote-p default-directory)
-      (let* ((vec (tramp-dissect-file-name default-directory))
-             (user (tramp-file-name-user vec))
-             (host (tramp-file-name-host vec)))
-        (concat "/" user (when user "@") host ":" file))
-    file))
-
-(defun org-babel-tramp-localname (file)
+(defun org-babel-local-file-name (file)
   "Return the local name component of FILE."
   (if (file-remote-p file)
       (let (localname)
@@ -1785,6 +1774,17 @@ the remote connection."
 	  localname))
     file))
 
+(defun org-babel-process-file-name (name &optional no-quote-p)
+  "Prepare NAME to be used in an external process.
+If NAME specifies a remote location, the remote portion of the
+name is removed, since in that case the process will be executing
+remotely. The file name is then processed by
+`expand-file-name'. Unless second argument NO-QUOTE-P is non-nil,
+the file name is additionally processed by
+`shell-quote-argument'"
+  ((lambda (f) (if no-quote-p f (shell-quote-argument f)))
+   (expand-file-name (org-babel-local-file-name name))))
+
 ;; (defvar org-babel-temporary-directory
 ;;   (or (and (boundp 'org-babel-temporary-directory)
 ;; 	   org-babel-temporary-directory)