summaryrefslogtreecommitdiff
path: root/devtools/bin/fixcopyrights.py
blob: ba43986f1f36f3e3b39ca2da64622a82b815713c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
# ========= Copyright Valve Corporation, All rights reserved. ============

import subprocess
import re
import os
import sys

reValve = re.compile( "valve", flags = re.IGNORECASE )
reTurtleRock = re.compile( "turtle rock", flags = re.IGNORECASE )
reCopyright = re.compile( "copyright", flags = re.IGNORECASE )
sOutputCopyright = "//========= Copyright Valve Corporation, All rights reserved. ============//\n"

def IsOldCopyrightLine( line ):
	if( len( reCopyright.findall( line ) ) == 0 ):
		return False
	if( len( reValve.findall( line ) ) == 0 
		and len( reTurtleRock.findall( line ) ) == 0 ):
		return False
	
	return True


rFilesWithNoCopyrightNotice = []

def FixCopyrightNotice( sFullPath ):
	nLine = 0

	f = open( sFullPath, "r" )
	if( not f ):
		print( "Unable to open file " + sFullPath + "\n" )
		return

	rFileContents = f.readlines()
	f.close()
	nOldCopyright = -1
	for line in rFileContents:
		if( nLine < 10 ):
			if( line == sOutputCopyright ):
				# File already has the right notice
				return
			if( IsOldCopyrightLine( line ) ):
				nOldCopyright = nLine
				break

		nLine += 1
	if( nOldCopyright == -1 ):
		rFilesWithNoCopyrightNotice.append( sFullPath )
		rFileContents.insert( 0, sOutputCopyright )
	else:
		rFileContents[ nOldCopyright ] = sOutputCopyright

	# open the file for edit
	subprocess.call( [ "p4", "edit", sFullPath ], stdout = subprocess.PIPE )

	# open the file for writing
	f = open( sFullPath, "w" )
	f.writelines( rFileContents )
	f.close()

rDirsToSkip = [ 
	'thirdparty', 
	'external', 
	'BinkSDK',
	'bink',
	'bink_x360',
	'freetype',
	'GL',
	'maya',
	'miles',
	'curl',
	'ihfx',
	'lxma',
	'modo',
	'openal',
	'opengl',
	'p4api',
	'python',
	'quicktime_win32',
	'xsi',
	'speex',
	'ocaml',
	'perl5',
	'dx10sdk',
	'dx11sdk',
	'dx9sdk',
	'haptics',
	'ajb',
	'stb',
	'havok',
	'hk_physics',
	'lua',
	'maxsdk',
	'x360xdk',
	'swigwin-1.3.34',
	'sapi51',
	'WMPSDK10',
	'FontMaker',
	'mxtk',
	'nvtristriplib',
	'g15',
	'lzma',
	'libparsifal-0.8.3',
	'parsifal',
	'libpng',
	'mysql',
	'zip',
	'zlib',
	'Zlib',
	'windowssdk',
	'bzip2',
	'jpeglib',
	'MakeGameData',
	'toollib',
	]

rFileExtensionsToSkip = [
	'.pb.h',
	'.pb.cpp',
	'.spa.h',
	'ATI_Compress.h',
	'luaxlib.h',
	'lua.h',
	'luaconf.h',
	'lualib.h',
	'eax.h',
	'IceKey.cpp',
	'nvtc.h',
	'amd3dx.h',
	'halton.h',
	'snappy',
	'extendedtrace',
	]

def FixCopyrightNoticeWalk( sPath ):
	for root, dirs, files in os.walk( sPath ):
		print "Walking directory", root
		#print root, dirs
		for sDir in rDirsToSkip:
			if sDir in dirs:
				print "Skipping dir ", os.path.join( root, sDir )
				dirs.remove( sDir )

		for sFilename in files:
			sShortFilename, sFileExt = os.path.splitext( sFilename )

			if( sFileExt in [ '.cpp', '.h' ] ):
				bSkip = False
				for sExt in rFileExtensionsToSkip:
					if sExt in sFilename:
						bSkip = True

				#print "filename=", sFilename 
				if( bSkip ):
					print "Skipping ", sFilename, "because of its extension"
				else:
					FixCopyrightNotice( os.path.join( root, sFilename ) )


#FixCopyrightNotice( os.path.join( "..", "..", "bitmap", "bitmap.cpp" ) )
#FixCopyrightNoticeWalk( os.path.join( "..", "..", "bitmap" ) )

if( len( sys.argv ) != 2 ):
	print "Usage: fixcopyrights.py <path>"
	sys.exit(1)

FixCopyrightNoticeWalk( sys.argv[1] )

if( len( rFilesWithNoCopyrightNotice ) ):

	f = open( "newcopyrights.txt", "w" )
	for file in rFilesWithNoCopyrightNotice:
		f.write( file + "\n" )
	f.close()

	print "Copyright notices added to", len( rFilesWithNoCopyrightNotice ), "files. See newcopyrights.txt for a list\n"