chore: 新增replace_comments.py 脚本,使用python replace_comments.py --path ./src来去掉版权注释及空注释

This commit is contained in:
2025-11-29 16:45:45 +08:00
parent 9d79b2585e
commit 1c9e72e28d

170
replace_comments.py Normal file
View File

@@ -0,0 +1,170 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import argparse
# 定义要查找和替换的注释内容
# 使用原始字符串数组支持多个旧内容避免Unicode转义问题
OLD_COMMENTS = [
r"""
<?php
/**
*/
""".strip(),
r"""
<?php
/**
*/
""".strip(),
# 格式1标准注释格式
r"""
<?php
/**
*/
""".strip(),
r"""
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
""".strip(),
# 格式1标准注释格式
r"""
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/""".strip(),
# 格式2带有额外空行的注释格式
r"""
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/""".strip(),
]
NEW_COMMENT = r"""
<?php
""".strip()
# 定义要处理的文件类型
FILE_TYPES = [".php", ".js", ".css", ".html", ".vue", ".ts", ".tsx", ".jsx", ".scss", ".less"]
def replace_comments(file_path):
"""
替换文件中的注释
"""
try:
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 检查文件是否包含任何旧注释
has_old_comment = False
for old_comment in OLD_COMMENTS:
if old_comment in content:
has_old_comment = True
break
if has_old_comment:
# 替换所有旧注释
new_content = content
for old_comment in OLD_COMMENTS:
if old_comment in new_content:
new_content = new_content.replace(old_comment, NEW_COMMENT)
# 写回文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"✓ 已处理: {file_path}")
return True
return False
except UnicodeDecodeError:
# 跳过二进制文件
print(f"✗ 跳过二进制文件: {file_path}")
return False
except PermissionError:
# 跳过没有权限的文件
print(f"✗ 权限不足: {file_path}")
return False
except Exception as e:
# 处理其他异常
print(f"✗ 处理失败 {file_path}: {str(e)}")
return False
def main():
"""
主函数
"""
parser = argparse.ArgumentParser(description="替换Niushop商城系统的注释")
parser.add_argument("--path", type=str, default=".", help="要遍历的目录路径")
args = parser.parse_args()
root_path = args.path
total_files = 0
processed_files = 0
print(f"开始遍历目录: {root_path}")
print(f"将处理的文件类型: {', '.join(FILE_TYPES)}")
print("=" * 60)
# 遍历所有文件
for root, dirs, files in os.walk(root_path):
# 跳过某些目录(如.git、vendor、node_modules等
dirs[:] = [d for d in dirs if d not in ['.git', 'vendor', 'node_modules', 'runtime', 'upload', 'public', 'static']]
for file in files:
# 检查文件类型
if any(file.endswith(ext) for ext in FILE_TYPES):
total_files += 1
file_path = os.path.join(root, file)
if replace_comments(file_path):
processed_files += 1
print("=" * 60)
print(f"处理完成!")
print(f"总文件数: {total_files}")
print(f"已处理文件数: {processed_files}")
print(f"替换率: {processed_files / total_files * 100:.2f}%" if total_files > 0 else "未找到匹配的文件")
if __name__ == "__main__":
main()