emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* file-mode source code block header argument
@ 2020-07-24 17:49 John Herrlin
  2020-07-30 19:47 ` Russell Adams
  2020-08-01  5:21 ` Kyle Meyer
  0 siblings, 2 replies; 5+ messages in thread
From: John Herrlin @ 2020-07-24 17:49 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 576 bytes --]


Hey,

I am looking for a way to set permission on a file created from source
code block result when :file header argument is used. I was looking for
something like :tangle-mode but could not find anything. I wrote a patch
that does just that and it works for my small use case. It's a header
argument called :file-mode and can be used in the same way as
:tangle-mode.

Example usage:

#+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
  echo "#!/bin/bash"
  echo "echo Hello World"
#+END_SRC

Is this a suitable way of doing it?

Best regards
John


[-- Attachment #2: 0001-ob-core-file-mode-option-in-source-code-block-argume.patch --]
[-- Type: text/x-patch, Size: 3079 bytes --]

From a58d960092ad944e17e9b22f337379ed90638b65 Mon Sep 17 00:00:00 2001
From: John Herrlin <jherrlin@gmail.com>
Date: Fri, 24 Jul 2020 13:45:18 +0200
Subject: [PATCH] ob-core: file-mode option in source code block arguments

* ob-core.el (org-babel-execute-src-block): Source code block header
argument :file-mode can set file permission if :file argument is provided
---
 doc/org-manual.org      | 12 ++++++++++++
 lisp/ob-core.el         |  5 ++++-
 testing/lisp/test-ob.el | 17 +++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index b616446..2919139 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17440,6 +17440,18 @@ default behavior is to automatically determine the result type.
   uses the generated file name for both the "link" and
   "description" parts of the link.
 
+  #+cindex: @samp{file-mode}, header argument
+  The =file-mode= header argument defines the file permission. For
+  example, to make a read-only file, use ‘:file-mode (identity
+  #o444)’. To make it executable, use ‘:file-mode (identity #o755)’
+
+  #+begin_example
+  ,#+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
+    echo "#!/bin/bash"
+    echo "echo Hello World"
+  ,#+END_SRC
+  #+end_example
+
   #+cindex: @samp{sep}, header argument
   By default, Org assumes that a table written to a file has
   TAB-delimited output.  You can choose a different separator with
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e798595..cc3e002 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -731,7 +731,10 @@ block."
 		    (with-temp-file file
 		      (insert (org-babel-format-result
 			       result
-			       (cdr (assq :sep params))))))
+			       (cdr (assq :sep params)))))
+		    ;; Set permissions if header argument `:file-mode' is provided
+		    (when (assq :file-mode params)
+		      (set-file-modes file (cdr (assq :file-mode params)))))
 		  (setq result file))
 		;; Possibly perform post process provided its
 		;; appropriate.  Dynamically bind "*this*" to the
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index 7c44622..c4aaad1 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -1746,6 +1746,23 @@ line 1
 		   (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
     ))
 
+(ert-deftest test-ob/file-mode ()
+  "Ensure that file have correct permissions."
+  (let* ((file       (org-babel-temp-file "file-mode-" ".sh"))
+	 (filename   (file-name-nondirectory file))
+	 (path       (file-name-directory file)))
+    (org-test-with-temp-text
+	(concat
+	 "#+BEGIN_SRC emacs-lisp :results file "
+	 ":file " filename " "
+	 ":output-dir " path " "
+	 ":file-mode (identity #o755)
+     nil
+     #+END_SRC")
+      (org-babel-execute-src-block))
+    (should (equal (file-modes file)
+		   493))))
+
 (ert-deftest test-ob-core/dir-mkdirp ()
   "Test :mkdirp with :dir header combination."
   (should-not
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: file-mode source code block header argument
  2020-07-24 17:49 file-mode source code block header argument John Herrlin
@ 2020-07-30 19:47 ` Russell Adams
  2020-08-01  5:21 ` Kyle Meyer
  1 sibling, 0 replies; 5+ messages in thread
From: Russell Adams @ 2020-07-30 19:47 UTC (permalink / raw)
  To: emacs-orgmode

On Fri, Jul 24, 2020 at 07:49:24PM +0200, John Herrlin wrote:
> I am looking for a way to set permission on a file created from source
> code block result when :file header argument is used. I was looking for
> something like :tangle-mode but could not find anything. I wrote a patch
> that does just that and it works for my small use case. It's a header
> argument called :file-mode and can be used in the same way as
> :tangle-mode.
>
> Example usage:
>
> #+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
>   echo "#!/bin/bash"
>   echo "echo Hello World"
> #+END_SRC
>
> Is this a suitable way of doing it?
>
> Best regards
> John

Sounds like a good use case and reuse of an existing feature. No idea how to get
it added though. Patience! =]

------------------------------------------------------------------
Russell Adams                            RLAdams@AdamsInfoServ.com

PGP Key ID:     0x1160DCB3           http://www.adamsinfoserv.com/

Fingerprint:    1723 D8CA 4280 1EC9 557F  66E8 1154 E018 1160 DCB3


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: file-mode source code block header argument
  2020-07-24 17:49 file-mode source code block header argument John Herrlin
  2020-07-30 19:47 ` Russell Adams
