uid = kwargs.pop("uid", None)
kwargs.setdefault("python", is_python(args))
if not user and not uid: return self.call(*args, **kwargs)
- if util.get_operator_name():
+ if os.getenv("SSH_GSSAPI_NAME"):
# This might be generalized as "preserve some environment"
- args.insert(0, "SSH_GSSAPI_NAME=" + util.get_operator_name())
+ args.insert(0, "SSH_GSSAPI_NAME=" + os.getenv("SSH_GSSAPI_NAME"))
if uid: return self.call("sudo", "-u", "#" + str(uid), *args, **kwargs)
if user: return self.call("sudo", "-u", user, *args, **kwargs)
def safeCall(self, *args, **kwargs):
import subprocess
import pwd
import sys
+import socket
import wizard
.. note::
This function uses the passwd database and thus
- only works on scripts servers.
+ only works on scripts servers when querying directories
+ that live on AFS.
"""
- return pwd.getpwuid(get_dir_uid(dir)).pw_name
+ pwentry = pwd.getpwuid(get_dir_uid(dir))
+ # XXX: Error handling!
+ return pwentry.pw_name
def get_revision():
"""Returns the commit ID of the current Wizard install."""
def get_operator_info():
"""
- Returns tuple of ``(realname, email)`` from Hesiod about the
- this operator of this script from :func:`get_operator_name`.
- Useful when generating commit messages.
- """
- username = get_operator_name()
- hesinfo = subprocess.Popen(["hesinfo", username, "passwd"],stdout=subprocess.PIPE).communicate()[0]
- fields = hesinfo.partition(",")[0]
- realname = fields.rpartition(":")[2]
- return realname, username + "@mit.edu"
+ Returns tuple of ``(realname, email)`` about the person running
+ the script. If run from a scripts server, get info from Hesiod.
+ Otherwise, use the passwd database (email generated probably won't
+ actually accept mail). Useful when generating commit messages.
+ """
+ username = get_operator_name_from_gssapi()
+ if username:
+ # scripts approach
+ hesinfo = subprocess.Popen(["hesinfo", username, "passwd"],stdout=subprocess.PIPE).communicate()[0]
+ fields = hesinfo.partition(",")[0]
+ realname = fields.rpartition(":")[2]
+ return realname, username + "@mit.edu"
+ else:
+ # more traditional approach, but the email probably doesn't work
+ uid = os.getuid()
+ if not uid:
+ # since root isn't actually a useful designation, but maybe
+ # SUDO_USER contains something helpful
+ sudo_user = os.getenv("SUDO_USER")
+ if not sudo_user:
+ raise NoOperatorInfo
+ pwdentry = pwd.getpwnam(sudo_user)
+ else:
+ pwdentry = pwd.getpwuid(uid)
+ # XXX: error checking might be nice
+ # We follow the Ubuntu convention of gecos being a comma split field
+ # with the person's realname being the first entry.
+ return pwdentry.pw_gecos.split(",")[0], pwdentry.pw_name + "@" + socket.gethostname()
def get_operator_git():
"""
"""
return "%s <%s>" % get_operator_info()
-def get_operator_name():
+def get_operator_name_from_gssapi():
"""
Returns username of the person operating this script based
- off of the :envvar:`SSH_GSSAPI_NAME` environment variable. Throws
- :exc:`NoOperatorInfo` if environment variable is not available.
+ off of the :envvar:`SSH_GSSAPI_NAME` environment variable.
.. note::
environment variable.
"""
principal = os.getenv("SSH_GSSAPI_NAME")
- if not principal: raise NoOperatorInfo
+ if not principal:
+ return None
instance, _, _ = principal.partition("@")
if instance.endswith("/root"):
username, _, _ = principal.partition("/")
variables if applicable. Does nothing if :func:`get_dir_owner` fails.
"""
try:
+ # XXX: should check if the directory is in AFS, and if not, use
+ # a more traditional metric
lockername = get_dir_owner()
os.putenv("GIT_AUTHOR_NAME", "%s locker" % lockername)
os.putenv("GIT_AUTHOR_EMAIL", "%s@scripts.mit.edu" % lockername)