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