yang.li | ffa60e5 | 2022-01-11 14:38:56 +0800 | [diff] [blame^] | 1 | #!/usr/bin/python3 |
| 2 | #coding:utf-8 |
| 3 |
|
| 4 | # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
|
| 5 |
|
| 6 | # SPDX-License-Identifier: MIT
|
| 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import string |
| 11 | import re |
| 12 | |
| 13 | def add_Header_01(filepath, filename): |
| 14 | if os.path.exists(filepath) : |
| 15 | head_info = string.Template( |
| 16 | '''/* |
| 17 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 18 | * |
| 19 | * SPDX-License-Identifier: MIT |
| 20 | */\n |
| 21 | ''') |
| 22 | head = head_info.substitute(vars()) |
| 23 | f = open(filepath, "r+", errors='ignore') |
| 24 | content = f.read() |
| 25 | |
| 26 | f.seek(0,0) |
| 27 | new_content = re.sub("\r\n", "\n", content) |
| 28 | f.write(head) |
| 29 | f.write(new_content) |
| 30 | f.close |
| 31 | |
| 32 | |
| 33 | def add_Header_02(filepath, filename): |
| 34 | if os.path.exists(filepath) : |
| 35 | head_info = string.Template( |
| 36 | '''# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 37 | |
| 38 | # SPDX-License-Identifier: MIT\n |
| 39 | ''') |
| 40 | head = head_info.substitute(vars()) |
| 41 | f = open(filepath, "r+", errors='ignore') |
| 42 | content = f.read() |
| 43 | |
| 44 | f.seek(0,0) |
| 45 | new_content = re.sub("\r\n", "\n", content) |
| 46 | f.write(head) |
| 47 | f.write(new_content) |
| 48 | f.close |
| 49 | |
| 50 | def del_C_Header(filepath): |
| 51 | if os.path.exists(filepath) : |
| 52 | file = open(filepath, "r", errors='ignore') |
| 53 | lines = file.readlines() |
| 54 | beforeTag = True |
| 55 | writer = open(filepath, 'w', errors='ignore') |
| 56 | for line in lines : |
| 57 | if '#include' in line: |
| 58 | beforeTag = False |
| 59 | if beforeTag == False: |
| 60 | writer.write(line) |
| 61 | |
| 62 | def del_H_Header(filepath): |
| 63 | if os.path.exists(filepath): |
| 64 | file = open(filepath, "r", errors='ignore') |
| 65 | lines = file.readlines() |
| 66 | beforeTag = True |
| 67 | writer = open(filepath, 'w', errors='ignore') |
| 68 | for line in lines : |
| 69 | if '#ifndef ' in line or '#include ' in line or '#define ' in line: |
| 70 | beforeTag = False |
| 71 | if beforeTag == False: |
| 72 | writer.write(line) |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | path=sys.argv[1] |
| 76 | list = os.walk(path, True) |
| 77 | for dir in list: |
| 78 | files = dir[2] |
| 79 | for file in files: |
| 80 | filepath = os.path.join(dir[0], file) |
| 81 | print(filepath)
|
| 82 | if 'Kconfig' in file or 'CMakeList' in file:
|
| 83 | add_Header_02(filepath, file)
|
| 84 | elif '.cmake' in file:
|
| 85 | add_Header_02(filepath, file)
|
| 86 | elif '.h' in file or '.ld' in file: |
| 87 | del_H_Header(filepath) |
| 88 | add_Header_01(filepath, file) |
| 89 | elif '.c' in file: |
| 90 | del_C_Header(filepath) |
| 91 | add_Header_01(filepath, file) |
| 92 | |
| 93 | print( 'Complete!!!!!!!!!!!!!!!') |