From d8b5c3eb73f45e768a51674cfe6fd66224119c58 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Thu, 11 Mar 2021 15:23:56 +0100 Subject: [PATCH] prompt: don't crash on missing /etc/passwd --- lib/prompt.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/prompt.py b/lib/prompt.py index 4cb3f97..79b9c03 100755 --- a/lib/prompt.py +++ b/lib/prompt.py @@ -220,16 +220,20 @@ class LoginPart(Part): self.fragments.append(style_reset()) def list_homes(): - already_listed = set() + usernames = [] + home_dirs = [] - with open("/etc/passwd") as f: - for line in f: - tokens = line.split(":") - - if tokens[5] != "/dev/null": - if tokens[5] not in already_listed: - already_listed.add(tokens[5]) - yield (tokens[5], tokens[0]) + if os.path.exists("/etc/passwd"): + with open("/etc/passwd") as f: + for line in f: + tokens = line.split(":") + + if tokens[5] != "/dev/null": + if tokens[5] not in home_dirs: + usernames.append(tokens[0]) + home_dirs.append(tokens[5]) + + return list(zip(home_dirs, usernames)) class Dir: def __init__(self, path, name=None, slash="/"):