From 7b1f13c069f4a08d90060ec07c667bfcebacb273 Mon Sep 17 00:00:00 2001 From: "mila@ringwald.ch" Date: Fri, 6 Feb 2015 14:08:38 +0000 Subject: [PATCH] add update copyright function --- tools/btstack_memory_generator.py | 5 +- tools/update_copyright.py | 103 ++++++++++++++++++++---------- 2 files changed, 70 insertions(+), 38 deletions(-) diff --git a/tools/btstack_memory_generator.py b/tools/btstack_memory_generator.py index cf7b42726..6b3027f57 100755 --- a/tools/btstack_memory_generator.py +++ b/tools/btstack_memory_generator.py @@ -1,8 +1,7 @@ #!/usr/bin/env python -copyright = """ -/* - * Copyright (C) 2009 by BlueKitchen GmbH +copyright = """/* + * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions diff --git a/tools/update_copyright.py b/tools/update_copyright.py index ab0728fdf..76351ce30 100644 --- a/tools/update_copyright.py +++ b/tools/update_copyright.py @@ -2,8 +2,7 @@ import os import re -copyright = """ -/* +copyright = """/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without @@ -41,7 +40,7 @@ copyright = """ */ """ -onlyDumpDifferentCopyright = True +onlyDumpDifferentCopyright = False copyrightString = "Copyright (C) 2014 BlueKitchen GmbH" copyrighters = ["BlueKitchen", "Matthias Ringwald"] @@ -52,11 +51,48 @@ ignoreFiles = ["ant_cmds.h", "rijndael.c", "btstack-config.h", "version.h", "pro class State: SearchStartComment = 0 SearchCopyrighter = 1 + SearchEndComment = 2 def updateCopyright(file_name): - global onlyDumpDifferentCopyright - if not onlyDumpDifferentCopyright: - print file_name, ": Update copyright" + print file_name, ": Update copyright" + + outfile = "tmp_"+file_name + with open(outfile, 'w') as fout: + fout.write(copyright) + + bufferComment = "" + state = State.SearchStartComment + + with open(file_name, 'rb') as fin: + for line in fin: + if state == State.SearchStartComment: + parts = re.match('\s*(/\*).*',line, re.I) + if parts: + state = State.SearchCopyrighter + else: + fout.write(line) + + if state == State.SearchCopyrighter: + parts = re.match('.*(Copyright).*',line, re.I) + if parts: + # drop buffer + bufferComment = "" + state = State.SearchEndComment + else: + bufferComment = bufferComment + line + parts = re.match('\s*(\*/).*',line, re.I) + if parts: + # end of comment, no copyright + fout.write(bufferComment) + bufferComment = "" + state = State.SearchStartComment + + if state == State.SearchEndComment: + parts = re.match('\s*(\*/).*',line, re.I) + if parts: + state = State.SearchStartComment + + os.rename(outfile, file_name) def requiresCopyrightUpdate(file_name): global copyrightString, onlyDumpDifferentCopyright @@ -64,39 +100,36 @@ def requiresCopyrightUpdate(file_name): with open(file_name, "rb") as fin: parts = [] allowedCopyrighters = [] - state = State.SearchStartComment - + for line in fin: - if state == State.SearchStartComment: - parts = re.match('\s*(/\*).*',line, re.I) - if parts: - state = State.SearchCopyrighter - - if state == State.SearchCopyrighter: - parts = re.match('.*(Copyright).*',line, re.I) - if parts: - allowedCopyrighterFound = False - for name in copyrighters: - allowedCopyrighters = re.match('.*('+name+').*',line, re.I) - if allowedCopyrighters: - allowedCopyrighterFound = True - return re.match(copyrightString,line) - - if onlyDumpDifferentCopyright: - print file_name, ": Copyrighter not allowed > ", parts.group() - return False - + parts = re.match('.*(Copyright).*',line, re.I) + if parts: + allowedCopyrighterFound = False + for name in copyrighters: + allowedCopyrighters = re.match('.*('+name+').*',line, re.I) + if allowedCopyrighters: + allowedCopyrighterFound = True + return not re.match('.*('+copyrightString+').*',line) + + if not allowedCopyrighterFound and onlyDumpDifferentCopyright: + print file_name, ": Copyrighter not allowed > ", parts.group() + return False + print file_name, ": File has no copyright" return False -for root, dirs, files in os.walk('../', topdown=True): - dirs[:] = [d for d in dirs if d not in ignoreFolders] - files[:] = [f for f in files if f not in ignoreFiles] - for f in files: - if f.endswith(".h") or f.endswith(".c"): - file_name = root + "/" + f - if requiresCopyrightUpdate(file_name): - updateCopyright(file_name) + +file_name = "att_server.h" +updateCopyright(file_name) + +# for root, dirs, files in os.walk('../', topdown=True): +# dirs[:] = [d for d in dirs if d not in ignoreFolders] +# files[:] = [f for f in files if f not in ignoreFiles] +# for f in files: +# if f.endswith(".h") or f.endswith(".c"): +# file_name = root + "/" + f +# if requiresCopyrightUpdate(file_name): +# updateCopyright(file_name) \ No newline at end of file