10 from wizard import shell, util
12 def fill_url(dir, url=None):
19 homedir, _, web_path = dir.partition("/web_scripts")
21 return urlparse.ParseResult(
23 util.get_dir_owner(homedir) + ".scripts.mit.edu",
28 host = os.getenv("WIZARD_WEB_HOST")
29 path = os.getenv("WIZARD_WEB_PATH")
30 if host is not None and path is not None:
31 return urlparse.ParseResult(
39 def get_quota_usage_and_limit(dir=None):
41 Returns a tuple (quota usage, quota limit). Works only for scripts
42 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
45 # sometimes the volume is busy; so we try several times
46 for i in range(0, end + 1):
48 return _get_quota_usage_and_limit(dir)
49 except QuotaParseError as e:
52 time.sleep(3) # give it a chance to unbusy
53 assert False # should not get here
55 def _get_quota_usage_and_limit(dir=None):
56 # XXX: The correct way is to implement Python modules implementing
57 # bindings for all the appropriate interfaces
58 def parse_last_quote(ret):
59 return ret.rstrip('\'').rpartition('\'')[2]
64 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
65 except shell.CallError:
68 if e.errno == errno.ENOENT:
74 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
76 except shell.CallError:
77 dir = os.path.dirname(dir)
79 if e.errno == errno.ENOENT:
82 if not volume: return (0, None)
84 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
87 result = sh.eval("/usr/sbin/vos", "examine", "-id", volume, "-cell", cell).splitlines()
90 except shell.CallError:
93 usage = int(result[0].split()[3])
94 limit = int(result[3].split()[1]) # XXX: FRAGILE
96 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
99 # XXX: Possibly in the wrong module
100 def get_disk_usage(dir=None, excluded_dir=".git"):
102 Recursively determines the disk usage of a directory, excluding
103 .git directories. Value is in bytes.
105 if dir is None: dir = os.getcwd()
107 for root, _, files in os.walk(dir):
109 if not os.path.join(root, name).startswith(dir + excluded_dir):
110 file = os.path.join(root, name)
112 sum_sizes += os.path.getsize(file)
115 logging.warning("%s disappeared before we could stat", file)
119 class QuotaParseError(wizard.Error):
120 def __init__(self, msg):
125 ERROR: Could not parse quota. %s