blob: b79a77dc53769ca693bdd87ef18204656bd14c39 [file] [log] [blame]
Javi Merinoaace7c02015-08-10 14:10:47 +01001# Copyright 2015-2015 ARM Limited
Javi Merinoa55880f2015-03-10 17:47:10 +00002#
Javi Merinoaace7c02015-08-10 14:10:47 +01003# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
Javi Merinoa55880f2015-03-10 17:47:10 +000016
17from datetime import date
Javi Merino2f7ee662015-09-22 19:15:58 +010018from glob import glob
Javi Merinoa55880f2015-03-10 17:47:10 +000019import os
Javi Merinodea8e9d2015-08-10 16:37:55 +010020import re
Javi Merinoa55880f2015-03-10 17:47:10 +000021import unittest
22
23
24def copyright_is_valid(fname):
25 """Return True if fname has a valid copyright"""
26 with open(fname) as fin:
27 # Read the first 2K of the file. If the copyright is not there, you
28 # are probably doing something wrong
29 lines = fin.readlines(2048)
30
Javi Merinodea8e9d2015-08-10 16:37:55 +010031 # Either the first or the second line must have a "Copyright:" line
32 first_line = re.compile(r"(#| \*) Copyright")
Javi Merino69b10c22015-09-22 14:47:06 +010033 try:
34 if not first_line.search(lines[0]):
35 if first_line.search(lines[1]):
36 # Drop the first line to align the copyright to lines[0]
37 lines = lines[1:]
38 else:
39 return False
40 except IndexError:
41 return False
Javi Merinoa55880f2015-03-10 17:47:10 +000042
Javi Merinoaace7c02015-08-10 14:10:47 +010043 # The copyright mentions ARM Limited
44 if "ARM Limited" not in lines[0]:
Javi Merinoa55880f2015-03-10 17:47:10 +000045 return False
46
Javi Merinoaace7c02015-08-10 14:10:47 +010047 # The Copyright includes the current year
Javi Merinoa55880f2015-03-10 17:47:10 +000048 current_year = date.today().year
Javi Merinoaace7c02015-08-10 14:10:47 +010049 if str(current_year) not in lines[0]:
Javi Merinoa55880f2015-03-10 17:47:10 +000050 return False
51
Javi Merinoaace7c02015-08-10 14:10:47 +010052 # It's the apache license
Javi Merinodea8e9d2015-08-10 16:37:55 +010053 if "http://www.apache.org/licenses/LICENSE-2.0" not in lines[6]:
Javi Merinoa55880f2015-03-10 17:47:10 +000054 return False
55
56 return True
57
58
59class TestCopyRight(unittest.TestCase):
60 def test_copyrights(self):
61 """Check that all files have valid copyrights"""
Javi Merinoa55880f2015-03-10 17:47:10 +000062
Kapileshwar Singh84cbf3b2015-09-02 15:49:42 +010063 tests_dir = os.path.dirname(os.path.abspath(__file__))
64 base_dir = os.path.dirname(tests_dir)
Javi Merino2f7ee662015-09-22 19:15:58 +010065 patterns_to_ignore = {}
Kapileshwar Singh84cbf3b2015-09-02 15:49:42 +010066
Javi Merino45010cb2015-09-22 15:39:01 +010067 for root, dirs, files in os.walk(base_dir):
Javi Merino2f7ee662015-09-22 19:15:58 +010068 if ".gitignore" in files:
69 fname = os.path.join(root, ".gitignore")
70 with open(fname) as fin:
71 lines = fin.readlines()
72
73 patterns_to_ignore[root] = [l.strip() for l in lines]
74
75 files_to_ignore = []
76 for directory, patterns in patterns_to_ignore.iteritems():
77 if root.startswith(directory):
78 for pat in patterns:
79 pat = os.path.join(root, pat)
80 files_to_ignore.extend(glob(pat))
81
82 for dirname in dirs:
83 full_dirname = os.path.join(root, dirname)
84 if full_dirname in files_to_ignore:
85 dirs.remove(dirname)
86
87
Kapileshwar Singh84cbf3b2015-09-02 15:49:42 +010088 for fname in files:
89 fname = os.path.join(root, fname)
Javi Merino2f7ee662015-09-22 19:15:58 +010090 if fname in files_to_ignore:
91 continue
92
Kapileshwar Singh84cbf3b2015-09-02 15:49:42 +010093 extension = os.path.splitext(fname)[1]
94 if extension in [".py", ".js", ".css"]:
95 if not copyright_is_valid(fname):
96 print("Invalid copyright in {}".format(fname))
97 self.fail()
Javi Merino45010cb2015-09-22 15:39:01 +010098
99 if '.git' in dirs:
100 dirs.remove('.git')