check_stamped.py 4.53 KB
Newer Older
1
2
3
4
#!/usr/bin/env python3
#####################################################################################
#  The MIT License (MIT)
#
5
#  Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
#####################################################################################
import subprocess
import sys

debug = False
# The filetypes we want to check for that are stamped
30
# LICENSE is included here as it SHOULD have a license in it otherwise flag it as unstamped
31
32
33
supported_file_types = (".cpp", ".hpp", ".h", ".ipynb", ".py", ".txt", ".sh",
                        ".bsh", "LICENSE", ".cmake")

34
# add general stuff we shouldn't stamp and any exceptions here
35
36
37
38
39
40
41
42
unsupported_file_types = [
    ".onnx", ".pb", ".rst", ".jpg", ".jpeg", ".proto", ".md", ".clang",
    ".weight", ".ini", ".json", ".docker", ".git", ".rules", ".yml"
]

specificIgnores = ("digits.txt", "Dockerfile", "Jenkinsfile", "")


43
def hasKeySequence(inputfile: str, key_message: str) -> bool:
44
    if key_message in inputfile:
45
46
        return True
    return False
47
48


49
50
51
52
# Simple just open and write stuff to each file with the license stamp
def needStampCheck(filename: str) -> bool:
    # open save old contents and append things here
    if debug: print("Open", filename, end=' ')
53
54
55
56

    try:
        file = open(filename, 'r')
    except OSError as e:
57
        if debug: print(str(e) + "....Open Error: Skipping  file ")
58
        file.close()
59
        return False
60
61
62
63
64
    else:
        with file as contents:
            try:
                save = contents.read()

65
66
67
68
69
                # Check if we have a license stamp already
                if hasKeySequence(
                        save,
                        "Advanced Micro Devices, Inc. All rights reserved"):
                    if debug: print("....Already Stamped: Skipping  file ")
70
                    contents.close()
71
                    return False
72
73

            except UnicodeDecodeError as eu:
74
                if debug: print(f"{str(eu)}...Skipping binary file ")
75
                contents.close()
76
                return False
77

78
    return True
79
80


81
82
83
84
85
# Check if any element in fileTuple is in filename
def check_filename(filename: str, fileTuple: tuple or list) -> bool:
    if any([x in filename for x in fileTuple]):
        return True
    return False
86
87


88
def main() -> None:
89
90
    unsupported_file_types.extend(specificIgnores)

91
    # Get a list of all the tracked files in our git repo
92
93
94
95
96
    proc = subprocess.run("git ls-files --exclude-standard",
                          shell=True,
                          stdout=subprocess.PIPE)
    fileList = proc.stdout.decode().split('\n')

97
    if debug: print("Target file list:\n" + str(fileList))
98
99
100
101
102
103

    unsupportedFiles = []
    unstampedFiles = []
    unknownFiles = []

    for file in fileList:
104
105
        if check_filename(file, supported_file_types):
            if needStampCheck(file):
106
107
                unstampedFiles.append(file)

108
109
        elif check_filename(file, unsupported_file_types):
            unsupportedFiles.append(file)
110
        else:
111
            unknownFiles.append(file)
112

113
    # Do a bunch of checks based on our file lists
114
    if len(unstampedFiles) > 0:
115
        print("\nError: The following " + str(len(unstampedFiles)) +
116
117
118
119
120
              " files are currently without a license:")
        print(str(unstampedFiles))
        sys.exit(1)

    if len(unknownFiles) > 0:
121
        print("\nError: The following " + str(len(unknownFiles)) +
122
123
124
125
126
127
128
              " files not handled:")
        print(str(unknownFiles))
        sys.exit(2)


if __name__ == "__main__":
    main()