TransformerEngine
155 строк · 4.4 Кб
1#!/usr/bin/env python
2# coding: utf-8
3
4# Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5#
6# See LICENSE for license information.
7
8
9import os
10import sys
11import json
12import datetime
13
14if len(sys.argv) < 2:
15print("Usage: python copyright_checker.py <path>")
16
17path = sys.argv[1]
18
19config_path = os.path.dirname(os.path.realpath(__file__)) + "/config.json"
20
21class bcolors:
22OKGREEN = '\033[92m'
23WARNING = '\033[93m'
24FAIL = '\033[91m'
25ENDC = '\033[0m'
26
27def print_ok(msg):
28print(f"{bcolors.OKGREEN}{msg}{bcolors.ENDC}")
29
30def print_fail(msg):
31print(f"{bcolors.FAIL}{msg}{bcolors.ENDC}")
32
33def print_warn(msg):
34print(f"{bcolors.WARNING}{msg}{bcolors.ENDC}")
35
36with open(config_path, "r") as f:
37c = json.load(f)
38current_year = datetime.date.today().year
39if c["initial_year"] == current_year:
40year_string = str(current_year)
41else:
42year_string = str(c["initial_year"]) + "-" + str(current_year)
43copyright_string = c["copyright"].replace("<YEAR>", year_string)
44license = c["license"].split('\n')
45excludes = c["exclude"]
46root_path = os.path.abspath(path)
47copyright_only = c["copyright_only"]
48exclude_copyright = c["exclude_copyright"]
49
50has_gitignore = os.path.exists(root_path + "/.gitignore")
51
52def strip_star_slash(s):
53ret = s
54if ret.startswith('*'):
55ret = ret[1:]
56if ret.endswith('/'):
57ret = ret[:-1]
58return ret
59
60if has_gitignore:
61with open(root_path + "/.gitignore", "r") as f:
62for line in f.readlines():
63excludes.append(strip_star_slash(line.strip()))
64
65def get_file_type(path):
66ext = {"c": ["c", "cpp", "cu", "h", "cuh"],
67"py": ["py"],
68"rst": ["rst"],
69"txt": ["txt"],
70"cfg": ["cfg"],
71"sh": ["sh"],
72"md": ["md"],
73}
74tmp = path.split(".")
75for filetype, ext_list in ext.items():
76if tmp[-1] in ext_list:
77return filetype
78return "unknown"
79
80success = True
81
82def check_file(path):
83global success
84N = 10
85ftype = get_file_type(path)
86if ftype == "unknown":
87print_warn("Unknown filetype")
88return
89check_copyright = True
90for e in exclude_copyright:
91if path.endswith(e):
92check_copyright = False
93with open(path, "r") as f:
94copyright_found = False
95license_found = True
96try:
97if check_copyright:
98for _ in range(N):
99line = f.readline()
100if line.find(copyright_string) != -1:
101copyright_found = True
102break
103if not copyright_only:
104first_license_line = True
105for l in license:
106if first_license_line:
107# may skip some lines
108first_license_line = False
109for _ in range(N):
110line = f.readline()
111if line.find(l) != -1:
112break
113else:
114line = f.readline()
115if line.find(l) == -1:
116license_found = False
117break
118except:
119pass
120finally:
121if not copyright_found:
122print_fail("No copyright found!")
123success = False
124if not license_found:
125print_fail("No license found!")
126success = False
127if copyright_found and license_found:
128print_ok("OK")
129
130for root, dirs, files in os.walk(root_path):
131print(f"Entering {root}")
132hidden = [d for d in dirs if d.startswith('.')] + [f for f in files if f.startswith('.')]
133all_excludes = excludes + hidden
134to_remove = []
135for d in dirs:
136d_path = root + "/" + d
137for e in all_excludes:
138if d_path.endswith(e):
139to_remove.append(d)
140for f in files:
141f_path = root + "/" + f
142for e in all_excludes:
143if f_path.endswith(e):
144to_remove.append(f)
145for d in to_remove:
146if d in dirs:
147dirs.remove(d)
148if d in files:
149files.remove(d)
150for filename in files:
151print(f"Checking {filename}")
152check_file(os.path.abspath(root + "/" + filename))
153
154if not success:
155raise Exception("Some copyrights/licenses are missing!")
156