Skip to content

Instantly share code, notes, and snippets.

@mungojelly
Created December 7, 2018 05:12
Show Gist options
  • Save mungojelly/2a26213894563b3f75af1edb30d6ad56 to your computer and use it in GitHub Desktop.
Save mungojelly/2a26213894563b3f75af1edb30d6ad56 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys
import sqlite3
import random
import os
import re
import uuid
import subprocess
def process_root_dir_from_uuid(uuid):
return os.environ['EVOLPROCROOT'] + "/" + \
uuid[0:2] + "/" + \
uuid[2:4] + "/" + \
uuid[4:6] + "/" + \
uuid[6:8] + "/"
def process_data_dir_from_uuid(uuid):
return process_root_dir_from_uuid(uuid) + uuid
def process_genome_dir_from_uuid(uuid):
return process_data_dir_from_uuid(uuid) + "/genome"
def process_file_name_from_uuid(uuid):
return process_root_dir_from_uuid(uuid) + uuid + ".evolproc"
def copy_process(text_to_copy, cursor, parent_uuid):
child_uuid = str(uuid.uuid4())
child_file_name = process_file_name_from_uuid(child_uuid)
os.makedirs(os.path.dirname(child_file_name))
with open(child_file_name, 'w') as child_file:
child_file.write("This process's UUID: " + child_uuid + "\n")
child_file.write("Parent's UUID: " + parent_uuid + "\n\n")
child_file.write(text_to_copy)
child_file.write("\n~*~ GENOME COPY BELOW THIS LINE ~*~\n")
child_file.write(text_to_copy)
parent_genome_dir = process_genome_dir_from_uuid(parent_uuid)
if os.path.exists(parent_genome_dir):
child_data_dir = process_data_dir_from_uuid(child_uuid)
os.makedirs(child_data_dir)
subprocess.call(['cp', '-r', parent_genome_dir, child_data_dir])
set_pool(child_uuid, cursor, "Newly Created")
def delete_pool_entries(uuid_to_delete, cursor):
cursor.execute("delete from pools where uuid = ?",
(uuid_to_delete,))
def set_pool(uuid_to_set, cursor, pool):
delete_pool_entries(uuid_to_set, cursor)
cursor.execute("insert into pools(uuid, pool) values(?, ?)",
(uuid_to_set, pool))
def updated_pool_of(uuid_to_update, cursor):
process_file = process_file_name_from_uuid(uuid_to_update)
if not os.path.isfile(process_file):
delete_pool_entries(uuid_to_update, cursor)
return(False)
with open(process_file) as process:
line = process.readline()
while line:
copyline = re.search(r'~\*~.*~\*~', line)
if copyline:
rest_of_file = process.read()
copy_process(rest_of_file, cursor, uuid_to_update)
copy_process(rest_of_file, cursor, uuid_to_update)
copy_process(rest_of_file, cursor, uuid_to_update)
copy_process(rest_of_file, cursor, uuid_to_update)
copy_process(rest_of_file, cursor, uuid_to_update)
delete_pool_entries(uuid_to_update, cursor)
cursor.execute("insert into completed(uuid) values(?)",
(uuid_to_update,))
return(False)
unchecked = re.search(r'\[\[\s*\]\]\s*([^:]*):', line)
if unchecked:
current_pool = unchecked.group(1)
set_pool(uuid_to_update, cursor, current_pool)
return(current_pool)
line = process.readline()
delete_pool_entries(uuid_to_update, cursor)
return(False)
# todo should be argparse
user_target_pool = sys.argv[1]
while True:
processes_db = os.path.join(os.environ['EVOLPROCROOT'], "processes.db")
db_connection = sqlite3.connect(processes_db)
with db_connection:
db_connection.row_factory = sqlite3.Row
cursor = db_connection.cursor()
cursor.execute("select uuid from pools where pool = ?",
(user_target_pool,))
possibilities = cursor.fetchall()
if not possibilities:
sys.exit()
uuid_to_try = (random.choice(possibilities))['uuid']
current_pool = updated_pool_of(uuid_to_try, cursor)
if current_pool == user_target_pool:
sys.exit(uuid_to_try)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment