diff --git a/general/g.tempfile/g.tempfile.html b/general/g.tempfile/g.tempfile.html index 8952e531b78..144f89ae58e 100644 --- a/general/g.tempfile/g.tempfile.html +++ b/general/g.tempfile/g.tempfile.html @@ -1,19 +1,25 @@

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 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 $$. +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. @@ -23,6 +29,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:
@@ -36,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

diff --git a/general/g.tempfile/main.c b/general/g.tempfile/main.c index cb630a166a4..1dad2056496 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) + G_mkdir(tempfile); + else + close(creat(tempfile, 0666)); + } fprintf(stdout, "%s\n", tempfile); exit(EXIT_SUCCESS); diff --git a/python/grass/script/core.py b/python/grass/script/core.py index 40709a11e80..e1d3bd74b00 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 directory, created with g.tempfile. - return tmp + :param env: environment + + :return: path to a temporary directory + """ + return read_command("g.tempfile", flags="f", pid=os.getpid(), env=env).strip() def tempname(length, lowercase=False):