Skip to content

Commit 70a1982

Browse files
committed
[build] allow copyright script to work with dotnet files
1 parent 8186582 commit 70a1982

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

scripts/update_copyright.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ def __init__(self, comment_characters='//', prefix=None):
2828

2929
def update(self, files):
3030
for file in files:
31-
with open(file, 'r') as f:
31+
with open(file, 'r', encoding='utf-8-sig') as f:
3232
lines = f.readlines()
3333

3434
index = -1
3535
for i, line in enumerate(lines):
3636
if line.startswith(self._comment_characters) or \
37-
self.valid_copyright_notice_line(line, index):
37+
self.valid_copyright_notice_line(line, index, file):
3838
index += 1
3939
else:
4040
break
@@ -43,20 +43,24 @@ def update(self, files):
4343
self.write_update_notice(file, lines)
4444
else:
4545
current = ''.join(lines[:index + 1])
46-
if current != self.copyright_notice:
46+
if current != self.copyright_notice(file):
4747
self.write_update_notice(file, lines[index + 1:])
4848

49-
def valid_copyright_notice_line(self, line, index):
50-
return index + 1 < len(self.copyright_notice_lines) and \
51-
line.startswith(self.copyright_notice_lines[index + 1])
49+
def valid_copyright_notice_line(self, line, index, file):
50+
return index + 1 < len(self.copyright_notice_lines(file)) and \
51+
line.startswith(self.copyright_notice_lines(file)[index + 1])
5252

53-
@property
54-
def copyright_notice(self):
55-
return ''.join(self.copyright_notice_lines)
53+
def copyright_notice(self, file):
54+
return ''.join(self.copyright_notice_lines(file))
5655

57-
@property
58-
def copyright_notice_lines(self):
59-
return self._prefix + self.commented_notice_lines
56+
def copyright_notice_lines(self, file):
57+
return self.dotnet(file) if file.endswith("cs") else self._prefix + self.commented_notice_lines
58+
59+
def dotnet(self, file):
60+
file_name = os.path.basename(file)
61+
first = f"{self._comment_characters} <copyright file=\"{file_name}\" company=\"Selenium Committers\">\n"
62+
last = f"{self._comment_characters} </copyright>"
63+
return [first] + self.commented_notice_lines + [last]
6064

6165
@property
6266
def commented_notice_lines(self):
@@ -65,8 +69,11 @@ def commented_notice_lines(self):
6569
def write_update_notice(self, file, lines):
6670
print(f"Adding notice to {file}")
6771
with open(file, 'w') as f:
68-
f.write(self.copyright_notice + "\n")
69-
f.writelines(lines)
72+
f.write(self.copyright_notice(file) + "\n")
73+
if lines and lines[0] != "\n":
74+
f.write("\n")
75+
trimmed_lines = [line.rstrip() + "\n" for line in lines]
76+
f.writelines(trimmed_lines)
7077

7178
ROOT = Path(os.path.realpath(__file__)).parent.parent
7279

@@ -109,3 +116,4 @@ def update_files(file_pattern, exclusions, comment_characters='//', prefix=None)
109116
update_files(f"{ROOT}/rb/**/*.rb", [], comment_characters="#", prefix=["# frozen_string_literal: true\n", "\n"])
110117
update_files(f"{ROOT}/java/**/*.java", [])
111118
update_files(f"{ROOT}/rust/**/*.rs", [])
119+
update_files(f"{ROOT}/dotnet/**/*.cs", [])

0 commit comments

Comments
 (0)