Skip to content

Instantly share code, notes, and snippets.

@ana-balica
Last active December 12, 2015 10:18
Show Gist options
  • Save ana-balica/4757639 to your computer and use it in GitHub Desktop.
Save ana-balica/4757639 to your computer and use it in GitHub Desktop.
MIDPS Labwork N#2 Command Line Interface (CLI). Scripting
import smtplib
from subprocess import call
from getpass import getpass
from email.header import Header
from email.mime.text import MIMEText
# global variables
script_names = ["C", "C++", "Java", "Python", "Ruby"]
paths = ["c/", "cpp/", "java/", "python/", "ruby/"]
scripts = [
"gcc c/hello.c -o c/hello && ./c/hello",
"g++ cpp/hello.cpp -o cpp/hello && ./cpp/hello",
"javac java/hello.java && java -classpath java/ HelloWorld",
"python python/hello.py",
"ruby ruby/hello.rb"
]
# Set up credentials, email body/subject
# and send it to the destination email
def send_error_report(script_name, error):
login, password = 'ana.balica@gmail.com', \
getpass('ana.balica@gmail.com password:')
message = '''
An error with code %s occurred in an attempt to compile/run
the %s program.''' % (error, script_name)
msg = MIMEText(message, _charset='utf-8')
msg['Subject'] = Header('Error report', 'utf-8')
msg['From'] = login
msg['To'] = login
s = smtplib.SMTP_SSL('smtp.gmail.com', 465, timeout=10)
s.set_debuglevel(0)
try:
s.login(login, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())
finally:
s.quit()
# create a git repo
def init_git():
call('git init', shell=True)
call('git add script.py', shell=True)
call('git commit -m "initial commit"', shell=True)
call('git checkout -b development', shell=True)
def git_merge():
call('git checkout master', shell=True)
call('git merge development', shell=True)
# compiling and running the scripts
def run_scripts():
for i, script in enumerate(scripts):
print "%s program:" % script_names[i]
result = call(script, shell=True)
if result == 0:
print "The %s program compiled and run with success" \
% script_names[i]
call('git add ' + paths[i], shell=True)
call('git commit -m "success message"', shell=True)
else:
send_error_report(script_names[i], result)
init_git()
run_scripts()
git_merge()
#!/bin/bash
echo "This script will run all HelloWorld scripts."
SCRIPT_NAMES=(C C++ Java Python Ruby)
# This function compiles .c file and executes the compiled program
# $1 represents the path to the c file without .c
# compile_and_run()
# {
# gcc $1.c -o $1
# ./$1
# }
C="`gcc c/hello.c -o c/hello` ./c/hello"
CPP="`g++ cpp/hello.cpp -o cpp/hello` ./cpp/hello"
JAVA="`javac java/hello.java` java -classpath java/ HelloWorld"
PYTHON="python python/hello.py"
RUBY="ruby ruby/hello.rb"
SCRIPTS=( eval $C eval $CPP eval $JAVA $PYTHON $RUBY )
for SCRIPT in "${SCRIPTS[@]}"
do
$SCRIPT
RESULT = $?
if [ $RESULT -eq 0 ]:then
echo "The script executed with success."
else
echo "The execution of script raised an error."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment