9 class ChangeDirectory(object):
10 """Context for temporarily changing directory"""
11 def __init__(self, dir):
15 self.olddir = os.getcwd()
17 def __exit__(self, *args):
20 def get_exception_name(output):
21 """Reads the stderr output of another Python command and grabs the
22 fully qualified exception name"""
23 lines = output.split("\n")
24 for line in lines[1:]: # skip the "traceback" line
26 if line[0] == ' ': continue
33 """Finds the uid of the person who owns this directory."""
34 return os.stat(dir).st_uid
36 def get_dir_owner(dir = "."):
37 """Finds the name of the locker this directory is in."""
38 return pwd.getpwuid(get_dir_uid(dir)).pw_name
41 """Returns the commit ID of the current Wizard install."""
42 wizard_git = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), ".git")
43 return subprocess.Popen(["git", "--git-dir=" + wizard_git, "rev-parse", "HEAD"], stdout=subprocess.PIPE).communicate()[0].rstrip()
45 def get_operator_info():
46 """Returns tuple of (realname, email) of person who is operating
47 this script, as told to use by the Kerberos principal name.
48 Useful for commit messages."""
49 username = get_operator_name()
50 hesinfo = subprocess.Popen(["hesinfo", username, "passwd"],stdout=subprocess.PIPE).communicate()[0]
51 fields = hesinfo.partition(",")[0]
52 realname = fields.rpartition(":")[2]
53 return realname, username + "@mit.edu"
55 def get_operator_git():
56 """Returns Real Name <username@mit.edu> suitable for use in
57 Git Something-by: string."""
58 return "%s <%s>" % get_operator_info()
60 def get_operator_name():
61 """Returns username of the person operating this script."""
62 principal = os.getenv("SSH_GSSAPI_NAME")
63 if not principal: raise NoOperatorInfo()
64 instance, _, _ = principal.partition("@")
65 if instance.endswith("/root"):
66 username, _, _ = principal.partition("/")
71 def set_operator_env():
73 op_realname, op_email = get_operator_info()
74 os.putenv("GIT_COMMITTER_NAME", op_realname)
75 os.putenv("GIT_COMMITTER_EMAIL", op_email)
76 except NoOperatorInfo:
81 lockername = get_dir_owner()
82 os.putenv("GIT_AUTHOR_NAME", "%s locker" % lockername)
83 os.putenv("GIT_AUTHOR_EMAIL", "%s@scripts.mit.edu" % lockername)
92 return "\n".join(["Wizard-revision: %s" % get_revision()
93 ,"Wizard-args: %s" % " ".join(sys.argv)
96 class Error(wizard.Error):
99 class NoOperatorInfo(Error):