1# -*- coding: utf-8 -*-
2
3import os
4import re
5import sys
6import time
7import fnmatch
8
9SRC_FILE_PATTERNS		= [ "*.c", "*.h", "*.cpp", "*.hpp", "*.inl", "*.java", "*.aidl", "*.py" ]
10COPYRIGHT_PATTERN		= r'Copyright \(C\) ([0-9]{4})(-[0-9]{4})? drawElements Ltd.'
11COPYRIGHT_REPLACEMENT	= r'Copyright (C) \1-' + time.strftime("%Y") + r' drawElements Ltd.'
12
13def isSrcFile (filename):
14	for pattern in SRC_FILE_PATTERNS:
15		if fnmatch.fnmatch(filename, pattern):
16			return True
17	return False
18
19def findSrcFiles (dir):
20	srcFiles = []
21	for root, dirs, files in os.walk(dir):
22		for file in files:
23			if isSrcFile(file):
24				srcFiles.append(os.path.join(root, file))
25	return srcFiles
26
27def processFile (filename):
28	print filename
29	file = open(filename, "rb")
30	data = file.read()
31	file.close()
32	data = re.sub(COPYRIGHT_PATTERN, COPYRIGHT_REPLACEMENT, data)
33	file = open(filename, "wb")
34	file.write(data)
35	file.close()
36
37def processDir (dir):
38	srcFiles = findSrcFiles(dir)
39	for file in srcFiles:
40		processFile(file)
41
42if __name__ == "__main__":
43	if len(sys.argv) < 2:
44		print sys.argv[0] + ": [directory]"
45	else:
46		processDir(sys.argv[1])
47