icode2github.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # !/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. ################################################################################
  4. #
  5. # Copyright (c) 2024 Baidu.com, Inc. All Rights Reserved
  6. #
  7. ################################################################################
  8. """
  9. Author: PaddlePaddle Authors
  10. """
  11. import os
  12. import re
  13. NEW_COPYRIGHT = '# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.\n\
  14. # \n\
  15. # Licensed under the Apache License, Version 2.0 (the "License");\n\
  16. # you may not use this file except in compliance with the License.\n\
  17. # You may obtain a copy of the License at\n\
  18. #\n\
  19. # http://www.apache.org/licenses/LICENSE-2.0\n\
  20. #\n\
  21. # Unless required by applicable law or agreed to in writing, software\n\
  22. # distributed under the License is distributed on an "AS IS" BASIS,\n\
  23. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\
  24. # See the License for the specific language governing permissions and\n\
  25. # limitations under the License.\n'
  26. def replace_copyright_in_file(file_path, new_copyright):
  27. """
  28. Replace copyright information in single Python file.
  29. Args:
  30. file_path (str): The path of the file to be processed.
  31. new_copyright (str): The new copyright information.
  32. Returns:
  33. None
  34. """
  35. print(f"Processing file: {file_path}")
  36. try:
  37. with open(file_path, 'r+', encoding='utf-8') as file:
  38. content = file.read()
  39. pattern = re.compile(r'(# !/usr/bin/env python3[\s\S]*?Authors\s*\n""")', re.MULTILINE)
  40. new_content = pattern.sub(new_copyright + '\n', content)
  41. if new_content != content:
  42. print(f"Copyright information replaced in {file_path}")
  43. file.seek(0) # Reset the file pointer to the beginning of the file.
  44. file.write(new_content)
  45. file.truncate()
  46. except Exception as e:
  47. print(f"Error processing file {file_path}: {e}")
  48. def replace_copyright_in_directory(directory, new_copyright):
  49. """
  50. Replace copyright information in Python files under the specified directory.
  51. Args:
  52. directory (str): The directory path where Python files are located.
  53. new_copyright (str): The new copyright information to be replaced.
  54. Returns:
  55. None.
  56. """
  57. for root, dirs, files in os.walk(directory):
  58. for file in files:
  59. if file.endswith('.py'):
  60. file_path = os.path.join(root, file)
  61. replace_copyright_in_file(file_path, new_copyright)
  62. if __name__ == '__main__':
  63. replace_copyright_in_directory('./paddlex', NEW_COPYRIGHT)