Skip to content

Commit

Permalink
Merge pull request #2927 from DaanDeMeyer/home
Browse files Browse the repository at this point in the history
Check for $HOME environment variable as well
  • Loading branch information
DaanDeMeyer authored Jul 29, 2024
2 parents b40c58a + 4672d5c commit 9886bff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ runs:
shell: bash
run: cat /sys/devices/system/clocksource/clocksource0/current_clocksource

- name: Show environment
shell: bash
run: env

- name: Enable unprivileged user namespaces
shell: bash
run: |
Expand Down
25 changes: 22 additions & 3 deletions mkosi/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,30 @@ def is_running_user(cls) -> bool:
@classmethod
@functools.lru_cache(maxsize=1)
def name(cls) -> str:
return os.getenv("USER", pwd.getpwuid(cls.uid).pw_name)
try:
return pwd.getpwuid(cls.uid).pw_name
except KeyError:
if cls.uid == 0:
return "root"

if not (user := os.getenv("USER")):
die(f"Could not find user name for UID {cls.uid}")

return user

@classmethod
@functools.lru_cache(maxsize=1)
def home(cls) -> Path:
if cls.invoked_as_root and Path.cwd().is_relative_to("/home") and len(Path.cwd().parents) > 2:
return list(Path.cwd().parents)[-3]

return Path(f"~{cls.name()}").expanduser()
try:
return Path(pwd.getpwuid(cls.uid).pw_dir or "/")
except KeyError:
if not (home := os.getenv("HOME")):
die(f"Could not find home directory for UID {cls.uid}")

return Path(home)

@classmethod
@functools.lru_cache(maxsize=1)
Expand All @@ -62,7 +77,11 @@ def is_regular_user(cls) -> bool:
def cache_dir(cls) -> Path:
if (env := os.getenv("XDG_CACHE_HOME")) or (env := os.getenv("CACHE_DIRECTORY")):
cache = Path(env)
elif cls.is_regular_user() and (Path.cwd().is_relative_to(INVOKING_USER.home()) or not cls.invoked_as_root):
elif (
cls.is_regular_user() and
INVOKING_USER.home() != Path("/") and
(Path.cwd().is_relative_to(INVOKING_USER.home()) or not cls.invoked_as_root)
):
cache = INVOKING_USER.home() / ".cache"
else:
cache = Path("/var/cache")
Expand Down

0 comments on commit 9886bff

Please sign in to comment.