mirror of
https://github.com/xzb23/check_duplicate_files.git
synced 2026-11-24 10:25:26 +08:00
99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
from _in_package import *
|
|
import config
|
|
import common
|
|
import runing_data
|
|
import I18n
|
|
|
|
|
|
def traverse_dir(root_dir):
|
|
print(config.l10n_data.get_text("tip_traverse_dir"))
|
|
for path, dirs, files in os.walk(root_dir):
|
|
path = common.formatting_path(path)
|
|
for filename in files:
|
|
runing_data.file_path_list.append(path + filename)
|
|
print(common.safe_format(config.l10n_data.get_text("info_traverse_dir"), len(runing_data.file_path_list)))
|
|
|
|
|
|
def hash_files_from_glist(algorithm: function):
|
|
print(config.l10n_data.get_text("tip_hash_files_from_glist"))
|
|
i = 0
|
|
p_old_i = -1
|
|
for f_path in runing_data.file_path_list:
|
|
with open(f_path, "rb") as f:
|
|
f_hash = hashlib.file_digest(f, algorithm)
|
|
# runing_data.file_hash_results.append(runing_data.FileHashResult(f_path, f_hash.hexdigest(), algorithm.__name__))
|
|
runing_data.hash_to_files[(f_hash.hexdigest(), algorithm.__name__)].append(f_path)
|
|
i += 1
|
|
if i % config.wait_c == 0:
|
|
p_old_i = i
|
|
print(f"\r{i}/{len(runing_data.file_path_list)}", end="")
|
|
if p_old_i != -1 and i != p_old_i:
|
|
print(f"\r{i}/{len(runing_data.file_path_list)}", end="")
|
|
print()
|
|
|
|
|
|
|
|
def print_info(hash_to_files_list: list = runing_data.hash_to_files):
|
|
size_total = 0
|
|
size_if_one = 0
|
|
print_size_total = 0
|
|
print_size_if_one = 0
|
|
count = 0
|
|
for t in runing_data.hash_to_files:
|
|
st_size = os.stat(runing_data.hash_to_files[t][0]).st_size
|
|
size_total += st_size * len(runing_data.hash_to_files[t])
|
|
size_if_one += st_size
|
|
if st_size < config.flag_size:
|
|
continue
|
|
print_size_total += st_size * len(runing_data.hash_to_files[t])
|
|
print_size_if_one += st_size
|
|
if len(runing_data.hash_to_files[t]) > 1:
|
|
count += 1
|
|
print(t, f"size: {st_size} Byte")
|
|
for fp in runing_data.hash_to_files[t]:
|
|
print("\t", fp)
|
|
print()
|
|
if count == 0:
|
|
print(config.l10n_data.get_text("info_print_info_not_duplicate_msg"))
|
|
|
|
size_duplicate = size_total - size_if_one
|
|
print_size_duplicate = print_size_total - print_size_if_one
|
|
|
|
size_total = common.add_unit(size_total)
|
|
size_duplicate = common.add_unit(size_duplicate)
|
|
print_size_total = common.add_unit(print_size_total)
|
|
print_size_duplicate = common.add_unit(print_size_duplicate)
|
|
print(config.l10n_data.get_text("info_print_info_duplicate_msg"), end="\t")
|
|
print(f"\t{size_duplicate} / {size_total}")
|
|
|
|
if config.flag_size > 0:
|
|
print(common.safe_format(config.l10n_data.get_text("info_print_info_if_size-flag_duplicate_msg"), config.flag_size), end="\t")
|
|
print(f"\t{print_size_duplicate} / {print_size_total}")
|
|
|
|
|
|
def reg_opts():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-P", "--path", default=None, required=False)
|
|
parser.add_argument("--fsize", default=None, required=False, help=config.l10n_data.get_text("help_fsize"))
|
|
args = parser.parse_args()
|
|
if args.path != None:
|
|
config.work_dir = args.path
|
|
if args.fsize != None:
|
|
config.flag_size = int(args.fsize)
|
|
|
|
if __name__ == "__main__":
|
|
if not os.path.exists(config.work_dir):
|
|
exit(-1)
|
|
config.exe_dir = common.get_exe_dir()
|
|
|
|
lang = common.get_config("lang")
|
|
if lang != None:
|
|
config.lang = lang
|
|
else: lang = config.lang
|
|
config.l10n_data = I18n.get_lang_file(lang)
|
|
reg_opts()
|
|
|
|
traverse_dir(config.work_dir)
|
|
hash_files_from_glist(hashlib.sha256)
|
|
|
|
print_info() |