#!/usr/bin/python3 import sys, os, subprocess, pwd, termcolor, time from importlib.machinery import SourceFileLoader from twilio.rest import TwilioRestClient SANDBOXPATH="/tmp/hankgit" INFOFILE=".hankgit" RESOLVEDFILE=".hankgitfixed" def notice(x): sys.stderr.write(termcolor.colored(x, 'red', attrs=['bold'])) sys.stderr.write("\n") sys.stderr.flush() if len(sys.argv) > 1 and sys.argv[1] == "fix": os.chdir(SANDBOXPATH) f = open(os.path.join(SANDBOXPATH, INFOFILE)) firstname = f.readline().strip() cmdstr = f.readline().strip() notice("%s encountered a merge conflict when trying to run '%s'" % (firstname, cmdstr)) notice("Help fix the conflict!") notice("Exit this shell when done.") os.system("/bin/bash") f = open(os.path.join(SANDBOXPATH, RESOLVEDFILE), "w") f.close() notice("Thanks!") else: # Who am I? firstname = pwd.getpwuid(os.getuid())[4].split(" ")[0] # Run git proc = subprocess.Popen(["/usr/bin/git"] + sys.argv[1:], stdout=subprocess.PIPE) # Detect conflict conflict = False for line in iter(proc.stdout.readline,''): if line == b'': break sys.stdout.buffer.write(line) sys.stdout.flush() if line.startswith(b"CONFLICT"): conflict = True if conflict: if os.path.exists(SANDBOXPATH): notice("Another conflict is being merged!") sys.exit(1) gitbase = bytes.decode(subprocess.check_output(["git rev-parse --show-toplevel"], shell=True)).strip() subprocess.check_call(["/bin/cp", "-r", gitbase, SANDBOXPATH]) subprocess.check_call(["/bin/chmod", "-R", "a+rw", SANDBOXPATH]) subprocess.check_call(["/usr/bin/find", SANDBOXPATH, "-type", "d", "-exec", "chmod", "6777", "{}", "+"]) infofile = open(os.path.join(SANDBOXPATH, INFOFILE), "w") infofile.write(firstname + "\n") infofile.write(" ".join(sys.argv[0:]) + "\n") infofile.close() notice("Fixing your merge conflict...") twinfo = SourceFileLoader("twilio", "/homes/sys/drkp/twilio.py").load_module() client = TwilioRestClient(twinfo.SID, twinfo.AUTHTOKEN) call = client.calls.create(from_=twinfo.NUMBER, to="+12066851963", #to="+14157131141", url="http://alpaca.cs.washington.edu/call.xml") while not os.path.exists(os.path.join(SANDBOXPATH, RESOLVEDFILE)): time.sleep(1) os.unlink(os.path.join(SANDBOXPATH, INFOFILE)) os.unlink(os.path.join(SANDBOXPATH, RESOLVEDFILE)) subprocess.check_call(["/usr/bin/rsync", "-r", SANDBOXPATH+"/", gitbase]) subprocess.check_call(["/bin/rm", "-rf", SANDBOXPATH]) notice("All fixed!")