1e5c31af7Sopenharmony_ci# -*- coding: utf-8 -*- 2e5c31af7Sopenharmony_ci 3e5c31af7Sopenharmony_ci#------------------------------------------------------------------------- 4e5c31af7Sopenharmony_ci# drawElements Quality Program utilities 5e5c31af7Sopenharmony_ci# -------------------------------------- 6e5c31af7Sopenharmony_ci# 7e5c31af7Sopenharmony_ci# Copyright 2015 The Android Open Source Project 8e5c31af7Sopenharmony_ci# 9e5c31af7Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 10e5c31af7Sopenharmony_ci# you may not use this file except in compliance with the License. 11e5c31af7Sopenharmony_ci# You may obtain a copy of the License at 12e5c31af7Sopenharmony_ci# 13e5c31af7Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 14e5c31af7Sopenharmony_ci# 15e5c31af7Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 16e5c31af7Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 17e5c31af7Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18e5c31af7Sopenharmony_ci# See the License for the specific language governing permissions and 19e5c31af7Sopenharmony_ci# limitations under the License. 20e5c31af7Sopenharmony_ci# 21e5c31af7Sopenharmony_ci#------------------------------------------------------------------------- 22e5c31af7Sopenharmony_ci 23e5c31af7Sopenharmony_ciimport os 24e5c31af7Sopenharmony_ciimport re 25e5c31af7Sopenharmony_ciimport sys 26e5c31af7Sopenharmony_ciimport time 27e5c31af7Sopenharmony_ciimport fnmatch 28e5c31af7Sopenharmony_ci 29e5c31af7Sopenharmony_ciSRC_FILE_PATTERNS = [ "*.c", "*.h", "*.cpp", "*.hpp", "*.inl", "*.java", "*.aidl", "*.py" ] 30e5c31af7Sopenharmony_ciCOPYRIGHT_PATTERN = r'Copyright \(C\) ([0-9]{4})(-[0-9]{4})? drawElements Ltd.' 31e5c31af7Sopenharmony_ciCOPYRIGHT_REPLACEMENT = r'Copyright (C) \1-' + time.strftime("%Y") + r' drawElements Ltd.' 32e5c31af7Sopenharmony_ci 33e5c31af7Sopenharmony_cidef isSrcFile (filename): 34e5c31af7Sopenharmony_ci for pattern in SRC_FILE_PATTERNS: 35e5c31af7Sopenharmony_ci if fnmatch.fnmatch(filename, pattern): 36e5c31af7Sopenharmony_ci return True 37e5c31af7Sopenharmony_ci return False 38e5c31af7Sopenharmony_ci 39e5c31af7Sopenharmony_cidef findSrcFiles (dir): 40e5c31af7Sopenharmony_ci srcFiles = [] 41e5c31af7Sopenharmony_ci for root, dirs, files in os.walk(dir): 42e5c31af7Sopenharmony_ci for file in files: 43e5c31af7Sopenharmony_ci if isSrcFile(file): 44e5c31af7Sopenharmony_ci srcFiles.append(os.path.join(root, file)) 45e5c31af7Sopenharmony_ci return srcFiles 46e5c31af7Sopenharmony_ci 47e5c31af7Sopenharmony_cidef processFile (filename): 48e5c31af7Sopenharmony_ci print(filename) 49e5c31af7Sopenharmony_ci file = open(filename, "rb") 50e5c31af7Sopenharmony_ci data = file.read() 51e5c31af7Sopenharmony_ci file.close() 52e5c31af7Sopenharmony_ci data = re.sub(COPYRIGHT_PATTERN, COPYRIGHT_REPLACEMENT, data) 53e5c31af7Sopenharmony_ci file = open(filename, "wb") 54e5c31af7Sopenharmony_ci file.write(data) 55e5c31af7Sopenharmony_ci file.close() 56e5c31af7Sopenharmony_ci 57e5c31af7Sopenharmony_cidef processDir (dir): 58e5c31af7Sopenharmony_ci srcFiles = findSrcFiles(dir) 59e5c31af7Sopenharmony_ci for file in srcFiles: 60e5c31af7Sopenharmony_ci processFile(file) 61e5c31af7Sopenharmony_ci 62e5c31af7Sopenharmony_ciif __name__ == "__main__": 63e5c31af7Sopenharmony_ci if len(sys.argv) < 2: 64e5c31af7Sopenharmony_ci print(sys.argv[0] + ": [directory]") 65e5c31af7Sopenharmony_ci else: 66e5c31af7Sopenharmony_ci processDir(sys.argv[1]) 67