@ 2020-08-01  5:21 ` Kyle Meyer
  2020-08-02 10:15   ` John Herrlin
  1 sibling, 1 reply; 5+ messages in thread
From: Kyle Meyer @ 2020-08-01  5:21 UTC (permalink / raw)
  To: John Herrlin; +Cc: emacs-orgmode

John Herrlin writes:

> I am looking for a way to set permission on a file created from source
> code block result when :file header argument is used. I was looking for
> something like :tangle-mode but could not find anything. I wrote a patch
> that does just that and it works for my small use case.

Thanks for the patch.

> It's a header argument called :file-mode and can be used in the same
> way as :tangle-mode.
>
> Example usage:
>
> #+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
>   echo "#!/bin/bash"
>   echo "echo Hello World"
> #+END_SRC
>
> Is this a suitable way of doing it?

Looks sensible to me.  Hopefully others with more knowledge and interest
in Babel will chime in if they disagree.  In the meantime, I mostly just
have convention nit-picks...

> Subject: [PATCH] ob-core: file-mode option in source code block arguments
>
> * ob-core.el (org-babel-execute-src-block): Source code block header
> argument :file-mode can set file permission if :file argument is provided

Please end this sentence with a period.

Also, assuming the listing at
https://orgmode.org/worg/org-contribute.html is up to date, this should
come with a TINYCHANGE cookie.

> ---
>  doc/org-manual.org      | 12 ++++++++++++
>  lisp/ob-core.el         |  5 ++++-
>  testing/lisp/test-ob.el | 17 +++++++++++++++++
>  3 files changed, 33 insertions(+), 1 deletion(-)

Please also add an ORG-NEWS entry.

> diff --git a/doc/org-manual.org b/doc/org-manual.org
> index b616446..2919139 100644
> --- a/doc/org-manual.org
> +++ b/doc/org-manual.org
> @@ -17440,6 +17440,18 @@ default behavior is to automatically determine the result type.
>    uses the generated file name for both the "link" and
>    "description" parts of the link.
>  
> +  #+cindex: @samp{file-mode}, header argument
> +  The =file-mode= header argument defines the file permission. For

s/permission/permissions/

Also, the convention for this project is two spaces following a
sentence.

