import os.path import ldap def get_exception_name(output): """Reads the stderr output of another Python command and grabs the fully qualified exception name""" lines = output.split("\n") for line in lines[1:]: # skip the "traceback" line line = line.rstrip() if line[0] == ' ': continue if line[-1] == ":": return line[:-1] else: return line def get_dir_user(dir): """Finds the username of the person who owns this directory, via LDAP. Only works for directories under web_scripts""" dir = os.path.realpath(dir) homedir, _, _ = dir.partition("/web_scripts") if homedir == dir: return None con = ldap.initialize('ldap://scripts.mit.edu') return con.search_s( "ou=People,dc=scripts,dc=mit,dc=edu", # base ldap.SCOPE_SUBTREE, # default "homeDirectory=" + homedir, # search ['uid'] # attr )[0][1]["uid"][0] # unwrap the result def get_dir_url(dir): """Finds the URL a path would correspond to in the filesystem""" _, _, path = dir.partition("/web_scripts") return "http://%s.scripts.mit.edu%s" % (get_dir_user(dir), path)