#!/usr/bin/python
from sys import stdout, stdin, exit
import sys
import traceback

import os

lockFile = "/var/lock/socketLock"
c = ""

exists = os.path.isfile(lockFile)
if exists:
    stdout.write("socket is already connected.")
    stdout.flush()
    exit()
else:
    with open(lockFile, 'a'):
        os.utime(lockFile, None)
    # Store configuration file values

while True:
    s = stdin.readline().replace('\n','')  #  raw_input()
    if s == 'exit':
        os.remove(lockFile)
        exit()
    if s == 'go':
        description='source string'
        try:
            exec(c)
            stdout.flush()
            c = ""
        except SyntaxError as err:
            error_class = err.__class__.__name__
            detail = err.args[0]
            line_number = err.lineno
            stdout.write(("%s at line %d of %s: %s\n\n" % (error_class, line_number, description, detail)))
            stdout.flush()
            c = ""
        except Exception as err:
            error_class = err.__class__.__name__
            detail = err.args[0]
            cl, exc, tb = sys.exc_info()
            line_number = traceback.extract_tb(tb)[-1][1]
            stdout.write(("%s at line %d of %s: %s\n\n" % (error_class, line_number, description, detail)))
            stdout.flush()
            c = ""
    else:
        s.replace('print','stdout.write')
        c += s + '\n'
