add boudle_code.py
This commit is contained in:
parent
9e33822bdd
commit
499530575d
1
.gitignore
vendored
1
.gitignore
vendored
@ -37,3 +37,4 @@ build/
|
||||
.DS_Store
|
||||
/knowledge-base-server-log/
|
||||
knowledge-base-server.iml
|
||||
project_code**
|
||||
77
boudle_code.py
Normal file
77
boudle_code.py
Normal file
@ -0,0 +1,77 @@
|
||||
import os
|
||||
import datetime
|
||||
|
||||
# ================= 配置区域 =================
|
||||
# 输出文件名包含版本号(时间戳)
|
||||
TIMESTAMP = datetime.datetime.now().strftime("%Y%m%d_%H%M")
|
||||
OUTPUT_FILE = f"project_code_v{TIMESTAMP}.txt"
|
||||
|
||||
# 需要包含的文件后缀 (根据你的截图,主要是 Java, XML, Yaml, Dockerfile, Shell)
|
||||
INCLUDE_EXTENSIONS = {'.java', '.xml', '.yml', '.yaml', '.properties', '.sql', '.sh', 'Dockerfile', '.md'}
|
||||
|
||||
# 需要完全匹配的文件名 (无后缀的文件,如 Dockerfile)
|
||||
INCLUDE_FILENAMES = {'Dockerfile', 'Makefile', 'Jenkinsfile'}
|
||||
|
||||
# 需要忽略的目录
|
||||
IGNORE_DIRS = {
|
||||
'target', '.idea', '.git', '.mvn', 'wrapper', 'build',
|
||||
'arthas-output', 'knowledge-base-server-log', 'test' # 如果不需要测试代码,可以加上 'test'
|
||||
}
|
||||
# ===========================================
|
||||
|
||||
def generate_tree(startpath):
|
||||
"""生成项目目录树结构字符串"""
|
||||
tree_str = "【项目目录结构】:\n"
|
||||
for root, dirs, files in os.walk(startpath):
|
||||
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS]
|
||||
level = root.replace(startpath, '').count(os.sep)
|
||||
indent = ' ' * 4 * (level)
|
||||
tree_str += '{}{}/\n'.format(indent, os.path.basename(root))
|
||||
subindent = ' ' * 4 * (level + 1)
|
||||
for f in files:
|
||||
# 简单过滤显示在树中的文件
|
||||
if any(f.endswith(ext) for ext in INCLUDE_EXTENSIONS) or f in INCLUDE_FILENAMES:
|
||||
tree_str += '{}{}\n'.format(subindent, f)
|
||||
return tree_str + "\n" + "="*50 + "\n\n"
|
||||
|
||||
def is_target_file(filename):
|
||||
"""判断是否为目标文件"""
|
||||
if filename in INCLUDE_FILENAMES:
|
||||
return True
|
||||
_, ext = os.path.splitext(filename)
|
||||
return ext in INCLUDE_EXTENSIONS
|
||||
|
||||
def merge_files():
|
||||
root_dir = os.getcwd() # 获取当前脚本所在目录
|
||||
|
||||
with open(OUTPUT_FILE, 'w', encoding='utf-8') as outfile:
|
||||
# 1. 写入目录树
|
||||
print("正在生成目录结构...")
|
||||
outfile.write(generate_tree(root_dir))
|
||||
|
||||
# 2. 遍历并写入文件内容
|
||||
print("正在合并代码文件...")
|
||||
for root, dirs, files in os.walk(root_dir):
|
||||
# 移除忽略的目录
|
||||
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS]
|
||||
|
||||
for file in files:
|
||||
if is_target_file(file):
|
||||
file_path = os.path.join(root, file)
|
||||
rel_path = os.path.relpath(file_path, root_dir)
|
||||
|
||||
# 写入文件头分割线
|
||||
header = f"\n\n{'='*20}\nFile Path: {rel_path}\n{'='*20}\n"
|
||||
outfile.write(header)
|
||||
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as infile:
|
||||
outfile.write(infile.read())
|
||||
except Exception as e:
|
||||
outfile.write(f"\n[Error reading file: {e}]\n")
|
||||
print(f"Error reading {rel_path}: {e}")
|
||||
|
||||
print(f"✅ 完成!文件已保存为: {OUTPUT_FILE}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
merge_files()
|
||||
@ -1,4 +1,7 @@
|
||||
redis-cli --raw
|
||||
|
||||
# 获取redis中的key-value
|
||||
redis-cli keys "kb*" | while read key; do echo "$key : $(redis-cli get "$key")"; done
|
||||
redis-cli keys "kb*" | while read key; do echo "$key : $(redis-cli get "$key")"; done
|
||||
|
||||
# 清空redis数据库
|
||||
flushdb
|
||||
Loading…
x
Reference in New Issue
Block a user