2 This is ostensibly the place where Scripts specific code should live.
14 from wizard import shell, util
16 def fill_url(dir, url=None):
18 Attempts to determine the URL a directory would be web-accessible at.
19 If ``url`` is specified, automatically use it.
27 homedir, _, web_path = dir.partition("/web_scripts")
29 return urlparse.ParseResult(
31 util.get_dir_owner(homedir) + ".scripts.mit.edu",
36 host = os.getenv("WIZARD_WEB_HOST")
37 path = os.getenv("WIZARD_WEB_PATH")
38 if host is not None and path is not None:
39 return urlparse.ParseResult(
47 def get_quota_usage_and_limit(dir=None):
49 Returns a tuple (quota usage, quota limit). Works only for scripts
50 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
53 # sometimes the volume is busy; so we try several times
54 for i in range(0, end + 1):
56 return _get_quota_usage_and_limit(dir)
57 except QuotaParseError as e:
60 time.sleep(3) # give it a chance to unbusy
61 assert False # should not get here
63 def _get_quota_usage_and_limit(dir=None):
64 # XXX: The correct way is to implement Python modules implementing
65 # bindings for all the appropriate interfaces
66 def parse_last_quote(ret):
67 return ret.rstrip('\'').rpartition('\'')[2]
72 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
73 except shell.CallError:
76 if e.errno == errno.ENOENT:
82 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
84 except shell.CallError:
85 dir = os.path.dirname(dir)
87 if e.errno == errno.ENOENT:
90 if not volume: return (0, None)
92 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
95 result = sh.eval("/usr/sbin/vos", "examine", "-id", volume, "-cell", cell).splitlines()
98 except shell.CallError:
101 usage = int(result[0].split()[3])
102 limit = int(result[3].split()[1]) # XXX: FRAGILE
104 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
105 return (usage, limit)
107 # XXX: Possibly in the wrong module
108 def get_disk_usage(dir=None, excluded_dir=".git"):
110 Recursively determines the disk usage of a directory, excluding
111 .git directories. Value is in bytes.
113 if dir is None: dir = os.getcwd()
115 for root, _, files in os.walk(dir):
117 if not os.path.join(root, name).startswith(dir + excluded_dir):
118 file = os.path.join(root, name)
120 sum_sizes += os.path.getsize(file)
123 logging.warning("%s disappeared before we could stat", file)
128 class QuotaParseError(wizard.Error):
129 """Could not parse quota information."""
130 def __init__(self, msg):
135 ERROR: Could not parse quota. %s