blob: 9585fa3d3e43a192a464763b20db1877ecf96788 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001#!/usr/bin/env python3
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002import glob
3import os.path
Anthony Barbierf7d418c2018-12-19 11:51:09 +00004import sys
Anthony Barbier6ff3b192017-09-04 18:44:23 +01005
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01006mit_copyright = open("scripts/copyright_mit.txt",'r').read()
Anthony Barbier6ff3b192017-09-04 18:44:23 +01007
8def add_cpp_copyright( f, content):
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01009 global mit_copyright
Anthony Barbier6ff3b192017-09-04 18:44:23 +010010 out = open(f,'w')
11 out.write("/*\n")
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010012 for line in mit_copyright.split('\n')[:-1]:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010013 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
22def add_python_copyright( f, content):
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010023 global mit_copyright
Anthony Barbier6ff3b192017-09-04 18:44:23 +010024 out = open(f,'w')
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010025 for line in mit_copyright.split('\n')[:-1]:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026 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
34def 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
49def 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 Barbierf7d418c2018-12-19 11:51:09 +000060def 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 Barbier6ff3b192017-09-04 18:44:23 +010065
Anthony Barbierf7d418c2018-12-19 11:51:09 +000066 with open(path, 'r', encoding='utf-8') as fd:
67 content = fd.read()
68 _, extension = os.path.splitext(f)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
Anthony Barbierf7d418c2018-12-19 11:51:09 +000070 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 Barbier6ff3b192017-09-04 18:44:23 +010081
Anthony Barbierf7d418c2018-12-19 11:51:09 +000082if len(sys.argv) > 1:
83 for path in sys.argv[1:]:
84 check_file(path)
85else:
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)