Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,42 @@
|
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
os.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
os.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
|
|
|
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
|
| 5 |
+
def get_writable_dir(name, subfolder=None):
|
| 6 |
+
"""
|
| 7 |
+
Tries multiple locations and returns the first writable path.
|
| 8 |
+
If all fail, returns None.
|
| 9 |
+
"""
|
| 10 |
+
candidates = [
|
| 11 |
+
os.path.join("/code", name),
|
| 12 |
+
os.path.join(tempfile.gettempdir(), name)
|
| 13 |
+
]
|
| 14 |
+
for path in candidates:
|
| 15 |
+
if subfolder:
|
| 16 |
+
path = os.path.join(path, subfolder)
|
| 17 |
+
try:
|
| 18 |
+
os.makedirs(path, exist_ok=True)
|
| 19 |
+
return path
|
| 20 |
+
except PermissionError:
|
| 21 |
+
continue
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
# Try to assign a safe cache root
|
| 25 |
+
CACHE_ROOT = get_writable_dir("my_app_cache") or tempfile.gettempdir()
|
| 26 |
+
|
| 27 |
+
# Apply safe directories for matplotlib and others
|
| 28 |
+
matplotlib_dir = get_writable_dir("my_app_cache", "matplotlib")
|
| 29 |
+
if matplotlib_dir:
|
| 30 |
+
os.environ["MPLCONFIGDIR"] = matplotlib_dir
|
| 31 |
+
|
| 32 |
+
cartopy_dir = get_writable_dir("my_app_cache", "cartopy")
|
| 33 |
+
if cartopy_dir:
|
| 34 |
+
os.environ["CARTOPY_CACHE_DIR"] = cartopy_dir
|
| 35 |
+
os.environ["CARTOPY_USER_BACKGROUNDS"] = cartopy_dir
|
| 36 |
+
|
| 37 |
+
xdg_cache = get_writable_dir("my_app_cache", "contextily")
|
| 38 |
+
if xdg_cache:
|
| 39 |
+
os.environ["XDG_CACHE_HOME"] = xdg_cache
|
| 40 |
+
|
| 41 |
|
| 42 |
|
| 43 |
|