Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g.tempfile: allow creation of temporary directories #4397

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions general/g.tempfile/g.tempfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ <h2>DESCRIPTION</h2>

<p>
<em>g.tempfile</em>
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.
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
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 (<b>-f</b>) 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 $$.
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
It is recommended that $$ be specified as the process-id for
<em>g.tempfile</em>.

Expand All @@ -23,6 +23,11 @@ <h2>EXAMPLE</h2>
<div class="code"><pre>
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=$$`

</pre></div>
For /bin/csh scripts, the following can be used:
<div class="code"><pre>
Expand Down
16 changes: 14 additions & 2 deletions general/g.tempfile/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d is unfortunately used, but f is not great: folder, file, or directory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt the same but had no better idea. I considered -D but that did not seem better than -f for folder mode...
Any suggestions?

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);
Expand All @@ -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, 0777);
else
close(creat(tempfile, 0666));
ninsbl marked this conversation as resolved.
Show resolved Hide resolved
}
fprintf(stdout, "%s\n", tempfile);

exit(EXIT_SUCCESS);
Expand Down
10 changes: 6 additions & 4 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading