<commit_before>import os, subprocess, traceback
import debug_cron_common

def main():
    if not set_up():
        return
    while 1:
        process_command()

def log(log_msg):
    print log_msg
    f = open(debug_cron_common.LOG_PATH, "a")
    f.write(log_msg + "\n")
    f.close()

def quit_signal():
    return os.path.exists(debug_cron_common.QUIT_SIGNAL_PATH)

def process_command():
    log("Listening for command")
    insock = open(debug_cron_common.SOCKET_IN_PATH, "r")
    outsock = open(debug_cron_common.SOCKET_OUT_PATH, "w")
    subprocess.call("sh", stdout=outsock, stderr=outsock, stdin=insock)

def set_up():
    # A crude lock. There are nice lockfile libraries, but we don't want
    # to deal with pip environment under cron.
    try:
        os.mkfifo(debug_cron_common.LOCKFILE_PATH)
    except os.error:
        msg = (
            "Already running. Likely from a previous cron run. If not, try cleaning up files."
        )
        log(msg)
        return False

    for path in [debug_cron_common.SOCKET_IN_PATH, debug_cron_common.SOCKET_OUT_PATH]:
        os.mkfifo(path, 0600)
    return True

def clean_up():
    log("Cleaning Up")
    for path in [debug_cron_common.SOCKET_IN_PATH,
                 debug_cron_common.SOCKET_OUT_PATH,
                 debug_cron_common.QUIT_SIGNAL_PATH,
                 debug_cron_common.LOCKFILE_PATH]:
        os.remove(path)

if __name__ == "__main__":
    if set_up():
        try:
            while not quit_signal():
                process_command()
        except BaseException as e:
            # Particularly useful for keyboard
            clean_up()
            log("Exception:")
            log(traceback.format_exc())
            raise
        else:
            log("Normal Exit")
            clean_up()<commit_msg>Moves main server logic to main()<commit_after>import os, subprocess, traceback
import debug_cron_common

def main():
    if set_up():
        try:
            while not quit_signal():
                process_command()
        except BaseException:
            # Particularly useful for keyboard
            clean_up()
            log("Exception:")
            log(traceback.format_exc())
            raise
        else:
            log("Normal Exit")
            clean_up()

def log(log_msg):
    print log_msg
    f = open(debug_cron_common.LOG_PATH, "a")
    f.write(log_msg + "\n")
    f.close()

def quit_signal():
    return os.path.exists(debug_cron_common.QUIT_SIGNAL_PATH)

def process_command():
    log("Listening for command")
    insock = open(debug_cron_common.SOCKET_IN_PATH, "r")
    outsock = open(debug_cron_common.SOCKET_OUT_PATH, "w")
    subprocess.call("sh", stdout=outsock, stderr=outsock, stdin=insock)

def set_up():
    # A crude lock. There are nice lockfile libraries, but we don't want
    # to deal with pip environment under cron.
    try:
        os.mkfifo(debug_cron_common.LOCKFILE_PATH)
    except os.error:
        msg = (
            "Already running. Likely from a previous cron run. If not, try cleaning up files."
        )
        log(msg)
        return False

    for path in [debug_cron_common.SOCKET_IN_PATH, debug_cron_common.SOCKET_OUT_PATH]:
        os.mkfifo(path, 0600)
    return True

def clean_up():
    log("Cleaning Up")
    for path in [debug_cron_common.SOCKET_IN_PATH,
                 debug_cron_common.SOCKET_OUT_PATH,
                 debug_cron_common.QUIT_SIGNAL_PATH,
                 debug_cron_common.LOCKFILE_PATH]:
        os.remove(path)

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.09324667985306584, 'fraction_numerical': 0.002543091268719977, 'mean_word_length': 2.4104046242774566, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '1029283', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1129, 'n_words': 264}