prompt: don't crash on missing /etc/passwd

This commit is contained in:
Martin Sekera 2021-03-11 15:23:56 +01:00
parent 7bfa0deecd
commit d8b5c3eb73

View file

@ -220,16 +220,20 @@ class LoginPart(Part):
self.fragments.append(style_reset())
def list_homes():
already_listed = set()
usernames = []
home_dirs = []
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 already_listed:
already_listed.add(tokens[5])
yield (tokens[5], tokens[0])
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="/"):