2021-06-06 01:25:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import fnmatch
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
2021-07-30 10:47:39 +00:00
|
|
|
from textwrap import wrap
|
2021-06-06 01:25:14 +00:00
|
|
|
|
|
|
|
PROJECT = os.path.dirname(__file__)
|
2021-07-21 07:37:10 +00:00
|
|
|
EXTENSIONS = ('.c', '.cpp', '.h', '.nsi', '.rc')
|
2021-06-06 01:25:14 +00:00
|
|
|
START_YEAR = 2017
|
|
|
|
CURRENT_YEAR = datetime.date.today().year
|
|
|
|
|
2021-08-10 07:44:05 +00:00
|
|
|
reignore = re.compile('^vendor/|.*/shader/')
|
2021-06-06 01:25:14 +00:00
|
|
|
recopyright = re.compile(r'\A/\*.*?\*/\s+', re.DOTALL)
|
|
|
|
|
2021-08-04 09:47:29 +00:00
|
|
|
project_name = 'Looking Glass'
|
|
|
|
copyright = f'Copyright © {START_YEAR}-{CURRENT_YEAR} The Looking Glass Authors'
|
|
|
|
project_url = 'https://looking-glass.io'
|
|
|
|
header = [project_name, copyright, project_url]
|
2021-06-06 01:25:14 +00:00
|
|
|
|
2021-07-30 10:47:39 +00:00
|
|
|
paragraphs = ['''\
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
any later version.''',
|
|
|
|
'''\
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.''',
|
|
|
|
'''\
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
Temple Place, Suite 330, Boston, MA 02111-1307 USA''']
|
2021-06-06 01:25:14 +00:00
|
|
|
|
2021-07-30 10:47:39 +00:00
|
|
|
|
|
|
|
def make_comment_block():
|
|
|
|
lines = ['/**']
|
2021-08-04 09:47:29 +00:00
|
|
|
lines += [' * ' + line for line in header]
|
2021-07-30 10:47:39 +00:00
|
|
|
|
|
|
|
for paragraph in paragraphs:
|
|
|
|
lines.append(' *')
|
|
|
|
lines += wrap(paragraph, width=78, initial_indent=' * ', subsequent_indent=' * ')
|
|
|
|
|
|
|
|
lines.append(' */')
|
|
|
|
return '\n'.join(lines) + '\n\n'
|
|
|
|
|
|
|
|
|
|
|
|
def gen_c_literal():
|
2021-08-04 09:47:29 +00:00
|
|
|
lines = [''] + header
|
2021-07-30 10:47:39 +00:00
|
|
|
for paragraph in paragraphs:
|
|
|
|
lines.append('')
|
|
|
|
lines += wrap(paragraph, width=79)
|
|
|
|
lines.append('')
|
|
|
|
return '\n'.join(f' "{line}\\n"' for line in lines) + '\n'
|
|
|
|
|
|
|
|
|
|
|
|
def update_c_style(file, copyright):
|
2021-06-06 01:25:14 +00:00
|
|
|
print(f'Updating copyright for {file}...')
|
|
|
|
with open(file, encoding='utf-8') as f:
|
|
|
|
data = recopyright.sub('', f.read())
|
|
|
|
with open(file, 'w', encoding='utf-8') as f:
|
|
|
|
f.write(copyright)
|
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
|
2021-07-30 10:47:39 +00:00
|
|
|
def update_config_c(file):
|
|
|
|
prefix = []
|
|
|
|
suffix = []
|
|
|
|
current = prefix
|
|
|
|
|
|
|
|
print(f'Refresh embedded copyright string in {file}...')
|
|
|
|
|
|
|
|
with open(file, encoding='utf-8') as f:
|
|
|
|
for line in f:
|
|
|
|
if '// BEGIN LICENSE BLOCK' in line:
|
|
|
|
prefix.append(line)
|
|
|
|
current = []
|
|
|
|
elif '// END LICENSE BLOCK' in line:
|
|
|
|
suffix.append(line)
|
|
|
|
current = suffix
|
|
|
|
else:
|
|
|
|
current.append(line)
|
|
|
|
|
|
|
|
with open(file, 'w', encoding='utf-8') as f:
|
|
|
|
f.writelines(prefix)
|
|
|
|
f.write(gen_c_literal())
|
|
|
|
f.writelines(suffix)
|
|
|
|
|
|
|
|
|
2021-08-04 09:47:29 +00:00
|
|
|
def appstring_license():
|
|
|
|
lines = []
|
|
|
|
for paragraph in paragraphs:
|
|
|
|
paragraph = wrap(paragraph, width=75)
|
|
|
|
for line in paragraph[:-1]:
|
|
|
|
lines.append(f' "{line} "')
|
|
|
|
lines.append(f' "{paragraph[-1]}\\n"')
|
|
|
|
lines.append(r' "\n"')
|
|
|
|
lines.pop()
|
|
|
|
lines[-1] = f'{lines[-1][:-3]}";'
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def update_appstrings(file):
|
|
|
|
print(f'Refresh app string in {file}...')
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
with open(file, encoding='utf-8') as f:
|
|
|
|
f = iter(f)
|
|
|
|
for line in f:
|
|
|
|
lines.append(line)
|
|
|
|
if 'LG_COPYRIGHT_STR' in line:
|
|
|
|
next(f)
|
|
|
|
lines.append(f' "{copyright}";\n')
|
|
|
|
elif 'LG_WEBSITE_STR' in line:
|
|
|
|
next(f)
|
|
|
|
lines.append(f' "{project_url}";\n')
|
|
|
|
elif 'LG_LICENSE_STR' in line:
|
|
|
|
lines += [f'{line}\n' for line in appstring_license()]
|
|
|
|
for line in f:
|
|
|
|
if '";' in line:
|
|
|
|
break
|
|
|
|
|
|
|
|
with open(file, 'w', encoding='utf-8') as f:
|
|
|
|
f.writelines(lines)
|
|
|
|
|
|
|
|
|
2021-06-06 01:25:14 +00:00
|
|
|
def main():
|
2021-07-30 10:47:39 +00:00
|
|
|
comment_block = make_comment_block()
|
2021-06-06 01:25:14 +00:00
|
|
|
files = subprocess.check_output(['git', '-C', PROJECT, 'ls-files', '-z']).decode('utf-8').split('\0')
|
|
|
|
for file in files:
|
|
|
|
if reignore.match(file):
|
|
|
|
continue
|
|
|
|
if file.endswith(EXTENSIONS):
|
2021-07-30 10:47:39 +00:00
|
|
|
update_c_style(os.path.join(PROJECT, file), comment_block)
|
2021-06-06 01:25:14 +00:00
|
|
|
|
2021-07-30 10:47:39 +00:00
|
|
|
update_config_c(os.path.join(PROJECT, 'client', 'src', 'config.c'))
|
2021-08-04 09:47:29 +00:00
|
|
|
update_appstrings(os.path.join(PROJECT, 'common', 'src', 'appstrings.c'))
|
2021-06-06 01:25:14 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|