]> scripts.mit.edu Git - wizard.git/blob - wizard/scripts.py
Implement 'wizard blacklist', tweaks.
[wizard.git] / wizard / scripts.py
1 import os
2 import shlex
3
4 from wizard import shell, util
5
6 def get_sql_credentials(vars=None):
7     """
8     Attempts to determine a user's MySQL credentials.  They are
9     returned as a three-tuple (host, user, password).
10     """
11     sh = shell.Shell()
12     host = os.getenv("WIZARD_MYSQL_HOST")
13     user = os.getenv("WIZARD_MYSQL_USER")
14     password = os.getenv("WIZARD_MYSQL_PASSWORD")
15     if host is not None and user is not None and password is not None:
16         return (host, user, password)
17     # XXX: this is very fragile
18     elif vars and "WIZARD_DBSERVER" in vars and "WIZARD_DBUSER" in vars and "WIZARD_DBPASSWORD" in vars:
19         return (shlex.split(vars[x])[0] for x in ("WIZARD_DBSERVER", "WIZARD_DBUSER", "WIZARD_DBPASSWORD"))
20     try:
21         tuple = sh.eval("/mit/scripts/sql/bin/get-password").split()
22         if len(tuple) == 3:
23             return tuple
24         return None
25     except CallError:
26         return None
27
28 def get_web_host_and_path(dir=None):
29     """
30     Attempts to determine webhost and path for the current directory
31     as it would be accessible from the web.  Works only for scripts
32     servers.  Returns a tuple web_host, web_path, or None if it failed.
33     """
34     # XXX: THIS CODE SUCKS
35     host = os.getenv("WIZARD_WEB_HOST")
36     path = os.getenv("WIZARD_WEB_PATH")
37     if host is not None and path is not None:
38         return (host, path)
39     if not dir:
40         dir = os.getcwd()
41     _, _, web_path = dir.partition("/web_scripts")
42     if not web_path:
43         return None
44     return (util.get_dir_owner(dir) + ".scripts.mit.edu", web_path)
45