Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/utils/add_copyright.py
1560 views
1
#!/usr/bin/env python
2
# Copyright 2015 The Shaderc Authors. All rights reserved.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
"""Adds copyright notices to all the files that need them under the current
16
directory.
17
18
usage: add_copyright.py [--check]
19
20
With --check, prints out all the files missing the copyright notice and exits
21
with status 1 if any such files are found, 0 if none.
22
"""
23
24
import fileinput
25
import fnmatch
26
import os
27
import re
28
import sys
29
30
COPYRIGHTRE = re.compile(
31
r'Copyright \d+ The Shaderc Authors. All rights reserved.')
32
COPYRIGHT = 'Copyright 2016 The Shaderc Authors. All rights reserved.'
33
LICENSED = """
34
Licensed under the Apache License, Version 2.0 (the "License");
35
you may not use this file except in compliance with the License.
36
You may obtain a copy of the License at
37
38
http://www.apache.org/licenses/LICENSE-2.0
39
40
Unless required by applicable law or agreed to in writing, software
41
distributed under the License is distributed on an "AS IS" BASIS,
42
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43
See the License for the specific language governing permissions and
44
limitations under the License."""
45
46
47
def find(top, filename_glob, skip_glob_list):
48
"""Returns files in the tree rooted at top matching filename_glob but not
49
in directories matching skip_glob_list."""
50
51
file_list = []
52
for path, dirs, files in os.walk(top):
53
for glob in skip_glob_list:
54
for match in fnmatch.filter(dirs, glob):
55
dirs.remove(match)
56
for filename in fnmatch.filter(files, filename_glob):
57
file_list.append(os.path.join(path, filename))
58
return file_list
59
60
61
def filtered_descendants(glob):
62
"""Returns glob-matching filenames under the current directory, but skips
63
some irrelevant paths."""
64
return find('.', glob, ['third_party', 'external', 'build*', 'out*',
65
'CompilerIdCXX', '.venv', 'glslang'])
66
67
68
def skip(line):
69
"""Returns true if line is all whitespace or shebang."""
70
stripped = line.lstrip()
71
return stripped == '' or stripped.startswith('#!')
72
73
74
def comment(text, prefix):
75
"""Returns commented-out text.
76
77
Each line of text will be prefixed by prefix and a space character.
78
Any trailing whitespace will be trimmed.
79
"""
80
accum = []
81
for line in text.split('\n'):
82
accum.append((prefix + ' ' + line).rstrip())
83
return '\n'.join(accum)
84
85
86
def insert_copyright(glob, comment_prefix):
87
"""Finds all glob-matching files under the current directory and inserts
88
the copyright message into them unless they already have it or are empty.
89
90
The copyright message goes into the first non-whitespace, non-
91
shebang line in a file. It is prefixed on each line by
92
comment_prefix and a space.
93
"""
94
copyright = comment(COPYRIGHT, comment_prefix) + '\n'
95
licensed = comment(LICENSED, comment_prefix) + '\n\n'
96
for file in filtered_descendants(glob):
97
has_copyright = False
98
for line in fileinput.input(file, inplace=1):
99
has_copyright = has_copyright or COPYRIGHTRE.search(line)
100
if not has_copyright and not skip(line):
101
sys.stdout.write(copyright)
102
sys.stdout.write(licensed)
103
has_copyright = True
104
sys.stdout.write(line)
105
if not has_copyright:
106
open(file, 'a').write(copyright + licensed)
107
108
109
def alert_if_no_copyright(glob, comment_prefix):
110
"""Prints names of all files missing a copyright message.
111
112
Finds all glob-matching files under the current directory and checks if they
113
contain the copyright message. Prints the names of all the files that
114
don't.
115
116
Returns the total number of file names printed.
117
"""
118
printed_count = 0
119
for file in filtered_descendants(glob):
120
has_copyright = False
121
with open(file) as contents:
122
for line in contents:
123
if COPYRIGHTRE.search(line):
124
has_copyright = True
125
break
126
if not has_copyright:
127
print(file, ' has no copyright message.')
128
printed_count += 1
129
return printed_count
130
131
132
def main():
133
glob_comment_pairs = [('*.h', '//'), ('*.hpp', '//'), ('*.cc', '//'),
134
('*.py', '#'), ('*.cpp', '//')]
135
if '--check' in sys.argv:
136
count = 0
137
for pair in glob_comment_pairs:
138
count += alert_if_no_copyright(*pair)
139
sys.exit(count > 0)
140
else:
141
for pair in glob_comment_pairs:
142
insert_copyright(*pair)
143
144
145
if __name__ == '__main__':
146
main()
147
148