widelands-dev team mailing list archive
-
widelands-dev team
-
Mailing list archive
-
Message #09468
Re: [Merge] lp:~widelands-dev/widelands/update_copyright_script into lp:widelands
Ahh, my favourite - but I have not time to check this tonight, argh :-)
The test will actually be to run this will all variants of python I can find.
Code LGTM
Diff comments:
>
> === added file 'utils/file_utils.py'
> --- utils/file_utils.py 1970-01-01 00:00:00 +0000
> +++ utils/file_utils.py 2017-01-23 12:35:34 +0000
> @@ -0,0 +1,32 @@
> +#!/usr/bin/env python
> +# -*- coding: utf-8 -*-
> +
> +
> +"""Some common file util functions."""
> +
> +import os
> +import sys
> +
> +PYTHON3 = sys.version_info >= (3, 0)
> +
> +def read_text_file(filename):
> + """Reads the contens of a text file."""
> + if PYTHON3:
> + return open(filename, 'r', encoding='utf-8').read()
> + else:
> + return open(filename, 'r').read().decode('utf-8')
Are you really sure all out files are UTF8?, but may be enough for this purpose
> +
> +
> +def write_text_file(filename, content):
> + """Writes 'content' into a text file."""
> + if PYTHON3:
> + open(filename, 'w', encoding='utf-8').write(content)
> + else:
> + open(filename, 'w').write(content.encode('utf-8'))
> +
> +
> +def find_files(startpath, extensions):
> + for (dirpath, _, filenames) in os.walk(startpath):
> + for filename in filenames:
> + if os.path.splitext(filename)[-1].lower() in extensions:
> + yield os.path.join(dirpath, filename)
>
> === added file 'utils/update_copyright.py'
> --- utils/update_copyright.py 1970-01-01 00:00:00 +0000
> +++ utils/update_copyright.py 2017-01-23 12:35:34 +0000
> @@ -0,0 +1,54 @@
> +#!/usr/bin/env python
> +# encoding: utf-8
> +
> +import os.path
> +import re
> +import sys
> +
> +file_utils_script = os.path.abspath(os.path.join(os.path.dirname(__file__), 'file_utils.py'))
> +exec(compile(source=open(file_utils_script).read(), filename=file_utils_script, mode='exec'))
> +
> +def main():
> + """Updates the copyright year in all source files to the given year."""
> + if len(sys.argv) != 2:
> + print('Usage: update_copyright.py <year>')
> + return 1
> +
> + try:
> + year = sys.argv[1]
> + sys.stdout.write('Updating copyright year to: ' + year + ' ')
> + src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../src"))
> + # Fix copyright headers in C++ files
> + for filename in find_files(src_path, ['.h', '.cc']):
> + sys.stdout.write('.')
> + sys.stdout.flush()
> + lines = read_text_file(filename).strip().split('\n')
> + new_lines = []
> + regex = re.compile('(.*Copyright \(C\) \d\d\d\d)(.*)( by the Widelands Development Team.*)')
> + for line in lines:
> + match = regex.match(line)
> + if match:
> + line = match.group(1) + "-" + year + match.group(3)
> + new_lines.append(line.rstrip() + '\n')
> + write_text_file(filename, ''.join(new_lines))
Mhh, this may fetch inline comments as well, but they should not have this format, well
> +
> + # Now update the Buildinfo
> + filename = os.path.join(src_path, "build_info.h")
> + lines = read_text_file(filename).strip().split('\n')
> + new_lines = []
> + regex = re.compile('(.*constexpr uint16_t kWidelandsCopyrightEnd = )(\d\d\d\d)(;)')
> + for line in lines:
> + match = regex.match(line)
> + if match:
> + line = match.group(1) + year + match.group(3)
> + new_lines.append(line.rstrip() + '\n')
> + write_text_file(filename, ''.join(new_lines))
> + print(' done.')
> +
> + except Exception:
> + print('Something went wrong:')
> + traceback.print_exc()
> + return 1
> +
> +if __name__ == '__main__':
> + sys.exit(main())
--
https://code.launchpad.net/~widelands-dev/widelands/update_copyright_script/+merge/315352
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/update_copyright_script into lp:widelands.
References