]> scripts.mit.edu Git - wizard.git/blob - wizard/util.py
Add comments on possible ways to fix theoretical 64KB buffer problem.
[wizard.git] / wizard / util.py
1 import os.path
2 import ldap
3
4 def get_exception_name(output):
5     """Reads the stderr output of another Python command and grabs the
6     fully qualified exception name"""
7     lines = output.split("\n")
8     for line in lines[1:]: # skip the "traceback" line
9         line = line.rstrip()
10         if line[0] == ' ': continue
11         if line[-1] == ":":
12             return line[:-1]
13         else:
14             return line
15
16 def get_dir_user(dir):
17     """Finds the username of the person who owns this directory, via LDAP.
18     Only works for directories under web_scripts"""
19     dir = os.path.realpath(dir)
20     homedir, _, _ = dir.partition("/web_scripts")
21     if homedir == dir: return None
22     con = ldap.initialize('ldap://scripts.mit.edu')
23     return con.search_s(
24             "ou=People,dc=scripts,dc=mit,dc=edu", # base
25             ldap.SCOPE_SUBTREE, # default
26             "homeDirectory=" + homedir, # search
27             ['uid'] # attr
28         )[0][1]["uid"][0]  # unwrap the result
29
30 def get_dir_url(dir):
31     """Finds the URL a path would correspond to in the filesystem"""
32     _, _, path = dir.partition("/web_scripts")
33     return "http://%s.scripts.mit.edu%s" % (get_dir_user(dir), path)
34