aboutsummaryrefslogtreecommitdiff
path: root/scripts/add_copyright.py
blob: a9d4929db8405b3dd9a086bed2fff33a7e5b59f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
#FIXME: Remove this file before the release

import glob
import os.path

mit_copyright = open("scripts/copyright_mit.txt",'r').read()

def add_cpp_copyright( f, content):
    global mit_copyright
    out = open(f,'w')
    out.write("/*\n")
    for line in mit_copyright.split('\n')[:-1]:
        out.write(" *");
        if line.strip() != "":
            out.write(" %s" %line)
        out.write("\n")
    out.write(" */\n")
    out.write(content.strip())
    out.write("\n")
    out.close()

def add_python_copyright( f, content):
    global mit_copyright
    out = open(f,'w')
    for line in mit_copyright.split('\n')[:-1]:
        out.write("#");
        if line.strip() != "":
            out.write(" %s" %line)
        out.write("\n")
    out.write(content.strip())
    out.write("\n")
    out.close()

def remove_comment( content ):
    comment=True
    out=""
    for line in content.split('\n'):
        if comment:
            if line.startswith(' */'):
                comment=False
            elif line.startswith('/*') or line.startswith(' *'):
                #print(line)
                continue
            else:
                raise Exception("ERROR: not a comment ? '%s'"% line)
        else:
            out += line + "\n"
    return out
def remove_comment_python( content ):
    comment=True
    out=""
    for line in content.split('\n'):
        if comment and line.startswith('#'):
            continue
        else:
            comment = False
            out += line + "\n"
    return out

for top in ['./arm_compute', './tests','./src','./examples','./utils/','./framework','./support']:
    for root, _, files in os.walk(top):
        for f in files:
            path = os.path.join(root, f)

            if f in ['.clang-tidy', '.clang-format']:
                print("Skipping file: {}".format(path))
                continue

            with open(path, 'r', encoding='utf-8') as fd:
                content = fd.read()
                _, extension = os.path.splitext(f)

                if extension in ['.cpp', '.h', '.hpp', '.inl', '.cl']:
                    if not  content.startswith('/*'):
                        add_cpp_copyright(path, content)
                elif extension == '.py' or f in ['SConstruct', 'SConscript']:
                    if not content.startswith('# Copyright'):
                        add_python_copyright(path, content)
                elif f == 'CMakeLists.txt':
                    if not content.startswith('# Copyright'):
                        add_python_copyright(path, content)
                else:
                    raise Exception("Unhandled file: {}".format(path))