5 from wizard import shell, util
7 def get_sql_credentials(vars=None):
9 Attempts to determine a user's MySQL credentials. They are
10 returned as a three-tuple (host, user, password).
13 host = os.getenv("WIZARD_MYSQL_HOST")
14 user = os.getenv("WIZARD_MYSQL_USER")
15 password = os.getenv("WIZARD_MYSQL_PASSWORD")
16 if host is not None and user is not None and password is not None:
17 return (host, user, password)
18 # XXX: this is very fragile
19 elif vars and "WIZARD_DBSERVER" in vars and "WIZARD_DBUSER" in vars and "WIZARD_DBPASSWORD" in vars:
20 return (shlex.split(vars[x])[0] for x in ("WIZARD_DBSERVER", "WIZARD_DBUSER", "WIZARD_DBPASSWORD"))
22 tuple = sh.eval("/mit/scripts/sql/bin/get-password").split()
26 except shell.CallError:
29 def get_web_host_and_path(dir=None):
31 Attempts to determine webhost and path for the current directory
32 as it would be accessible from the web. Works only for scripts
33 servers. Returns a tuple web_host, web_path, or None if it failed.
35 # XXX: THIS CODE SUCKS
36 host = os.getenv("WIZARD_WEB_HOST")
37 path = os.getenv("WIZARD_WEB_PATH")
38 if host is not None and path is not None:
42 _, _, web_path = dir.partition("/web_scripts")
45 return (util.get_dir_owner(dir) + ".scripts.mit.edu", web_path)
47 def get_quota_usage_and_limit(dir=None):
49 Returns a tuple (quota usage, quota limit). Works only for scripts
50 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
52 # XXX: The correct way is to implement Python modules implementing
53 # bindings for all the appropriate interfaces
54 def parse_last_quote(ret):
55 return ret.rstrip('\'').rpartition('\'')[2]
60 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
61 except shell.CallError:
66 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
68 except shell.CallError:
69 dir = os.path.dirname(dir)
70 if not volume: return (0, None)
72 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
73 except shell.CallError:
76 usage = int(result[0].split()[3])
77 limit = int(result[3].split()[1]) # XXX: FRAGILE
79 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
82 # XXX: Possibly in the wrong module
83 def get_disk_usage(dir=None):
85 Recursively determines the disk usage of a directory, excluding
86 .git directories. Value is in bytes.
88 if dir is None: dir = os.getcwd()
89 return int(shell.Shell().eval("du", "--exclude=.git", "-s", dir).split()[0])
91 class QuotaParseError(wizard.Error):
92 def __init__(self, msg):
97 ERROR: Could not parse quota. %s