yang.li | 8c78ed6 | 2022-03-08 13:50:52 +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 | |
| 9 | import os |
| 10 | import sys |
| 11 | import string |
| 12 | import re |
| 13 | |
| 14 | def add_Header_01(filepath, filename): |
| 15 | if os.path.exists(filepath) : |
| 16 | head_info = string.Template( |
| 17 | '''/* |
| 18 | * Copyright (c) 2022 Amlogic, Inc. All rights reserved. |
| 19 | * |
| 20 | * This source code is subject to the terms and conditions defined in the |
| 21 | * file 'LICENSE' which is part of this source code package. |
| 22 | * |
| 23 | */\n |
| 24 | ''') |
| 25 | head = head_info.substitute(vars()) |
| 26 | f = open(filepath, "r", errors='ignore', newline='') |
| 27 | content = f.read() |
| 28 | f_w = open(filepath, "w", errors='ignore', newline='') |
| 29 | f_w.seek(0,0) |
| 30 | |
| 31 | if(content.find('\r\n') > 0): |
| 32 | head_new = re.sub("\n", "\r\n", head) |
| 33 | f_w.write(head_new) |
| 34 | else: |
| 35 | f_w.write(head) |
| 36 | |
| 37 | f_w.write(content) |
| 38 | f_w.close |
| 39 | |
| 40 | |
| 41 | def add_Header_02(filepath, filename): |
| 42 | if os.path.exists(filepath) : |
| 43 | head_info = string.Template( |
| 44 | '''# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 45 | |
| 46 | # SPDX-License-Identifier: MIT\n |
| 47 | ''') |
| 48 | head = head_info.substitute(vars()) |
| 49 | f = open(filepath, "r", errors='ignore', newline='') |
| 50 | content = f.read() |
| 51 | f_w = open(filepath, "w", errors='ignore', newline='') |
| 52 | f_w.seek(0,0) |
| 53 | |
| 54 | if(content.find('\r\n') > 0): |
| 55 | head_new = re.sub("\n", "\r\n", head) |
| 56 | f_w.write(head_new) |
| 57 | else: |
| 58 | f_w.write(head) |
| 59 | |
| 60 | f_w.write(content) |
| 61 | f_w.close |
| 62 | |
| 63 | def add_Header_03(filepath, filename): |
| 64 | if os.path.exists(filepath) : |
| 65 | head_info = string.Template( |
| 66 | '''# |
| 67 | # Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 68 | # |
| 69 | # SPDX-License-Identifier: MIT |
| 70 | #\n |
| 71 | ''') |
| 72 | head = head_info.substitute(vars()) |
| 73 | f = open(filepath, "r", errors='ignore', newline='') |
| 74 | content = f.read() |
| 75 | f_w = open(filepath, "w", errors='ignore', newline='') |
| 76 | f_w.seek(0,0) |
| 77 | |
| 78 | if(content.find('\r\n') > 0): |
| 79 | head_new = re.sub("\n", "\r\n", head) |
| 80 | f_w.write(head_new) |
| 81 | else: |
| 82 | f_w.write(head) |
| 83 | |
| 84 | f_w.write(content) |
| 85 | f_w.close |
| 86 | |
| 87 | def del_C_Header(filepath): |
| 88 | if os.path.exists(filepath) : |
| 89 | file = open(filepath, "r", errors='ignore', newline='') |
| 90 | lines = file.readlines() |
| 91 | beforeTag = True |
| 92 | writer = open(filepath, 'w', errors='ignore', newline='') |
| 93 | for line in lines : |
| 94 | if '#include' in line: |
| 95 | beforeTag = False |
| 96 | if beforeTag == False: |
| 97 | writer.write(line) |
| 98 | |
| 99 | def del_H_Header(filepath): |
| 100 | if os.path.exists(filepath): |
| 101 | file = open(filepath, "r", errors='ignore', newline='') |
| 102 | lines = file.readlines() |
| 103 | beforeTag = True |
| 104 | writer = open(filepath, 'w', errors='ignore', newline='') |
| 105 | for line in lines : |
| 106 | if '#ifndef ' in line or '#include ' in line or '#define ' in line or '#ifdef' in line: |
| 107 | beforeTag = False |
| 108 | if beforeTag == False: |
| 109 | writer.write(line) |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | path=sys.argv[1] |
| 113 | list = os.walk(path, True) |
| 114 | for dir in list: |
| 115 | files = dir[2] |
| 116 | for file in files: |
| 117 | filepath = os.path.join(dir[0], file) |
| 118 | print(filepath) |
| 119 | if 'Kconfig' in file or 'CMakeList' in file: |
| 120 | add_Header_02(filepath, file) |
| 121 | elif 'defconfig' in file or 'prj.conf' in file: |
| 122 | add_Header_03(filepath, file) |
| 123 | elif '.h' in file or '.ld' in file: |
| 124 | del_H_Header(filepath) |
| 125 | add_Header_01(filepath, file) |
| 126 | elif '.c' in file: |
| 127 | del_C_Header(filepath) |
| 128 | add_Header_01(filepath, file) |
| 129 | |
| 130 | print( 'Complete!!!!!!!!!!!!!!!') |