import os
import shutil
import json

SOURCE_DIR = 'I:/mwreports/malware/analyses'
SAVE_DIR = 'I:/exe_group'

dirs = os.listdir(SOURCE_DIR)
print(dirs)

fopen = open('dic_pku.json','r')
dic = eval(fopen.read())
fopen.close()

for idx, dir in enumerate(dirs):
    print(idx, len(dirs))
    if idx <= 5935:
        print('pass')
        continue
    fopen = open(os.path.join(SOURCE_DIR, dir, 'reports/report.json'), 'r')
    try:
        new_dict = json.load(fopen)
    except:
        print('打开json失败')
        continue

    # target = new_dict['target']
    md5 = new_dict['target']['file']['md5']
    if not md5 in dic:
        print('找不到key')
        continue
    label = dic[md5]['name'].split('.')[0]
    # dir = os.path.join(SOURCE_DIR, dir)
    if label == None:
        label = 'None'
    sourceSrcDir = os.path.join(SOURCE_DIR, dir, 'reports')
    dstSrcDir = os.path.join(SAVE_DIR, label, dir, 'reports')
    print(sourceSrcDir, dstSrcDir)
    if os.path.exists(dstSrcDir):
        print('have copied')
        continue
    # try:
    shutil.copytree(sourceSrcDir, dstSrcDir)
    print('copy finished')
        # continue
    # except :
    #     print('copy error')
        # continue





