7 from wizard import shell, util
9 def get_sql_credentials(vars=None):
11 Attempts to determine a user's MySQL credentials. They are
12 returned as a three-tuple (host, user, password).
15 host = os.getenv("WIZARD_MYSQL_HOST")
16 user = os.getenv("WIZARD_MYSQL_USER")
17 password = os.getenv("WIZARD_MYSQL_PASSWORD")
18 if host is not None and user is not None and password is not None:
19 return (host, user, password)
20 # XXX: this is very fragile
21 elif vars and "WIZARD_DBSERVER" in vars and "WIZARD_DBUSER" in vars and "WIZARD_DBPASSWORD" in vars:
22 return (shlex.split(vars[x])[0] for x in ("WIZARD_DBSERVER", "WIZARD_DBUSER", "WIZARD_DBPASSWORD"))
24 tuple = sh.eval("/mit/scripts/sql/bin/get-password").split()
28 except shell.CallError:
31 def get_web_host_and_path(dir=None):
33 Attempts to determine webhost and path for the current directory
34 as it would be accessible from the web. Works only for scripts
35 servers. Returns a tuple web_host, web_path, or None if it failed.
37 # XXX: THIS CODE SUCKS
38 host = os.getenv("WIZARD_WEB_HOST")
39 path = os.getenv("WIZARD_WEB_PATH")
40 if host is not None and path is not None:
41 return (host, path.rstrip('/'))
44 # XXX: possible security risk?
45 homedir, _, web_path = dir.partition("/web_scripts")
48 return (util.get_dir_owner(homedir) + ".scripts.mit.edu", web_path.rstrip('/'))
50 def get_quota_usage_and_limit(dir=None):
52 Returns a tuple (quota usage, quota limit). Works only for scripts
53 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
55 # XXX: The correct way is to implement Python modules implementing
56 # bindings for all the appropriate interfaces
57 def parse_last_quote(ret):
58 return ret.rstrip('\'').rpartition('\'')[2]
63 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
64 except shell.CallError:
67 if e.errno == errno.ENOENT:
73 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
75 except shell.CallError:
76 dir = os.path.dirname(dir)
78 if e.errno == errno.ENOENT:
81 if not volume: return (0, None)
83 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
84 except shell.CallError:
87 usage = int(result[0].split()[3])
88 limit = int(result[3].split()[1]) # XXX: FRAGILE
90 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
93 # XXX: Possibly in the wrong module
94 def get_disk_usage(dir=None, excluded_dir=".git"):
96 Recursively determines the disk usage of a directory, excluding
97 .git directories. Value is in bytes.
99 if dir is None: dir = os.getcwd()
101 for root, _, files in os.walk(dir):
103 if not os.path.join(root, name).startswith(dir + excluded_dir):
104 sum_sizes += os.path.getsize(os.path.join(root, name))
106 class QuotaParseError(wizard.Error):
107 def __init__(self, msg):
112 ERROR: Could not parse quota. %s