Skip to content

Commit e1ccd30

Browse files
authored
fix: tmpdir to properly use all the POSIX environment variables
1 parent b1397e9 commit e1ccd30

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

src/pyinfra/facts/server.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,28 @@ def command(self):
5252

5353
class TmpDir(FactBase):
5454
"""
55-
Returns the temporary directory of the current server, if configured.
55+
Returns the temporary directory of the current server.
56+
57+
According to POSIX standards, checks environment variables in this order:
58+
1. TMPDIR (if set and accessible)
59+
2. TMP (if set and accessible)
60+
3. TEMP (if set and accessible)
61+
4. Falls back to empty string
5662
"""
5763

5864
@override
5965
def command(self):
60-
return "echo $TMPDIR"
66+
return """
67+
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ] && [ -w "$TMPDIR" ]; then
68+
echo "$TMPDIR"
69+
elif [ -n "$TMP" ] && [ -d "$TMP" ] && [ -w "$TMP" ]; then
70+
echo "$TMP"
71+
elif [ -n "$TEMP" ] && [ -d "$TEMP" ] && [ -w "$TEMP" ]; then
72+
echo "$TEMP"
73+
else
74+
echo ""
75+
fi
76+
""".strip()
6177

6278

6379
class Hostname(FactBase):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"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",
3+
"output": ["/tmp"],
4+
"fact": "/tmp"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"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",
3+
"output": ["/temp"],
4+
"fact": "/temp"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"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",
3+
"output": ["/usr/tmp"],
4+
"fact": "/usr/tmp"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"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",
3+
"output": ["/var/tmp"],
4+
"fact": "/var/tmp"
5+
}

0 commit comments

Comments
 (0)