]> scripts.mit.edu Git - wizard.git/blob - wizard/scripts.py
Hookify research interface.
[wizard.git] / wizard / scripts.py
1 import os
2 import shlex
3 import errno
4 import logging
5 import urlparse
6
7 import wizard
8 from wizard import shell, util
9
10 def fill_url(dir, url=None):
11     if url:
12         return url
13
14     # hook hook
15
16     # try the directory
17     homedir, _, web_path = dir.partition("/web_scripts")
18     if web_path:
19         return urlparse.ParseResult(
20                 "http",
21                 util.get_dir_owner(homedir) + ".scripts.mit.edu",
22                 web_path.rstrip('/'),
23                 "", "", "")
24
25     # try the environment
26     host = os.getenv("WIZARD_WEB_HOST")
27     path = os.getenv("WIZARD_WEB_PATH")
28     if host is not None and path is not None:
29         return urlparse.ParseResult(
30                 "http",
31                 host,
32                 path.rstrip('/'),
33                 "", "", "")
34
35     return None
36
37 def get_quota_usage_and_limit(dir=None):
38     """
39     Returns a tuple (quota usage, quota limit).  Works only for scripts
40     servers.  Values are in KiB.  Returns ``(0, None)`` if we couldn't figure it out.
41     """
42     # XXX: The correct way is to implement Python modules implementing
43     # bindings for all the appropriate interfaces
44     def parse_last_quote(ret):
45         return ret.rstrip('\'').rpartition('\'')[2]
46     if dir is None:
47         dir = os.getcwd()
48     sh = shell.Shell()
49     try:
50         cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
51     except shell.CallError:
52         return (0, None)
53     except OSError as e:
54         if e.errno == errno.ENOENT:
55             return (0, None)
56         raise
57     mount = None
58     while dir:
59         try:
60             volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
61             break
62         except shell.CallError:
63             dir = os.path.dirname(dir)
64         except OSError as e:
65             if e.errno == errno.ENOENT:
66                 return (0, None)
67             raise
68     if not volume: return (0, None)
69     try:
70         result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
71     except OSError:
72         try:
73             result = sh.eval("/usr/sbin/vos", "examine", "-id", volume, "-cell", cell).splitlines()
74         except OSError:
75             return (0, None)
76     except shell.CallError:
77         return (0, None)
78     try:
79         usage = int(result[0].split()[3])
80         limit = int(result[3].split()[1]) # XXX: FRAGILE
81     except ValueError:
82         raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
83     return (usage, limit)
84
85 # XXX: Possibly in the wrong module
86 def get_disk_usage(dir=None, excluded_dir=".git"):
87     """
88     Recursively determines the disk usage of a directory, excluding
89     .git directories.  Value is in bytes.
90     """
91     if dir is None: dir = os.getcwd()
92     sum_sizes = 0
93     for root, _, files in os.walk(dir):
94         for name in files:
95             if not os.path.join(root, name).startswith(dir + excluded_dir):
96                 sum_sizes += os.path.getsize(os.path.join(root, name))
97     return sum_sizes
98 class QuotaParseError(wizard.Error):
99     def __init__(self, msg):
100         self.msg = msg
101     def __str__(self):
102         return """
103
104 ERROR: Could not parse quota. %s
105 """ % self.msg