9 from wizard import shell, util
11 def fill_url(dir, url=None):
18 homedir, _, web_path = dir.partition("/web_scripts")
20 return urlparse.ParseResult(
22 util.get_dir_owner(homedir) + ".scripts.mit.edu",
27 host = os.getenv("WIZARD_WEB_HOST")
28 path = os.getenv("WIZARD_WEB_PATH")
29 if host is not None and path is not None:
30 return urlparse.ParseResult(
38 def get_quota_usage_and_limit(dir=None):
40 Returns a tuple (quota usage, quota limit). Works only for scripts
41 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
44 # sometimes the volume is busy; so we try several times
47 return _get_quota_usage_and_limit(dir)
48 except QuotaParseError as e:
50 time.sleep(3) # give it a chance
53 def _get_quota_usage_and_limit(dir=None):
54 # XXX: The correct way is to implement Python modules implementing
55 # bindings for all the appropriate interfaces
56 def parse_last_quote(ret):
57 return ret.rstrip('\'').rpartition('\'')[2]
62 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
63 except shell.CallError:
66 if e.errno == errno.ENOENT:
72 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
74 except shell.CallError:
75 dir = os.path.dirname(dir)
77 if e.errno == errno.ENOENT:
80 if not volume: return (0, None)
82 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
85 result = sh.eval("/usr/sbin/vos", "examine", "-id", volume, "-cell", cell).splitlines()
88 except shell.CallError:
91 usage = int(result[0].split()[3])
92 limit = int(result[3].split()[1]) # XXX: FRAGILE
94 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
97 # XXX: Possibly in the wrong module
98 def get_disk_usage(dir=None, excluded_dir=".git"):
100 Recursively determines the disk usage of a directory, excluding
101 .git directories. Value is in bytes.
103 if dir is None: dir = os.getcwd()
105 for root, _, files in os.walk(dir):
107 if not os.path.join(root, name).startswith(dir + excluded_dir):
108 sum_sizes += os.path.getsize(os.path.join(root, name))
110 class QuotaParseError(wizard.Error):
111 def __init__(self, msg):
116 ERROR: Could not parse quota. %s