Skip to content

Instantly share code, notes, and snippets.

@bitoffdev
Created December 17, 2017 01:11
Show Gist options
  • Save bitoffdev/cfebbb6573873c8efbac8e0ef7f4755a to your computer and use it in GitHub Desktop.
Save bitoffdev/cfebbb6573873c8efbac8e0ef7f4755a to your computer and use it in GitHub Desktop.
Save the files submitted to try in the current directory.
#!/usr/bin/env python3
"""
Save the files submitted to try in the current directory.
Try guide: https://cs.rit.edu/~cs1/Misc/try.html
Usage:
[python3] try_capture.py instructor-account assignment-code
:author: Elliot Miller
:website: www.bitoffdev.com
"""
import os, re
from sys import argv
# exit if the number of arguments is incorrect
if len(argv) != 3:
print("Incorrect number of arguments.")
print("Usage: python3 try_capture.py instructor-account assignment-code")
raise SystemExit()
# execute try command
try_proc = os.popen("echo y | try -q {} {}".format(argv[1], argv[2]))
try_text = try_proc.readlines()
try_status = try_proc.close()
# exit if try responded with a non-zero exit code
if try_status:
raise SystemExit()
# parse through the try output
fname = ""
fdata = ""
fname_regex = re.compile(r'\={10} ([a-zA-Z1-9\-\.]*) \={10}')
fend_regex = re.compile(r'Press RETURN or ENTER to continue')
for line in try_text:
# check for a file starting
match = fname_regex.match(line)
if match:
fname = match.group(1)
fdata = ""
continue
# check for a file ending
match = fend_regex.match(line)
if match:
with open(fname, "w") as fh:
fh.write(fdata)
continue
# otherwise, append the line to fdata
fdata += line
# add a newline to compensate for try output
print("\r" + 60 * " " + "\r",end="")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment