import subprocess
import os
import json
from tqdm import tqdm
from multiprocessing import Pool

from script.cc_utils import readlines_cc
from script.time_stat import TimeStat
truffle_projs_path = 'ilf_datas/UTSC'
# result_file = "ilf_datas/ilf_test_results.jl"
# log_file = "ilf_datas/ilf.log"

def ilf_proj(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --contract {contract} --fuzzer imitation --model ./model/ ' + \
        '--limit 2000 --time_limit 120 ' + \
        '--log_to_file {log_file} --result_file {result_file}'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    contract_name = proj_name.split('-0x')[0]
    result_file = "ilf_datas/ilf_test_results.jl"
    log_file = "ilf_datas/ilf.log"
    tmp_args = {
            'proj': proj_path,
            'contract': contract_name,
            'log_file': log_file,
            'result_file': result_file,
        }
    subprocess.call(command_str.format(**tmp_args), shell=True)


def random_proj(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --contract {contract} --fuzzer random ' + \
        '--limit 15000 --time_limit 120 ' + \
        '--log_to_file {log_file} --result_file {result_file}'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    contract_name = proj_name.split('-0x')[0]
    result_file = "ilf_datas/random_test_results.jl"
    log_file = "ilf_datas/random.log"
    tmp_args = {
        'proj': proj_path,
        'contract': contract_name,
        'log_file': log_file,
        'result_file': result_file,
    }
    subprocess.call(command_str.format(**tmp_args), shell=True)


def ga_proj(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --contract {contract} --fuzzer ga --model ./model/ ' + \
        '--limit 100 --time_limit 120 ' + \
        '--log_to_file {log_file} --result_file {result_file}'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    contract_name = proj_name.split('-0x')[0]
    result_file = "ilf_datas/ga_test_results.jl"
    log_file = "ilf_datas/ga.log"
    tmp_args = {
            'proj': proj_path,
            'contract': contract_name,
            'log_file': log_file,
            'result_file': result_file,
        }
    subprocess.call(command_str.format(**tmp_args), shell=True)

def stat_info(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --fuzzer ga --model ./model/ ' + \
        '--limit 100 --time_limit 120'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    tmp_args = {
            'proj': proj_path,
        }
    subprocess.call(command_str.format(**tmp_args), shell=True)

if __name__ == '__main__':
    time_stat = TimeStat()
    time_stat.start()
    with open('ilf_datas/final_projs2.txt', 'r', encoding='utf-8') as f:
        truffle_projs = [line.strip() for line in f.readlines()]
    print('total: ', len(truffle_projs))
    # result_file = "ilf_datas/random_test_results.jl"
    # if os.path.exists(result_file):
    #     fuzzed_projs = []
    #     with open(result_file, 'r', encoding='utf-8') as f:
    #         line = f.readline()
    #         while line:
    #             line = json.loads(line.rstrip())
    #             fuzzed_projs.append(line['proj'])
    #             line = f.readline()
    #     truffle_projs = [
    #         proj for proj in truffle_projs if proj not in fuzzed_projs]

    p = Pool(8)
    list(tqdm(iterable=p.imap_unordered(stat_info, truffle_projs),
         desc="stat_info", total=len(truffle_projs)))
    p.close()
    p.join()
    print('stat_info finished')
    print('used time: ', time_stat.used_time())

import subprocess
import os
import json
from tqdm import tqdm
from multiprocessing import Pool

from script.cc_utils import readlines_cc
from script.time_stat import TimeStat
truffle_projs_path = 'ilf_datas/UTSC'
# result_file = "ilf_datas/ilf_test_results.jl"
# log_file = "ilf_datas/ilf.log"

def ilf_proj(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --contract {contract} --fuzzer imitation --model ./model/ ' + \
        '--limit 2000 --time_limit 120 ' + \
        '--log_to_file {log_file} --result_file {result_file}'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    contract_name = proj_name.split('-0x')[0]
    result_file = "ilf_datas/ilf_test_results.jl"
    log_file = "ilf_datas/ilf.log"
    tmp_args = {
            'proj': proj_path,
            'contract': contract_name,
            'log_file': log_file,
            'result_file': result_file,
        }
    subprocess.call(command_str.format(**tmp_args), shell=True)


def random_proj(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --contract {contract} --fuzzer random ' + \
        '--limit 15000 --time_limit 120 ' + \
        '--log_to_file {log_file} --result_file {result_file}'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    contract_name = proj_name.split('-0x')[0]
    result_file = "ilf_datas/random_test_results.jl"
    log_file = "ilf_datas/random.log"
    tmp_args = {
        'proj': proj_path,
        'contract': contract_name,
        'log_file': log_file,
        'result_file': result_file,
    }
    subprocess.call(command_str.format(**tmp_args), shell=True)


def ga_proj(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --contract {contract} --fuzzer ga --model ./model/ ' + \
        '--limit 100 --time_limit 120 ' + \
        '--log_to_file {log_file} --result_file {result_file}'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    contract_name = proj_name.split('-0x')[0]
    result_file = "ilf_datas/ga_test_results.jl"
    log_file = "ilf_datas/ga.log"
    tmp_args = {
            'proj': proj_path,
            'contract': contract_name,
            'log_file': log_file,
            'result_file': result_file,
        }
    subprocess.call(command_str.format(**tmp_args), shell=True)

def stat_info(proj_name):
    command_str = 'python3 -m ilf --proj {proj} --fuzzer ga --model ./model/ ' + \
        '--limit 100 --time_limit 120'
    proj_path = os.path.join(truffle_projs_path, proj_name)
    tmp_args = {
            'proj': proj_path,
        }
    subprocess.call(command_str.format(**tmp_args), shell=True)

if __name__ == '__main__':
    time_stat = TimeStat()
    time_stat.start()
    with open('ilf_datas/final_projs2.txt', 'r', encoding='utf-8') as f:
        truffle_projs = [line.strip() for line in f.readlines()]
    print('total: ', len(truffle_projs))
    # result_file = "ilf_datas/random_test_results.jl"
    # if os.path.exists(result_file):
    #     fuzzed_projs = []
    #     with open(result_file, 'r', encoding='utf-8') as f:
    #         line = f.readline()
    #         while line:
    #             line = json.loads(line.rstrip())
    #             fuzzed_projs.append(line['proj'])
    #             line = f.readline()
    #     truffle_projs = [
    #         proj for proj in truffle_projs if proj not in fuzzed_projs]

    p = Pool(8)
    list(tqdm(iterable=p.imap_unordered(stat_info, truffle_projs),
         desc="stat_info", total=len(truffle_projs)))
    p.close()
    p.join()
    print('stat_info finished')
    print('used time: ', time_stat.used_time())

# result_file = "ilf_datas/ilf_test_results.jl" # log_file = "ilf_datas/ilf.log" # result_file = "ilf_datas/random_test_results.jl" # if os.path.exists(result_file): #     fuzzed_projs = [] #     with open(result_file, 'r', encoding='utf-8') as f: #         line = f.readline() #         while line: #             line = json.loads(line.rstrip()) #             fuzzed_projs.append(line['proj']) #             line = f.readline() #     truffle_projs = [ #         proj for proj in truffle_projs if proj not in fuzzed_projs]