X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/72177f26b9e2eb66c307b0ff15d4840903b434f6..db709749a3e3b1cba85a6869c87af925ae33d441:/wizard/scripts.py diff --git a/wizard/scripts.py b/wizard/scripts.py index 196da97..9092f26 100644 --- a/wizard/scripts.py +++ b/wizard/scripts.py @@ -1,13 +1,23 @@ +""" +This is ostensibly the place where Scripts specific code should live. +""" + import os import shlex import errno import logging import urlparse +import time +import errno import wizard from wizard import shell, util -def fill_url(dir, url=None): +def fill_url(dir, url=None, old_style=False): + """ + Attempts to determine the URL a directory would be web-accessible at. + If ``url`` is specified, automatically use it. + """ if url: return url @@ -16,11 +26,18 @@ def fill_url(dir, url=None): # try the directory homedir, _, web_path = dir.partition("/web_scripts") if web_path: - return urlparse.ParseResult( - "http", - util.get_dir_owner(homedir) + ".scripts.mit.edu", - web_path.rstrip('/'), - "", "", "") + if old_style: + return urlparse.ParseResult( + "http", + "scripts.mit.edu", + "/~" + util.get_dir_owner(homedir) + web_path.rstrip('/'), + "", "", "") + else: + return urlparse.ParseResult( + "http", + util.get_dir_owner(homedir) + ".scripts.mit.edu", + web_path.rstrip('/'), + "", "", "") # try the environment host = os.getenv("WIZARD_WEB_HOST") @@ -39,6 +56,18 @@ def get_quota_usage_and_limit(dir=None): Returns a tuple (quota usage, quota limit). Works only for scripts servers. Values are in KiB. Returns ``(0, None)`` if we couldn't figure it out. """ + end = 2 + # sometimes the volume is busy; so we try several times + for i in range(0, end + 1): + try: + return _get_quota_usage_and_limit(dir) + except QuotaParseError as e: + if i == end: + raise e + time.sleep(3) # give it a chance to unbusy + assert False # should not get here + +def _get_quota_usage_and_limit(dir=None): # XXX: The correct way is to implement Python modules implementing # bindings for all the appropriate interfaces def parse_last_quote(ret): @@ -93,9 +122,19 @@ def get_disk_usage(dir=None, excluded_dir=".git"): for root, _, files in os.walk(dir): for name in files: if not os.path.join(root, name).startswith(dir + excluded_dir): - sum_sizes += os.path.getsize(os.path.join(root, name)) + file = os.path.join(root, name) + try: + if os.path.islink(file): continue + sum_sizes += os.path.getsize(file) + except OSError as e: + if e.errno == errno.ENOENT: + logging.warning("%s disappeared before we could stat", file) + else: + raise return sum_sizes + class QuotaParseError(wizard.Error): + """Could not parse quota information.""" def __init__(self, msg): self.msg = msg def __str__(self):