> +  example, to make a read-only file, use ‘:file-mode (identity
> +  #o444)’. To make it executable, use ‘:file-mode (identity #o755)’

These ‘...’ should be =...=, I think.

I know this text is just following the text for :tangle-mode, but from
my point of view, "For example ..." through to the example block could
be dropped.  If the reader knows about file permissions, then it's
unnecessary; if they don't, I don't see how it'd be enough to help them
figure out what's going on.

> +  #+begin_example
> +  ,#+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
> +    echo "#!/bin/bash"
> +    echo "echo Hello World"
> +  ,#+END_SRC
> +  #+end_example

> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
> index e798595..cc3e002 100644
> --- a/lisp/ob-core.el
> +++ b/lisp/ob-core.el
> @@ -731,7 +731,10 @@ block."
>  		    (with-temp-file file
>  		      (insert (org-babel-format-result
>  			       result
> -			       (cdr (assq :sep params))))))
> +			       (cdr (assq :sep params)))))
> +		    ;; Set permissions if header argument `:file-mode' is provided

To continue my nit-picking: Please follow the style of the surrounding
comments, ending with a period and filling the paragraph.

> +		    (when (assq :file-mode params)
> +		      (set-file-modes file (cdr (assq :file-mode params)))))

Looks good.

>  		  (setq result file))
>  		;; Possibly perform post process provided its
>  		;; appropriate.  Dynamically bind "*this*" to the
> diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
> index 7c44622..c4aaad1 100644
> --- a/testing/lisp/test-ob.el
> +++ b/testing/lisp/test-ob.el
> @@ -1746,6 +1746,23 @@ line 1
>  		   (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
>      ))
>  
> +(ert-deftest test-ob/file-mode ()
> +  "Ensure that file have correct permissions."
> +  (let* ((file       (org-babel-temp-file "file-mode-" ".sh"))
> +	 (filename   (file-name-nondirectory file))
> +	 (path       (file-name-directory file)))
> +    (org-test-with-temp-text
> +	(concat
> +	 "#+BEGIN_SRC emacs-lisp :results file "
> +	 ":file " filename " "
> +	 ":output-dir " path " "
> +	 ":file-mode (identity #o755)
> +     nil
> +     #+END_SRC")

Rather than using org-babel-temp-file to generate a temporary file, I
think the preferred way to do this would be to use
org-test-with-temp-text-in-file.

> +      (org-babel-execute-src-block))
> +    (should (equal (file-modes file)
> +		   493))))

To my eyes, spelling this as #o755 would be clearer.

So, perhaps something like this:

(ert-deftest test-ob/file-mode ()
  "Ensure that file have correct permissions."
  (should
   (equal #o755
          (org-test-with-temp-text-in-file "
#+begin_src emacs-lisp :results file :file t.sh :file-mode (identity #o755)
nil
#+end_src"
            (org-babel-next-src-block)
            (org-babel-execute-src-block)
            (unwind-protect
                (file-modes "t.sh")
              (delete-file "t.sh"))))))


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: file-mode source code block header argument
  2020-08-01  5:21 ` Kyle Meyer
@ 2020-08-02 10:15   ` John Herrlin
  2020-08-03  3:06     ` Kyle Meyer
  0 siblings, 1 reply; 5+ messages in thread
From: John Herrlin @ 2020-08-02 10:15 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 5519 bytes --]


Thank you for the comments Kyle! I updated the patch accordingly. Took
your test straight of as I think it's really clean and easy to reason
about.

Best regards

Kyle Meyer <kyle@kyleam.com> writes:

