Skip to content

Commit

Permalink
Merge pull request #128 from cs50/halt-rebased
Browse files Browse the repository at this point in the history
Solve halting problem
  • Loading branch information
Kareem Zidane authored Aug 3, 2017
2 parents c095608 + 460d5c0 commit c51e7cb
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions submit50.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,22 @@ def authenticate(org):
pass
authenticate.SOCKET = os.path.join(cache, ORG)

# check cache, then config for credentials
credentials = run("git -c credential.helper='cache --socket {}' credential fill".format(authenticate.SOCKET),
lines=[""]*3,
quiet=True)
run("git credential-cache --socket {} exit".format(authenticate.SOCKET))
matches = re.search("^username=([^\r]+)\r\npassword=([^\r]+)\r?$", credentials, re.MULTILINE)
if matches:
username = matches.group(1)
password = matches.group(2)
spawn = pexpect.spawn if sys.version_info < (3, 0) else pexpect.spawnu
child = spawn("git -c credential.helper='cache --socket {}' credential fill".format(authenticate.SOCKET))
child.sendline("")

if child.expect(["Username:", pexpect.EOF]):
# Credentials are already cached
clear_credentials()
username, password = re.search("username=([^\r]+)\r\npassword=([^\r]+)", child.before, re.MULTILINE).groups()
else:
# No cached credentials found
try:
username = run("git config --global credential.https://github.com/submit50.username")
except:
except Error:
username = None
password = None
child.close()

def rlinput(prompt, prefill=""):
"""
Expand Down Expand Up @@ -247,6 +248,16 @@ def rlinput(prompt, prefill=""):
authenticate.SOCKET = None


def clear_credentials():
"""Clear git credential cache """
run("git credential-cache --socket {} exit".format(authenticate.SOCKET))
# OSX will sometimes store git credentials in the keyring. Try to remove them
try:
run("git credential-osxkeychain erase", lines=["host=github.com", "protocol=https", ""])
except Error:
pass


def cprint(text="", color=None, on_color=None, attrs=None, **kwargs):
"""Colorizes text (and wraps to terminal's width)."""

Expand Down Expand Up @@ -277,32 +288,31 @@ def excepthook(type, value, tb):
cprint(_("Sorry, something's wrong! Let [email protected] know!"), "yellow")
if authenticate.SOCKET: # not set when using SSH
try:
run("git credential-cache --socket {} exit".format(authenticate.SOCKET))
clear_credentials()
except Exception:
pass
cprint(_("Submission cancelled."), "red")

cprint(_("Submission cancelled."), "red")

sys.excepthook = excepthook


def handler(number, frame):
"""Handle SIGINT."""
os.system("stty sane") # in case signalled from input_with_prefill
os.system("stty sane 2> {}".format(os.devnull)) # in case signalled from input_with_prefill
if progress.progressing:
progress(False)
else:
cprint()
try:
run("git credential-cache --socket {} exit".format(authenticate.SOCKET))
clear_credentials()
except Exception:
pass
teardown()
cprint(_("Submission cancelled."), "red")
os._exit(0)


def run(command, cwd=None, env=None, lines=[], password=None, quiet=False):
def run(command, cwd=None, env=None, lines=[], password=None, quiet=False, timeout=None):
"""Run a command."""

# echo command
Expand All @@ -323,9 +333,9 @@ def run(command, cwd=None, env=None, lines=[], password=None, quiet=False):

# spawn command
if sys.version_info < (3, 0):
child = pexpect.spawn(command, cwd=cwd, env=env, ignore_sighup=True, timeout=None)
child = pexpect.spawn(command, cwd=cwd, env=env, ignore_sighup=True, timeout=timeout)
else:
child = pexpect.spawnu(command, cwd=cwd, encoding="utf-8", env=env, ignore_sighup=True, timeout=None)
child = pexpect.spawnu(command, cwd=cwd, encoding="utf-8", env=env, ignore_sighup=True, timeout=timeout)

# send output of command to stdout only if run with --verbose (and not quieted by caller)
if run.verbose and not quiet:
Expand Down

0 comments on commit c51e7cb

Please sign in to comment.