+"""
+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
# 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")
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):
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):