forked from bin-liu/TYComponent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateLicense.py
More file actions
executable file
·96 lines (87 loc) · 2.38 KB
/
updateLicense.py
File metadata and controls
executable file
·96 lines (87 loc) · 2.38 KB
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
import sys
import os
import re
import shutil
FILES = []
LICENSE = []
TEMP_SUFFIX = '.tmp'
def scanfiles(basedir):
print basedir
global FILES
pattern = re.compile(r'.*\.(java)')
patternOfCompileDir = re.compile(r'.*\/((bin|gen)(\/.*)?|\..*)$')
for dirpath, dirnames, filenames in os.walk(basedir):
if patternOfCompileDir.match(dirpath):
continue
for filename in filenames:
type = filename[filename.rfind('.'):]
if len(type) < 1:
continue
match = pattern.match(type)
if match:
FILES.append(os.path.join(dirpath, filename))
print r'SCAN FILE RESULT LIST'
print r'---------------------'
for filename in FILES:
print filename
print 'count is {0}'.format(len(FILES))
print r'---------------------'
def readLicense():
global LICENSE
with open ('LICENSE.txt', 'r') as file:
LICENSE = file.readlines()
print 'LICENSE'
print r'---------------------'
for line in LICENSE:
print line[:line.rfind('\n')]
print r'---------------------'
def updateLicense():
global FILES
global LICENSE
patternOfStart = re.compile(r'^\s*\/\s*\*.*')
patternOfEnd = re.compile(r'.*\*\s*\/\s*$')
startMatch = ''
endMatch = ''
print 'start update...'
for filename in FILES:
with open (filename, 'rw') as file:
lines = file.readlines()
if len(lines) < 1:
continue
startMatch = patternOfStart.match(lines[0])
if startMatch:
##print 'match header'
for index, line in enumerate(lines):
endMatch = patternOfEnd.match(line)
if endMatch:
print 'old license is from {0} to {1}'.format(0, index)
writeToTmpFile(filename, LICENSE, lines[index + 1:])
replaceTmpFile(filename)
print 'modify ' + filename
break
else:
##print 'add license'
writeToTmpFile(filename, LICENSE, lines)
replaceTmpFile(filename)
print 'modify ' + filename
print 'end update'
def createTmpFile(filename):
return filename + TEMP_SUFFIX;
def writeToTmpFile(filename, *lines):
if len(lines) < 1:
return
with open (createTmpFile(filename), 'w') as file:
for alines in lines:
if len(alines) < 1:
continue
for line in alines:
##print >> file, line
file.write('%s' % line)
def replaceTmpFile(filename):
shutil.move(createTmpFile(filename), filename)
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit('please enter a root dir which will be iterated to update license.')
scanfiles(sys.argv[1])
readLicense()
updateLicense()