--- a/mercurial/util.py Wed Aug 23 00:19:24 2006 +0200
+++ b/mercurial/util.py Tue Aug 22 20:45:03 2006 -0300
@@ -15,7 +15,7 @@
from i18n import gettext as _
from demandload import *
demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile")
-demandload(globals(), "os threading time")
+demandload(globals(), "os threading time pwd grp")
# used by parsedate
defaultdateformats = ('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M',
@@ -509,6 +509,38 @@
raise Abort(_('user name not available - set USERNAME '
'environment variable'))
+def username(uid=None):
+ """Return the name of the user with the given uid.
+
+ If uid is None, return the name of the current user."""
+ try:
+ # force an ImportError if there's no module pwd
+ getpwuid = pwd.getpwuid
+ if uid is None:
+ uid = os.getuid()
+ try:
+ return getpwuid(uid)[0]
+ except KeyError:
+ return str(uid)
+ except ImportError:
+ return None
+
+def groupname(gid=None):
+ """Return the name of the group with the given gid.
+
+ If gid is None, return the name of the current group."""
+ try:
+ # force an ImportError if there's no module grp
+ getgrgid = grp.getgrgid
+ if gid is None:
+ gid = os.getgid()
+ try:
+ return getgrgid(gid)[0]
+ except KeyError:
+ return str(gid)
+ except ImportError:
+ return None
+
# Platform specific variants
if os.name == 'nt':
demandload(globals(), "msvcrt")