import os import shlex from wizard import shell, util def get_sql_credentials(vars=None): """ Attempts to determine a user's MySQL credentials. They are returned as a three-tuple (host, user, password). """ sh = shell.Shell() host = os.getenv("WIZARD_MYSQL_HOST") user = os.getenv("WIZARD_MYSQL_USER") password = os.getenv("WIZARD_MYSQL_PASSWORD") if host is not None and user is not None and password is not None: return (host, user, password) # XXX: this is very fragile elif vars and "WIZARD_DBSERVER" in vars and "WIZARD_DBUSER" in vars and "WIZARD_DBPASSWORD" in vars: return (shlex.split(vars[x])[0] for x in ("WIZARD_DBSERVER", "WIZARD_DBUSER", "WIZARD_DBPASSWORD")) try: tuple = sh.eval("/mit/scripts/sql/bin/get-password").split() if len(tuple) == 3: return tuple return None except CallError: return None def get_web_host_and_path(dir=None): """ Attempts to determine webhost and path for the current directory as it would be accessible from the web. Works only for scripts servers. Returns a tuple web_host, web_path, or None if it failed. """ # XXX: THIS CODE SUCKS host = os.getenv("WIZARD_WEB_HOST") path = os.getenv("WIZARD_WEB_PATH") if host is not None and path is not None: return (host, path) if not dir: dir = os.getcwd() _, _, web_path = dir.partition("/web_scripts") if not web_path: return None return (util.get_dir_owner(dir) + ".scripts.mit.edu", web_path)