面對龐大的 C/C++ 混合代碼庫(.c/.cc/.cpp/.h/.hh),需刪除包含特定關鍵行(如版權聲明)的多行註釋塊,且註釋格式多樣(/* */、//、前後夾雜其他文字)。本文提供基於行掃描的健壯 Python 解決方案。
INSIDE_COMMENT、COMMENT_START_LINE、MATCHED_FEATURE。/* 或 // 進入註釋狀態,記錄起始行號。MATCHED_FEATURE=True。*/ 或行結束(//)退出註釋狀態,若 MATCHED_FEATURE 則記錄結束行號。#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, fileinput
TARGET_EXTS = {'.c', '.cc', '.cpp', '.h', '.hh'}
FEATURE_LINES = [
"Copyright 2002 Sun Microsystems, Inc. All rights reserved.",
"Redistribution and use in source and binary forms",
"Neither the name of Sun Microsystems"
]
def is_target_file(path):
return os.path.splitext(path)[1] in TARGET_EXTS and not os.path.islink(path)
def process_file(path):
# 第一遍:掃描確定要刪除的行區間
delete_ranges = [] # list of (start, end) 1-based line numbers
in_comment = False
comment_start = 0
matched = False
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
for lineno, line in enumerate(f, 1):
stripped = line.strip()
# 進入註釋
if not in_comment:
if stripped.startswith('/*') or stripped.startswith('//'):
in_comment = True
comment_start = lineno
matched = any(feat in line for feat in FEATURE_LINES)
# 在註釋中
if in_comment:
if any(feat in line for feat in FEATURE_LINES):
matched = True
# 退出註釋
if stripped.endswith('*/') or stripped.startswith('//'):
if matched:
delete_ranges.append((comment_start, lineno))
in_comment = False
matched = False
if not delete_ranges:
return False # 無需修改
# 第二遍:寫入臨時文件,跳過刪除區間
tmp_path = path + '.tmp'
with open(path, 'r', encoding='utf-8', errors='ignore') as fin, \
open(tmp_path, 'w', encoding='utf-8') as fout:
for lineno, line in enumerate(fin, 1):
if not any(start <= lineno <= end for start, end in delete_ranges):
fout.write(line)
# 原子替換(保留權限/時間戳)
os.replace(tmp_path, path)
return True
def main(root):
if not os.path.exists(root):
print(f"Error: {root} not exist")
return
count = 0
for dirpath, _, filenames in os.walk(root):
for fn in filenames:
full = os.path.join(dirpath, fn)
if is_target_file(full):
if process_file(full):
print(f"Cleaned: {full}")
count += 1
print(f"Done. {count} files modified.")
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python strip_copyright.py ")
sys.exit(1)
main(sys.argv[1])
| 問題 | 解決方案 |
|---|---|
| 編碼未知 | open(..., errors='ignore') 忽略非法字節 |
| 軟鏈接 | os.path.islink() 跳過 |
| 大文件內存 | 兩遍流式讀寫,不載入全文 |
| 原子性 | os.replace() 原子替換,避免寫坏源文件 |
| 權限保留 | os.replace 保留 inode、權限、時間戳 |