Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 18 additions & 2 deletions src/pyinfra/facts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,28 @@ def command(self):

class TmpDir(FactBase):
"""
Returns the temporary directory of the current server, if configured.
Returns the temporary directory of the current server.

According to POSIX standards, checks environment variables in this order:
1. TMPDIR (if set and accessible)
2. TMP (if set and accessible)
3. TEMP (if set and accessible)
4. Falls back to empty string
"""

@override
def command(self):
return "echo $TMPDIR"
return """
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ] && [ -w "$TMPDIR" ]; then
echo "$TMPDIR"
elif [ -n "$TMP" ] && [ -d "$TMP" ] && [ -w "$TMP" ]; then
echo "$TMP"
elif [ -n "$TEMP" ] && [ -d "$TEMP" ] && [ -w "$TEMP" ]; then
echo "$TEMP"
else
echo ""
fi
""".strip()


class Hostname(FactBase):
Expand Down
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/default_fallback.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"\"\nfi",
"output": ["/tmp"],
"fact": "/tmp"
}
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/temp_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"\"\nfi",
"output": ["/temp"],
"fact": "/temp"
}
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/tmp_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"\"\nfi",
"output": ["/usr/tmp"],
"fact": "/usr/tmp"
}
5 changes: 5 additions & 0 deletions tests/facts/server.TmpDir/tmpdir_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "if [ -n \"$TMPDIR\" ] && [ -d \"$TMPDIR\" ] && [ -w \"$TMPDIR\" ]; then\n echo \"$TMPDIR\"\nelif [ -n \"$TMP\" ] && [ -d \"$TMP\" ] && [ -w \"$TMP\" ]; then\n echo \"$TMP\"\nelif [ -n \"$TEMP\" ] && [ -d \"$TEMP\" ] && [ -w \"$TEMP\" ]; then\n echo \"$TEMP\"\nelse\n echo \"\"\nfi",
"output": ["/var/tmp"],
"fact": "/var/tmp"
}