test_copyright.py revision 84cbf3b8bd8861743cdd63339b746d3e7049e51d
1aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino#    Copyright 2015-2015 ARM Limited
2aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino#
3aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# Licensed under the Apache License, Version 2.0 (the "License");
4aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# you may not use this file except in compliance with the License.
5aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# You may obtain a copy of the License at
6aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino#
7aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino#     http://www.apache.org/licenses/LICENSE-2.0
8aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino#
9aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# Unless required by applicable law or agreed to in writing, software
10aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# distributed under the License is distributed on an "AS IS" BASIS,
11aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# See the License for the specific language governing permissions and
13aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino# limitations under the License.
14a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino#
15a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
16aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino
17a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merinofrom datetime import date
18a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merinoimport os
19dea8e9d314e5b0f9213c5f3ecf87ef4369537082Javi Merinoimport re
20a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merinoimport subprocess
21a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merinoimport unittest
22a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
23a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
24a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merinodef copyright_is_valid(fname):
25a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino    """Return True if fname has a valid copyright"""
26a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino    with open(fname) as fin:
27a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        # Read the first 2K of the file.  If the copyright is not there, you
28a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        # are probably doing something wrong
29a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        lines = fin.readlines(2048)
30a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
31dea8e9d314e5b0f9213c5f3ecf87ef4369537082Javi Merino    # Either the first or the second line must have a "Copyright:" line
32dea8e9d314e5b0f9213c5f3ecf87ef4369537082Javi Merino    first_line = re.compile(r"(#| \*)    Copyright")
33dea8e9d314e5b0f9213c5f3ecf87ef4369537082Javi Merino    if not first_line.search(lines[0]):
34dea8e9d314e5b0f9213c5f3ecf87ef4369537082Javi Merino        if first_line.search(lines[1]):
35a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino            # Drop the first line to align the copyright to lines[0]
36a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino            lines = lines[1:]
37a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        else:
38a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino            return False
39a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
40aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino    # The copyright mentions ARM Limited
41aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino    if "ARM Limited" not in lines[0]:
42a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        return False
43a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
44aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino    # The Copyright includes the current year
45a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino    current_year = date.today().year
46aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino    if str(current_year) not in lines[0]:
47a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        return False
48a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
49aace7c0732cac769f1ffe95a89591b6217fa9447Javi Merino    # It's the apache license
50dea8e9d314e5b0f9213c5f3ecf87ef4369537082Javi Merino    if "http://www.apache.org/licenses/LICENSE-2.0" not in lines[6]:
51a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        return False
52a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
53a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino    return True
54a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
55a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
56a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merinoclass TestCopyRight(unittest.TestCase):
57a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino    def test_copyrights(self):
58a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino        """Check that all files have valid copyrights"""
59a55880f6f01e74a3f2ba6974e0643d0ebbeb68caJavi Merino
6084cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh        tests_dir = os.path.dirname(os.path.abspath(__file__))
6184cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh        base_dir = os.path.dirname(tests_dir)
6284cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh
6384cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh        for root, _, files in os.walk(base_dir):
6484cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh            for fname in files:
6584cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh                fname = os.path.join(root, fname)
6684cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh                extension = os.path.splitext(fname)[1]
6784cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh                if extension in [".py", ".js", ".css"]:
6884cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh                    if not copyright_is_valid(fname):
6984cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh                        print("Invalid copyright in {}".format(fname))
7084cbf3b8bd8861743cdd63339b746d3e7049e51dKapileshwar Singh                        self.fail()
71