From 647dccb44292e7b2c666cf2eb05c29688aaee332 Mon Sep 17 00:00:00 2001 From: ninsbl Date: Fri, 27 Sep 2024 10:43:38 +0200 Subject: [PATCH 1/7] allow creation of tempdir --- general/g.tempfile/g.tempfile.html | 13 +++++++++---- general/g.tempfile/main.c | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/general/g.tempfile/g.tempfile.html b/general/g.tempfile/g.tempfile.html index 8952e531b78..421fc6ec78e 100644 --- a/general/g.tempfile/g.tempfile.html +++ b/general/g.tempfile/g.tempfile.html @@ -10,10 +10,10 @@

DESCRIPTION

g.tempfile -creates an unique file and prints the name. The user is required to provide -a process-id which will be used as part of the name of the file. -Most Unix shells provide a way to get the process id of the current shell. -For /bin/sh and /bin/csh this is $$. +creates an unique file or directory (-f) and prints the name. +The user is required to provide a process-id which will be used as part of +the name of the file or directory. Most Unix shells provide a way to get the +process id of the current shell. For /bin/sh and /bin/csh this is $$. It is recommended that $$ be specified as the process-id for g.tempfile. @@ -23,6 +23,11 @@

EXAMPLE

 temp1=`g.tempfile pid=$$`
 temp2=`g.tempfile pid=$$`
+# Get the tempile path but do not create it
+temp3=`g.tempfile -d pid=$$`
+# Create a temporary directory
+temp3=`g.tempfile -f pid=$$`
+
 
For /bin/csh scripts, the following can be used:
diff --git a/general/g.tempfile/main.c b/general/g.tempfile/main.c
index cb630a166a4..4c4f28cff20 100644
--- a/general/g.tempfile/main.c
+++ b/general/g.tempfile/main.c
@@ -29,6 +29,7 @@ int main(int argc, char *argv[])
     struct GModule *module;
     struct Option *pid;
     struct Flag *dry_run;
+    struct Flag *directory;
     char *tempfile;
     int p;
 
@@ -52,6 +53,13 @@ int main(int argc, char *argv[])
     dry_run->description =
         _("Dry run - don't create a file, just prints it's file name");
 
+    directory = G_define_flag();
+    directory->key = 'f';
+    directory->description =
+        _("Folder mode - create a temporary directory, not a file");
+
+    G_option_exclusive(dry_run, directory, NULL);
+
     G_disable_interactive();
     if (G_parser(argc, argv))
         exit(EXIT_FAILURE);
@@ -63,8 +71,12 @@ int main(int argc, char *argv[])
     tempfile = G_tempfile_pid(p);
 
     /* create tempfile so next run of this program will create a unique name */
-    if (!dry_run->answer)
-        close(creat(tempfile, 0666));
+    if (!dry_run->answer) {
+        if (!directory->answer)
+            mkdir(tempfile, 0666);
+        else
+            close(creat(tempfile, 0666));
+    }
     fprintf(stdout, "%s\n", tempfile);
 
     exit(EXIT_SUCCESS);

From 74de25b62356ef59f9f6dbdc123f97be98262935 Mon Sep 17 00:00:00 2001
From: ninsbl 
Date: Fri, 27 Sep 2024 10:58:05 +0200
Subject: [PATCH 2/7] propagate dir mode in g.tempfile

---
 python/grass/script/core.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/python/grass/script/core.py b/python/grass/script/core.py
index a86738926f0..584b959ebf8 100644
--- a/python/grass/script/core.py
+++ b/python/grass/script/core.py
@@ -955,11 +955,13 @@ def tempfile(create=True, env=None):
 
 
 def tempdir(env=None):
-    """Returns the name of a temporary dir, created with g.tempfile."""
-    tmp = tempfile(create=False, env=env)
-    os.mkdir(tmp)
+    """Returns the name of a temporary dir, created with g.tempfile.
 
-    return tmp
+    :param env: environment
+
+    :return: path to a tmp file
+    """
+    return read_command("g.tempfile", flags="f", pid=os.getpid(), env=env).strip()
 
 
 def tempname(length, lowercase=False):

From 16cd9d0fa3a3abc6a064c0769397a15289f6801f Mon Sep 17 00:00:00 2001
From: ninsbl 
Date: Fri, 27 Sep 2024 11:04:13 +0200
Subject: [PATCH 3/7] fix copy-paste-mistake

---
 python/grass/script/core.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/python/grass/script/core.py b/python/grass/script/core.py
index 584b959ebf8..84ac7466889 100644
--- a/python/grass/script/core.py
+++ b/python/grass/script/core.py
@@ -955,11 +955,11 @@ def tempfile(create=True, env=None):
 
 
 def tempdir(env=None):
-    """Returns the name of a temporary dir, created with g.tempfile.
+    """Returns the name of a temporary directory, created with g.tempfile.
 
     :param env: environment
 
