import configparser
import hashlib
import os
import shutil


def get_file_list(file_path):
    _fileList = []
    for _root, _dirs, _files in os.walk(file_path):
        if len(_files) + len(_dirs) == 0:
            print("delete empty fold:", _root)
            os.rmdir(_root)
        for _file in _files:
            _fileList.append(_root + '\\' + _file)
    return _fileList


def calc_sha1(filepath):
    with open(filepath, 'rb') as f:
        sha1obj = hashlib.sha1()
        sha1obj.update(f.read())
        _hash = sha1obj.hexdigest()
        return _hash


def calc_md5(filepath):
    with open(filepath, 'rb') as f:
        md5obj = hashlib.md5()
        md5obj.update(f.read())
        _hash = md5obj.hexdigest()
        return _hash


def calc_hash(filepath):
    ret = (calc_md5(filepath), calc_sha1(filepath))
    return ret


if __name__ == '__main__':
    cp = configparser.ConfigParser()
    cp.read('conf.ini')
    file_list = get_file_list(cp['Paths']['inputPath'])
    out_path = cp['Paths']['outputPath']
    lt = 0
    t = 0
    L = len(file_list)
    dic = {}
    for i in range(L):
        file = file_list[i]
        t = int((i + 1) * 100 / L)
        if t != lt:
            lt = t
            print("%d%%" % t)
        myhash = calc_hash(file)
        if myhash in dic:
            print(dic[myhash] + ' equal to ' + file)
            # shutil.copy(dic[myhash], out_path)
            # shutil.copy(file, out_path)
            os.remove(file)
        else:
            dic[myhash] = file
