discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Subject: [PATCH] b4: Clean up temporary directories by default
Date: Sun, 27 Sep 2020 02:14:46 -0400	[thread overview]
Message-ID: <20200927061446.2301-1-kyle@kyleam.com> (raw)

Each piem-b4-am-from-mid call works in a new temporary directory.
Aside from debugging, there's no reason to keep these directories
around, polluting temporary-file-directory.
---
 piem-b4.el | 45 ++++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/piem-b4.el b/piem-b4.el
index 75b2344..31bf6f1 100644
--- a/piem-b4.el
+++ b/piem-b4.el
@@ -50,12 +50,18 @@ (defcustom piem-b4-b4-executable "b4"
 \f
 ;;;; Internals
 
+(defvar piem-b4-keep-temp-directory nil
+  "Don't clean up the directory created by `piem-b4-am-from-mid'.
+This is intended to be used for debugging purposes.")
+
 (defun piem-b4--get-am-files (mid coderepo args)
   (let* ((outdir (file-name-as-directory
                   (make-temp-file "piem-b4-" t)))
          (root (concat outdir "m"))
          (mbox-thread (concat root "-piem"))
-         (local-mbox-p nil))
+         (local-mbox-p nil)
+         (clean-fn (and (not piem-b4-keep-temp-directory)
+                        (lambda () (delete-directory outdir t)))))
     (when-let ((fn (run-hook-with-args-until-success
                     'piem-mid-to-thread-functions mid)))
       (with-temp-file mbox-thread
@@ -78,19 +84,25 @@ (defun piem-b4--get-am-files (mid coderepo args)
         (setq local-mbox-p t)))
     ;; Move to the coderepo so that we pick up any b4 configuration
     ;; from there.
-    (apply #'piem-process-call coderepo piem-b4-b4-executable "am"
-           (and local-mbox-p
-                (concat "--use-local-mbox=" mbox-thread))
-           (concat "--outdir=" outdir)
-           (concat "--mbox-name=m")
-           (append args (list mid)))
+    (condition-case err
+        (apply #'piem-process-call coderepo piem-b4-b4-executable "am"
+               (and local-mbox-p
+                    (concat "--use-local-mbox=" mbox-thread))
+               (concat "--outdir=" outdir)
+               (concat "--mbox-name=m")
+               (append args (list mid)))
+      (piem-process-error
+       (when clean-fn
+         (funcall clean-fn))
+       (signal (car err) (cdr err))))
     (let ((mbox-cover (concat root ".cover"))
           (mbox-am (concat root ".mbx")))
       (list (and (file-exists-p mbox-cover)
                  mbox-cover)
             (if (file-exists-p mbox-am)
                 mbox-am
-              (error "Expected mbox file does not exist: %s" mbox-am))))))
+              (error "Expected mbox file does not exist: %s" mbox-am))
+            clean-fn))))
 
 \f
 ;;;; Commands
@@ -135,15 +147,18 @@ (defun piem-b4-am-from-mid (mid &optional args)
                       args)))
     (user-error "%s is incompatible with this command" badopt))
   (pcase-let* ((coderepo (piem-inbox-coderepo-maybe-read))
-               (`(,cover ,mbox-file)
+               (`(,cover ,mbox-file ,clean-fn)
                 (piem-b4--get-am-files mid coderepo args))
                (default-directory coderepo))
-    (piem-am mbox-file
-             nil
-             (with-temp-buffer
-               (insert-file-contents (or cover mbox-file))
-               (piem-extract-mbox-info))
-             coderepo)))
+    (unwind-protect
+        (piem-am mbox-file
+                 nil
+                 (with-temp-buffer
+                   (insert-file-contents (or cover mbox-file))
+                   (piem-extract-mbox-info))
+                 coderepo)
+      (when clean-fn
+        (funcall clean-fn)))))
 
 (define-infix-argument piem-b4-am:--outdir ()
   :description "Output directory"

base-commit: 4cc9f39ad142600eea706094d3e2a58eba13e073
-- 
2.28.0


                 reply	other threads:[~2020-09-27  6:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://git.kyleam.com/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200927061446.2301-1-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.kyleam.com/piem/

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).