4 from wizard import shell, util
6 def get_sql_credentials(vars=None):
8 Attempts to determine a user's MySQL credentials. They are
9 returned as a three-tuple (host, user, password).
12 host = os.getenv("WIZARD_MYSQL_HOST")
13 user = os.getenv("WIZARD_MYSQL_USER")
14 password = os.getenv("WIZARD_MYSQL_PASSWORD")
15 if host is not None and user is not None and password is not None:
16 return (host, user, password)
17 # XXX: this is very fragile
18 elif vars and "WIZARD_DBSERVER" in vars and "WIZARD_DBUSER" in vars and "WIZARD_DBPASSWORD" in vars:
19 return (shlex.split(vars[x])[0] for x in ("WIZARD_DBSERVER", "WIZARD_DBUSER", "WIZARD_DBPASSWORD"))
21 tuple = sh.eval("/mit/scripts/sql/bin/get-password").split()
28 def get_web_host_and_path(dir=None):
30 Attempts to determine webhost and path for the current directory
31 as it would be accessible from the web. Works only for scripts
32 servers. Returns a tuple web_host, web_path, or None if it failed.
34 # XXX: THIS CODE SUCKS
35 host = os.getenv("WIZARD_WEB_HOST")
36 path = os.getenv("WIZARD_WEB_PATH")
37 if host is not None and path is not None:
41 _, _, web_path = dir.partition("/web_scripts")
44 return (util.get_dir_owner(dir) + ".scripts.mit.edu", web_path)
46 def get_quota_usage_and_limit(dir=None):
48 Returns a tuple (quota usage, quota limit). Works only for scripts
49 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
51 # XXX: The correct way is to implement Python modules implementing
52 # bindings for all the appropriate interfaces
53 def parse_last_quote(ret):
54 return ret.rstrip('\'').rpartition('\'')[2]
59 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
60 except shell.CallError:
65 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
67 except shell.CallError:
68 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:
75 usage = int(result[0].split()[3])
76 limit = int(result[3].split()[1]) # XXX: FRAGILE
79 # XXX: Possibly in the wrong module
80 def get_disk_usage(dir=None):
82 Recursively determines the disk usage of a directory, excluding
83 .git directories. Value is in bytes.
85 if dir is None: dir = os.getcwd()
86 return int(shell.Shell().eval("du", "--exclude=.git", "-s", dir).split()[0])