Project Ne10
An Open Optimized Software Library Project for the ARM Architecture
Loading...
Searching...
No Matches
change_copyright.py
2# Copyright 2014 ARM Limited and Contributors.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7# * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution.
12# * Neither the name of ARM Limited nor the
13# names of its contributors may be used to endorse or promote products
14# derived from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19# DISCLAIMED. IN NO EVENT SHALL ARM LIMITED AND CONTRIBUTORS BE LIABLE FOR ANY
20# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27
28'''change the copy right year
29there are 4 cases
30 1 2014 => 2014
31 2 20xx => 20xx-14
32 3 20xx-bb => 20xx-14
33 4 20xx-14 => 20xx-14
34'''
35import re, os, sys
36import fileinput
37import argparse
38
39def extend_copyright(line, year):
40 '''year is the year which you want to extend to'''
41
42 #match the format like 'Copyright 2014 ARM Limited' or 'Copyright 2011-14 ARM Limited'
43 p2014 = re.compile(r'.*{}.*'.format(year))
44 if p2014.match(line):
45 return line
46
47 #match the format like 'Copyright 2011-12 ARM Limited'
48 p20xx_bb = re.compile(r'(.*)(20\d\d)(-)(\d\d)(.*)')
49 m = p20xx_bb.match(line)
50 if m:
51 return p20xx_bb.sub(r'\g<1>\g<2>\g<3>{}\g<5>'.format(year), line)
52
53 #match the format like 'Copyright 2012 ARM Limited'
54 p20xx = re.compile(r'(.*)(20\d\d)(.*)')
55 m = p20xx.match(line)
56 if m:
57 return p20xx.sub(r'\g<1>\g<2>-{}\g<3>'.format(year), line)
58
59def replace_line(file,search_exp,replace_exp):
60 for line in fileinput.input(file, inplace=1):
61 if search_exp in line:
62 line = line.replace(search_exp, replace_exp)
63 sys.stdout.write(line)
64
65def test():
66 year = '14'
67 if extend_copyright('Copyright 2011-12 ARM Limited', year) != 'Copyright 2011-14 ARM Limited':
68 print "test failed"
69 return
70
71 if extend_copyright('Copyright 2013-14 ARM Limited', year) != 'Copyright 2013-14 ARM Limited':
72 print "test failed"
73 return
74
75 if extend_copyright('Copyright 2012 ARM Limited', year) != 'Copyright 2012-14 ARM Limited':
76 print "test failed"
77 return
78
79 if extend_copyright('Copyright 2014 ARM Limited', year) != 'Copyright 2014 ARM Limited':
80 print "test failed"
81 return
82
83 print "test success."
84
85
86def extend_copyright_all(extend_to_year):
87 all_files = []
88 for root, dirs, files in os.walk(os.getcwd()):
89 for f in files:
90 #exclude this script file
91 if f != os.path.basename(sys.argv[0]):
92 all_files.append(os.path.join(root, f))
93
94 pcopy_right = re.compile(r'.*Copyright [0-9-]* ARM Limited.*')
95 for f in all_files:
96 fd = open(f, 'r')
97 for line in fd.readlines():
98 m = pcopy_right.match(line)
99 if m:
100 old_line = m.group(0)
101 new_line = extend_copyright(old_line, extend_to_year)
102 fd.close()
103 replace_line(f, old_line, new_line)
104
105def main():
106 parser = argparse.ArgumentParser(description='Extend copyright year to the year you specified.')
107 parser.add_argument('year', nargs='?', help='year you want to extend, only 2 digitals, e.g.\'14\'')
108 parser.add_argument('-t', '--test', action='store_true', help='run the test')
109 args = parser.parse_args()
110 if args.test:
111 test()
112 return
113 else:
114 #check input year includes 2 digitals
115 pdigital2 = re.compile(r'^\d\d$')
116 if args.year and pdigital2.search(args.year):
117 extend_copyright_all(args.year)
118 else:
119 parser.print_help()
120
121if __name__ == '__main__':
122 main()