mirror of
https://github.com/xzb23/check_duplicate_files.git
synced 2026-11-24 18:45:31 +08:00
Compare commits
5
Commits
c63609c901
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c41a9e2fd
|
||
|
|
4765499f64
|
||
|
|
42f023e69c
|
||
|
|
c8c30ffe84
|
||
|
|
e3f838132e
|
No files matched your search
@@ -5,3 +5,7 @@ build
|
||||
*.spec
|
||||
*.backup
|
||||
/.venv
|
||||
/.vscode
|
||||
/conf.d
|
||||
/test.py
|
||||
conf.dir
|
||||
@@ -0,0 +1,29 @@
|
||||
from _in_package import *
|
||||
import config
|
||||
|
||||
class I18nText:
|
||||
langid: str
|
||||
i18n_data: dict[str:str]
|
||||
def __init__(self, langid: str, i18n_data: str):
|
||||
self.langid = langid
|
||||
self.i18n_data = i18n_data
|
||||
# return self
|
||||
def get_text(self, i18nid: str) -> str:
|
||||
if not (i18nid in self.i18n_data.keys()):
|
||||
return "__langerr__"
|
||||
return self.i18n_data[i18nid]
|
||||
|
||||
|
||||
def get_lang_file(lang: str = config.lang):
|
||||
lang_file = config.exe_dir + f"lang/{lang}.lang"
|
||||
if not (os.path.isfile(lang_file)):
|
||||
return I18nText(lang, {})
|
||||
with open(lang_file, "r", encoding="utf-8") as lf:
|
||||
l10n_lines = lf.read().strip().split("\n")
|
||||
i18n_data = {}
|
||||
for l10n_line in l10n_lines:
|
||||
if len(l10n_line.split("=")) != 2:
|
||||
continue
|
||||
i18n_data[l10n_line.split("=")[0].strip()] = l10n_line.split("=")[1].strip()
|
||||
l10n_data = I18nText(lang, i18n_data)
|
||||
return l10n_data
|
||||
@@ -22,11 +22,11 @@
|
||||
```
|
||||
-h, --help 查看帮助
|
||||
-P, --path 指定检查的目录, 默认当前工作目录
|
||||
--fsize 只打印文件大小大于fsize Byte的重复文件, 默认10240Byte
|
||||
--fsize 只打印文件大小不小于fsize Byte的重复文件, 默认为0
|
||||
```
|
||||
|
||||
# 更新计划
|
||||
- [ ] GUI界面(Windows)
|
||||
- [ ] 支持一键清理/整理重复文件
|
||||
- [ ] 支持对结果列表重排序
|
||||
- [ ] I18n
|
||||
- [x] I18n
|
||||
@@ -5,3 +5,4 @@ import hashlib
|
||||
import typing
|
||||
import collections
|
||||
import json
|
||||
import platform
|
||||
@@ -2,19 +2,20 @@ from _in_package import *
|
||||
import config
|
||||
import common
|
||||
import runing_data
|
||||
import I18n
|
||||
|
||||
|
||||
def traverse_dir(root_dir):
|
||||
print("正在遍历文件ing...")
|
||||
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(f"完成\t总计 {len(runing_data.file_path_list)} 个文件")
|
||||
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("正在计算文件散列ing...")
|
||||
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:
|
||||
@@ -37,37 +38,60 @@ def print_info(hash_to_files_list: list = runing_data.hash_to_files):
|
||||
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:
|
||||
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()
|
||||
print(f"重复部分占用/总占用:\t{(size_total-size_if_one)//1024}KiB / {size_total//1024}KiB")
|
||||
print(f"占用大于 {config.flag_size} Byte 的文件中,重复部分占用/总占用:\t{(print_size_total-print_size_if_one)//1024}KiB / {print_size_total//1024}KiB")
|
||||
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="只打印文件大小大于fsize Byte的重复文件")
|
||||
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)
|
||||
|
||||
reg_opts()
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from _in_package import *
|
||||
from config import *
|
||||
import config
|
||||
|
||||
|
||||
def formatting_path(path: str) -> str:
|
||||
@@ -8,3 +8,48 @@ def formatting_path(path: str) -> str:
|
||||
if path[-1] != "/":
|
||||
path += "/"
|
||||
return path
|
||||
|
||||
def get_exe_dir():
|
||||
if getattr(sys, "frozen", False):
|
||||
exe_dir = os.path.dirname(os.path.abspath(sys.executable))
|
||||
else:
|
||||
exe_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
return formatting_path(exe_dir)
|
||||
|
||||
def get_config(config_key: str):
|
||||
if config.configdir_path == None:
|
||||
match platform.system():
|
||||
case "Linux":
|
||||
if os.path.isfile(config.exe_dir + "conf.dir"):
|
||||
with open(config.exe_dir + "conf.dir") as cpf:
|
||||
configdir_path = cpf.read().strip()
|
||||
else:
|
||||
configdir_path = "/etc/check_duplicate_files/"
|
||||
case "Windows":
|
||||
if os.path.isfile(config.exe_dir + "conf.dir"):
|
||||
with open(config.exe_dir + "conf.dir") as cpf:
|
||||
configdir_path = cpf.read().strip()
|
||||
else:
|
||||
configdir_path = config.exe_dir + "conf.d/"
|
||||
config.configdir_path = formatting_path(configdir_path)
|
||||
config_path = config.configdir_path + config_key
|
||||
if not (os.path.isfile(config_path)):
|
||||
return None
|
||||
with open(config_path, "r", encoding="utf-8") as conf:
|
||||
conf_v = conf.read().strip()
|
||||
return conf_v
|
||||
|
||||
|
||||
def safe_format(fmt_str: str, *args) -> str:
|
||||
try:
|
||||
return fmt_str.format(*args)
|
||||
except: return "__langerr__"
|
||||
|
||||
def add_unit(num: int) -> str:
|
||||
for unit in config.unit_list:
|
||||
if num >= 1024:
|
||||
num //= 1024
|
||||
else:
|
||||
num = str(num)
|
||||
break
|
||||
return num+unit
|
||||
@@ -2,5 +2,16 @@ from _in_package import *
|
||||
|
||||
flag_size: int = 0
|
||||
work_dir: str = "."
|
||||
exe_dir: str
|
||||
|
||||
wait_c: int = 100
|
||||
|
||||
lang: str = "zh_cn"
|
||||
l10n_data: I18n.I18nText
|
||||
|
||||
configdir_path: str = None
|
||||
|
||||
unit_list = ["Byte", "KiB", "MiB", "GiB", "TiB"]
|
||||
|
||||
|
||||
import I18n
|
||||
@@ -0,0 +1,7 @@
|
||||
help_fsize = 只打印文件大小不小于fsize Byte的重复文件
|
||||
tip_traverse_dir = 正在遍历文件ing...
|
||||
info_traverse_dir = 完成 总计 {} 个文件
|
||||
tip_hash_files_from_glist = 正在计算文件散列ing...
|
||||
info_print_info_duplicate_msg = 重复部分占用/总占用:
|
||||
info_print_info_if_size-flag_duplicate_msg = 占用大于 {} Byte 的文件中,重复部分占用/总占用:
|
||||
info_print_info_not_duplicate_msg = 没有找到重复文件
|
||||
Reference in new issue
Block a user