]> scripts.mit.edu Git - wizard.git/blob - wizard/scripts.py
Implement disk quota checking.
[wizard.git] / wizard / scripts.py
1 import os
2 import shlex
3
4 from wizard import shell, util
5
6 def get_sql_credentials(vars=None):
7     """
8     Attempts to determine a user's MySQL credentials.  They are
9     returned as a three-tuple (host, user, password).
10     """
11     sh = shell.Shell()
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"))
20     try:
21         tuple = sh.eval("/mit/scripts/sql/bin/get-password").split()
22         if len(tuple) == 3:
23             return tuple
24         return None
25     except CallError:
26         return None
27
28 def get_web_host_and_path(dir=None):
29     """
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.
33     """
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:
38         return (host, path)
39     if not dir:
40         dir = os.getcwd()
41     _, _, web_path = dir.partition("/web_scripts")
42     if not web_path:
43         return None
44     return (util.get_dir_owner(dir) + ".scripts.mit.edu", web_path)
45
46 def get_quota_usage_and_limit(dir=None):
47     """
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.
50     """
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]
55     if dir is None:
56         dir = os.getcwd()
57     sh = shell.Shell()
58     try:
59         cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
60     except shell.CallError:
61         return (0, None)
62     mount = None
63     while dir:
64         try:
65             volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
66             break
67         except shell.CallError:
68             dir = os.path.dirname(dir)
69     print volume
70     if not volume: return (0, None)
71     try:
72         result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
73     except shell.CallError:
74         return (0, None)
75     usage = int(result[0].split()[3])
76     limit = int(result[3].split()[1]) # XXX: FRAGILE
77     return (usage, limit)
78
79 # XXX: Possibly in the wrong module
80 def get_disk_usage(dir=None):
81     """
82     Recursively determines the disk usage of a directory, excluding
83     .git directories.  Value is in bytes.
84     """
85     if dir is None: dir = os.getcwd()
86     return int(shell.Shell().eval("du", "--exclude=.git", "-s", dir).split()[0])
87