[all] update refresh-copyright to handle UTF BOM markers

This commit is contained in:
Geoffrey McRae
2026-05-31 15:13:02 +10:00
parent e9b7bdfea1
commit 0743d49abe

View File

@@ -12,6 +12,8 @@ EXTENSIONS = ('.c', '.cpp', '.h', '.nsi', '.rc')
START_YEAR = 2017 START_YEAR = 2017
CURRENT_YEAR = datetime.date.today().year CURRENT_YEAR = datetime.date.today().year
UTF8_BOM = '\ufeff'
reignore = re.compile('^vendor/|.*/shader/|.*/d3d12.h$') reignore = re.compile('^vendor/|.*/shader/|.*/d3d12.h$')
recopyright = re.compile(r'\A/\*.*?\*/\s+', re.DOTALL) recopyright = re.compile(r'\A/\*.*?\*/\s+', re.DOTALL)
@@ -36,6 +38,24 @@ with this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place, Suite 330, Boston, MA 02111-1307 USA'''] Temple Place, Suite 330, Boston, MA 02111-1307 USA''']
def read_utf8_preserve_bom(file):
with open(file, encoding='utf-8') as f:
data = f.read()
has_bom = data.startswith(UTF8_BOM)
if has_bom:
data = data[len(UTF8_BOM):]
return data, has_bom
def write_utf8_preserve_bom(file, data, has_bom):
with open(file, 'w', encoding='utf-8') as f:
if has_bom:
f.write(UTF8_BOM)
f.write(data)
def make_comment_block(): def make_comment_block():
lines = ['/**'] lines = ['/**']
lines += [' * ' + line for line in header] lines += [' * ' + line for line in header]
@@ -59,11 +79,11 @@ def gen_c_literal():
def update_c_style(file, copyright): def update_c_style(file, copyright):
print(f'Updating copyright for {file}...') print(f'Updating copyright for {file}...')
with open(file, encoding='utf-8') as f:
data = recopyright.sub('', f.read()) data, has_bom = read_utf8_preserve_bom(file)
with open(file, 'w', encoding='utf-8') as f: data = recopyright.sub('', data)
f.write(copyright)
f.write(data) write_utf8_preserve_bom(file, copyright + data, has_bom)
def update_config_c(file): def update_config_c(file):
@@ -73,8 +93,9 @@ def update_config_c(file):
print(f'Refresh embedded copyright string in {file}...') print(f'Refresh embedded copyright string in {file}...')
with open(file, encoding='utf-8') as f: data, has_bom = read_utf8_preserve_bom(file)
for line in f:
for line in data.splitlines(keepends=True):
if '// BEGIN LICENSE BLOCK' in line: if '// BEGIN LICENSE BLOCK' in line:
prefix.append(line) prefix.append(line)
current = [] current = []
@@ -84,10 +105,8 @@ def update_config_c(file):
else: else:
current.append(line) current.append(line)
with open(file, 'w', encoding='utf-8') as f: data = ''.join(prefix) + gen_c_literal() + ''.join(suffix)
f.writelines(prefix) write_utf8_preserve_bom(file, data, has_bom)
f.write(gen_c_literal())
f.writelines(suffix)
def appstring_license(): def appstring_license():
@@ -105,31 +124,36 @@ def appstring_license():
def update_appstrings(file): def update_appstrings(file):
print(f'Refresh app string in {file}...') print(f'Refresh app string in {file}...')
lines = []
with open(file, encoding='utf-8') as f: data, has_bom = read_utf8_preserve_bom(file)
f = iter(f)
lines = []
f = iter(data.splitlines(keepends=True))
for line in f: for line in f:
lines.append(line) lines.append(line)
if 'LG_COPYRIGHT_STR' in line: if 'LG_COPYRIGHT_STR' in line:
next(f) next(f)
lines.append(f' "{copyright}";\n') lines.append(f' "{copyright}";\n')
elif 'LG_WEBSITE_STR' in line: elif 'LG_WEBSITE_STR' in line:
next(f) next(f)
lines.append(f' "{project_url}";\n') lines.append(f' "{project_url}";\n')
elif 'LG_LICENSE_STR' in line: elif 'LG_LICENSE_STR' in line:
lines += [f'{line}\n' for line in appstring_license()] lines += [f'{line}\n' for line in appstring_license()]
for line in f: for line in f:
if '";' in line: if '";' in line:
break break
with open(file, 'w', encoding='utf-8') as f: write_utf8_preserve_bom(file, ''.join(lines), has_bom)
f.writelines(lines)
def main(): def main():
comment_block = make_comment_block() comment_block = make_comment_block()
files = subprocess.check_output(['git', '-C', PROJECT, 'ls-files', '-z']).decode('utf-8').split('\0') files = subprocess.check_output(['git', '-C', PROJECT, 'ls-files', '-z']).decode('utf-8').split('\0')
for file in files: for file in files:
if reignore.match(file): if reignore.match(file):
continue continue
@@ -139,5 +163,6 @@ def main():
update_config_c(os.path.join(PROJECT, 'client', 'src', 'config.c')) update_config_c(os.path.join(PROJECT, 'client', 'src', 'config.c'))
update_appstrings(os.path.join(PROJECT, 'common', 'src', 'appstrings.c')) update_appstrings(os.path.join(PROJECT, 'common', 'src', 'appstrings.c'))
if __name__ == '__main__': if __name__ == '__main__':
main() main()