diff --git a/web/copyright_updater.py b/web/copyright_updater.py new file mode 100644 index 000000000..d2a54ad87 --- /dev/null +++ b/web/copyright_updater.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- + +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2019 The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +# This utility will allow us to change the copyright year information from +# all the files present in FILE_EXTENSIONS variable at once + +import os +import sys +import re + +ALLOWED_FILE_EXTENSIONS = ('.py', '.js', '.sql', '.cpp', '.h', '.rc', '.am') +EXCLUDE_DIR = ('node_modules') + +# Filter the files by its extension +def is_code_file(filename, extensions=ALLOWED_FILE_EXTENSIONS): + return any(filename.endswith(e) for e in extensions) + +# Main function which will iterate and replace the copyright year +def findReplace(directory, find, replace): + total = 0 + COPYRIGHT_PATTERN = re.compile( + r'(Copyright \(C\) \d{{4}} - ){0},'.format(find) + ) + + for path, dirs, files in os.walk(os.path.abspath(directory), topdown=True): + # Exclude the specified directory + dirs[:] = [d for d in dirs if d not in EXCLUDE_DIR] + + for filename in filter(is_code_file, files): + current_file = os.path.join(path, filename) + print( + "Changing copyright information of file: `{0}`".format( + current_file + ) + ) + + # Read the file + with open(current_file) as fp: + content = fp.read() + + is_update_required = False + new_content = COPYRIGHT_PATTERN.sub( + r'\g<1>{}'.format(replace), content + ) + if new_content != content: + is_update_required = True + + if is_update_required: + total += 1 + with open(current_file, "w") as fp: + fp.write(new_content) + print(" ... Done") + else: + print(" ... N/A") + + return total + +def help(): + print("\nPlease provide proper input to the script") + print("\t{0} ".format(sys.argv[0])) + print("\tExample: {0} 2018 2019\n".format(sys.argv[0])) + exit(1) + +if __name__ == '__main__': + if len(sys.argv) < 3: + help() + + if not sys.argv[1].isdigit() or not sys.argv[2].isdigit(): + help() + + # Search and Replace the Copyright information from Parent folder + files_affected = findReplace('..', sys.argv[1], sys.argv[2]) + print("\n\nTotal {} files has been changed".format(files_affected))