<commit_before>
import os
import sys
import pickle
import zipfile
import threading

PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

from simple_host_target.definition import ResultWrapper, ExecutorWrapper, HOST_PIPEIN_NAME,\
                        HOST_PIPEOUT_NAME

def get_serialized_execution_wrapper():
    ba = None
    with open("program.zip", "rb") as fn:
        ba = fn.read()

    wrapper = ExecutorWrapper(ba, loader_scripts)
    serialized_executor_wrapper = pickle.dumps(wrapper)
    return serialized_executor_wrapper

def project_sender():
    print("[Sender] Press s + <Enter> to send a task !")
    if not os.path.exists(HOST_PIPEIN_NAME):
        os.mkfifo(HOST_PIPEIN_NAME)

    try:
        recv_thread = None
        for line in sys.stdin:
            if "s" in line and recv_thread == None:
                print("Got s, going to send ... ")
                pipeout = os.open(HOST_PIPEIN_NAME, os.O_WRONLY)
                bmsg = get_serialized_execution_wrapper()
                os.write(pipeout, bmsg)
                os.close(pipeout)
                recv_thread = threading.Thread(target=project_reciver)
                recv_thread.daemont = True
                recv_thread.start()
                recv_thread.join()
                recv_thread = None
    except:
        pass

def project_reciver():
    if not os.path.exists(HOST_PIPEOUT_NAME):
        os.mkfifo(HOST_PIPEOUT_NAME)

    pipein = open(HOST_PIPEOUT_NAME, 'rb')
    line = pipein.read()
    if len(line) != 0:
        print("[Project_reciver] recv : %s"%(str(line)))
    os.unlink(HOST_PIPEOUT_NAME)

def create_zip():
    with zipfile.ZipFile('program.zip', 'w') as myzip:
        myzip.writestr('program.py', "print('I ==>> PROGRAM')")
    ba = None
    with open("program.zip", "rb") as fn:
        ba = fn.read()
    return ba

loader_scripts = """
def bytes_program_loader(ba):
    import pickle
    import zipfile

    # Convert bytes array data into zip file
    with open("../program.zip", "wb") as fn:
        fn.write(ba)

    # Extract files to execute
    with zipfile.ZipFile('../program.zip') as myzip:
        myzip.extractall('../')

    # Execution
    with open("../program.py") as f:
        code = compile(f.read(), "program.py", 'exec')
        exec(code)

    # Create results !!
    with zipfile.ZipFile('result.zip', 'w') as myzip:
        myzip.writestr('result.info', "IAMRESULT")

    ba_result = None
    with open("result.zip", "rb") as fn:<commit_msg>Correct the program package path to avoid permission problem.<commit_after>
import os
import sys
import pickle
import zipfile
import threading

PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

from simple_host_target.definition import ResultWrapper, ExecutorWrapper, HOST_PIPEIN_NAME,\
                        HOST_PIPEOUT_NAME

def get_serialized_execution_wrapper():
    ba = None
    with open("program.zip", "rb") as fn:
        ba = fn.read()

    wrapper = ExecutorWrapper(ba, loader_scripts)
    serialized_executor_wrapper = pickle.dumps(wrapper)
    return serialized_executor_wrapper

def project_sender():
    print("[Sender] Press s + <Enter> to send a task !")
    if not os.path.exists(HOST_PIPEIN_NAME):
        os.mkfifo(HOST_PIPEIN_NAME)

    try:
        recv_thread = None
        for line in sys.stdin:
            if "s" in line and recv_thread == None:
                print("Got s, going to send ... ")
                pipeout = os.open(HOST_PIPEIN_NAME, os.O_WRONLY)
                bmsg = get_serialized_execution_wrapper()
                os.write(pipeout, bmsg)
                os.close(pipeout)
                recv_thread = threading.Thread(target=project_reciver)
                recv_thread.daemont = True
                recv_thread.start()
                recv_thread.join()
                recv_thread = None
    except:
        pass

def project_reciver():
    if not os.path.exists(HOST_PIPEOUT_NAME):
        os.mkfifo(HOST_PIPEOUT_NAME)

    pipein = open(HOST_PIPEOUT_NAME, 'rb')
    line = pipein.read()
    if len(line) != 0:
        print("[Project_reciver] recv : %s"%(str(line)))
    os.unlink(HOST_PIPEOUT_NAME)

def create_zip():
    with zipfile.ZipFile('program.zip', 'w') as myzip:
        myzip.writestr('program.py', "print('I ==>> PROGRAM')")
    ba = None
    with open("program.zip", "rb") as fn:
        ba = fn.read()
    return ba

loader_scripts = """
def bytes_program_loader(ba):
    import pickle
    import zipfile

    # Convert bytes array data into zip file
    with open("./program.zip", "wb") as fn:
        fn.write(ba)

    # Extract files to execute
    with zipfile.ZipFile('./program.zip') as myzip:
        myzip.extractall('./')

    # Execution
    with open("./program.py") as f:
        code = compile(f.read(), "program.py", 'exec')
        exec(code)

    # Create results !!
    with zipfile.ZipFile('result.zip', 'w') as myzip:
        myzip.writestr('result.info', "IAMRESULT")

    ba_result = None
    with open("result.zip", "rb") as fn:
{'fraction_non_alphanumeric': 0.12226827430293896, 'fraction_numerical': 0.00037678975131876413, 'mean_word_length': 2.592016238159675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5433299', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1714, 'n_words': 381}