Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 2 | import glob |
| 3 | import os.path |
Anthony Barbier | f7d418c | 2018-12-19 11:51:09 +0000 | [diff] [blame] | 4 | import sys |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 5 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 6 | mit_copyright = open("scripts/copyright_mit.txt",'r').read() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 7 | |
| 8 | def add_cpp_copyright( f, content): |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 9 | global mit_copyright |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 10 | out = open(f,'w') |
| 11 | out.write("/*\n") |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 12 | for line in mit_copyright.split('\n')[:-1]: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 13 | out.write(" *"); |
| 14 | if line.strip() != "": |
| 15 | out.write(" %s" %line) |
| 16 | out.write("\n") |
| 17 | out.write(" */\n") |
| 18 | out.write(content.strip()) |
| 19 | out.write("\n") |
| 20 | out.close() |
| 21 | |
| 22 | def add_python_copyright( f, content): |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 23 | global mit_copyright |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 24 | out = open(f,'w') |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 25 | for line in mit_copyright.split('\n')[:-1]: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | out.write("#"); |
| 27 | if line.strip() != "": |
| 28 | out.write(" %s" %line) |
| 29 | out.write("\n") |
| 30 | out.write(content.strip()) |
| 31 | out.write("\n") |
| 32 | out.close() |
| 33 | |
| 34 | def remove_comment( content ): |
| 35 | comment=True |
| 36 | out="" |
| 37 | for line in content.split('\n'): |
| 38 | if comment: |
| 39 | if line.startswith(' */'): |
| 40 | comment=False |
| 41 | elif line.startswith('/*') or line.startswith(' *'): |
| 42 | #print(line) |
| 43 | continue |
| 44 | else: |
| 45 | raise Exception("ERROR: not a comment ? '%s'"% line) |
| 46 | else: |
| 47 | out += line + "\n" |
| 48 | return out |
| 49 | def remove_comment_python( content ): |
| 50 | comment=True |
| 51 | out="" |
| 52 | for line in content.split('\n'): |
| 53 | if comment and line.startswith('#'): |
| 54 | continue |
| 55 | else: |
| 56 | comment = False |
| 57 | out += line + "\n" |
| 58 | return out |
| 59 | |
Anthony Barbier | f7d418c | 2018-12-19 11:51:09 +0000 | [diff] [blame] | 60 | def check_file( path ): |
| 61 | root, f = os.path.split(path) |
| 62 | if f in ['.clang-tidy', '.clang-format']: |
| 63 | print("Skipping file: {}".format(path)) |
| 64 | return |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | |
Anthony Barbier | f7d418c | 2018-12-19 11:51:09 +0000 | [diff] [blame] | 66 | with open(path, 'r', encoding='utf-8') as fd: |
| 67 | content = fd.read() |
| 68 | _, extension = os.path.splitext(f) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | |
Anthony Barbier | f7d418c | 2018-12-19 11:51:09 +0000 | [diff] [blame] | 70 | if extension in ['.cpp', '.h', '.hpp', '.inl', '.cl', '.in', '.cs']: |
| 71 | if not content.startswith('/*'): |
| 72 | add_cpp_copyright(path, content) |
| 73 | elif extension == '.py' or f in ['SConstruct', 'SConscript']: |
| 74 | if not content.startswith('# Copyright'): |
| 75 | add_python_copyright(path, content) |
| 76 | elif f == 'CMakeLists.txt': |
| 77 | if not content.startswith('# Copyright'): |
| 78 | add_python_copyright(path, content) |
| 79 | else: |
| 80 | raise Exception("Unhandled file: {}".format(path)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
Anthony Barbier | f7d418c | 2018-12-19 11:51:09 +0000 | [diff] [blame] | 82 | if len(sys.argv) > 1: |
| 83 | for path in sys.argv[1:]: |
| 84 | check_file(path) |
| 85 | else: |
| 86 | for top in ['./arm_compute', './tests','./src','./examples','./utils/','./opencl-1.2-stubs/','./opengles-3.1-stubs/','./support']: |
| 87 | for root, _, files in os.walk(top): |
| 88 | for f in files: |
| 89 | path = os.path.join(root, f) |
| 90 | check_file(path) |