1#!/usr/bin/env python 2import os 3import re 4 5copyright = """/* 6 * Copyright (C) 2014 BlueKitchen GmbH 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the copyright holders nor the names of 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 4. Any redistribution, use, or modification is done solely for 21 * personal benefit and not for any commercial purpose or for 22 * monetary gain. 23 * 24 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 28 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 34 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * Please inquire about commercial licensing options at 38 * [email protected] 39 * 40 */ 41""" 42 43onlyDumpDifferentCopyright = False 44copyrightString = "Copyright \(C\) 2014 BlueKitchen GmbH" 45copyrighters = ["BlueKitchen", "Matthias Ringwald"] 46 47ignoreFolders = ["cpputest", "test", "msp-exp430f5438-cc2564b", "msp430f5229lp-cc2564b", "ez430-rf2560", "ios", "chipset/cc256x", "docs", "mtk"] 48ignoreFiles = ["ant_cmds.h", "rijndael.c", "btstack_config.h", "btstack_version.h", "profile.h", "bluetoothdrv.h", 49 "ancs_client_demo.h", "spp_and_le_counter.h", "bluetoothdrv-stub.c", "minimal_peripheral.c", "BTstackDaemonRespawn.c"] 50 51class State: 52 SearchStartComment = 0 53 SearchCopyrighter = 1 54 SearchEndComment = 2 55 SkipCopyright = 3 56 57def updateCopyright(dir_name, file_name): 58 infile = dir_name + "/" + file_name 59 outfile = dir_name + "/tmp_" + file_name 60 61 #print "Update copyright: ", infile 62 63 with open(outfile, 'wt') as fout: 64 fout.write(copyright) 65 66 bufferComment = "" 67 state = State.SearchStartComment 68 69 with open(infile, 'rt') as fin: 70 for line in fin: 71 if state == State.SearchStartComment: 72 parts = re.match('\s*(/\*).*(\*/)',line) 73 if parts: 74 if len(parts.groups()) == 2: 75 # one line comment 76 fout.write(line) 77 continue 78 79 parts = re.match('\s*(/\*).*',line) 80 if parts: 81 # beginning of comment 82 state = State.SearchCopyrighter 83 else: 84 # command line 85 fout.write(line) 86 continue 87 88 if state == State.SearchCopyrighter: 89 parts = re.match('.*(Copyright).*',line) 90 if parts: 91 # ignore Copyright 92 # drop buffer 93 bufferComment = "" 94 state = State.SkipCopyright 95 else: 96 bufferComment = bufferComment + line 97 parts = re.match('.*(\*/)',line) 98 if parts: 99 # end of comment 100 fout.write(bufferComment) 101 bufferComment = "" 102 state = State.SearchStartComment 103 104 if state == State.SkipCopyright: 105 parts = re.match('.*(\*/)',line) 106 if parts: 107 state = State.SearchStartComment 108 109 os.rename(outfile, infile) 110 111 112def requiresCopyrightUpdate(file_name): 113 global copyrightString, onlyDumpDifferentCopyright 114 exactCopyrightFound = False 115 116 with open(file_name, "rb") as fin: 117 for line in fin: 118 parts = re.match('.*('+copyrightString+').*',line) 119 if parts: 120 exactCopyrightFound = True 121 continue 122 123 parts = re.match('.*(Copyright).*',line) 124 if not parts: 125 continue 126 for name in copyrighters: 127 allowedCopyrighters = re.match('.*('+name+').*',line, re.I) 128 if allowedCopyrighters: 129 return True 130 131 if onlyDumpDifferentCopyright: 132 print(file_name, ": Copyrighter not allowed > ", parts.group()) 133 return False 134 135 if not exactCopyrightFound: 136 print(file_name, ": File has no copyright") 137 138 return False 139 140 141# if requiresCopyrightUpdate("../example/panu_demo.c"): 142# print "UPdate" 143# updateCopyright("../example", "panu_demo.c") 144 145 146for root, dirs, files in os.walk('../', topdown=True): 147 dirs[:] = [d for d in dirs if d not in ignoreFolders] 148 files[:] = [f for f in files if f not in ignoreFiles] 149 for f in files: 150 if f.endswith(".h") or f.endswith(".c"): 151 file_name = root + "/" + f 152 if requiresCopyrightUpdate(file_name): 153 updateCopyright(root, f) 154 155 156