Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/scripts/update_copyright.py
2864 views
1
#!/usr/bin/env python
2
3
import glob
4
import os
5
from pathlib import Path
6
7
8
class Copyright:
9
NOTICE = """Licensed to the Software Freedom Conservancy (SFC) under one
10
or more contributor license agreements. See the NOTICE file
11
distributed with this work for additional information
12
regarding copyright ownership. The SFC licenses this file
13
to you under the Apache License, Version 2.0 (the
14
"License"); you may not use this file except in compliance
15
with the License. You may obtain a copy of the License at
16
17
http://www.apache.org/licenses/LICENSE-2.0
18
19
Unless required by applicable law or agreed to in writing,
20
software distributed under the License is distributed on an
21
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22
KIND, either express or implied. See the License for the
23
specific language governing permissions and limitations
24
under the License."""
25
26
def __init__(self, comment_characters="//", prefix=None):
27
self._comment_characters = comment_characters
28
self._prefix = prefix or []
29
30
def update(self, files):
31
for file in files:
32
with open(file, "r", encoding="utf-8-sig") as f:
33
lines = f.readlines()
34
35
index = -1
36
for i, line in enumerate(lines):
37
if line.startswith(
38
self._comment_characters
39
) or self.valid_copyright_notice_line(line, index, file):
40
index += 1
41
else:
42
break
43
44
if index == -1:
45
self.write_update_notice(file, lines)
46
else:
47
current = "".join(lines[: index + 1])
48
if current != self.copyright_notice(file):
49
self.write_update_notice(file, lines[index + 1 :])
50
51
def valid_copyright_notice_line(self, line, index, file):
52
return index + 1 < len(self.copyright_notice_lines(file)) and line.startswith(
53
self.copyright_notice_lines(file)[index + 1]
54
)
55
56
def copyright_notice(self, file):
57
return "".join(self.copyright_notice_lines(file))
58
59
def copyright_notice_lines(self, file):
60
return (
61
self.dotnet(file)
62
if file.endswith("cs")
63
else self._prefix + self.commented_notice_lines
64
)
65
66
def dotnet(self, file):
67
file_name = os.path.basename(file)
68
first = f'{self._comment_characters} <copyright file="{file_name}" company="Selenium Committers">\n'
69
last = f"{self._comment_characters} </copyright>"
70
return [first] + self.commented_notice_lines + [last]
71
72
@property
73
def commented_notice_lines(self):
74
return [
75
f"{self._comment_characters} {line}".rstrip() + "\n"
76
for line in self.NOTICE.split("\n")
77
]
78
79
def write_update_notice(self, file, lines):
80
print(f"Adding notice to {file}")
81
with open(file, "w") as f:
82
f.write(self.copyright_notice(file) + "\n")
83
if lines and lines[0] != "\n":
84
f.write("\n")
85
trimmed_lines = [line.rstrip() + "\n" for line in lines]
86
f.writelines(trimmed_lines)
87
88
89
ROOT = Path(os.path.realpath(__file__)).parent.parent
90
91
JS_EXCLUSIONS = [
92
f"{ROOT}/javascript/atoms/test/jquery.min.js",
93
f"{ROOT}/javascript/jsunit/**/*.js",
94
f"{ROOT}/javascript/selenium-webdriver/node_modules/**/*.js",
95
f"{ROOT}/javascript/selenium-core/lib/**/*.js",
96
f"{ROOT}/javascript/selenium-core/scripts/ui-element.js",
97
f"{ROOT}/javascript/selenium-core/scripts/ui-map-sample.js",
98
f"{ROOT}/javascript/selenium-core/scripts/user-extensions.js",
99
f"{ROOT}/javascript/selenium-core/scripts/xmlextras.js",
100
f"{ROOT}/javascript/selenium-core/xpath/**/*.js",
101
f"{ROOT}/javascript/grid-ui/node_modules/**/*.js",
102
f"{ROOT}/javascript/node/selenium-webdriver/node_modules/**/*.js",
103
]
104
105
PY_EXCLUSIONS = [
106
f"{ROOT}/py/selenium/webdriver/common/bidi/cdp.py",
107
f"{ROOT}/py/generate.py",
108
f"{ROOT}/py/selenium/webdriver/common/devtools/**/*",
109
f"{ROOT}/py/venv/**/*",
110
]
111
112
113
def update_files(file_pattern, exclusions, comment_characters="//", prefix=None):
114
included = set(glob.glob(file_pattern, recursive=True))
115
excluded = set()
116
for pattern in exclusions:
117
excluded.update(glob.glob(pattern, recursive=True))
118
files = included - excluded
119
120
copyright = Copyright(comment_characters, prefix)
121
copyright.update(files)
122
123
124
if __name__ == "__main__":
125
update_files(f"{ROOT}/javascript/**/*.js", JS_EXCLUSIONS)
126
update_files(f"{ROOT}/javascript/**/*.tsx", [])
127
update_files(f"{ROOT}/py/**/*.py", PY_EXCLUSIONS, comment_characters="#")
128
update_files(
129
f"{ROOT}/rb/**/*.rb",
130
[],
131
comment_characters="#",
132
prefix=["# frozen_string_literal: true\n", "\n"],
133
)
134
update_files(f"{ROOT}/java/**/*.java", [])
135
update_files(f"{ROOT}/rust/**/*.rs", [])
136
update_files(f"{ROOT}/dotnet/**/*.cs", [])
137
138