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, old_style=False):
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")
30 return urlparse.ParseResult(
33 "/~" + util.get_dir_owner(homedir) + web_path.rstrip('/'),
36 return urlparse.ParseResult(
38 util.get_dir_owner(homedir) + ".scripts.mit.edu",
43 host = os.getenv("WIZARD_WEB_HOST")
44 path = os.getenv("WIZARD_WEB_PATH")
45 if host is not None and path is not None:
46 return urlparse.ParseResult(
54 def get_quota_usage_and_limit(dir=None):
56 Returns a tuple (quota usage, quota limit). Works only for scripts
57 servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out.
60 # sometimes the volume is busy; so we try several times
61 for i in range(0, end + 1):
63 return _get_quota_usage_and_limit(dir)
64 except QuotaParseError as e:
67 time.sleep(3) # give it a chance to unbusy
68 assert False # should not get here
70 def _get_quota_usage_and_limit(dir=None):
71 # XXX: The correct way is to implement Python modules implementing
72 # bindings for all the appropriate interfaces
73 def parse_last_quote(ret):
74 return ret.rstrip('\'').rpartition('\'')[2]
79 cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
80 except shell.CallError:
83 if e.errno == errno.ENOENT:
89 volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
91 except shell.CallError:
92 dir = os.path.dirname(dir)
94 if e.errno == errno.ENOENT:
97 if not volume: return (0, None)
99 result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
102 result = sh.eval("/usr/sbin/vos", "examine", "-id", volume, "-cell", cell).splitlines()
105 except shell.CallError:
108 usage = int(result[0].split()[3])
109 limit = int(result[3].split()[1]) # XXX: FRAGILE
111 raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
112 return (usage, limit)
114 # XXX: Possibly in the wrong module
115 def get_disk_usage(dir=None, excluded_dir=".git"):
117 Recursively determines the disk usage of a directory, excluding
118 .git directories. Value is in bytes.
120 if dir is None: dir = os.getcwd()
122 for root, _, files in os.walk(dir):
124 if not os.path.join(root, name).startswith(dir + excluded_dir):
125 file = os.path.join(root, name)
127 if os.path.islink(file): continue
128 sum_sizes += os.path.getsize(file)
130 if e.errno == errno.ENOENT:
131 logging.warning("%s disappeared before we could stat", file)
136 class QuotaParseError(wizard.Error):
137 """Could not parse quota information."""
138 def __init__(self, msg):
143 ERROR: Could not parse quota. %s