> John Herrlin writes:
>
>> I am looking for a way to set permission on a file created from source
>> code block result when :file header argument is used. I was looking for
>> something like :tangle-mode but could not find anything. I wrote a patch
>> that does just that and it works for my small use case.
>
> Thanks for the patch.
>
>> It's a header argument called :file-mode and can be used in the same
>> way as :tangle-mode.
>>
>> Example usage:
>>
>> #+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
>>   echo "#!/bin/bash"
>>   echo "echo Hello World"
>> #+END_SRC
>>
>> Is this a suitable way of doing it?
>
> Looks sensible to me.  Hopefully others with more knowledge and interest
> in Babel will chime in if they disagree.  In the meantime, I mostly just
> have convention nit-picks...
>
>> Subject: [PATCH] ob-core: file-mode option in source code block arguments
>>
>> * ob-core.el (org-babel-execute-src-block): Source code block header
>> argument :file-mode can set file permission if :file argument is provided
>
> Please end this sentence with a period.
>
> Also, assuming the listing at
> https://orgmode.org/worg/org-contribute.html is up to date, this should
> come with a TINYCHANGE cookie.
>
>> ---
>>  doc/org-manual.org      | 12 ++++++++++++
>>  lisp/ob-core.el         |  5 ++++-
>>  testing/lisp/test-ob.el | 17 +++++++++++++++++
>>  3 files changed, 33 insertions(+), 1 deletion(-)
>
> Please also add an ORG-NEWS entry.
>
>> diff --git a/doc/org-manual.org b/doc/org-manual.org
>> index b616446..2919139 100644
>> --- a/doc/org-manual.org
>> +++ b/doc/org-manual.org
>> @@ -17440,6 +17440,18 @@ default behavior is to automatically determine the result type.
>>    uses the generated file name for both the "link" and
>>    "description" parts of the link.
>>
>> +  #+cindex: @samp{file-mode}, header argument
>> +  The =file-mode= header argument defines the file permission. For
>
> s/permission/permissions/
>
> Also, the convention for this project is two spaces following a
> sentence.
>
>> +  example, to make a read-only file, use ‘:file-mode (identity
>> +  #o444)’. To make it executable, use ‘:file-mode (identity #o755)’
>
> These ‘...’ should be =...=, I think.
>
> I know this text is just following the text for :tangle-mode, but from
> my point of view, "For example ..." through to the example block could
> be dropped.  If the reader knows about file permissions, then it's
> unnecessary; if they don't, I don't see how it'd be enough to help them
> figure out what's going on.
>
>> +  #+begin_example
>> +  ,#+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
>> +    echo "#!/bin/bash"
>> +    echo "echo Hello World"
>> +  ,#+END_SRC
>> +  #+end_example
>
>> diff --git a/lisp/ob-core.el b/lisp/ob-core.el
>> index e798595..cc3e002 100644
>> --- a/lisp/ob-core.el
>> +++ b/lisp/ob-core.el
>> @@ -731,7 +731,10 @@ block."
>>  		    (with-temp-file file
>>  		      (insert (org-babel-format-result
>>  			       result
>> -			       (cdr (assq :sep params))))))
>> +			       (cdr (assq :sep params)))))
>> +		    ;; Set permissions if header argument `:file-mode' is provided
>
> To continue my nit-picking: Please follow the style of the surrounding
> comments, ending with a period and filling the paragraph.
>
>> +		    (when (assq :file-mode params)
>> +		      (set-file-modes file (cdr (assq :file-mode params)))))
>
> Looks good.
>
>>  		  (setq result file))
>>  		;; Possibly perform post process provided its
>>  		;; appropriate.  Dynamically bind "*this*" to the
>> diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
>> index 7c44622..c4aaad1 100644
>> --- a/testing/lisp/test-ob.el
>> +++ b/testing/lisp/test-ob.el
>> @@ -1746,6 +1746,23 @@ line 1
>>  		   (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
>>      ))
>>
>> +(ert-deftest test-ob/file-mode ()
>> +  "Ensure that file have correct permissions."
>> +  (let* ((file       (org-babel-temp-file "file-mode-" ".sh"))
>> +	 (filename   (file-name-nondirectory file))
>> +	 (path       (file-name-directory file)))
>> +    (org-test-with-temp-text
>> +	(concat
>> +	 "#+BEGIN_SRC emacs-lisp :results file "
>> +	 ":file " filename " "
>> +	 ":output-dir " path " "
>> +	 ":file-mode (identity #o755)
>> +     nil
>> +     #+END_SRC")
>
> Rather than using org-babel-temp-file to generate a temporary file, I
> think the preferred way to do this would be to use
> org-test-with-temp-text-in-file.
>
>> +      (org-babel-execute-src-block))
>> +    (should (equal (file-modes file)
>> +		   493))))
>
> To my eyes, spelling this as #o755 would be clearer.
>
> So, perhaps something like this:
>
> (ert-deftest test-ob/file-mode ()
>   "Ensure that file have correct permissions."
>   (should
>    (equal #o755
>           (org-test-with-temp-text-in-file "
> #+begin_src emacs-lisp :results file :file t.sh :file-mode (identity #o755)
> nil
> #+end_src"
>             (org-babel-next-src-block)
>             (org-babel-execute-src-block)
>             (unwind-protect
>                 (file-modes "t.sh")
>               (delete-file "t.sh"))))))


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-core-file-mode-option-in-source-code-block-argume.patch --]
[-- Type: text/x-patch, Size: 3596 bytes --]

From 43ef5de5267b463b9656b7f5db37eafe62cb7f61 Mon Sep 17 00:00:00 2001
From: John Herrlin <jherrlin@gmail.com>
Date: Sun, 2 Aug 2020 10:39:02 +0200
Subject: [PATCH] ob-core: file-mode option in source code block arguments

* ob-core.el (org-babel-execute-src-block): Source code block header
argument `:file-mode' can set file permissions if `:file' argument is
provided.
(org-babel-common-header-args-w-values): Add `:file-mode' to common
header arguments.

TINYCHANGE
---
 doc/org-manual.org      | 11 +++++++++++
 etc/ORG-NEWS            |  5 +++++
 lisp/ob-core.el         |  7 ++++++-
 testing/lisp/test-ob.el | 14 ++++++++++++++
 4 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 0f012d4..3eb745b 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17444,6 +17444,17 @@ default behavior is to automatically determine the result type.
   TAB-delimited output.  You can choose a different separator with
   the =sep= header argument.
 
+  #+cindex: @samp{file-mode}, header argument
+  The =file-mode= header argument defines the file permissions.  To
+  make it executable, use =:file-mode (identity #o755)=.
+
+  #+begin_example
+  ,#+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
+    echo "#!/bin/bash"
+    echo "echo Hello World"
+  ,#+END_SRC
+  #+end_example
+
 *** Format
 :PROPERTIES:
 :UNNUMBERED: notoc
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1ac7486..e754615 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -261,6 +261,11 @@ explicitly:
 In situations where ~org-return~ calls ~newline~, multiple newlines
 can now be inserted with this prefix argument.
 
+*** New source code block header argument `:file-mode'
+
+Source code block header argument `:file-mode' can set file
+permissions if `:file' argument is provided.
+
 ** New commands
 *** ~org-table-header-line-mode~
 
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e798595..adc5358 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -400,6 +400,7 @@ then run `org-babel-switch-to-session'."
     (file	. :any)
     (file-desc  . :any)
     (file-ext   . :any)
+    (file-mode  . ((#o755 #o555 #o444 :any)))
     (hlines	. ((no yes)))
     (mkdirp	. ((yes no)))
     (no-expand)
@@ -731,7 +732,11 @@ block."
 		    (with-temp-file file
 		      (insert (org-babel-format-result
 			       result
-			       (cdr (assq :sep params))))))
+			       (cdr (assq :sep params)))))
+		    ;; Set file permissions if header argument
+		    ;; `:file-mode' is provided.
+		    (when (assq :file-mode params)
+		      (set-file-modes file (cdr (assq :file-mode params)))))
 		  (setq result file))
 		;; Possibly perform post process provided its
 		;; appropriate.  Dynamically bind "*this*" to the
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index 7c44622..03296ba 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -1746,6 +1746,20 @@ line 1
 		   (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
     ))
 
