411 lines
9.6 KiB
Python
411 lines
9.6 KiB
Python
|
import os
|
|||
|
from operator import itemgetter, attrgetter
|
|||
|
import re
|
|||
|
import shutil
|
|||
|
|
|||
|
|
|||
|
def remove_lua_comments(lua_script: str) -> str:
|
|||
|
# 使用正则表达式去除多行注释
|
|||
|
lua_script = re.sub(r'--+ *\[=*\[.*?]=*]', '', lua_script, flags=re.DOTALL)
|
|||
|
|
|||
|
# 使用正则表达式去除单行注释
|
|||
|
lua_script = re.sub(r'--.*$', '', lua_script, flags=re.MULTILINE)
|
|||
|
|
|||
|
return lua_script
|
|||
|
|
|||
|
|
|||
|
def extFileExtension(f: str) -> str:
|
|||
|
values: list = f.split('.')
|
|||
|
return values[len(values) - 1]
|
|||
|
|
|||
|
|
|||
|
# 加载文件夹中所有文件
|
|||
|
def listFiles(path: str, ext: str | list) -> list[str]:
|
|||
|
files: list[str] = []
|
|||
|
for filename in os.listdir(path):
|
|||
|
file_path = os.path.join(path, filename)
|
|||
|
localFileExt = extFileExtension(file_path)
|
|||
|
if os.path.isfile(file_path):
|
|||
|
if type(ext) is str:
|
|||
|
if localFileExt == ext:
|
|||
|
files.append(file_path)
|
|||
|
elif type(ext) is list:
|
|||
|
for i in ext:
|
|||
|
if i == localFileExt:
|
|||
|
files.append(file_path)
|
|||
|
elif os.path.isdir(file_path):
|
|||
|
for i in listFiles(file_path, ext):
|
|||
|
files.append(i)
|
|||
|
return files
|
|||
|
|
|||
|
|
|||
|
def to_one_line(filename: str) -> str:
|
|||
|
res = ""
|
|||
|
with open(file=filename, mode="r+", encoding="utf-8") as file:
|
|||
|
for line in file:
|
|||
|
line.replace("\n", " ")
|
|||
|
line.replace("\r", " ")
|
|||
|
if line.endswith("\\"):
|
|||
|
line.replace("\\", "\n")
|
|||
|
res += line
|
|||
|
|
|||
|
file.close()
|
|||
|
return res
|
|||
|
|
|||
|
|
|||
|
def one_line(filecontent: str) -> str:
|
|||
|
filecontent = filecontent.replace("\n", " ")
|
|||
|
filecontent = filecontent.replace("\r\n", " ")
|
|||
|
filecontent = filecontent.replace("\r", " ")
|
|||
|
if filecontent.endswith("\\"):
|
|||
|
filecontent = filecontent.replace("\\", "\n")
|
|||
|
return filecontent
|
|||
|
|
|||
|
|
|||
|
def load_file(filename: str, encode) -> str:
|
|||
|
with open(file=filename, mode="r", encoding=encode) as f:
|
|||
|
res = f.read()
|
|||
|
f.close()
|
|||
|
return res
|
|||
|
|
|||
|
|
|||
|
# 判断文件是否存在,如果存在,则删除并重新创建;如果不存在,则创建
|
|||
|
def file_exist(filename: str):
|
|||
|
if os.path.exists(filename):
|
|||
|
os.remove(filename)
|
|||
|
target_folder_path = os.path.dirname(filename)
|
|||
|
if not os.path.isdir(target_folder_path):
|
|||
|
os.makedirs(target_folder_path)
|
|||
|
with open(file=filename, mode="w") as f:
|
|||
|
f.close()
|
|||
|
|
|||
|
|
|||
|
def createFolder(foldername: str):
|
|||
|
if os.path.exists(foldername):
|
|||
|
return
|
|||
|
os.makedirs(foldername)
|
|||
|
|
|||
|
|
|||
|
def removeFiles(foldername: str):
|
|||
|
shutil.rmtree(foldername)
|
|||
|
|
|||
|
|
|||
|
def add_type(folder: str):
|
|||
|
files = listFiles(folder, 'lua')
|
|||
|
decode = 'utf-8'
|
|||
|
simple = '--Edit Below--'
|
|||
|
for i in files:
|
|||
|
with open(i, mode='r', encoding=decode) as f:
|
|||
|
content = f.read()
|
|||
|
f.close()
|
|||
|
# 说明当前是对应文件类
|
|||
|
if content.startswith('---@class '):
|
|||
|
names = re.findall('---@class (.*):', content)
|
|||
|
if len(names) == 0:
|
|||
|
continue
|
|||
|
name = names[0]
|
|||
|
type_name = '---@type ' + name
|
|||
|
|
|||
|
# 查找是否存在 @type
|
|||
|
if content.find(type_name) != -1:
|
|||
|
content = re.sub(simple + '\n' + type_name + '\r*\n*', simple + '\n' + type_name + '\n', content)
|
|||
|
with open(i, mode='w', encoding=decode) as g:
|
|||
|
g.write(content)
|
|||
|
g.close()
|
|||
|
continue
|
|||
|
content = content.replace(simple, simple + '\n' + type_name)
|
|||
|
content = re.sub(simple + '\n' + type_name + '\r*\n*', simple + '\n' + type_name + '\n', content)
|
|||
|
with open(i, mode='w', encoding=decode) as f:
|
|||
|
f.write(content)
|
|||
|
f.close()
|
|||
|
|
|||
|
|
|||
|
def write_to_file(filename: str, mode: str, content: str):
|
|||
|
with open(file=filename, mode=mode, encoding='utf-8') as f:
|
|||
|
f.write(content)
|
|||
|
f.close()
|
|||
|
|
|||
|
|
|||
|
class FileInfo:
|
|||
|
lines: int
|
|||
|
filename: str
|
|||
|
|
|||
|
def to_str(self):
|
|||
|
return 'lines = %d, filename = %s' % (self.lines, self.filename)
|
|||
|
|
|||
|
|
|||
|
# 按行读取文件,并放到表中
|
|||
|
def file_to_map(file: str, file_map):
|
|||
|
if file_map is None:
|
|||
|
file_map = {}
|
|||
|
with open(file=file, mode='r', encoding='utf-8') as f:
|
|||
|
while True:
|
|||
|
line = f.readline()
|
|||
|
if not line:
|
|||
|
break
|
|||
|
|
|||
|
line = line.strip()
|
|||
|
if len(line) > 0:
|
|||
|
if line not in file_map:
|
|||
|
fi = FileInfo()
|
|||
|
fi.filename = file
|
|||
|
fi.lines = 1
|
|||
|
file_map[line] = [fi]
|
|||
|
else:
|
|||
|
HadFound = False
|
|||
|
for i in file_map[line]:
|
|||
|
if i.filename == file:
|
|||
|
i.lines += 1
|
|||
|
HadFound = True
|
|||
|
if not HadFound:
|
|||
|
fi = FileInfo()
|
|||
|
fi.filename = file
|
|||
|
fi.lines = 1
|
|||
|
file_map[line].append(fi)
|
|||
|
f.close()
|
|||
|
return file_map
|
|||
|
|
|||
|
|
|||
|
def file_to_list(file: str, bRemoveComment):
|
|||
|
with open(file=file, mode='r', encoding='utf-8') as f:
|
|||
|
content = f.read()
|
|||
|
if bRemoveComment:
|
|||
|
content = remove_lua_comments(content)
|
|||
|
files_map = content.split('\n')
|
|||
|
f.close()
|
|||
|
return files_map
|
|||
|
|
|||
|
|
|||
|
def file_to_map1(file: str, file_map):
|
|||
|
if file_map is None:
|
|||
|
file_map = {}
|
|||
|
with open(file=file, mode='r', encoding='utf-8') as f:
|
|||
|
content = f.read()
|
|||
|
content = remove_lua_comments(content)
|
|||
|
line_list = content.split('\n')
|
|||
|
|
|||
|
for line in line_list:
|
|||
|
line = line.strip()
|
|||
|
if len(line) > 7:
|
|||
|
if len(file_map) == 0 or line not in file_map:
|
|||
|
fi = FileInfo()
|
|||
|
fi.filename = file
|
|||
|
fi.lines = 1
|
|||
|
file_map[line] = [fi]
|
|||
|
else:
|
|||
|
HadFound = False
|
|||
|
for i in file_map[line]:
|
|||
|
if i.filename == file:
|
|||
|
i.lines += 1
|
|||
|
HadFound = True
|
|||
|
if not HadFound:
|
|||
|
fi = FileInfo()
|
|||
|
fi.filename = file
|
|||
|
fi.lines = 1
|
|||
|
file_map[line].append(fi)
|
|||
|
f.close()
|
|||
|
return file_map
|
|||
|
|
|||
|
|
|||
|
def folder_to_map(folder: str, files_map: {str, FileInfo}) -> {str, FileInfo}:
|
|||
|
if files_map is None:
|
|||
|
files_map = {}
|
|||
|
files = listFiles(folder, 'lua')
|
|||
|
for i in files:
|
|||
|
file_to_map1(i, files_map)
|
|||
|
return files_map
|
|||
|
|
|||
|
|
|||
|
def files_to_map(files: list, files_map):
|
|||
|
for i in files:
|
|||
|
file_to_map1(i, files_map)
|
|||
|
|
|||
|
|
|||
|
# 对比两个文件
|
|||
|
def compareFiles(file1: str, file2: str, filename: str):
|
|||
|
file1_map: {str, int} = {}
|
|||
|
file_to_map1(file1, file1_map)
|
|||
|
file2_map: {str, int} = {}
|
|||
|
file_to_map1(file2, file2_map)
|
|||
|
files = []
|
|||
|
# with open(file=filename, mode='w+', encoding='utf-8') as f:
|
|||
|
for i in file1_map.keys():
|
|||
|
if i in file2_map.keys():
|
|||
|
count1 = 0
|
|||
|
for c in file1_map[i]:
|
|||
|
count1 += c.lines
|
|||
|
count2 = 0
|
|||
|
for c in file2_map[i]:
|
|||
|
count2 += c.lines
|
|||
|
|
|||
|
count = count2 if count1 > count2 else count1
|
|||
|
s = 'file: %s\t\t %d\n' % (file2, count)
|
|||
|
files.append(s)
|
|||
|
# f.write(s)
|
|||
|
print(s)
|
|||
|
# f.close()
|
|||
|
return '\n'.join(files)
|
|||
|
|
|||
|
|
|||
|
class CompareItem:
|
|||
|
lua: str
|
|||
|
count: int
|
|||
|
files: str
|
|||
|
|
|||
|
|
|||
|
# 将 dict 转换成 列表 并排序
|
|||
|
def map_to_list(m: dict):
|
|||
|
fileList = []
|
|||
|
for i in m.keys():
|
|||
|
item = CompareItem()
|
|||
|
item.lua = i
|
|||
|
item.count = 0
|
|||
|
lists = []
|
|||
|
for c in m[i]:
|
|||
|
item.count += c.lines
|
|||
|
lists.append(c.to_str())
|
|||
|
item.files = '\n'.join(lists)
|
|||
|
fileList.append(item)
|
|||
|
fileList.sort(key=attrgetter('count'), reverse=True)
|
|||
|
return fileList
|
|||
|
|
|||
|
|
|||
|
class smae_line:
|
|||
|
file: str # 文件名
|
|||
|
total: int # 总数
|
|||
|
|
|||
|
|
|||
|
class same_line_item:
|
|||
|
lua: str
|
|||
|
folder1: str
|
|||
|
folder2: str
|
|||
|
|
|||
|
|
|||
|
# 对比两个文件夹
|
|||
|
def compareFolders(folder1: str, folder2: str, filename: str):
|
|||
|
folder1_map = {}
|
|||
|
folder_to_map(folder1, folder1_map)
|
|||
|
# folder2_map = {}
|
|||
|
# folder_to_map(folder2, folder2_map)
|
|||
|
|
|||
|
fileList = map_to_list(folder1_map)
|
|||
|
# sorted(fileList, key=attrgetter('count'), reverse=True)
|
|||
|
|
|||
|
folder2_map = {}
|
|||
|
folder_to_map(folder2, folder2_map)
|
|||
|
fileList2 = map_to_list(folder2_map)
|
|||
|
|
|||
|
sameList = []
|
|||
|
with open(file=filename, mode='w+', encoding='utf-8') as f:
|
|||
|
for i in folder2_map.keys():
|
|||
|
if i in folder1_map.keys():
|
|||
|
# 显示当前有多少行是相同的
|
|||
|
count1 = 0
|
|||
|
count2 = 0
|
|||
|
arr1 = []
|
|||
|
arr2 = []
|
|||
|
for c in folder1_map[i]:
|
|||
|
count1 += c.lines
|
|||
|
arr1.append(c.to_str())
|
|||
|
for d in folder2_map[i]:
|
|||
|
count2 += d.lines
|
|||
|
arr2.append(d.to_str())
|
|||
|
f.write(
|
|||
|
'%s 在 %s 文件夹中出现 %d 次,在 %s 文件夹中出现 %d 次\n' % (i, folder2, count1, folder1, count2))
|
|||
|
f.write('\n'.join(arr2))
|
|||
|
f.write('\r\n')
|
|||
|
f.close()
|
|||
|
|
|||
|
|
|||
|
def compareFolders1(folder1: str, folder2: str, filename: str):
|
|||
|
folder1_map = {}
|
|||
|
folder_to_map(folder1, folder1_map)
|
|||
|
# fileList = map_to_list(folder1_map)
|
|||
|
|
|||
|
folder2_map = {}
|
|||
|
folder_to_map(folder2, folder2_map)
|
|||
|
# fileList2 = map_to_list(folder2_map)
|
|||
|
|
|||
|
lists = {}
|
|||
|
|
|||
|
|
|||
|
# with open(file=filename, mode='w+', encoding='utf-8') as f:
|
|||
|
# for i in folder2_map.keys():
|
|||
|
# if i in folder1_map.keys():
|
|||
|
#
|
|||
|
# f.close()
|
|||
|
|
|||
|
|
|||
|
# def compareFilesWithFilename(file1, file2):
|
|||
|
|
|||
|
|
|||
|
def getFileName(file: str):
|
|||
|
if file.find('\\') != -1:
|
|||
|
lists = file.split('\\')
|
|||
|
return lists[len(lists) - 1]
|
|||
|
elif file.find('/') != -1:
|
|||
|
lists = file.split('/')
|
|||
|
return lists[len(lists) - 1]
|
|||
|
else:
|
|||
|
return file
|
|||
|
|
|||
|
|
|||
|
def compareFolders2(folder1: str, folder2: str, filename: str):
|
|||
|
# 查询是否有一样的文件,如果有,那么就使用一样的文件;如果没有,那么就对比全局
|
|||
|
files1 = listFiles(folder1, 'lua')
|
|||
|
files2 = listFiles(folder2, 'lua')
|
|||
|
|
|||
|
foundFiles = []
|
|||
|
notFoundFiles = []
|
|||
|
for i in files2:
|
|||
|
fn = getFileName(i)
|
|||
|
hadFound = False
|
|||
|
for c in files1:
|
|||
|
if fn == getFileName(c):
|
|||
|
foundFiles.append([i, c])
|
|||
|
hadFound = True
|
|||
|
break
|
|||
|
if not hadFound:
|
|||
|
notFoundFiles.append(i)
|
|||
|
|
|||
|
folder1_notFound = []
|
|||
|
# 针对发现的文件进行处理
|
|||
|
for i in files1:
|
|||
|
if i not in foundFiles:
|
|||
|
folder1_notFound.append(i)
|
|||
|
|
|||
|
with open(file=filename, mode='w+', encoding='utf-8') as f:
|
|||
|
for i in foundFiles:
|
|||
|
f.write('\n')
|
|||
|
f.close()
|
|||
|
|
|||
|
notFoundLines = {}
|
|||
|
|
|||
|
|
|||
|
# 将其他文件放入
|
|||
|
# files_to_map(notFoundFiles, notFoundLines)
|
|||
|
|
|||
|
|
|||
|
def compareTwoFiles(file1: str, file2: str):
|
|||
|
# 1. 列举相同的文件
|
|||
|
# 2. 对比相同的文件
|
|||
|
# 比较两个文件有多少行是相同的
|
|||
|
# 列举一个文件的所有行,然后依次对比另一个文件的所有行,如果相同 + 1,不同不管他
|
|||
|
# 3. 对于不同的文件,加入到不同文件数组中进行比较
|
|||
|
# 该数组中,相同行数 + 1
|
|||
|
pass
|
|||
|
|
|||
|
|
|||
|
def replace_first_occurrence(text, old_word, new_word):
|
|||
|
index = text.find(old_word)
|
|||
|
if index != -1:
|
|||
|
return text[:index] + new_word + text[index + len(old_word):]
|
|||
|
else:
|
|||
|
return text
|
|||
|
|
|||
|
|
|||
|
def check_char(c, encode):
|
|||
|
code = c.encode(encode)
|
|||
|
print(code)
|
|||
|
print(encode, " code = ", code.hex())
|