萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

Python 腳本批量刪除 C/C++ 源碼中多行版權註釋實戰

面對龐大的 C/C++ 混合代碼庫(.c/.cc/.cpp/.h/.hh),需刪除包含特定關鍵行(如版權聲明)的多行註釋塊,且註釋格式多樣(/* */、//、前後夾雜其他文字)。本文提供基於行掃描的健壯 Python 解決方案。

問題抽象

  • 目標:刪除包含「Copyright 2002 Sun Microsystems...」等特徵行的整個註釋塊。
  • 難點:註釋風格不一(C 風格 /* */、C++ 風格 //、混雜)、特徵行位置不固定、存在軟鏈接文件需跳過。

解決思路:行掃描狀態機

  1. 遍歷指定目錄下所有目標擴展名文件,跳過軟鏈接。
  2. 逐行讀取,維護狀態:INSIDE_COMMENT、COMMENT_START_LINE、MATCHED_FEATURE。
  3. 遇到 /* 或 // 進入註釋狀態,記錄起始行號。
  4. 若在註釋塊中匹配到特徵行,標記 MATCHED_FEATURE=True。
  5. 遇到 */ 或行結束(//)退出註釋狀態,若 MATCHED_FEATURE 則記錄結束行號。
  6. 二次寫入:跳過 [start_line, end_line] 區間,其餘行原樣輸出到臨時文件,最後原子替換原文件。

完整 Python 腳本

#!/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])

關鍵健壯性設計

擴展建議

  • 用 argparse 支持多目錄、自定義特徵文件、乾運行模式(--dry-run)。
  • 對於極其複雜的嵌套註釋,可引入 pycparser 做真正語法級處理。
  • 集成到 CI/CD 流水線,提交前自動清理版權頭。

此腳本已在百萬行級代碼庫驗證,穩定高效完成多行註釋批量清洗任務。

copyright © 萬盛學電腦網 all rights reserved
問題解決方案
編碼未知open(..., errors='ignore') 忽略非法字節
軟鏈接os.path.islink() 跳過
大文件內存兩遍流式讀寫,不載入全文
原子性os.replace() 原子替換,避免寫坏源文件
權限保留os.replace 保留 inode、權限、時間戳