blob: 8cf31d35516ba92fc71962e3f9f91f1e1fe769cc [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
18import os
Javi Merinodea8e9d2015-08-10 16:37:55 +010019import re
Javi Merinoa55880f2015-03-10 17:47:10 +000020import unittest
21
22
23def copyright_is_valid(fname):
24 """Return True if fname has a valid copyright"""
25 with open(fname) as fin:
26 # Read the first 2K of the file. If the copyright is not there, you
27 # are probably doing something wrong
28 lines = fin.readlines(2048)
29
Javi Merinodea8e9d2015-08-10 16:37:55 +010030 # Either the first or the second line must have a "Copyright:" line
31 first_line = re.compile(r"(#| \*) Copyright")
Javi Merino69b10c22015-09-22 14:47:06 +010032 try:
33 if not first_line.search(lines[0]):
34 if first_line.search(lines[1]):
35 # Drop the first line to align the copyright to lines[0]
36 lines = lines[1:]
37 else:
38 return False
39 except IndexError:
40 return False
Javi Merinoa55880f2015-03-10 17:47:10 +000041
Javi Merinoaace7c02015-08-10 14:10:47 +010042 # The copyright mentions ARM Limited
43 if "ARM Limited" not in lines[0]:
Javi Merinoa55880f2015-03-10 17:47:10 +000044 return False
45
Javi Merinoaace7c02015-08-10 14:10:47 +010046 # The Copyright includes the current year
Javi Merinoa55880f2015-03-10 17:47:10 +000047 current_year = date.today().year
Javi Merinoaace7c02015-08-10 14:10:47 +010048 if str(current_year) not in lines[0]:
Javi Merinoa55880f2015-03-10 17:47:10 +000049 return False
50
Javi Merinoaace7c02015-08-10 14:10:47 +010051 # It's the apache license
Javi Merinodea8e9d2015-08-10 16:37:55 +010052 if "http://www.apache.org/licenses/LICENSE-2.0" not in lines[6]:
Javi Merinoa55880f2015-03-10 17:47:10 +000053 return False
54
55 return True
56
57
58class TestCopyRight(unittest.TestCase):
59 def test_copyrights(self):
60 """Check that all files have valid copyrights"""
Javi Merinoa55880f2015-03-10 17:47:10 +000061
Kapileshwar Singh84cbf3b2015-09-02 15:49:42 +010062 tests_dir = os.path.dirname(os.path.abspath(__file__))
63 base_dir = os.path.dirname(tests_dir)
64
Javi Merino45010cb2015-09-22 15:39:01 +010065 for root, dirs, files in os.walk(base_dir):
Kapileshwar Singh84cbf3b2015-09-02 15:49:42 +010066 for fname in files:
67 fname = os.path.join(root, fname)
68 extension = os.path.splitext(fname)[1]
69 if extension in [".py", ".js", ".css"]:
70 if not copyright_is_valid(fname):
71 print("Invalid copyright in {}".format(fname))
72 self.fail()
Javi Merino45010cb2015-09-22 15:39:01 +010073
74 if '.git' in dirs:
75 dirs.remove('.git')