7 from wizard import shell, util
9 def get_web_host_and_path(dir=None):
11 Attempts to determine webhost and path for the current directory
12 as it would be accessible from the web. Works only for scripts
13 servers. Returns a tuple web_host, web_path, or None if it failed.
15 # XXX: THIS CODE SUCKS
16 host = os.getenv("WIZARD_WEB_HOST")
17 path = os.getenv("WIZARD_WEB_PATH")
18 if host is not None and path is not None:
19 return (host, path.rstrip('/'))
22 # XXX: possible security risk?
23 homedir, _, web_path = dir.partition("/web_scripts")
26 return (util.get_dir_owner(homedir) + ".scripts.mit.edu", web_path.rstrip('/'))
28 def get_quota_usage_and_limit(dir=None):
30 Returns a tuple (quota usage, quota limit). Works only for scripts
31 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
33 # XXX: The correct way is to implement Python modules implementing
34 # bindings for all the appropriate interfaces
35 def parse_last_quote(ret):
36 return ret.rstrip('\'').rpartition('\'')[2]
41 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
42 except shell.CallError:
45 if e.errno == errno.ENOENT:
51 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
53 except shell.CallError:
54 dir = os.path.dirname(dir)
56 if e.errno == errno.ENOENT:
59 if not volume: return (0, None)
61 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
62 except shell.CallError:
65 usage = int(result[0].split()[3])
66 limit = int(result[3].split()[1]) # XXX: FRAGILE
68 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
71 # XXX: Possibly in the wrong module
72 def get_disk_usage(dir=None, excluded_dir=".git"):
74 Recursively determines the disk usage of a directory, excluding
75 .git directories. Value is in bytes.
77 if dir is None: dir = os.getcwd()
79 for root, _, files in os.walk(dir):
81 if not os.path.join(root, name).startswith(dir + excluded_dir):
82 sum_sizes += os.path.getsize(os.path.join(root, name))
84 class QuotaParseError(wizard.Error):
85 def __init__(self, msg):
90 ERROR: Could not parse quota. %s