-    :return: path to a tmp file
+    :return: path to a temporary directory
     """
     return read_command("g.tempfile", flags="f", pid=os.getpid(), env=env).strip()
 

From 35f470a65496991b86bfc643b597f34cf22a9e45 Mon Sep 17 00:00:00 2001
From: ninsbl 
Date: Fri, 27 Sep 2024 12:02:19 +0200
Subject: [PATCH 4/7] fix logic inversion

---
 general/g.tempfile/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/general/g.tempfile/main.c b/general/g.tempfile/main.c
index 4c4f28cff20..5f80302e337 100644
--- a/general/g.tempfile/main.c
+++ b/general/g.tempfile/main.c
@@ -72,7 +72,7 @@ int main(int argc, char *argv[])
 
     /* create tempfile so next run of this program will create a unique name */
     if (!dry_run->answer) {
-        if (!directory->answer)
+        if (directory->answer)
             mkdir(tempfile, 0666);
         else
             close(creat(tempfile, 0666));

From 8c61b6397ce1b5b9608a1a2aeb7070b8b0cc0731 Mon Sep 17 00:00:00 2001
From: ninsbl 
Date: Fri, 27 Sep 2024 12:40:29 +0200
Subject: [PATCH 5/7] give proper permissions

---
 general/g.tempfile/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/general/g.tempfile/main.c b/general/g.tempfile/main.c
index 5f80302e337..d6f1757c60f 100644
--- a/general/g.tempfile/main.c
+++ b/general/g.tempfile/main.c
@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
     /* create tempfile so next run of this program will create a unique name */
     if (!dry_run->answer) {
         if (directory->answer)
-            mkdir(tempfile, 0666);
+            mkdir(tempfile, 0777);
         else
             close(creat(tempfile, 0666));
     }

From e8f16d22380aaeede1403601fd5dae3a069360fa Mon Sep 17 00:00:00 2001
From: ninsbl 
Date: Mon, 30 Sep 2024 22:47:41 +0200
Subject: [PATCH 6/7] use g_mkdir

---
 general/g.tempfile/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/general/g.tempfile/main.c b/general/g.tempfile/main.c
index d6f1757c60f..1dad2056496 100644
--- a/general/g.tempfile/main.c
+++ b/general/g.tempfile/main.c
@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
     /* create tempfile so next run of this program will create a unique name */
     if (!dry_run->answer) {
         if (directory->answer)
-            mkdir(tempfile, 0777);
+            G_mkdir(tempfile);
         else
             close(creat(tempfile, 0666));
     }

From fb30b7be2e1633e815c93fa09f4a6179137702c5 Mon Sep 17 00:00:00 2001
From: ninsbl 
Date: Mon, 30 Sep 2024 23:18:22 +0200
Subject: [PATCH 7/7] update manual

---
 general/g.tempfile/g.tempfile.html | 52 ++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 14 deletions(-)

diff --git a/general/g.tempfile/g.tempfile.html b/general/g.tempfile/g.tempfile.html
index 421fc6ec78e..144f89ae58e 100644
--- a/general/g.tempfile/g.tempfile.html
+++ b/general/g.tempfile/g.tempfile.html
@@ -1,18 +1,24 @@
 

DESCRIPTION

g.tempfile -is designed for shell scripts that need to use large temporary files. -GRASS provides a mechanism for temporary files that does not depend on -/tmp. GRASS temporary files are created in the data base with the assumption -that there will be enough space under the data base for large files. +creates a unique temporary file or directory (-f) and prints the path. +It is designed for shell scripts that need to use large temporary files. +GRASS provides a mechanism for temporary files or directories that does not +depend on /tmp. GRASS temporary files and directories are created in the +GRASS GIS database with the assumption that there will be enough space for +large files. The base directory is: $PROJECT/$MAPSET/.tmp/$HOSTNAME/ + +

GRASS periodically removes temporary files that have been left behind by programs that failed to remove them before terminating.

-g.tempfile -creates an unique file or directory (-f) and prints the name. -The user is required to provide a process-id which will be used as part of -the name of the file or directory. Most Unix shells provide a way to get the +g.tempfile requires the user to provide an integer number, +usually a process-id (pid), which will be used as part of the file or +directory name of the resulting path, suffixed by a dot and the 0-indexed +number of temporary files with that PID. + +Most Unix shells provide a way to get the process id of the current shell. For /bin/sh and /bin/csh this is $$. It is recommended that $$ be specified as the process-id for g.tempfile. @@ -41,12 +47,30 @@

NOTES

creates a different (i.e. unique) name. Although GRASS does eventually get around to removing -tempfiles that have been left behind, the programmer should -make every effort to remove these files. They often get -large and take up disk space. If you write /bin/sh scripts, -learn to use the /bin/sh trap command. If you -write /bin/csh scripts, learn to use the /bin/csh -onintr command. +temporary files and directories that have been left behind, +the programmer should make every effort to remove these files. +They often get large and take up disk space. If you write /bin/sh +scripts, learn to use the /bin/sh trap command. If you +write /bin/csh scripts, learn to use the /bin/csh onintr +command. In Python, use an atexit procedure. + +

+If the size of temporary files is not an issue, it is recommended +to use NamedTemporaryFile with a context manager to create a +temporary file in Python.
+In this example below, we open a temporary file for writing, +write something and then we can use it in another tool. +Once we do not need it anymore, we need to delete it ourselves. + +

+import tempfile
+
+with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp_file:
+    file_path = tmp_file.name
+    tmp_file.write(...)
+
+gs.try_remove(file_path)
+

AUTHOR