import os
import re
import argparse
import shutil
import json
import stat
from typing import List
_ANI_HEADER_LISTS = [
"ani.h",
"native_node_ani.h",
]
_HEADER_PATTERN = re.compile(
r'^\s*#\s*include\s+["<](.*/)?({})[">]'.format(
'|'.join(map(re.escape, _ANI_HEADER_LISTS))
)
)
def process_header_file(file_path):
"""processing single header file"""
modified = False
def process_lines(content):
"""处理文件内容,过滤匹配的行,返回新内容和是否修改的标记"""
new_content = []
line_modified = False
for line in content.splitlines():
if not _HEADER_PATTERN.match(line):
new_content.append(line)
else:
line_modified = True
return '\n'.join(new_content), line_modified
try:
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.read()
new_content, modified = process_lines(content)
if modified:
f.seek(0)
f.write(new_content)
f.truncate()
except Exception as e:
print(f"process file {file_path} failed: {str(e)}")
return modified
def _delete_ani_file(file_path):
try:
os.remove(file_path)
print(f"Deleted ani header file: {file_path}")
except OSError as e:
print(f"Error deleting {file_path}: {str(e)}")
def clean_ndk_ani_headers(ndk_header_path):
if not _ANI_HEADER_LISTS:
print("Warning: ani header file list is empty")
return
file_paths = []
for root, _, files in os.walk(ndk_header_path):
for file in files:
if not file.endswith('.h'):
continue
file_path = os.path.join(root, file)
if file in _ANI_HEADER_LISTS:
_delete_ani_file(file_path)
else:
file_paths.append(file_path)
for file_path in file_paths:
process_header_file(file_path)
def clean_json_system_capability_headers(capability_header_path):
try:
with open(capability_header_path, 'r') as f:
system_capabilities = json.load(f)
except Exception as e:
print(f"Error reading JSON file: {str(e)}")
return
for system_capability in system_capabilities:
system_capabilities[system_capability] = [item for item in system_capabilities[system_capability]
if os.path.basename(item) not in _ANI_HEADER_LISTS]
try:
fd = os.open(capability_header_path, os.O_WRONLY | os.O_TRUNC | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR)
with os.fdopen(fd, 'w') as f:
json.dump(system_capabilities, f, indent=2)
print("JSON file updated successfully")
except Exception as e:
print(f"Error saving JSON file: {str(e)}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--ndk-header-path', help='ndk header path', required=True)
parser.add_argument('--system-capability-header-config', required=True)
args = parser.parse_args()
if not os.path.isdir(args.ndk_header_path):
print(f"Error:path {args.ndk_header_path} is not exist!")
return
clean_ndk_ani_headers(args.ndk_header_path)
clean_json_system_capability_headers(args.system_capability_header_config)
print("Ani Header file cleanup complete!")
if __name__ == '__main__':
main()