From 257f4c0a0cbe24e74c95c847976f8f87c2772569 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 12 Oct 2007 12:27:11 +0000
Subject: [PATCH] Enhance GetHomeDir to accept either names or UIDs

Currently GetHomeDir accepts UIDs only. Enhance it to accept either a
user name or a user id, to allow for nicer usage.

Reviewed-by: imsnah
---
 lib/utils.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/utils.py b/lib/utils.py
index fc7dcdbe9..3f6c2af8b 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -825,12 +825,22 @@ def ListVisibleFiles(path):
   return [i for i in os.listdir(path) if not i.startswith(".")]
 
 
-def GetHomeDir(uid, default=None):
-  """Try to get the homedir of the given user id.
+def GetHomeDir(user, default=None):
+  """Try to get the homedir of the given user.
+
+  The user can be passed either as a string (denoting the name) or as
+  an integer (denoting the user id). If the user is not found, the
+  'default' argument is returned, which defaults to None.
 
   """
   try:
-    result = pwd.getpwuid(uid)
+    if isinstance(user, basestring):
+      result = pwd.getpwnam(user)
+    elif isinstance(user, (int, long)):
+      result = pwd.getpwuid(user)
+    else:
+      raise errors.ProgrammerError("Invalid type passed to GetHomeDir (%s)" %
+                                   type(user))
   except KeyError:
     return default
   return result.pw_dir
-- 
GitLab