Brendan Jackman | e81fdcb | 2017-01-04 17:10:29 +0000 | [diff] [blame] | 1 | # Copyright 2015-2017 ARM Limited |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 2 | # |
Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame] | 3 | # 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 Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 16 | |
| 17 | from datetime import date |
Javi Merino | 2f7ee66 | 2015-09-22 19:15:58 +0100 | [diff] [blame] | 18 | from glob import glob |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 19 | import os |
Javi Merino | dea8e9d | 2015-08-10 16:37:55 +0100 | [diff] [blame] | 20 | import re |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 21 | import unittest |
| 22 | |
| 23 | |
| 24 | def 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 Merino | dea8e9d | 2015-08-10 16:37:55 +0100 | [diff] [blame] | 31 | # Either the first or the second line must have a "Copyright:" line |
| 32 | first_line = re.compile(r"(#| \*) Copyright") |
Javi Merino | 69b10c2 | 2015-09-22 14:47:06 +0100 | [diff] [blame] | 33 | 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 Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 42 | |
Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame] | 43 | # The copyright mentions ARM Limited |
| 44 | if "ARM Limited" not in lines[0]: |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 45 | return False |
| 46 | |
Kapileshwar Singh | e43094c | 2016-04-26 01:18:50 +0200 | [diff] [blame] | 47 | apache_line = 6 |
| 48 | if "Google Inc" in lines[1]: |
| 49 | apache_line += 1 |
| 50 | |
Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame] | 51 | # The Copyright includes the current year |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 52 | current_year = date.today().year |
Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame] | 53 | if str(current_year) not in lines[0]: |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 54 | return False |
| 55 | |
Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame] | 56 | # It's the apache license |
Kapileshwar Singh | e43094c | 2016-04-26 01:18:50 +0200 | [diff] [blame] | 57 | if "http://www.apache.org/licenses/LICENSE-2.0" not in lines[apache_line]: |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 58 | return False |
| 59 | |
| 60 | return True |
| 61 | |
| 62 | |
| 63 | class TestCopyRight(unittest.TestCase): |
| 64 | def test_copyrights(self): |
| 65 | """Check that all files have valid copyrights""" |
Javi Merino | a55880f | 2015-03-10 17:47:10 +0000 | [diff] [blame] | 66 | |
Kapileshwar Singh | 84cbf3b | 2015-09-02 15:49:42 +0100 | [diff] [blame] | 67 | tests_dir = os.path.dirname(os.path.abspath(__file__)) |
| 68 | base_dir = os.path.dirname(tests_dir) |
Javi Merino | 2f7ee66 | 2015-09-22 19:15:58 +0100 | [diff] [blame] | 69 | patterns_to_ignore = {} |
Kapileshwar Singh | 84cbf3b | 2015-09-02 15:49:42 +0100 | [diff] [blame] | 70 | |
Javi Merino | 45010cb | 2015-09-22 15:39:01 +0100 | [diff] [blame] | 71 | for root, dirs, files in os.walk(base_dir): |
Javi Merino | 2f7ee66 | 2015-09-22 19:15:58 +0100 | [diff] [blame] | 72 | if ".gitignore" in files: |
| 73 | fname = os.path.join(root, ".gitignore") |
| 74 | with open(fname) as fin: |
| 75 | lines = fin.readlines() |
| 76 | |
| 77 | patterns_to_ignore[root] = [l.strip() for l in lines] |
| 78 | |
| 79 | files_to_ignore = [] |
| 80 | for directory, patterns in patterns_to_ignore.iteritems(): |
| 81 | if root.startswith(directory): |
| 82 | for pat in patterns: |
| 83 | pat = os.path.join(root, pat) |
| 84 | files_to_ignore.extend(glob(pat)) |
| 85 | |
| 86 | for dirname in dirs: |
| 87 | full_dirname = os.path.join(root, dirname) |
| 88 | if full_dirname in files_to_ignore: |
| 89 | dirs.remove(dirname) |
| 90 | |
| 91 | |
Kapileshwar Singh | 84cbf3b | 2015-09-02 15:49:42 +0100 | [diff] [blame] | 92 | for fname in files: |
| 93 | fname = os.path.join(root, fname) |
Javi Merino | 2f7ee66 | 2015-09-22 19:15:58 +0100 | [diff] [blame] | 94 | if fname in files_to_ignore: |
| 95 | continue |
| 96 | |
Kapileshwar Singh | 84cbf3b | 2015-09-02 15:49:42 +0100 | [diff] [blame] | 97 | extension = os.path.splitext(fname)[1] |
| 98 | if extension in [".py", ".js", ".css"]: |
| 99 | if not copyright_is_valid(fname): |
| 100 | print("Invalid copyright in {}".format(fname)) |
| 101 | self.fail() |
Javi Merino | 45010cb | 2015-09-22 15:39:01 +0100 | [diff] [blame] | 102 | |
| 103 | if '.git' in dirs: |
| 104 | dirs.remove('.git') |