Skip to content

Commit e605ef9

Browse files
authored
Treat android shells as unix (#72)
1 parent d6ab2ca commit e605ef9

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

README.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,24 @@ On Android::
127127
>>> appname = "SuperApp"
128128
>>> appauthor = "Acme"
129129
>>> user_data_dir(appname, appauthor)
130-
'/data/data/com.termux/files/SuperApp'
130+
'/data/data/com.myApp/files/SuperApp'
131131
>>> user_cache_dir(appname, appauthor)
132-
'/data/data/com.termux/cache/SuperApp'
132+
'/data/data/com.myApp/cache/SuperApp'
133133
>>> user_log_dir(appname, appauthor)
134-
'/data/data/com.termux/cache/SuperApp/log'
134+
'/data/data/com.myApp/cache/SuperApp/log'
135135
>>> user_config_dir(appname)
136-
'/data/data/com.termux/shared_prefs/SuperApp'
136+
'/data/data/com.myApp/shared_prefs/SuperApp'
137137
>>> user_documents_dir()
138138
'/storage/emulated/0/Documents'
139139
>>> user_runtime_dir(appname, appauthor)
140-
'/data/data/com.termux/cache/SuperApp/tmp'
140+
'/data/data/com.myApp/cache/SuperApp/tmp'
141+
142+
Note: Some android apps like Termux and Pydroid are used as shells. These
143+
apps are used by the end user to emulate Linux environment. Presence of
144+
``SHELL`` environment variable is used by Platformdirs to differentiate
145+
between general android apps and android apps used as shells. Shell android
146+
apps also support ``XDG_*`` environment variables.
147+
141148

142149
``PlatformDirs`` for convenience
143150
================================

src/platformdirs/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]:
2525
from platformdirs.unix import Unix as Result
2626

2727
if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
28+
29+
if os.getenv("SHELL") is not None:
30+
return Result
31+
2832
from platformdirs.android import _android_folder
2933

3034
if _android_folder() is not None:

tests/test_api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ def test_function_interface_is_in_sync(func: str) -> None:
5353
@pytest.mark.parametrize("root", ["A", "/system", None])
5454
@pytest.mark.parametrize("data", ["D", "/data", None])
5555
@pytest.mark.parametrize("path", ["/data/data/a/files", "/C"])
56-
def test_android_active(monkeypatch: MonkeyPatch, root: str | None, data: str | None, path: str) -> None:
57-
for env_var, value in {"ANDROID_DATA": data, "ANDROID_ROOT": root}.items():
56+
@pytest.mark.parametrize("shell", ["/data/data/com.app/files/usr/bin/sh", "/usr/bin/sh", None])
57+
def test_android_active(
58+
monkeypatch: MonkeyPatch, root: str | None, data: str | None, path: str, shell: str | None
59+
) -> None:
60+
for env_var, value in {"ANDROID_DATA": data, "ANDROID_ROOT": root, "SHELL": shell}.items():
5861
if value is None:
5962
monkeypatch.delenv(env_var, raising=False)
6063
else:
@@ -65,7 +68,7 @@ def test_android_active(monkeypatch: MonkeyPatch, root: str | None, data: str |
6568
_android_folder.cache_clear()
6669
monkeypatch.setattr(sys, "path", ["/A", "/B", path])
6770

68-
expected = root == "/system" and data == "/data" and _android_folder() is not None
71+
expected = root == "/system" and data == "/data" and shell is None and _android_folder() is not None
6972
if expected:
7073
assert platformdirs._set_platform_dir_class() is Android
7174
else:

0 commit comments

Comments
 (0)