diff mercurial/util.py @ 3010:494521a3f142

Only read .hg/hgrc files from trusted users/groups The list of trusted users and groups is specified in the [trusted] section of a hgrc; the current user is always trusted; "*" can be used to trust all users/groups. Global hgrc files are always read. On Windows (and other systems that don't have the pwd and grp modules), all .hg/hgrc files are read.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Tue, 22 Aug 2006 20:45:03 -0300
parents 3d5547845158
children c27d1e1798a3
line wrap: on
line diff
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -15,7 +15,7 @@ platform-specific details from the core.
 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 @@ def getuser():
     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")