|
| 1 | +# Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style license that can be |
| 3 | +# found in the LICENSE file. |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +def ContainsLicenseBlock(source_file): |
| 10 | + # This check is somewhat easier than in the engine because all sources need to |
| 11 | + # have the same license. |
| 12 | + py_license = '''# Copyright 2013 The Flutter Authors. All rights reserved. |
| 13 | +# Use of this source code is governed by a BSD-style license that can be |
| 14 | +# found in the LICENSE file.''' |
| 15 | + c_license = py_license.replace("#", "//") |
| 16 | + |
| 17 | + # Make sure we don't read the entire file into memory. |
| 18 | + read_size = (max(len(py_license), len(c_license))) |
| 19 | + |
| 20 | + for license in [c_license, py_license]: |
| 21 | + with open(source_file) as source: |
| 22 | + if source.read(read_size).startswith(license): |
| 23 | + return True |
| 24 | + |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +def IsSourceFile(path): |
| 29 | + known_extensions = [ |
| 30 | + ".cc", |
| 31 | + ".cpp", |
| 32 | + ".c", |
| 33 | + ".h", |
| 34 | + ".hpp", |
| 35 | + ".py", |
| 36 | + ".sh", |
| 37 | + ".gn", |
| 38 | + ".gni", |
| 39 | + ".glsl", |
| 40 | + ".sl.h", |
| 41 | + ".vert", |
| 42 | + ".frag", |
| 43 | + ".tesc", |
| 44 | + ".tese", |
| 45 | + ".yaml", |
| 46 | + ".dart", |
| 47 | + ] |
| 48 | + for extension in known_extensions: |
| 49 | + if os.path.basename(path).endswith(extension): |
| 50 | + return True |
| 51 | + return False; |
| 52 | + |
| 53 | + |
| 54 | +# Checks that all source files have the same license preamble. |
| 55 | +def Main(): |
| 56 | + parser = argparse.ArgumentParser() |
| 57 | + parser.add_argument("--source-root", |
| 58 | + type=str, required=True, |
| 59 | + help="The source root.") |
| 60 | + args = parser.parse_args() |
| 61 | + |
| 62 | + assert(os.path.exists(args.source_root)) |
| 63 | + |
| 64 | + source_files = set() |
| 65 | + |
| 66 | + for root, dirs, files in os.walk(os.path.abspath(args.source_root)): |
| 67 | + for file in files: |
| 68 | + file_path = os.path.join(root, file) |
| 69 | + if IsSourceFile(file_path): |
| 70 | + source_files.add(file_path) |
| 71 | + |
| 72 | + for source_file in source_files: |
| 73 | + if not ContainsLicenseBlock(source_file): |
| 74 | + raise Exception("Could not find valid license block in source ", source_file) |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + Main() |
0 commit comments