+(ert-deftest test-ob/file-mode ()
+  "Ensure that file have correct permissions."
+  (should
+   (equal #o755
+          (org-test-with-temp-text-in-file "
+#+begin_src emacs-lisp :results file :file t.sh :file-mode (identity #o755)
+nil
+#+end_src"
+            (org-babel-next-src-block)
+            (org-babel-execute-src-block)
+            (unwind-protect
+                (file-modes "t.sh")
+              (delete-file "t.sh"))))))
+
 (ert-deftest test-ob-core/dir-mkdirp ()
   "Test :mkdirp with :dir header combination."
   (should-not
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: file-mode source code block header argument
  2020-08-02 10:15   ` John Herrlin
@ 2020-08-03  3:06     ` Kyle Meyer
  0 siblings, 0 replies; 5+ messages in thread
From: Kyle Meyer @ 2020-08-03  3:06 UTC (permalink / raw)
  To: John Herrlin; +Cc: emacs-orgmode

John Herrlin writes:

> Thank you for the comments Kyle! I updated the patch accordingly. Took
> your test straight of as I think it's really clean and easy to reason
> about.

Thanks for the updates.  Applied (ea946353a), tweaking the markup in the
NEWS entry and rewording the test docstring.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-08-03  3:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-24 17:49 file-mode source code block header argument John Herrlin
2020-07-30 19:47 ` Russell Adams
2020-08-01  5:21 ` Kyle Meyer
2020-08-02 10:15   ` John Herrlin
2020-08-03  3:06     ` Kyle Meyer

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).