X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/a8b52be70700da2453a925a8f4cf7523e713aa92..10fea9a7ddab6a654922514b13b135772cc98a01:/wizard/util.py diff --git a/wizard/util.py b/wizard/util.py index 876f8a9..8f1d1ba 100644 --- a/wizard/util.py +++ b/wizard/util.py @@ -25,6 +25,25 @@ import string import wizard from wizard import user +def boolish(val): + """ + Parse the contents of an environment variable as a boolean. + This recognizes more values as ``False`` than :func:`bool` would. + + >>> boolish("0") + False + >>> boolish("no") + False + >>> boolish("1") + True + """ + try: + return bool(int(val)) + except (ValueError, TypeError): + if val == "No" or val == "no" or val == "false" or val == "False": + return False + return bool(val) + class ChangeDirectory(object): """ Context for temporarily changing the working directory. @@ -233,6 +252,8 @@ def set_operator_env(): if op is None: return info = user.pwnam(op) + if not info.realname: + return os.putenv("GIT_COMMITTER_NAME", info.realname) os.putenv("GIT_COMMITTER_EMAIL", info.email) @@ -245,6 +266,8 @@ def set_author_env(): info = user.passwd() if info is None: return + if not info.realname: + return os.putenv("GIT_AUTHOR_NAME", "%s" % info.realname) os.putenv("GIT_AUTHOR_EMAIL", "%s" % info.email) @@ -349,6 +372,22 @@ def random_key(length=30): """Generates a random alphanumeric key of ``length`` size.""" return ''.join(random.choice(string.letters + string.digits) for i in xrange(length)) +def truncate(version): + """Truncates the Scripts specific version number.""" + return str(version).partition('-scripts')[0] + +def init_wizard_dir(): + """ + Generates a .wizard directory and initializes it with some common + files. This operation is idempotent. + """ + # no harm in doing this repeatedly + wizard_dir = ".wizard" + if not os.path.isdir(wizard_dir): + os.mkdir(wizard_dir) + open(os.path.join(wizard_dir, ".htaccess"), "w").write("Deny from all\n") + open(os.path.join(wizard_dir, ".gitignore"), "w").write("*\n") + class NoOperatorInfo(wizard.Error): """No information could be found about the operator from Kerberos.""" pass